I'm trying to place an object at a random place on a plane. So far, I found out how to get the vertices locations in real world.
import bpy
obj = bpy.context.active_object
vert = obj.data.vertices
vertex_0 = obj.matrix_world @ vert[0].co
vertex_1 = obj.matrix_world @ vert[1].co
vertex_2 = obj.matrix_world @ vert[2].co
vertex_3 = obj.matrix_world @ vert[3].co
print(vertex_0, vertex_1)
But now, I'm stuck. How can I give an object coordinates somewhere between those vertices?
I can imagine how this would work in 2 dimensions, but with a rotated plane in 3D? My head explodes.
EDIT:
Another idea was to work in local space. Parent an object to the plane and work from there. But then will the child object be scaled with the parent object. And that is not what I want. So I used the "Child of" constraint and disabled the scale like this:
import bpy
obj = bpy.context.object
# put the location on a vertex of the plane
obj.location = (-1,1,0)
# give the constraint to the selected object
obj.constraints.new('CHILD_OF')
obj.constraints["Child Of"].use_scale_x = False
obj.constraints["Child Of"].use_scale_y = False
obj.constraints["Child Of"].use_scale_z = False
obj.constraints["Child Of"].target = bpy.context.scene.objects['Plane']
But after clicking the "Clear Inverse" Button in the constraint, the objects location is not on the vertex of the plane how it should be (when the plane is scaled before the script is loaded). Instead, the origin of the object will be on the expected vertex, when the Scale X, Y and Z in the Constraint is enabled again. (Scale is disabled in the script).
So: How can I put an object on another object and having a child relationship without scaling but with the right location?