I’m assuming you’re in python here…
You probably want to read up about some simple maya API stuff, you only need to know a little to get some great functionality in your python scripts. Learn about MObjects and MDagPaths etc…
Find the docs for MFnMesh, browse down to closestPoint. Have a read.
I had a quick test of this code and it ran ok, see if it works for you
import maya, maya.OpenMaya
# Name of object
name = '|pCube1|pCubeShape1'
# Pointer to object in maya's memory...sort of
nodeDagPath = maya.OpenMaya.MObject()
try:
selectionList = maya.OpenMaya.MSelectionList()
selectionList.add(name)
nodeDagPath = maya.OpenMaya.MDagPath()
selectionList.getDagPath(0, nodeDagPath)
except:
raise RuntimeError('NameToNode() failed on %s' % name)
# A function set to wrap around the object, indicating you can perform mesh operations on it
mfnMesh = maya.OpenMaya.MFnMesh(nodeDagPath)
pointA = maya.OpenMaya.MPoint(0,0,0)
pointB = maya.OpenMaya.MPoint()
space = maya.OpenMaya.MSpace.kWorld
# Maya's way of dealing with pointers
util = maya.OpenMaya.MScriptUtil()
util.createFromInt(0)
idPointer = util.asIntPtr()
mfnMesh.getClosestPoint(pointA, pointB, space, idPointer)
print maya.OpenMaya.MScriptUtil(idPointer).asInt()