获取最近点


import maya.api.OpenMaya as om2
import maya.cmds as mc
 
def dagObjFromName(name):
        sel = om2.MSelectionList()
        sel.add(name)
       
        # 如果没有Dagpath会报错
        # dag =sel.getDagPath(0)
       
        mobj = sel.getDependNode(0)
       
        return mobj, dag
 
 
def locatorToPoint(locator):
        pos = mc.xform(locator, q=1, rp=1, ws=1)
        return om2.MPoint(pos[0], pos[1], pos[2],1.0)
 
def createLocatorAtClosestPoint(mySphere, myLocator):
        obj,dag = dagObjFromName(mySphere)     
        meshFn = om2.MFnMesh(dag)
 
        point = locatorToPoint(myLocator)
 
        resultPoint = meshFn.getClosestPoint(point,om2.MSpace.kWorld)[0]
       
        result = mc.spaceLocator()[0]
       
        mc.setAttr(result+'.t', resultPoint[0], resultPoint[1], resultPoint[2])
 
 
mySphere = mc.polySphere()[0]
myLocator = mc.spaceLocator()[0]
# now move locator arround and execute
createLocatorAtClosestPoint(mySphere, myLocator)

评论
  目录