Find Exact UV coordinates at position


#1

“HELP!!! Looking for a way using Python, Pymel, Python API, to find the exact UV coordinate on a nurbsSurface to create a follicle at the position. Can anybody help?”


#2
from maya import cmds
from maya.api import OpenMaya as om

plane = cmds.nurbsPlane()[0]

cmds.select(plane)
sel = om.MGlobal.getActiveSelectionList()

obj = sel.getDagPath(0)

nurbs = om.MFnNurbsSurface(obj)
test_location = om.MPoint(0,0,0)
point, u, v = nurbs.closestPoint(test_location)
print((u, v))

This is most direct way to go from a point, to a nurb’s UV.


#3
import random
import pymel.core as pm 

plane = pm.nurbsPlane()[0]
plane.s.set((2,2,2))

locators = []
for i in xrange(3):
    loc = pm.spaceLocator()
    loc.t.set((0, random.random(), random.random()))
    locators.append(loc)

for loc in locators:
    check = pm.xform(loc, t=True, q=True, ws=True)

    point, u, v = plane.closestPoint(check, space='world')
    print((u, v))

So the question on slack expanded to checking against a set of locators in the world.

This exposed a bug in Maya 2018’s python API 2.0 MFnNurbsSurface.closestPoint function where it would fail if you try to pass a space parameter. Which meant a quick trip over to Pymel, because I didn’t want to rewrite this with API1.0.


#4

This is perfect!!!. Thank you R.White


#5

What if, my scene already has a Nurbs Surface already in it, along with locators already in their position, how would I go about finding the exact UV Cords then?

The Error I receive when trying to do this is ‘str’ object has no Attribute ‘closestPoint’


#6

My guess is that you’re mixing cmds and PyMel, which can lead to error messages like that one.

Something like this should work to get the first nurbs surface, and all the locators in a scene.

plane = pm.ls(type=pm.nt.NurbsSurface)[0]
locators = pm.ls(type=pm.nt.Locator)

#7

Yes, that gets the correct items needed, but when trying this i get a pymel error. “No valid objects supplied to ‘xform’ command.”