I want to place a number of objects on the surface of a mesh (think: terrain, (displaced) ground plane mesh) along the global z-normal. I know my objects' x/y coordinates and need to find the corresponding z value on the mesh surface to do the translation of my objects.
I'd prefer a solution involving scripting. Performance is not a big issue, but the method should be fast enough to handle up to a couple of thousands of objects.
I have found a number of approaches, but I'm unsure where to dig deeper and would appreciate some pointers.
What I looked into so far:
Raycast: I understand that I could place 2 empties - number one just underneath my object, number two shifted far away down along the z-normal, cast a ray and would get a hit on my mesh if it is between the two. However, there may be situations when my target mesh is obstructed and there is no direct line of sight. Still looks like the most promising approach so far. (related issue)
closest_point_on_mesh
: that sounds good too and would avoid the line-of-sight issue, but I don't see a way to restrict this to the z-normal- mathutils-geometry:
intersect_line_plane
sounds also interesting, but my target is not a "plane" (or triangle) but a mesh. I'm unsure how to apply this function to a mesh - do I have to step through all the faces? Is that efficient and if yes, how is this done? - BVH:
mytree.ray_cast
ormytree.find
? - Snapping: maybe in this case, using userspace functions like snapping make sense? But I'd prefer something without reliance on context.
I've tried to use lattice/shrinkwrap to place all (joined) objects in one go, but the shrinkwrap modifier deforms them, which is not what I want.
Thank you!