How to get mesh face at pick point

Hi guys,

how do I determine what mesh face is at a given 3D location? I want to start off a script by analyzing the face at the pick point of rs.GetObjectEx()

Cheers!

Bump :slight_smile:
@pascal, do you know a simple way to find the mesh face that a point lies on?

I think you’re looking for Mesh.ClosestMeshPoint() method.

import Rhino.Geometry as rg
import rhinoscriptsyntax as rs
mesh = rs.GetObject("Select a mesh", rs.filter.mesh)
point = rs.GetPointOnMesh(mesh, "Select a point")
mesh = rs.coercemesh(mesh)
face = mesh.ClosestMeshPoint(point, 1).FaceIndex
print "The point is on face No." + str(face)

Holo.py (288 Bytes)

Important: FaceIndex is an index of a face in mesh.Faces. When ComponentIndex refers to a vertex, any face that uses the vertex may appear as FaceIndex. When ComponenctIndex refers to an Edge or EdgeIndex is set, then any face that uses that edge may appear as FaceIndex. read more

1 Like

Thanks, that works!

But some strange magic (science above my pay-grade) is going on here…
so it is difficult for me to decipher… :wink:

Please enlighten me:
1- you import Rhino.Geometry as rg, but no “rg.something” is being used, is Rhino.Geometry still needed for mesh.ClosestMeshPoint() to work? and was importing it as rg insignificant here?
2- you put the name “mesh” on both the “GetObject” and the “CoerceMesh” “object”, so I understand that now the “geometry object in the file” is forgotten and “mesh” is just a theoretical duplicate.
BUT then you use mesh.ClosestMeshPoint() and that was new to me. Where can I read up on how to call commands on containers(?) like what coerceMesh generates?

Thanks a lot for helping me out though, it was just what I needed!

Not really, I only import it to check available methods and properties of Mesh Class, and then use the proper method on the mesh object.
Rg

coerceX is used to lookup an object from the document, and get it as a scriptable RhinoCommon object.

THANKS!

in the meantime I added stuff to it to duplicate mesh face borders, so now I am good to go.
Are there obvious shortcuts I could have taken? (Should have used instead?)

import Rhino.Geometry as rg
import rhinoscriptsyntax as rs
meshOrig = rs.GetObject("Select a mesh", rs.filter.mesh)
mesh = rs.coercemesh(meshOrig)

while True:
    point = rs.GetPointOnMesh(meshOrig, "Select a point")
    if point:
        
        face = mesh.ClosestMeshPoint(point, 1).FaceIndex
        print "The point is on face No." + str(face)
        
        MeshFaceVertices= rs.MeshFaceVertices(mesh)
        VerticesOfPickedFace = MeshFaceVertices[face]
        
        linePoints=[]
        for vertex in VerticesOfPickedFace :
            
            linePoints.append ( mesh.Vertices[vertex] )
        
        #--- Add the first point to the list to form a closed loop
        linePoints.append ( linePoints[0])
        
        border = rs.AddCurve(linePoints, degree=1)
        
        rs.SelectObject(border)
    
    else:
        break