Nearest face of a mesh to a given world coordinate?

What would be the most efficient way to get the nearest face of a mesh to a given world coordinate? This would be done many times in a tight loop with different coordinates.

Is there any built in functions that would help with this? If not, I am leaning towards an octree solution.

there’s a closest_point_on_mesh method you can try

Thanks, I was looking in mesh and bmesh geometry functions. I’ll see how it goes.

There’s also a generic kdtree in mathutils – think that’s where it landed – now that’s backed by the C implementation.

When I was testing it I’d fill it with face center coords (forget the function call, was using bmesh) and using that to find matching faces between meshes.

You might wanna populate a mathutils.kdtree.KDTree with face centers, balance it once, then query as often as needed for closest coord (as long as the mesh doesn’t change). It should be very efficient.

http://www.blender.org/documentation/blender_python_api_2_70_release/mathutils.kdtree.html

bpy polygon centers:
http://www.blender.org/documentation/blender_python_api_2_70_release/bpy.types.MeshPolygon.html#bpy.types.MeshPolygon.center

bmesh face centers (there are 3 different ways to use, with different results):
http://www.blender.org/documentation/blender_python_api_2_70_release/bmesh.types.html#bmesh.types.BMFace.calc_center_bounds

Thanks a bunch. mathutils.kdtree works perfect.

SynaGl0W,

what are you working on? It sounds interesting

-Patrick

Way too many things at once. :eek:

Needed a way to highlight vertices (both vertex colors and vertex weights) that were either facing toward or away from the surface of another complex mesh.

Still can’t believe I scrolled over the kdtree in mathutils. It was one of those days…

It’s just another tool to add to my personal pile that I have been building up:

Vertex Paint:


I am currently in the process of re-factoring the code. Though not in that screenshot, “generative” also has a random color function, and a simplified “highlight vector.” The expression tool can do random colors as well. Highlight facing will also get a distance falloff option.

All tools have a channel option to isolate the operation to a specific channel if need be.

Started with the basics on the vertex paint tools little more than a year ago, and I add tools as I need them.

Cool, so you are vertex coloring based on proximity to another mesh and/or the face direction?

q. Have you seen the proximity vertex group modifier? That my take care of your vertex weights without having to script the kd tree. However the scripting is much more flexible. I’ve always wanted a way to transform the weights from the proximity modifier to a color map in vertex paint. Because switching to weight paint mode to visualize the proximity modifier results is a pain. It looks like you have achieved one of my “back of my mind” projects.


-P

Yes proximity vertex group modifier is not what I want. My need was facing/not facing and not distance. But I’ll combine it with distance just to make it that much more useful. My tool accepts all object types. The surface facing counts only for meshable objects (mesh, curve, surface, meta, text), using a sun lamp uses the sun vector, and for other object types the object origin is default. I can modify it for other object type specifics as I see the need. Right now this is a vertex color tool, but I just transfer the vertex colors to a weight layer when I am done.

All my vcol tools default to selected vertices (all loops of the vertex) unless face masking is enabled in which all tools use the selected face loops (except “set vertex colors soft” which would defeat its purpose).

I did this because I find it absolutely absurd to the highest degree that weight paint has vertex selection masking (
), but vertex paint does not.

Sounds super useful for several applications that I use. Both the coloring via a color map based on some parameter (proximity, face direction etc) and changing vertex weights. I don’t know if you would be willing to share, but I would love to take a peak. If this is a private/proprietary tool, no worries.

-P

I’ll eventually release the w0rkfl0w tools on here, I am right in the middle of other projects and in the middle of the code re-factor for it. Over time it grew into a huge multi-thousand line pile of redundant, disorganized code as a result of copy and paste any time I added another tool.

I code the same way :slight_smile: