animate_particles.py 4.7 KB
Newer Older
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
1
2
3
4
# -*- coding: utf-8 -*-
# animate_particles.py - Animate HEP events
#
#   For console only rendering (example):
5
#   $ blender -noaudio --background -P animate_particles.py -- -radius=0.05 -duration=1 -cameras="BarrelCamera OverviewCamera" \
6
7
#   -datafile="esd-detail.dat" -n_event=0 -simulated_t=0.02 -fps=24 -resolution=100 -transparency=1.2 -stamp_note="Texto no canto"\
#    -its=1 -tpc=0 -trd=1 -detailed_tpc=1 -emcal=0 -blendersave=0 -picpct=5 -tpc_blender_path="/home/files/blender" -output_path="/tmp/blender"
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#

import os
import bpy

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')
33
parser.add_argument('-cameras','--render_cameras')
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
34
35
36
37
38
parser.add_argument('-datafile','--datafile')
parser.add_argument('-simulated_t','--simulated_t')
parser.add_argument('-fps','--fps')
parser.add_argument('-resolution','--resolution_percent')
parser.add_argument('-stamp_note','--stamp_note')
39
parser.add_argument('-n_event','--n_event')
40
parser.add_argument('-transparency','--transp_par')
41
42
43
44
parser.add_argument('-its','--its')
parser.add_argument('-tpc','--tpc')
parser.add_argument('-trd','--trd')
parser.add_argument('-emcal','--emcal')
45
parser.add_argument('-blendersave','--blendersave')
46
parser.add_argument('-picpct','--picpct')
47
48
parser.add_argument('-tpc_blender_path','--tpc_blender_path')
parser.add_argument('-detailed_tpc','--detailed_tpc')
49
parser.add_argument('-output_path','--output_path')
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
50
51
52
args = parser.parse_args()

bpy.context.user_preferences.view.show_splash = False
53
# Import Drivers, particles and scence functions:
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
54
55
56
57
58
filename = os.path.join(os.path.basename(bpy.data.filepath), "drivers.py")
exec(compile(open(filename).read(), filename, 'exec'))

# Set animation parameters
r_part = float(args.r_part) # Particle radius
59
n_event = str(args.n_event) # Event number for video name
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
60
61
62
63
simulated_t = float(args.simulated_t) # in microsseconds
duration = int(args.duration) # in seconds
fps = int(args.fps)
resolution_percent = int(args.resolution_percent)
64
stamp_note = args.stamp_note
65
transp_par = float(args.transp_par)
66
datafile = str(args.datafile)
67
detectors = [int(args.its),int(args.tpc),int(args.trd),int(args.emcal),int(args.detailed_tpc)] # Array that stores which detectors to build
68
blendersave = int(args.blendersave) # 1 (save Blender file) or 0 (don't)
69
picpct = int(args.picpct) # percentage of animation to take picture
70
tpc_blender_path = str(args.tpc_blender_path) # path to 'animate' directory, where .blend file for detailed TPC is saved
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
71

72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Create array with cameras:
cams_string = str(args.render_cameras)
cams_list = []
j=0
last_space=0
for digit in range(0,len(cams_string)):
    if cams_string[digit] == ' ':
        cam = ""
        for i in range(j,digit):
            cam += cams_string[i]
        cams_list.append(cam)
        j=digit+1
cam = ""
for i in range(j,len(cams_string)):
    cam += cams_string[i]
cams_list.append(cam)

89
90
# Configure Output
outputPath = str(args.output_path)+"/"
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
91
92
fileIdentifier = "PhysicalTrajectories_"

93
renderAnimation = False # True
94
saveBlenderFile = blendersave # False
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
95
96
97
98
99

"""
# Create and configure animation driver
n_particles = 100 # Event Multiplicity
driver = genDriver("GaussianGenerator",n_particles,3.0) # Simple genDriver takes two parameters: number of particles and Gaussian width
100
driver.configure(cams_list, duration, fps, simulated_t, outputPath, fileIdentifier, resolution_percent)
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
101
102
103
"""

# Create and configure animation driver
104
driver = dataDriver("AlirootFileGenerator",n_event,datafile) # Simple dataDriver
105
driver.configure(cams_list, duration, fps, simulated_t, outputPath, fileIdentifier, resolution_percent)
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
106
107

### Build scene
108
init(stamp_note,transp_par,detectors,tpc_blender_path) # Cleanup, addCameras, addALICE_TPC
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
109
110
111
112
113
114
115
116
117
118
particles = driver.getParticles()
blender_particles, blender_tracks = createSceneParticles(particles,createTracks = True) # Create blender objects - one sphere per particle

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

bpy.context.scene.frame_current = 24

## Save blender file
119
if saveBlenderFile: bpy.ops.wm.save_as_mainfile(filepath=outputPath+fileIdentifier+"AlirootFileGenerator_"+datafile+"_Event_"+n_event+".blend")
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
120
121

# Render animation
122
if renderAnimation: driver.render(picpct)
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
123
124

#exit()