Closest_point_on_mesh - closest point from one mesh to another

Hi,

I have 2 meshes and I would like to get the closest point from one vertex in the first mesh to the second mesh.

I did it by taking the coordinate of the vertex in the source mesh:
vertex_index = 0
point = source_obj.data.vertices[vertex_index].co

Then, I define the target_obj to be the active and called closest_point_on_mesh to find the closest point to that point:

bpy.context.view_layer.objects.active = target_obj
closest_point = bpy.context.object.closest_point_on_mesh(point)

But it seems that it’s not the correct one, I see it by selecting the 2 points.
I think that it’s related to the following statement:

Find the nearest point in object space

But I don’t understand how to fix it.

Does anyone has some insight about it?

Closest Point on Tri:
https://docs.blender.org/api/current/mathutils.geometry.html#mathutils.geometry.closest_point_on_tri
KD Tree:
https://docs.blender.org/api/current/mathutils.kdtree.html

This is the way!

1 Like