scene_functions.py 6.63 KB
Newer Older
1
2

def init():
3
    bcs = bpy.context.scene
4
5
6
7
8
9
10
11
12

    # Configure Environment
    bcs.world.light_settings.use_environment_light = False
    bcs.world.light_settings.environment_energy = 0.1

    # Cleanup
    bpy.data.objects.remove(bpy.data.objects['Cube'])
    bpy.data.objects.remove(bpy.data.objects['Camera'])

13
14
15
    # Basic Objects
    addCameras() # Add cameras
    addALICE_TPC() # ALICE TPC
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

def addALICE_TPC():
    print("Adding ALICE TPC")
    bpy.data.materials.new(name="TPC")
    bpy.data.materials["TPC"].diffuse_color = (0, 0.635632, 0.8)
    bpy.data.materials["TPC"].use_shadows = False
    bpy.data.materials["TPC"].use_cast_shadows = False
    bpy.data.materials["TPC"].use_transparency = True
    bpy.data.materials["TPC"].alpha = 1
    bpy.data.materials["TPC"].specular_alpha = 0
    bpy.data.materials["TPC"].raytrace_transparency.fresnel_factor = 5
    bpy.data.materials["TPC"].raytrace_transparency.fresnel = 0.3
    bpy.ops.mesh.primitive_cylinder_add(radius=2.5, depth=5, view_align=False, enter_editmode=False, location=(0, 0, 0))
    TPC = bpy.context.object
    TPC.name = "TPC"
    TPC.data.materials.clear()
    TPC.data.materials.append(bpy.data.materials["TPC"])


def addCameras():
    # ForwardCamera
37
    bpy.ops.object.camera_add(location = (0,0,20), rotation = (0, 0, 0))
38
39
40
41
42
43
    bpy.context.object.name = "ForwardCamera"
    camera_forward=bpy.data.objects['ForwardCamera']
    camera_forward.data.type = 'ORTHO'
    camera_forward.data.ortho_scale = 10

    # OverviewCamera
44
    bpy.ops.object.camera_add(location = (6.98591, -19.7115, 23.9696), rotation = (-0.281366, 0.683857, -1.65684))
45
46
47
48
    bpy.context.object.name = "OverviewCamera"
    bpy.context.object.data.lens = 66.78

    # Barrel Camera
49
    bpy.ops.object.camera_add(location = (6, 0, 0), rotation = (0, 1.5708, 0))
50
51
52
53
54
    #bpy.context.object.rotation_euler[1] = 1.5708
    bpy.context.object.name = "BarrelCamera"

# Function that creates Blender Objects from input list of particles.
## Returns a list of blender objects
55
def createSceneParticles(particles, createTracks = False):
56
    # Associate particles and colors
57
    particle_types = ["Electron","Pion","Muon","Proton","Kaon","Unknown"]
58
59
60
61
62
    clRed = (1, 0, 0)
    clGreen = (0, 1, 0)
    clBlue = (0, 0, 1)
    clMagenta = (0.75, 0, 1)
    clYellow = (1, 1, 0)
63
64
    clWhite = (255, 255, 255)
    particle_colors = {"Electron":clRed, "Pion":clGreen, "Muon":clBlue, "Proton":clMagenta, "Kaon": clYellow, "Unknown": clWhite}
65
66
67
68
69
70
71
72
73
74

    #Create Materials
    for type in particle_types:
        bpy.data.materials.new(name=type)
        #bpy.context.object.active_material = (1, 0, 0)
        bpy.data.materials[type].diffuse_color = particle_colors[type]
        bpy.data.materials[type].use_shadows = False
        bpy.data.materials[type].use_cast_shadows = False

    # Create blender spheres (particles)
75
    blender_particles=[]
76
    n_particles=len(particles)
77
    for particle in particles:
78
        this_type=particle.p_type
79
80
81
82
        print("Adding Sphere - Particle " + str(len(blender_particles))+" of "+str(n_particles-1)+" - "+this_type)
        bpy.ops.mesh.primitive_uv_sphere_add()
        this_particle = bpy.context.object
        this_particle.name = "part"+str(particle.iDx)
83
84
        this_particle.location = ((particle.x,particle.y,particle.z))
        this_particle.delta_scale = (r_part,r_part,r_part)
85
86
87
        this_particle.data.materials.clear()
        this_particle.data.materials.append(bpy.data.materials[this_type])
        blender_particles.append(this_particle)
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122

    # Create blender curves (tracks)
    blender_tracks=[]
    if createTracks:
            for track in particles:
                this_type=random.choice(particle_types) #TO DO: make this not random, but according to file data
                print("Adding Curve - Track " + str(len(blender_tracks))+" of "+str(n_particles-1)+" - "+this_type)

                # create the Curve Datablock
                curveTrack = bpy.data.curves.new('CurveTrack', type='CURVE')
                curveTrack.dimensions = '3D'
                curveTrack.resolution_u = 2

                curveTrack.fill_mode = 'FULL'
                curveTrack.bevel_depth = 0.02
                curveTrack.bevel_resolution = 3


                # map coords to spline
                bcs = bpy.context.scene
                polyline = curveTrack.splines.new('NURBS')
                polyline.points.add(bcs.frame_end) # Add one point per frame
                for i in range(bcs.frame_end):
                    polyline.points[i].co = (particle.x,particle.y,particle.z, 1)

                # create Object
                trackOB = bpy.data.objects.new('Track', curveTrack)
                trackOB.data.materials.clear()
                trackOB.data.materials.append(bpy.data.materials[this_type])
                scn = bpy.context.scene
                scn.objects.link(trackOB)
                blender_tracks.append(trackOB)


    return blender_particles, blender_tracks
123
124

# Function that animates the scene using the particle propagator class
125
126
127
def animate(objects, particles, driver):
    bcs = bpy.context.scene

128
129
    #Animate particles
    for f in range(1, bcs.frame_end):
130
        t = driver.delta_t*f
131
        bcs.frame_current = f
132
        print("Configuring particles in frame: "+str(f)+" of "+str(bcs.frame_end))
133
134
135
136
        for i in range(0, len(objects)):
            bcs.objects.active=objects[i]
            objects[i].location=(particles[i].Propagate(t))
            objects[i].keyframe_insert(data_path='location')
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167

# Function that animates particle tracks using the particle propagator class
def animate_tracks(tracks, particles, driver):
    bcs = bpy.context.scene

    #Animate tracks
    for f in range(1, bcs.frame_end):
        t = driver.delta_t*f
        bcs.frame_current = f
        print("Configuring tracks in frame: "+ str(f) +" of "+ str(bcs.frame_end))
        for point in range(f,bcs.frame_end):
            for i in range(0, len(particles)):
                #bcs.objects.active=tracks[i]
                tracks[i].data.splines[0].points[point].keyframe_insert(data_path="co", frame = f)
                x, y, z = particles[i].Propagate(t)
                tracks[i].data.splines[0].points[point].co = (x, y, z, 1)



                ##polyline = curveTrack.splines.new('NURBS')
                ##polyline.points.add(len(coords))
                ##for i, coord in enumerate(coords):
                ##    x,y,z = coord
                ##    polyline.points[i].co = (x, y, z, 1)

                #curve = bpy.data.objects["Track"]
                #curve.data.splines[0].points[1].co


                #point.keyframe_insert(data_path="co", frame = i)
                # https://blender.stackexchange.com/questions/73630/animate-curves-by-changing-spline-data-using-a-python-script