Using getCloserPoint from Maya's Python API


#1

Hi !

I’m trying to get the closest point (actually the closest normal, but the problem is more obvious here) on a mesh relative to a point in space. I came up to the api.openmaya library and the MFnMesh.getClosestPoint method.
I think i managed to relate properly the actual mesh to an openmaya object, the MFnMesh. Now it’s almost working, but the point resulting ist not really the closest one as you can see on the picture.


Here’s my code :

#create an MFnMesh for the selected mesh
obj = cmds.ls(sl = True)[0]
selection_list = om.MSelectionList()
selection_list.add(obj)
obj= om.MObject()
dpath= om.MDagPath()
dpath= selection_list.getDagPath(0)
MObject=om.MFnMesh(dpath)

#calculate and display closest point
MPoint = MObject.getClosestPoint (point, 4)	
displayedCurve = cmds.curve( p=[(0, 0, 0), (MPoint[0][0],MPoint[0][1],MPoint[0][2])] )

Maybe i’m missing something on the getClosestPoint utility ?
I just discovered the whole openmaya stuff :slight_smile:
Thank’s for helping :slight_smile:


#2

this may or may not be helpful, but when i was trying to use the closestPointOnMesh node through the Python commands, i found that there was an issue with Transforming the geometry into the correct space.

instead of plugging the mesh directly into the closestPointOnMesh node, i had to first connect the mesh to a matrix node, then connect the matrix to a transformGeometry node, then connect the transformGeometry node into the closestPointOnMesh node.


#3

Thank’s for helping. Actually; I did’nt even think about creating and using the constraint node, since i was focused on using the openmaya objects.
So i’ll have a look on it and first see how to properly create the node and query the value.
But i’d still like to achieve it throught openmaya if possible :slight_smile:

EDIT : after a few experimentations, you are right, this is definitly related to a world/object coordinate problem, in both situation (openmaya or node).
So I’ll try to play with matrix to correct the problem. I’m not very used to it but OM seems to have great objects and methods to apply and modify thoses.
I think doing it whole in OM would be cleaner.


#6

Ok after a few more or less random trials, this seems to work in anycase :

#create an MFnMesh for the selected mesh
obj = cmds.ls(sl = True)[0]
selection_list = om.MSelectionList()
selection_list.add(obj)
print selection_list
obj= om.MObject()
dpath= om.MDagPath()
dpath= selection_list.getDagPath(0)
MObject=om.MFnMesh(dpath)

#get meshs transformation matrix
MMatrix =  dpath.inclusiveMatrix()

#calculate and display closest point
point=om.MPoint()
point = MMatrix*point
MPoint = MObject.getClosestPoint (point, 4)	
MPoint= MPoint[0]
displayedCurve = cmds.curve( p=[(0, 0, 0), (MPoint[0],MPoint[1],MPoint[2])] )

Thanks a lot !