animate_particles.py 2.79 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
5
#   For console only rendering (example):
#   $ blender -noaudio --background -P animate_particles.py -- -radius 0.05 -duration 10 -camera OverviewCamera -datafile esd-detail.dat
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
33
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')
args = parser.parse_args()
34

Rafael Pezzi's avatar
Rafael Pezzi committed
35
bpy.context.user_preferences.view.show_splash = False
36
37
# 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
38
39
exec(compile(open(filename).read(), filename, 'exec'))

40
# Set animation parameters
41
r_part = float(args.r_part) # Particle radius
42
simulated_t = 0.02 # in microsseconds
43
duration = int(args.duration) # in seconds
44
45
fps = 24
resolution_percent = 100
46
47

#configure output
48
49
50
outputPath = "/tmp/blender/"
fileIdentifier = "PhysicalTrajectories_"
##  RenderCameras: ["ForwardCamera", "OverviewCamera", "BarrelCamera"]
51
renderCamera= args.render_camera
52

53
renderAnimation = False #True # True
54
55
saveBlenderFile = False # False

Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
56
"""
57
# Create and configure animation driver
58
59
n_particles = 100 # Event Multiplicity
driver = genDriver("GaussianGenerator",n_particles,3.0) # Simple genDriver takes two parameters: number of particles and Gaussian width
60
driver.configure(renderCamera, duration, fps, simulated_t, outputPath, fileIdentifier, resolution_percent)
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
61
62
63
"""

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

### Build scene
init() # Cleanup, addCameras, addALICE_TPC
69
particles = driver.getParticles()
70
blender_particles, blender_tracks = createSceneParticles(particles,createTracks = True) # Create blender objects - one sphere per particle
71
72
73

#Animate scene using driver
animate(blender_particles,particles,driver)
74
75
animate_tracks(blender_tracks,particles,driver)

76
bpy.context.scene.frame_current = 24
77
78

## Save blender file
79
if saveBlenderFile: bpy.ops.wm.save_as_mainfile(filepath=outputPath+fileIdentifier+".blend")
80
81

# Render animation
82
if renderAnimation: driver.render()
83
84

#exit()