drivers.py 4.66 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
# -*- 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):
        self.name = name
14 15
    def configure(self, renderCameras, duration, fps, simulated_t, outputPath, fileIdentifier, resolution_percent=100):
        self.renderCameras=renderCameras # array with cameras to render animation with
16 17 18 19 20 21 22 23 24 25 26 27 28
        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

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    def render(self,pic_pct):
        bcs = bpy.context.scene

        self.output_prefix=[]
        for c in range(0,len(self.renderCameras)):

            # Set specific output info
            self.output_prefix.append(self.fileIdentifier+str(self.xpixels)+"px_"+self.name+self.renderCameras[c])
            bcs.render.filepath = "/tmp/alice_blender/"+self.output_prefix[c]
            bcs.camera = bpy.data.objects[self.renderCameras[c]]

            # Take picture of animation
            bcs.frame_current = int(bcs.frame_end * pic_pct/100)
            bpy.ops.render.render()
            bpy.data.images['Render Result'].save_render(filepath=bcs.render.filepath+".png")

            # Render actual animation
            bpy.ops.render.render(animation=True)
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

    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
63
        self.xpixels = int(bcsr.resolution_percentage * bcsr.resolution_x / 100)
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

class genDriver(animationDriver): # A driver for particle generators
    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
        particles=[]
        #loop over particles
        for i in range(0, self.N_particles):
            charge =  random.choice([+1,-1])
            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()
            part.SetMagneticField()
            part.SetProperties(random.gauss(0,self.par1),random.gauss(0,self.par1),random.gauss(0,self.par1))
            particles.append(part)
        return particles;

class dataDriver(animationDriver): # A driver for data from files.
84 85
    def __init__(self,name,nEvent,datafile):
        self.name = name+"_"+datafile+"_Event_"+nEvent+"_"
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
        self.datafile = datafile
    def getParticles(self):  # Create particles acording to parameters from file
        # Count number of lines in file = number of particles
        detail_file = open(self.datafile, 'r')
        lines = detail_file.readlines()
        N_particles = len(lines)
        # Create particles list
        particles=[]
        #loop over particles and get information from data file
        for i in range(0, N_particles):
            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]
            part = ParticlePropagator(i,float(x),float(y),float(z),float(charge),float(mass))
            part.SetType()
            part.SetMagneticField(0.5)
            part.SetProperties(float(Px),float(Py),float(Pz))
            particles.append(part)
        detail_file.close()
        return particles;