Setting a 'CHILD_OF' constraint and setting an inverse on an object.


bpy.data.objects['temp2'].select=True;
bpy.context.scene.objects.active=bpy.data.objects['temp2'];

bpy.ops.object.constraint_add(type='CHILD_OF');


context_copy = bpy.context.copy()
context_copy["constraint"] = bpy.data.objects['temp2'].constraints["Child Of"];
context_copy["constraint"].target = bpy.data.objects['temp1'];
#>>>RuntimeError: Error: Could not find constraint data for Child-Of Set Inverse
bpy.ops.constraint.childof_set_inverse(context_copy);

    
print('done');

Everything seems to go as planned until the inverse is set. The error coming out in the console is denoted above. The button can be clicked manually to ‘Set Inverse’ after the script fails at childof_set_inverse() successfully. Is there some reason why yhe constraint data can not be found?

Thank you

You might need to add a manual update inbetween before accessing the constraint?
I would also recommend a low-level access without operators, like:

temp1 = bpy.data.objects['temp1']
temp2 = bpy.data.objects['temp2']
const = temp2.constraints.new('CHILD_OF')
const.target = temp1 
#just a guess
const.inverse_matrix = temp1.matrix_world.inverted()

A similar issue seems to have been discussed here, and there it also was an update problem: