drivers.py 4.09 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
# -*- coding: utf-8 -*-

# Import Particle class
filename = os.path.join(os.path.basename(bpy.data.filepath), "particle.py")
exec(compile(open(filename).read(), filename, 'exec'))
# Import scene functions
filename = os.path.join(os.path.basename(bpy.data.filepath), "scene_functions.py")
exec(compile(open(filename).read(), filename, 'exec'))


class animationDriver:
    def __init__(self,name):
13
        self.name = name
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
    def configure(self, renderCamera, duration, fps, simulated_t, outputPath, fileIdentifier, resolution_percent=100):
        self.renderCamera=renderCamera
        self.duration=duration  # total video duration in seconds
        self.fps=fps
        self.simulated_t=simulated_t # total simulated time in microseconds. (0.01 -> light travels ~ 3 m)
        self.N_frames=duration*fps
        self.delta_t=self.simulated_t/self.N_frames # time elapsed per frame
        self.outputPath = outputPath
        self.fileIdentifier = fileIdentifier
        self.resolution_percent = resolution_percent
        self.configureOutput()
        bcs = bpy.context.scene
        bcs.frame_start = 0
        bcs.frame_end = self.N_frames

    def render(self):
        bpy.context.scene.camera = bpy.data.objects[self.renderCamera]
        bpy.ops.render.render(animation=True)

    def configureOutput(self):
        # Configure Output
        bcsr = bpy.context.scene.render
        bcsr.fps=self.fps
        bcsr.resolution_percentage = self.resolution_percent
        bcsr.image_settings.file_format = 'FFMPEG'
        bcsr.ffmpeg.format = "MPEG4"
        bcsr.ffmpeg.codec = "H264"
        bcsr.ffmpeg.use_max_b_frames = False
        bcsr.ffmpeg.video_bitrate = 6000
        bcsr.ffmpeg.maxrate = 9000
        bcsr.ffmpeg.minrate = 0
        bcsr.ffmpeg.buffersize = 224 * 8
        bcsr.ffmpeg.packetsize = 2048
        bcsr.ffmpeg.muxrate = 10080000
        xpixels = int(bcsr.resolution_percentage * bcsr.resolution_x / 100)
        output_prefix=fileIdentifier+str(xpixels)+"px_"+self.name
        bcsr.filepath = "/tmp/blender/"+output_prefix

class genDriver(animationDriver): # A driver for particle generators
53
54
55
56
57
    def __init__(self,name,N_particles, par1):
        self.name = name+"_N"+str(n_particles)+"_"
        self.N_particles = N_particles
        self.par1 = par1
    def getParticles(self, x = 0, y = 0, z = 0):  # Create particles at given position and return them in a list
58
59
        particles=[]
        #loop over particles
60
        for i in range(0, self.N_particles):
61
            charge =  random.choice([+1,-1])
62
63
64
            mass = random.choice([0.000510999, 0.13957, 0.105658, 0.938272, 0.493677]) # Available mass values
            part = ParticlePropagator(i,x,y,z,charge,mass)
            part.SetType()
65
            part.SetMagneticField()
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
66
            part.SetProperties(random.gauss(0,self.par1),random.gauss(0,self.par1),random.gauss(0,self.par1))
67
68
69
            particles.append(part)
        return particles;

Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
70
class dataDriver(animationDriver): # A driver for data from files.
71
72
73
    def __init__(self,name,datafile):
        self.name = name+"_"+datafile+"_"
        self.datafile = datafile
74
    def getParticles(self):  # Create particles acording to parameters from file
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
75
        # Count number of lines in file = number of particles
76
        detail_file = open(self.datafile, 'r')
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
77
        lines = detail_file.readlines()
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
78
79
80
        N_particles = len(lines)
        # Create particles list
        particles=[]
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
81
        #loop over particles and get information from data file
82
        for i in range(0, N_particles):
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
83
84
85
86
87
88
89
90
            x = lines[i].split(' ')[0]
            y = lines[i].split(' ')[1]
            z = lines[i].split(' ')[2]
            mass = lines[i].split(' ')[3]
            charge = lines[i].split(' ')[4]
            Px = lines[i].split(' ')[5]
            Py = lines[i].split(' ')[6]
            Pz = lines[i].split(' ')[7]
91
            part = ParticlePropagator(i,float(x),float(y),float(z),float(charge),float(mass))
92
            part.SetType()
93
94
            part.SetMagneticField(0.5)
            part.SetProperties(float(Px),float(Py),float(Pz))
95
            particles.append(part)
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
96
        detail_file.close()
97
        return particles;