Completely new to scripting and was hoping someone would help me out here.

I've imported a rig twice and want to add a "Child Of" bone constraint to each bone in the first rig using the matching bone in the second rig as the target. I also want to use "Set Inverse" function of this constraint.

enter image description here

The rigs are the same, so all the bones are named the same.

Rigs are called "root" and "root.001".

I figure I need to do something with:

import bpy

context = bpy.context
scene = context.scene

rig1 = scene.objects.get("root")
rig2 = scene.objects.get("root.001")

for bone in rig1.pose.bones:
    cbone = bone
    cbname = bone.name
    setchild = bone.constraints.new(type='CHILD_OF')
    setchild.target = rig2
    setchild.subtarget = cbname

But beyond that I have no clue. I'm not sure how to set the inverse option.

I'm looking to edit a number of animation files, so would need to do this process for each fbx file, and therefore would love to automate the process.

Hoping someone can help me out. Cheers.

  • $\begingroup$ Hi. There is no property called 'inverse' that can be set. That makes no sense. Could you clarify? $\endgroup$ Nov 22, 2018 at 15:48
  • $\begingroup$ Basically looking to do this: forums.unrealengine.com/development-discussion/animation/… In relation to the set inverse, I'm talking about when you add a constraint in pose mode to set a parent bone you have the option of clicking "set inverse" so the child keeps it's original position.(docs.blender.org/manual/en/dev/rigging/constraints/relationship/…) Seems to be "bpy.ops.constraint.childof_set_inverse()" ? But Again, I don't know anything about Python. $\endgroup$ Nov 22, 2018 at 16:07
  • $\begingroup$ OK, that makes sense then. You are talking about Child Of object constraint, not parenting. There is a difference. $\endgroup$ Nov 22, 2018 at 16:16
  • $\begingroup$ Or is it bone constraint, not object constraint? $\endgroup$ Nov 22, 2018 at 16:20
  • $\begingroup$ I'm assuming bone constraint? It's done in Pose mode? $\endgroup$ Nov 22, 2018 at 16:26

1 Answer 1

import bpy

ob = bpy.context.active_object

# Take a copy of current layers 
org_layers = ob.data.layers[:]

# Show all layers
for i in range(len(org_layers)):
    ob.data.layers[i] = True

for b in ob.pose.bones:
    for c in b.constraints: 
        if c.type == "CHILD_OF":
            context_py = bpy.context.copy()
            context_py["constraint"] = c
            ob.data.bones.active = b.bone
            bpy.ops.constraint.childof_set_inverse(context_py, constraint="Child Of", owner='BONE')

# Reset back to orginal layer state    
for i in range(len(org_layers)):
    ob.data.layers[i] = org_layers[i]

Found by Ratt, cheers guys! (let me know if this is what you meant)!


You must log in to answer this question.

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