0
$\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
0
$\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$
-1
$\begingroup$

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.

enter image description here

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

$\endgroup$
  • 2
    $\begingroup$ How can this be done via script for CLI? $\endgroup$ – batFINGER Jul 4 '18 at 9:47
  • $\begingroup$ If you right click the menu entry you can probably find the corresponding python operator that can be called from a script. $\endgroup$ – Duarte Farrajota Ramos Jul 4 '18 at 12:33
  • $\begingroup$ Can the same be done if I was to export the last frame of the simulation as an STL file? $\endgroup$ – Abhi Kondagunta Jul 9 '18 at 13:40
  • $\begingroup$ 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 $\endgroup$ – Abhi Kondagunta Jul 9 '18 at 14:30
  • $\begingroup$ 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 $\endgroup$ – Duarte Farrajota Ramos Jul 9 '18 at 17:24

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.