I wrote the following function to get the index of the closest vertex to a certain UV coordinate :
def getClosestVertexIndex(targetObject, targetUv):
vertexCount = cmds.polyEvaluate(targetObject, vertex=True)
minSqrDistance=2
minVertexIndex=-1
for i in xrange(vertexCount):
vertexAttr = "{0}.vtx[{1}]".format(targetObject,i)
uvAttr = cmds.polyListComponentConversion(vertexAttr, toUV=True)
print vertexAttr,uvAttr
uvs = cmds.getAttr(uvAttr)
for uv in uvs:
print uv,targetUv
sqrDistance = sum([(a-b)**2 for a,b in zip(targetUv,uv)])
if sqrDistance < minSqrDistance:
minSqrDistance = sqrDistance
minVertexIndex = i
return minVertexIndex
The problem is, that even though the attribute names for the getAttr call are legal (for example, pCube1.map[0] etc), I get (0,0) in all of the UVs. I tried manually calling cmds.getAttr('pCube1.vtx[0]') and also got a (0,0,0) vector.
What am I doing wrong?
(To make it simple, in Maya 2011 I create a cube (called pCube1) and do cmds.getAttr('pCube1.vtx[2]') , and get (0,0,0) as the result, no matter what the index of the vertex is)