-1

I'm trying to find the closest vertex on a mesh but within a radius. This is to avoid having to loop through all the vertices as that's time consuming. Example, I have 2 shirts with different vertex count and I'm trying to find the closest vertex of vertex1 that's on mesh2's right sleeve on mesh1's right sleeve. I don't want to loop through verts beyond the sleeve as logically no vertices will be close enough. I understand that there is an assumption that there could be other vertices closer, but for the usage I'm looking for, I don't foresee that being an issue.

I have the code to loop through vertices and get the closest point, but if the mesh has a high vert count, it takes a long time, even if it's using the API.

Is there a function in maya that lets you limit vertices based of a radius? Or any tips on how I can write a function that could do that?

dege
  • 65
  • 9

1 Answers1

1

You can use the node nearestPointOnMesh. In maya API, you can look for MFnMesh::closestIntersection class that can raycast (Querying of a point is within a mesh maya python api)

vtx = 'shirt1.vtx[0]'
pos = cmds.pointPosition(vtx)
m = 'shirts2'
objShape = cmds.listRelatives(m, ni=True, type='mesh')[0]
node = cmds.createNode('nearestPointOnMesh')
cmds.connectAttr(objShape + ".worldMesh", node + ".inMesh")
cmds.setAttr(node + ".inPosition", type = 'double3', *pos)
target_pos = cmds.getAttr(node + '.position')[0]
face = cmds.getAttr(node + ".nearestFaceIndex")
DrWeeny
  • 2,487
  • 1
  • 14
  • 17
  • Yeah I tried this out using the API's getClosestPoint function, and that works, but it only returns 1. I want to return a bunch of them within a certain radius. Sorry if my post wasn't clear – dege Feb 27 '19 at 18:10