animate_particles.py 3.11 KB
Newer Older
Rafael Pezzi's avatar
Rafael Pezzi committed
1
# -*- coding: utf-8 -*-
2
# animate_particles.py - Animate HEP events
Rafael Pezzi's avatar
Rafael Pezzi committed
3
#
4
#   For console only rendering (example):
5
#   $ blender -noaudio --background -P animate_particles.py -- -radius=0.05 -duration=1 -camera="BarrelCamera" -datafile="esd-detail.dat" -simulated_t=0.02 -fps=24 -resolution=100 -stamp_note="Texto no canto"
6
#
Rafael Pezzi's avatar
Rafael Pezzi committed
7

8
import os
Rafael Pezzi's avatar
Rafael Pezzi committed
9
import bpy
10

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import argparse
import sys

# Pass on command line arguments to script:
class ArgumentParserForBlender(argparse.ArgumentParser):
    def _get_argv_after_doubledash(self):
        try:
            idx = sys.argv.index("--")
            return sys.argv[idx+1:] # the list after '--'
        except ValueError as e: # '--' not in the list:
            return []

    # overrides superclass
    def parse_args(self):
        return super().parse_args(args=self._get_argv_after_doubledash())

parser = ArgumentParserForBlender()

parser.add_argument('-radius','--r_part')
parser.add_argument('-duration','--duration')
parser.add_argument('-camera','--render_camera')
parser.add_argument('-datafile','--datafile')
33
34
35
parser.add_argument('-simulated_t','--simulated_t')
parser.add_argument('-fps','--fps')
parser.add_argument('-resolution','--resolution_percent')
36
parser.add_argument('-stamp_note','--stamp_note')
37
args = parser.parse_args()
38

Rafael Pezzi's avatar
Rafael Pezzi committed
39
bpy.context.user_preferences.view.show_splash = False
40
41
# Import Drivers, partiles and scence functions:
filename = os.path.join(os.path.basename(bpy.data.filepath), "drivers.py")
Rafael Pezzi's avatar
Rafael Pezzi committed
42
43
exec(compile(open(filename).read(), filename, 'exec'))

44
# Set animation parameters
45
r_part = float(args.r_part) # Particle radius
46
simulated_t = float(args.simulated_t) # in microsseconds
47
duration = int(args.duration) # in seconds
48
49
fps = int(args.fps)
resolution_percent = int(args.resolution_percent)
50
51

#configure output
52
53
54
outputPath = "/tmp/blender/"
fileIdentifier = "PhysicalTrajectories_"
##  RenderCameras: ["ForwardCamera", "OverviewCamera", "BarrelCamera"]
55
renderCamera= args.render_camera
56

57
renderAnimation = True # True
58
59
saveBlenderFile = False # False

60
"""
61
# Create and configure animation driver
62
63
n_particles = 100 # Event Multiplicity
driver = genDriver("GaussianGenerator",n_particles,3.0) # Simple genDriver takes two parameters: number of particles and Gaussian width
64
driver.configure(renderCamera, duration, fps, simulated_t, outputPath, fileIdentifier, resolution_percent)
65
"""
66

Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
67
# Create and configure animation driver
68
driver = dataDriver("AlirootFileGenerator",args.datafile) # Simple dataDriver takes one parameters: filename
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
69
driver.configure(renderCamera, duration, fps, simulated_t, outputPath, fileIdentifier, resolution_percent)
70

71
### Build scene
72
init(args.stamp_note) # Cleanup, addCameras, addALICE_TPC
73
particles = driver.getParticles()
74
blender_particles, blender_tracks = createSceneParticles(particles,createTracks = True) # Create blender objects - one sphere per particle
75
76
77

#Animate scene using driver
animate(blender_particles,particles,driver)
78
79
animate_tracks(blender_tracks,particles,driver)

80
bpy.context.scene.frame_current = 24
81
82

## Save blender file
83
if saveBlenderFile: bpy.ops.wm.save_as_mainfile(filepath=outputPath+fileIdentifier+".blend")
84
85

# Render animation
86
if renderAnimation: driver.render()
87
88

#exit()