5
$\begingroup$

I am trying to apply Set Inverse on all the ChildOf Constraints on the armature, using animation nodes Script Node in blender 2.82. But when triggering the script I get only this error:

enter image description here

How can I fix that issue?

import bpy

if inverse: 
    oby = bpy.data.objects['metarig2']
    bpy.context.view_layer.objects.active = oby
    pbones = bpy.data.objects['metarig2'].pose.bones

    for pbone in pbones:

        isConstraint = False
        if len(pbone.constraints)>0:
            for const in pbone.constraints:

                if const.type == "CHILD_OF":
                    context_copy = bpy.context.copy()
                    context_copy["constraint"] = pbone.constraints["Child Of"]
                    #bpy.data.objects['metarig2'].data.bones.active = pbone.bone
                    bname = pbone.bone.name

                    bpy.data.objects['metarig2'].data.bones.active = bpy.data.objects['metarig2'].data.bones[bname]
                    bpy.ops.constraint.childof_set_inverse(context_copy, constraint="Child Of", owner='BONE')

    bpy.data.objects['metarig2'].select = False
    activ = bpy.data.objects['metarig']
    bpy.context.scene.objects.active = activ

$\endgroup$
  • $\begingroup$ The canonical answer requirement was set by mistake, the bounty was set to draw more attention. $\endgroup$ – Denis Apr 6 at 23:51
3
+100
$\begingroup$

Then you launch script from AN, bpy.context doesn't include active_object anymore. So, I suggest creating context object from scratch, just 2 elements are necessary:

context_py = {
    "constraint" : const,
    "active_object" : rig
    }

This code works for me (but constantly resetting Inverse makes weird result):

import bpy

if Inverse: 
    rig = bpy.data.objects['metarig2']
    pbones = rig.pose.bones

    for pbone in pbones:
          for const in pbone.constraints:

                if const.type == "CHILD_OF":
                    context_py = {
                        "constraint" : const,
                        "active_object" : rig
                        }

                    bpy.data.objects['metarig2'].data.bones.active = pbone.bone

                    bpy.ops.constraint.childof_set_inverse(context_py, constraint="Child Of", owner='BONE')
| improve this answer | |
$\endgroup$
  • $\begingroup$ I think what happens is that in blender 2.8 it needs first to clear inverse and then set inverse to avoid weird behavior. I will test it when I'm back home. $\endgroup$ – Denis Apr 7 at 23:08
  • $\begingroup$ Works perfect, I just added childof_clear_inverse before childof_set_inverse line to avoid weird behavior. $\endgroup$ – Denis Apr 8 at 4:23

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.