[Tips] “Set Inverse” for “Child Of” Constraint using Blender Python
This is a trouble shooting for using bpy.ops.constraint.childof_set_inverse.
Trouble
When I tried to set “Set Inverse” for “Child Of” constraint in bone and executed bpy.ops.constraint.childof_set_inverse(constraint=”Child Of”, owner=’BONE’) on Blender, the following error occurred. How can we solve it?
>>> bpy.ops.constraint.childof_set_inverse(constraint="Child Of", owner='BONE') Traceback (most recent call last): File "<blender_console>", line 1, in <module> File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\scripts\modules\bpy\ops.py", line 201, in __call__ ret = op_call(self.idname_py(), None, kw) RuntimeError: Operator bpy.ops.constraint.childof_set_inverse.poll() Context missing 'constraint'
Environment
Windows 10
Blender 2.83
Solution
To execute this operation, we should pass the context that contains target constraint and activate the target bone in pose mode.
import bpy bone_name = "foot.IK.L" bpy.ops.object.mode_set(mode='POSE', toggle=False) pose_bone = bpy.context.active_object.pose.bones[bone_name] # set the target constraint to the context context_py = bpy.context.copy() context_py["constraint"] = pose_bone.constraints["Child Of"] # activate target bone bpy.context.active_object.data.bones.active = pose_bone.bone bpy.ops.constraint.childof_set_inverse(context_py, constraint="Child Of", owner='BONE')