Closest Point on Curve (script)
Here's a python script i wrote recently... closestPointOnCurve. as i previously posted, maya doesn't have a built in node for getting this info, so i wrote my own. this code below is just the *script* version; i'll post the plugin node version another time.
you would call this with a location (world space point) and a curve object. for example:
import jc_closestPointOnCurve as cpoc
cpoc.jc_closestPointOnCurve([0, 0, 0], "curve1")
what you would get returned is the parameterU along the curve, and then the worldspace position of that point along the curve.
feel free to email me any questions :)
you would call this with a location (world space point) and a curve object. for example:
import jc_closestPointOnCurve as cpoc
cpoc.jc_closestPointOnCurve([0, 0, 0], "curve1")
what you would get returned is the parameterU along the curve, and then the worldspace position of that point along the curve.
import maya.OpenMaya as om
import maya.cmds as mc
# input curve
# input point
# output param
# output point
def jc_closestPointOnCurve(location, curveObject):
curve = curveObject
# put curve into the MObject
tempList = om.MSelectionList()
tempList.add(curve)
curveObj = om.MObject()
tempList.getDependNode(0, curveObj) # puts the 0 index of tempList's depend node into curveObj
# get the dagpath of the object
dagpath = om.MDagPath()
tempList.getDagPath(0, dagpath)
# define the curve object as type MFnNurbsCurve
curveMF = om.MFnNurbsCurve(dagpath)
# what's the input point (in world)
point = om.MPoint( location[0], location[1], location[2])
# define the parameter as a double * (pointer)
prm = om.MScriptUtil()
pointer = prm.asDoublePtr()
om.MScriptUtil.setDouble (pointer, 0.0)
# set tolerance
tolerance = .00000001
# set the object space
space = om.MSpace.kObject
# result will be the worldspace point
result = om.MPoint()
result = curveMF.closestPoint (point, pointer, 0.0, space)
position = [(result.x), (result.y), (result.z)]
curvePoint = om.MPoint ((result.x), (result.y), (result.z))
# creates a locator at the position
mc.spaceLocator (p=(position[0], position[1], position[2]))
parameter = om.MScriptUtil.getDouble (pointer)
# just return - parameter, then world space coord.
return [parameter, (result.x), (result.y), (result.z)]
feel free to email me any questions :)
3 Comments:
Hi Josh, Thanks for the script.
When I try to source the script I get this error:
# Error: ("'return' outside function",('{maya console}', 55, None, None))
# File"{maya console}, line 54
#SyntaxError: ("'return' outside function", ('{maya console}', 55, None, None)) #
tim
(I used {} brackets instead of the angled brackets <> because the comment window would not accept them)
ah, double check the spacing on that return line. it should just be tabbed in once. it looks like it has more whitespace than it should in my post...
i'll see if i can correct that. thanks!
Scratchin' my head over this one, but I'm pretty craptastic at python.
I LOVE the script. I've been wanting a fast way to get a point on the curve with a return of the parameter.
It works great when I run it from the script editor, but saving it out and sourcing it gives me the error:
// Error: "C:\...\jc_closestPointOnCurve.py" line 4.1: Syntax error
not sure what's causing the syntax error, I redid all the white space and everything.
Any ideas?
Post a Comment
<< Home