0
$\begingroup$

I am creating an exporter that includes a rigidbody simulation, so I need to bake the rigidbody simulation to an animation before exporting the whole file.

If I open Blender manually, then open my script within Blender and run it then everything works fine. As part of my workflow I run Blender with the --background and --python command line args. I have tried removing the background argument, with the same result.

In the script, I have selected the rigidbody objects and set up the context as I would when I manually bake the animation in Blender, but I receive the error message RuntimeError: Operator bpy.ops.anim.keyframe_insert.poll() failed, context is incorrect. keyframe_insert is called by rigidbody.bake_to_keyframes.

Here's the relevant code:

def bake_rigidbody_animation(rigidbody_object):
  bpy.ops.object.select_all(action='DESELECT')
  rigidbody_object.select_set(True)
  bpy.context.view_layer.objects.active = rigidbody_object
  print(f'Active object: {bpy.context.view_layer.objects.active}')
  print(f'Selected objects: {[obj for obj in bpy.context.view_layer.objects.selected]}')
  print(f'Current mode: {bpy.context.active_object.mode}')
  print(f'Rigidbody: {bpy.context.active_object.rigid_body}')
  bpy.ops.rigidbody.bake_to_keyframes()

And the corresponding output (all as expected):

Active object: <bpy_struct, Object("Scrap0") at 0x00000262C764AB08>
Selected objects: [bpy.data.objects['Scrap0']]
Current mode: OBJECT
Rigidbody: <bpy_struct, RigidBodyObject at 0x00000262C76C8CA8>

And the errors immediately afterwards:

Error: Python: Traceback (most recent call last):
  File "E:\Program Files (x86)\Steam\steamapps\common\Blender\3.0\scripts\startup\bl_operators\rigidbody.py", line 180, in execute
    bpy.ops.anim.keyframe_insert(override, type='BUILTIN_KSI_LocRot')
  File "E:\Program Files (x86)\Steam\steamapps\common\Blender\3.0\scripts\modules\bpy\ops.py", line 130, in __call__
    ret = _op_call(self.idname_py(), C_dict, kw, C_exec, C_undo)
RuntimeError: Operator bpy.ops.anim.keyframe_insert.poll() failed, context is incorrect

location: E:\Program Files (x86)\Steam\steamapps\common\Blender\3.0\scripts\modules\bpy\ops.py:132

Python: Traceback (most recent call last):
  File "E:\Program Files (x86)\Steam\steamapps\common\Blender\3.0\scripts\startup\bl_operators\rigidbody.py", line 180, in execute
    bpy.ops.anim.keyframe_insert(override, type='BUILTIN_KSI_LocRot')
  File "E:\Program Files (x86)\Steam\steamapps\common\Blender\3.0\scripts\modules\bpy\ops.py", line 130, in __call__
    ret = _op_call(self.idname_py(), C_dict, kw, C_exec, C_undo)
RuntimeError: Operator bpy.ops.anim.keyframe_insert.poll() failed, context is incorrect

location: E:\Program Files (x86)\Steam\steamapps\common\Blender\3.0\scripts\modules\bpy\ops.py:132
Error: Python: Traceback (most recent call last):
  File "E:\Program Files (x86)\Steam\steamapps\common\Blender\3.0\scripts\startup\bl_operators\rigidbody.py", line 180, in execute
    bpy.ops.anim.keyframe_insert(override, type='BUILTIN_KSI_LocRot')
  File "E:\Program Files (x86)\Steam\steamapps\common\Blender\3.0\scripts\modules\bpy\ops.py", line 130, in __call__
    ret = _op_call(self.idname_py(), C_dict, kw, C_exec, C_undo)
RuntimeError: Operator bpy.ops.anim.keyframe_insert.poll() failed, context is incorrect

location: E:\Program Files (x86)\Steam\steamapps\common\Blender\3.0\scripts\modules\bpy\ops.py:132

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "E:\Projects\ManGrenadeLateLfs\Scripts\Blender\ExportToUnitySource.py", line 106, in <module>
    main(args.output_file)
  File "E:\Projects\ManGrenadeLateLfs\Scripts\Blender\ExportToUnitySource.py", line 16, in main
    bake_physics_animations()
  File "E:\Projects\ManGrenadeLateLfs\Scripts\Blender\ExportToUnitySource.py", line 51, in bake_physics_animations
    bake_rigidbody_animation(rigidbody)
  File "E:\Projects\ManGrenadeLateLfs\Scripts\Blender\ExportToUnitySource.py", line 90, in bake_rigidbody_animation
    bpy.ops.rigidbody.bake_to_keyframes()
  File "E:\Program Files (x86)\Steam\steamapps\common\Blender\3.0\scripts\modules\bpy\ops.py", line 132, in __call__
    ret = _op_call(self.idname_py(), None, kw)
RuntimeError: Error: Python: Traceback (most recent call last):
  File "E:\Program Files (x86)\Steam\steamapps\common\Blender\3.0\scripts\startup\bl_operators\rigidbody.py", line 180, in execute
    bpy.ops.anim.keyframe_insert(override, type='BUILTIN_KSI_LocRot')
  File "E:\Program Files (x86)\Steam\steamapps\common\Blender\3.0\scripts\modules\bpy\ops.py", line 130, in __call__
    ret = _op_call(self.idname_py(), C_dict, kw, C_exec, C_undo)
RuntimeError: Operator bpy.ops.anim.keyframe_insert.poll() failed, context is incorrect

I can open Blender manually and use the operator search to start baking, and I can use the scripting panel to type bpy.ops.rigidbody.bake_to_keyframes(), which both work.

Other info

I found T63067, which talks about using external script files and context overrides. I am using an external script file. I am not using a context override. I tried the suggested fix anyway and it did not help.

$\endgroup$

1 Answer 1

0
$\begingroup$

Call the bake method inside the following context:

area = [a for a in bpy.context.screen.areas if a.type=="VIEW_3D"][0]
with bpy.context.temp_override(area=area):
    bpy.ops.rigidbody.bake_to_keyframes()

I got the idea from https://stackoverflow.com/a/67697363/4295037 then adjusted it to newer versions of Blender.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .