Setting a 'CHILD_OF' constraint and setting an inverse on an object.['temp2'].select=True;['temp2'];


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


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 =['temp1']
temp2 =['temp2']
const ='CHILD_OF') = 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: