HOWEVER.. my problem is that i want my script to find the exact spot on my mesh that is closest, not just a cv point...
It seems like it should be easy, because whenever i make an object 'live' and start to move another object around it slides smoothly along every fractional point of the 'live' surface. What my script achieves is a zigzag effect with the curve placing its points on the mesh vertex's rather than the literal closest point on the mesh.
can anyone suggest any ways around this. Im sure theres a simple solution. Im just being dumb.
here is my code. To make it work create a sphere and then draw a linear ep curve next to it and run code...THANKYOU IN ADVANCE!!!:
//////////////////////////////////////////////////
import maya.cmds as cmds
import maya.api.OpenMaya as OpenMaya
import pymel.core as pm
i=0
geo = pm.PyNode("pSphere1")
try:
selectionList = OpenMaya.MSelectionList()
selectionList.add(geo.name())
nodeDagPath = selectionList.getDagPath(0)
except:
raise RuntimeError('maya.api.OpenMaya.MDagPath() failed on %s' % geo.name())
curveSpanCount = cmds.getAttr('curve1' + ".spans")
curveCvCount = curveSpanCount+1
while i < curveCvCount:
curveCvPos = cmds.pointPosition('curve1' + ".cv" + str([i]))
mfnMesh = OpenMaya.MFnMesh(nodeDagPath)
point = OpenMaya.MPoint(curveCvPos[0], curveCvPos[1], curveCvPos[2])
space = OpenMaya.MSpace.kWorld
closestPoint, faceIdx = mfnMesh.getClosestPoint(point, space)
faceVerts = [geo.vtx[j] for j in geo.f[faceIdx].getVertices()]
closestVert = None
minLength = None
for v in faceVerts:
thisLength = (curveCvPos - v.getPosition(space='world')).length()
if minLength is None or thisLength < minLength:
minLength = thisLength
closestVert = v
vertPos=closestVert.getPosition(space='world')
cmds.xform('curve1' + ".cv" + str([i]), t= (vertPos[0], vertPos[1], vertPos[2]), a=True, ws=True)
i+=1
import maya.cmds as cmds
import maya.api.OpenMaya as OpenMaya
import pymel.core as pm
i=0
geo = pm.PyNode("pSphere1")
try:
selectionList = OpenMaya.MSelectionList()
selectionList.add(geo.name())
nodeDagPath = selectionList.getDagPath(0)
except:
raise RuntimeError('maya.api.OpenMaya.MDagPath() failed on %s' % geo.name())
curveSpanCount = cmds.getAttr('curve1' + ".spans")
for i in cmds.getAttr('curveShape1.controlPoints', multiIndices=True):
curveCvPos = cmds.pointPosition('curve1' + ".cv" + str([i]))
mfnMesh = OpenMaya.MFnMesh(nodeDagPath)
point = OpenMaya.MPoint(curveCvPos[0], curveCvPos[1], curveCvPos[2])
space = OpenMaya.MSpace.
kWorld
closestPoint, _ = mfnMesh.getClosestPoint(point, space)
for v in faceVerts:
cmds.xform('curve1' + ".cv" + str([i]), t= (closestPoint[0], closestPoint[1], closestPoint[2]), a=True, ws=True)