0
$\begingroup$

I need to reset my armature to the rest position with a python code. I could do it normally with but bpy.ops.pose.transforms_clear(), but sometimes it gives me errors However I cant use bpy.ops since it gives me the following error: RuntimeError: Operator bpy.ops.pose.transforms_clear.poll() failed, context is incorrect

So, is there an alternative way?

$\endgroup$
2
$\begingroup$

Set each pose bone basis matrix to identity

Note: assuming there is no animation involved

With an armature object active and selected

>>> rig = C.object 

>>> rig
bpy.data.objects['Armature']

Clearing all pose transforms is the equivalent of setting the pose bone matrix basis to identity

>>> for pb in rig.pose.bones:
...     pb.matrix_basis.identity()
...     
>>> 

Use context.selected_pose_bones to narrow selection to those selected. (to be more in line with operator function)

However, like the operator context issues, this is only available in pose mode, which is quite likely the reason for poll failed error for bpy.ops.pose... operators.

Test in console, showing selected pose bone list is None and operator doesn't poll in OBJECT mode

>>> C.mode
'OBJECT'

>>> C.selected_pose_bones
>>> C.selected_pose_bones is None
True

>>> bpy.ops.pose.transforms_clear.poll()
False

>>> bpy.ops.object.mode_set()
{'FINISHED'}

But if switch to POSE mode (Side note: left in hander messages from running script here FWIW changing mode updates the depsgraph)

>>> bpy.ops.object.mode_set(mode='POSE')
1 : depsgraph_update_pre
1 : depsgraph_update_post
{'FINISHED'}

>>> C.mode
'POSE'

>>> C.selected_pose_bones
[bpy.data.objects['Armature'].pose.bones["Bone"]]

>>> bpy.ops.pose.transforms_clear.poll()
True

Set armatures pose_postion enum.

To switch into REST mode, The switch is in the UI in the DATA properties panel

, meaning its a property of the data part, (the edit bones)

>>> rig.data
bpy.data.armatures['Armature']

It's in rest pose

>>> rig.data.pose_position
'REST'

Change to pose position

>>> rig.data.pose_position = 'POSE'
>>> 
$\endgroup$
1
  • 1
    $\begingroup$ Such a great answer. Thanks! $\endgroup$
    – Manveru
    Jul 8 at 11:31

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.