Skip to main content
2 of 2
added 363 characters in body

You need to specify the context and also make sure the bone is active:

import bpy

pbone = bpy.context.active_object.pose.bones["Bone_name"]

context_copy = bpy.context.copy()
context_copy["constraint"] = pbone.constraints["Child Of"]
bpy.context.active_object.data.bones.active = pbone.bone
bpy.ops.constraint.childof_set_inverse(context_copy, constraint="Child Of", owner='BONE')

If the constraint is targeting object + vertex_group instead of armature + bone, make sure the data of the object is updated before setting the inverse. This is for example needed if the target vertex group was also generated by code:

mesh_ob.update_tag({'DATA'})

If not updated entering edit-mode of the mesh or armature will reset the set-inverse.