Last active
September 11, 2024 19:04
-
-
Save hdlx/67a97305ffbe284e5f104d8b4f9eb0f2 to your computer and use it in GitHub Desktop.
Maya get closest vertex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import maya.api.OpenMaya as om | |
import maya.cmds as cmds | |
#returns the closest vertex given a mesh and a position [x,y,z] in world space. | |
#Uses om.MfnMesh.getClosestPoint() returned face ID and iterates through face's vertices. | |
def getClosestVertex(mayaMesh,pos=[0,0,0]): | |
mVector = om.MVector(pos)#using MVector type to represent position | |
selectionList = om.MSelectionList() | |
selectionList.add(mayaMesh) | |
dPath= selectionList.getDagPath(0) | |
mMesh=om.MFnMesh(dPath) | |
ID = mMesh.getClosestPoint(om.MPoint(mVector),space=om.MSpace.kWorld)[1] #getting closest face ID | |
list=cmds.ls( cmds.polyListComponentConversion (mayaMesh+'.f['+str(ID)+']',ff=True,tv=True),flatten=True)#face's vertices list | |
#setting vertex [0] as the closest one | |
d=mVector-om.MVector(cmds.xform(list[0],t=True,ws=True,q=True)) | |
smallestDist2=d.x*d.x+d.y*d.y+d.z*d.z #using distance squared to compare distance | |
closest=list[0] | |
#iterating from vertex [1] | |
for i in range(1,len(list)) : | |
d=mVector-om.MVector(cmds.xform(list[i],t=True,ws=True,q=True)) | |
d2=d.x*d.x+d.y*d.y+d.z*d.z | |
if d2<smallestDist2: | |
smallestDist2=d2 | |
closest=list[i] | |
return closest |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks - saved me some time to quickly test a thing.
Here's a quick cleanup/rewrite as thanks!