# Is there any way to export the last frame of a physics simulation from command line?

I have a basic rigid body physics sim, but I want to export the last frame of the simulation. So far I have tried setting the "current frame to the last frame of the simulation and exporting it that way, but that doesn't work.

#Select all objects other than the suitcase
bpy.ops.object.select_all(action='SELECT')
bpy.data.objects['Suitcase'].select = False

# store the location of current 3d cursor
saved_location = bpy.context.scene.cursor_location.copy()  # returns a copy of the vector

# give 3dcursor new coordinates
bpy.context.scene.cursor_location = (0.0,0.0,0.0)

# set the origin on the current object to the 3dcursor location
bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
bpy.ops.object.origin_set(type='ORIGIN_CENTER_OF_VOLUME')

# Randomly place objects in the air
bpy.ops.object.randomize_transform(random_seed= randint(1,9000), use_delta=False, use_loc=True, loc=(12.0, 8.0, 0.0), use_rot=True, rot=(360.0, 360.0, 360.0), use_scale=False, scale_even=False, scale=(1.0, 1.0, 1.0))
bpy.ops.object.select_all(action='SELECT')

#Runs physics simulation
bpy.context.scene.frame_set(1)
bpy.context.scene.frame_current += 1
bpy.context.scene.update()
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.visual_transform_apply()

# Define Directory
dir = bpy.path.abspath('//stlexport/')

# Create Directory (If Necessary)
if not os.path.exists(dir): os.makedirs(dir)

# Export STLs
bpy.ops.export_mesh.stl(filepath = dir, batch_mode = 'OBJECT')

• I want to run this out of the command line so I can run a batch file to automate the process multiple times – Abhi Kondagunta Jul 2 '18 at 13:18
• Please provide some details re: script (even if it "doesn't work") and cli call. – batFINGER Jul 4 '18 at 9:47
• This script exports only the location of the objects on the first frame, it doesn't update their position when I change frames – Abhi Kondagunta Jul 9 '18 at 13:47
• AFAIK scene.frame_set sets the current frame and also updates the scene. Does last frame export work from UI? If not add the operator described by @Duarte below before export. – batFINGER Jul 9 '18 at 14:17
• I have tried both, suggestions and neither exports the last frame of the animation. Would I need to bake the animation through the command line as well? PS: I have no idea what baking accomplishes, I am very new to blender animation – Abhi Kondagunta Jul 9 '18 at 14:33

Bake the point cache

Using same method as with particles here CLI How to render last frame of hair dynamics sym?

Replace "runs physics simulation" with a bake operator below. Not sure if playing, or frame setting, and baking happen in bg mode as they do when using GUI.

rbw.py

import bpy
context = bpy.context

scene = context.scene
rbw = scene.rigidbody_world
pc = rbw.point_cache

# match bake to animation
print(pc.frame_start, pc.frame_end)

bpy.ops.ptcache.bake({"point_cache": pc}, bake=True)

scene.frame_set(250)
bpy.ops.export_scene.obj(filepath="test.obj")


Testing in CLI with

blender -b xxx.blend -P rbw.py


can confirm test.obj is a result of simple physics sim at 250.

You need to make the physics simulation result "real" by applying the obtained transformations to each object.

Use the Apply Visual Transforms operator from the Space Bar menu before exporting.

For scripting the python operator name is bpy.ops.object.visual_transform_apply()

• How can this be done via script for CLI? – batFINGER Jul 4 '18 at 9:47
• If you right click the menu entry you can probably find the corresponding python operator that can be called from a script. – Duarte Farrajota Ramos Jul 4 '18 at 12:33
• Can the same be done if I was to export the last frame of the simulation as an STL file? – Abhi Kondagunta Jul 9 '18 at 13:40
• Applying the visual transforms still exports the initial positions when I export as an STL. I have updated my initial post with the code that applies visual transfoms – Abhi Kondagunta Jul 9 '18 at 14:30
• For how long are you running the simulation? Make sure your objects no longer any animation or physics, otherwise that you are at the correct frame number – Duarte Farrajota Ramos Jul 9 '18 at 17:24