I’m using Blender 2.9.3 and Python scripting to describe a very simple Rigid Body Dynamics (RBD) scene and then render the scene head-lessly. See python script Basic2b.py and the command line prompt below. When I try to do this, the Rigid Body Dynamics do not ‘activate’ and the scene appears static - i.e the rendered image stays constant. If I however run the same command but remove the -b option so that Blender interface opens, the motion of the RBD appears when I render the same python scripted scene. I don’t understand why. Can some kind person explain how to ‘activate’ RBD in this head-less python scripted scene in a command-line context as I really need to render this heedlessly. Cheers!
Tried on MacOS 10.13.6 Blender 2.9.3 and most recent Big Sur v 3.0.0.a
#Basic2b.py
#Headless RBD scene setup Sept 28 2021
import bpy
#Blender scene setup
scn = bpy.context.scene
scn.frame_start = 1
scn.frame_end = 100
scn.render.resolution_x = 160 #Set x resolution
scn.render.resolution_y = 120 #Set y resolution
#Add a plane and set as passive rigid body
bpy.ops.mesh.primitive_plane_add(size=25, enter_editmode=False, align='WORLD', location=(0, -4, -0.52), scale=(1, 1, 1))
bpy.ops.rigidbody.objects_add(type='PASSIVE')
#Add a Round cube and set as active rigid body with mass of 20
bpy.ops.mesh.primitive_round_cube_add(align='WORLD', location=(0, 0, 3), change=False, radius=1.5, arc_div=12)
bpy.ops.rigidbody.objects_add(type='ACTIVE')
bpy.context.object.rigid_body.collision_shape = 'SPHERE'
bpy.context.object.rigid_body.mass = 160
Command Line to render this scripted Blender scene:
/Applications/Blender.app/Contents/MacOS/Blender -P /Users/sam/Desktop/Render/basic2b.py -o /tmp/zxz -b -a
However each rendered frame is the same - it seems the RBD in this context is inactive.
If I remove the -b argument and open the python script in the Blender program interface, it renders fine, but I really need to make this work head-lessly in commandline batch mode.
/Applications/Blender.app/Contents/MacOS/Blender -P /Users/sam/Desktop/Render/basic2b.py
Following up with some more research and more questions - 30/09/2021
I have been investigating a bit more and found a partial solution, which is to bake the scene. This means that the Rigid Body Dynamics (RBD) simulation becomes visible (not sure if that's the correct term) to the renderer.
The python command to bake all is:
bpy.ops.ptcache.bake_all(bake=True)
However I've now run into another obstacle - try as I might I cannot get the simulation to run for more than 250 frames. I'm fully aware that its possible to extend the simulation from within the Blender interface - but cannot find a way to do so from within a python script.
The obvious solution is to right click the Simulation End button and see what Blender 2.93 offers, but frustratingly it doesnt offer the full text - part of it is redacted/missing - see below:
I've tried:
bpy.context.scene.frame_end = 402
which is what appears in the python info window but that doesnt work.
I tried permutations of:
bpy.data.scenes["Scene"].PointCache.frame_end = 402
But the blender consol keeps returning errors such as:
AttributeError: 'Scene' object has no attribute 'PointCache'
I'm not sure how to give the scene object a PointCache attribute using Python so can only presume its a contextual thing - but I've no idea where to go next...
I've tried extending my scene to 400 frames, then going to frame 400 and telling Blender to bake up to the current frame
bpy.ops.ptcache.free_bake_all() #remove any stored cache files
bpy.context.scene.frame_end = 400 #set scene end to 400 frames
bpy.context.scene.frame_current = 400 # go to frame 400
bpy.ops.ptcache.bake_all(bake=False) #bake to current frame
but while the command line reports that its baking to frame 400, when I watch the rendered sequence - the simulation stops at frame 250. It's almost like Blender won't allow the simulation space to be more than 250 frames unless you're using Blender interactively in which case you can use the interface to extend the scene. As I'm trying to produce and render Blender RBD scenes solely using python in a scripted, batch file context this isn't an option.
Can anyone suggest which python command would allow me to set the end of the PointCache?
