Skip to content

Instantly share code, notes, and snippets.

@AndresMWeber
Created March 15, 2017 04:18
Show Gist options
  • Save AndresMWeber/27ff60f361627d8bed1fa962929c37b8 to your computer and use it in GitHub Desktop.
Save AndresMWeber/27ff60f361627d8bed1fa962929c37b8 to your computer and use it in GitHub Desktop.
Closest position on mesh to point...
import maya.api.OpenMaya as om
import maya.cmds as mc
mpoint_to_point = lambda mpoint: [float(p) for p in mpoint][:3]
loc_at_mpoint = lambda point: mc.xform(mc.spaceLocator(), t=point, ws=True)
dag_iterator = om.MItDag(om.MItDag.kDepthFirst, om.MFn.kInvalid)
def get_dag_path(xform):
selectionList = om.MSelectionList()
try:
selectionList.add(xform)
except:
return None
return selectionList.getDagPath(0)
# if we're taking a cube that was just created at origin
dag_path = get_dag_path('pCubeShape1')
shape_mfnMesh = om.MFnMesh(dag_path)
test_point = om.MPoint(2.0, 0.0, 0.0)
uvs = list(shape_mfnMesh.getUVAtPoint(test_point, space=om.MSpace.kWorld, uvSet='map1'))
face_id = uvs.pop()
loc_at_mpoint(mpoint_to_point(test_point))
dag_path.fullPathName()
mesh_polygon = om.MItMeshPolygon(dag_path)
while not mesh_polygon.isDone():
if mesh_polygon.index() == face_id:
point = mesh_polygon.getPointAtUV(uvs, om.MSpace.kWorld)
loc_at_mpoint(mpoint_to_point(point))
mesh_polygon.next(None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment