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)


Den tisdagen den 12:e augusti 2014 kl. 01:11:14 UTC+2 skrev 
[email protected]:
>
> 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
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/265b7698-4aeb-424f-b5a7-6bab27f93826%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to