2.80 nla.bake - returns 'Nothing to bake' - when selecting bones through script

Hi, been reading here for while but this is my first post!

I’m trying to write a script that bakes constraints to bones. However if I select a bone via script the bpy.ops.nla.bake() operations won’t run, it just returns “Nothing to bake” in the system console.

If I select a bone via the view port and then run bpy.ops.nla.bake() the bake operations works.

Q. Am I doing this wrong?
Q. Is there something fundamentally different under the hood in mouse click selection?
Q. Perhaps this is a bug?

To repro, create a two bone armature. (Optionally a constraint also)
If anyone can help with this that would be great :slight_smile:

import bpy

obj = bpy.context.object
m_bone = obj.data.bones['Bone.001']

m_bone.select = True
obj.data.bones.active = m_bone

bpy.ops.nla.bake(frame_start=0, frame_end=10, step=1, only_selected=True,
                    visual_keying=True, clear_constraints=True, clear_parents=False,
                    use_current_action=True, bake_types={'POSE'})

Anyone out there with any insight into this?

I’ve seen that “Nothing to bake” error in 2.80 when another object is selected in addition to the armature. Try deselecting everything in Object mode, selecting the armature, re-entering Pose mode, then baking. Here is a code snippet that works for me:

scene = bpy.data.scenes[0]
arm   = bpy.data.objects['metarig']

# ensure that only the armature is selected in Object mode
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
arm.select_set(True)
scene.view_layers[0].objects.active = arm

# enter pose mode and bake
bpy.ops.object.mode_set(mode='POSE')
bpy.ops.pose.select_all(action='SELECT')
bpy.ops.nla.bake( ... )

Thanks for the input, I’ve not looked at this in a while, so will give it a go.

I did also wonder if I needed to focus a 3D viewport in the script, I needed to do that with the Graph editor for another automation that manipulated keys.