How to get correct context in animation nodes script 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:



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)&gt;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
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')