I have the following code:
import bpy
obj = bpy.data.objects["Cube"]
target = bpy.data.objects["Lamp"]
const = obj.constraints.new(type='CHILD_OF')
const.name = "Custom Const"
const.target = target
When I run this code in the default scene Cube jumps into the Lamp's position and gets Lamp's orientation (just as expected). Now I have to set inverse for the Cube. I could use something like:
C = bpy.context.copy()
C["constraint"] = const
bpy.ops.constraint.childof_set_inverse(C, constraint=const.name, owner='OBJECT')
But it looks hackish and not always works for multiple objects (at least for me). E.g.:
import bpy
objects = [bpy.data.objects["Cube"], bpy.data.objects["Camera"]]
target = bpy.data.objects["Lamp"]
for obj in objects:
const = obj.constraints.new(type='CHILD_OF')
const.name = "Custom Const"
const.target = target
C = bpy.context.copy()
C["constraint"] = const
bpy.ops.constraint.childof_set_inverse(C, constraint=const.name, owner='OBJECT')
So the main question is how to set inverse for a Child Of constraint without using bpy.ops? I understand that I've to do something with object's matrices but I've no idea what.