wrap a curve to an object....need a genius!!!!

407 views
Skip to first unread message

e955...@gmail.com

unread,
Aug 11, 2014, 7:11:14 PM8/11/14
to python_in...@googlegroups.com
Hi,
i managed to get as far as being able to wrap a curve of any length to any polygon object. My code runs through each point on the curve and checks for the closest point on the polygon mesh and then moves the curve point to the mesh point.

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

Nils

unread,
Aug 17, 2014, 5:21:12 AM8/17/14
to python_in...@googlegroups.com, e955...@gmail.com
Hi

You already had the data you needed in 'closestPoint' so you could just do that instead of the closest face. Here's a new version:

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)
Reply all
Reply to author
Forward
0 new messages