1
$\begingroup$

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')
$\endgroup$
  • $\begingroup$ I want to run this out of the command line so I can run a batch file to automate the process multiple times $\endgroup$ – Abhi Kondagunta Jul 2 '18 at 13:18
  • $\begingroup$ Please provide some details re: script (even if it "doesn't work") and cli call. $\endgroup$ – batFINGER Jul 4 '18 at 9:47
  • $\begingroup$ This script exports only the location of the objects on the first frame, it doesn't update their position when I change frames $\endgroup$ – Abhi Kondagunta Jul 9 '18 at 13:47
  • $\begingroup$ 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. $\endgroup$ – batFINGER Jul 9 '18 at 14:17
  • $\begingroup$ 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 $\endgroup$ – Abhi Kondagunta Jul 9 '18 at 14:33
2
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.