# Free fall physics simulation capture as a video

I have a project I am working on where I have objects positions randomly in the space and let them fall using rigid body simulation using python scripts and blender as (GUI-less) background. I am able to get the initial position and final position (after 500 frames). Now I would like to also generate video of the free fall of the objects from frame 1-500 into a video file as scene by the camera. I am able to generate images using node-editor at the end of the simulation. I am looking for a flag that is like below pseudo code.

start recording
start the free fall
end after 500 frames
stop recording


Any pointers would be of great help! Thanks in advance.

Edit1: To clarify more on what I want to do! Thing to note here is that, I have single python script and I am generating 10 different scenes one after the other, where each scene has objects positions randomly. Currently I am using bake-all method to get the final state of each scene and render the scene out to see whats the final stage. Now instead of getting the final frame for all these scenes, I want to start the animation before baking each scene and render it out to a video. I want to do this for each scene one by one in the loop.

Edit2: I guess I am in the process of figuring it out. I found out that

bpy.ops.rigidbody.objects_add(type='ACTIVE')
bpy.ops.ptcache.bake_all(bake=True)


Does do the physics simulation before hand store it in a file. Now all I have to do is to do the below to get the animaton? I will post the results when I find it.

bpy.context.scene.camera = camera
bpy.ops.render.render(write_still=True)

• Wouldn't you have to run through the frames to generate the sim cache, and then render out the frames starting from frame 0 after that? – Todd McIntosh Jul 22 '15 at 4:57
• How would you know where to position the camera if set up programmatically. You'd want to make sure most of the objects were in view? – Todd McIntosh Jul 22 '15 at 4:59
• I have the camera settings provided programmatically. I have all that set up in my project. – desinghkar Jul 22 '15 at 13:41
• Okay, well why can't you run a normal animation render via command line and call the python script as a startup script. You can specify a py script to run as a command line argument. – Todd McIntosh Jul 22 '15 at 13:49
• You mean this right - bpy.ops.render.render(animation=True) – desinghkar Jul 22 '15 at 14:00

I am posting a part of my code here that I believe will make sense. After setting up the scene I do the bake_all like the below and call the render_ogl method I wrote

    scene = bpy.context.screen.scene
scene.frame_end = 150
scene.rigidbody_world.point_cache.frame_end = 150 #If this is not     written the simulation ends at 250 default
scene.rigidbody_world.solver_iterations = 60
scene.rigidbody_world.steps_per_second = 240
scene.rigidbody_world.time_scale = 10
scene.rigidbody_world.use_split_impulse = 1
bpy.ops.ptcache.bake_all(bake=True)

#Create the camera with respect to which rendering should be done
my_camera = createCamera('my_camera', (1, 1, 1), list_ob[0])
set_camera_params(my_camera, render)

#Set lamps to see the ogl rendering
my_lamp = createLights('my_lamp', (15, 0, 50))
set_lights(my_lamp)

#Ogl rendering
render_ogl(my_camera, scene, fp, ind)


The render_ogl method is as below.

def render_ogl(camera, scene, fp, ind):
#Some scene nodes I have for the rendering
scene.use_nodes = True
nodes = scene.node_tree.nodes

#Use the default nodes generated in blender
render_layers = nodes['Render Layers']
output_viewer = nodes['Composite']

output_file = nodes.new('CompositorNodeOutputFile')
output_file.base_path = "/home/kar/Desktop/Blender_Render_Output/"

render_layers.outputs['Image'],
output_viewer.inputs['Image']
)

#The settings to save the file as per the given indices
scene.render.filepath = fp + 'zbuffer' +str(ind)
scene.render.image_settings.file_format = 'FFMPEG'
scene.render.image_settings.color_mode = 'BW'
bpy.context.scene.camera = camera
bpy.ops.render.render(animation=True)

return


If you're running blender in headless mode (ie command line mode), then a sample animation render call would look like this:

blender -b file.blend -s 1 -e 500 -a -P startscript.py


You can find more reference material on command arguments here: http://wiki.blender.org/index.php/Doc:2.4/Manual/Render/Command_Line

• Thanks Todd, but I guess my problem is slightly different. I might have missed some information in post so I am going to update the post now. And I don't have a blend file as the scenes are going to generated randomly one by one by my python script. – desinghkar Jul 22 '15 at 15:19
• How are you running the python script if you don't have a blend file? – Todd McIntosh Jul 22 '15 at 16:05
• Please check the question where I gave the code to do this - blender.stackexchange.com/q/32878/15189 – desinghkar Jul 22 '15 at 16:59
• Ok so you just want to run a sim, set the start and end frame and then call bpy.ops.render.render(animation=True)? So really you just want to know how to set the start and end frames? – Todd McIntosh Jul 22 '15 at 17:26

Ok, I'm still trying to understand what you really want. I'm going to write out pseudocode for what I think the answer is, and you can comment and then I'll edit it.

Via the command line you're going to launch blender in headless mode with a blank/default blend file, and call your custom python script.

The script will do the following:

• import required python libs
• start a scene loop
• for the first item in the loop, clear the scene then setup the camera, and the rigid bodies objects and appropriate settings
• bake the rigid body sims to disk with the Bake all command
• Render out frames 1-500 to Video1.mp4
• Loop back to start next scene cycle

Is this close to the workflow you're trying to achieve?

• Yes Todd, and I got what I want. – desinghkar Jul 22 '15 at 20:14
• Ok, well maybe you should post your own answer then. – Todd McIntosh Jul 22 '15 at 20:15