Reviving this old thread. It seems like creating a temporary node and sampling it is the way to go.
Here's a bit of python for returning either a list of nodes, or a list of uvVals
You will need a sample surface, an array of points, and a choice between uvVals or nodes.
Select your sample surface and assign it with 'sampleSurface = cmd.ls(sl=True)[0]'
Supply an array of points to sample. Select the objects - locators or whatever, and run this:
points = []
for i in cmd.ls(sl=True):
point = cmd.xform(i, q = True, t = True, ws = True)
points.append(point)
Here is the function to return either values or nodes, depending on what you need
import maya.cmds as cmd
import maya.mel as mel
gA = cmd.getAttr
sA = cmd.setAttr
cA = cmd.connectAttr
cN = cmd.createNode
def getClosestPointsOnSurface(points, sampleSurface, type = 'nodes'):
returnVal = None
cposNodes = []
vals = []
split = sampleSurface.split('_')
surf = cmd.listRelatives(sampleSurface, c = True, ni = True, type = 'nurbsSurface')[0]
if type == 'nodes':
for i in range(len(points)):
cPos = cN('closestPointOnSurface', ss = True, n = '%s_%02d_cPos' % (split[0], i))
cA('%s.worldSpace[0]' % surf, '%s.inputSurface' % cPos)
sA('%s.inPosition' % cPos, points[i][0], points[i][1], points[i][2])
cposNodes.append(cPos)
returnVal = cposNodes
if type == 'vals':
for i in range(len(points)):
cPos = cN('closestPointOnSurface', ss = True)
cA('%s.worldSpace[0]' % surf, '%s.inputSurface' % cPos)
sA('%s.inPosition' % cPos, points[i][0], points[i][1], points[i][2])
u = gA('%s.result.parameterU' % cPos )
v = gA('%s.result.parameterV' % cPos )
vals.append([u,v])
cmd.delete(cPos)
returnVal = vals
return returnVal
Finally, the example command to run
cposNodes = getClosestPointsOnSurface(points, sampleSurface, type = 'nodes')
uvVal = getClosestPointsOnSurface(points, sampleSurface, type = 'vals')
sampleSurface
Result: u'face_def_surf'
points
Result: [[-9.934378258772887e-05, 56.52158927169142, 13.096842334001089],
[-1.2986319080810456, 56.573714757526794, 13.019487214074514],
[-2.3988572845724603, 56.81671021609732, 12.825554268688125],
[-2.887766742811174, 57.28272070017139, 12.744081627653983],
[-2.9564472997142497, 57.50138883790414, 12.769442914295668],
[-2.879458555286166, 57.672685482014, 12.879225548927492],
[-2.192046597368875, 57.79139587725608, 13.341300849584687],
[-1.140138085103556, 57.811148683446966, 13.800117232849214],
[-0.0007878168005395606, 57.81488077210223, 13.925655903295926],
[1.1383058170299245, 57.81117323292432, 13.796743621692903],
[2.191000144083259, 57.79144462951285, 13.339583497622566],
[2.879303203717313, 57.67272400054267, 12.878886599401458],
[2.9563649858235075, 57.50140832665556, 12.769194174384452],
[2.887671243257571, 57.282744486066534, 12.743798227096866],
[2.398733183100953, 56.81673428803417, 12.825116354417371],
[1.2984150038822726, 56.57377235530355, 13.018506756780637]]
uvVal
Result: [[1.0004341542613102, 0.2301692487434747],
[1.2064782285634066, 0.21627448891449497],
[1.3722637684008845, 0.28391258246162415],
[1.509186903505303, 0.3387848920830241],
[1.563309727886676, 0.36916657892685023],
[1.6002270473405233, 0.369506881916711],
[1.6601835924689776, 0.24208844308577132],
[1.7978056591853022, 0.10984749089757448],
[1.9998468370341644, 0.10376750763367472],
[0.20215057952270601, 0.10949474728688213],
[0.33988142055839465, 0.24195245274626373],
[0.39978054787266903, 0.3695019564741386],
[0.4366984973876268, 0.3691695706042126],
[0.4908336188623674, 0.3387896663238795],
[0.6277812551226932, 0.2839743883775245],
[0.7935635789475637, 0.21646212421961572]] #
cposNodes
Result: [u'face_00_cPos',
u'face_01_cPos',
u'face_02_cPos',
u'face_03_cPos',
u'face_04_cPos',
u'face_05_cPos',
u'face_06_cPos',
u'face_07_cPos',
u'face_08_cPos',
u'face_09_cPos',
u'face_10_cPos',
u'face_11_cPos',
u'face_12_cPos',
u'face_13_cPos',
u'face_14_cPos',
u'face_15_cPos'] #