[Maya] closestPointOnMesh incorrect results

Anyone here used closestPointOnMesh to get UV parameters?

If I create a polyPlane and attach it to a CPOM node (either manually or using Constrain > Closest Point), as I change the inputPosition the result position updates correctly, but the UV values only change when I cross into a new polygon. So with a 10x10 plane, the only U values I ever get are .1, .2, .3, etc. The closest vertex index is similarly wonky, only changing when I cross an edge (so no matter where my position is, I will only ever get one closestVertex for each closestFace).

Anyone have experience with this??

i ran into similar issues with closest point on mesh. Here is what you need to do.

you need to connect the worldMesh of the target mesh to the inMesh of the CPOM node.
then you need to connect the worldMatrix of the target to the inputMatrix of the CPOM node.

Basically, the CPOM, by default, is working in Object space, so you need to do some wiring under the hood to get proper world space measurements.

With those connections, I only seem to get accurate measurements when transforms are frozen. If I create a plane and move it, then setup a CPOM, I will get stepped UV values. Once I freeze transforms the values are accurate. Am I doing something wrong? Know how I can get accurate measurements with a transformed piece of geo?

This is the code I’ve been testing with.

import maya.cmds as cmds

mesh = cmds.polyPlane()[0]
cmds.move(1, 0, 1, mesh)
cpom = cmds.createNode('closestPointOnMesh')
inloc = cmds.spaceLocator()[0]
cmds.move(.75, 0, .75, inloc)
shape = cmds.listRelatives(mesh, s=True, f=True)[0]
cmds.connectAttr('%s.worldMesh' % shape, '%s.inMesh' % cpom)
cmds.connectAttr('%s.worldMatrix' % shape, '%s.inputMatrix' % cpom)
cmds.connectAttr('%s.translate' % inloc, '%s.inPosition' % cpom)

print 'With xforms:', cmds.getAttr('%s.u' % cpom)
cmds.makeIdentity(mesh, a=True)
print 'Frozen xforms:', cmds.getAttr('%s.u' % cpom)

# With xforms: 0.300000011921
# Frozen xforms: 0.25

i tried it both with and without frozen xforms. my simple script gives me the same results, but the results are still not entirely accurate.

CPOM gives me the correct face, but not the correct vertex, or the correct UV coordinate.

Looking over my code again, I manually test the distance to each vertex of the closest face to get the REAL closest vertex.

Hmm, yeah, face works fine for me, as does position. Vtx and UV were the wonky ones.

I can get the results using the API, but it just takes a while to run when I’m sampling 10,000 points on a 250,000 poly mesh. I was thinking that using the CPOM node would be faster. In the end it’s not essential, but I was just curious if it was something I was doing wrong, or if the node has short comings.

Thanks.

you could still use CPOM to get the closest Face, then you are just looping over the vertices of the polygon instead of all the vertices in the model.

thats basically what I ended up doing, I have a helper function to get the closest vertex, edge, face, uvs and object.

I like that idea. So you use CPOM to get the closest face and then use that in API calls to get other relevant information?

thats the basic idea but i’m not using the python API, just “maya.cmds”.