Thursday, May 28, 2009

cPoc fyi...

Just to note, and clarify, the closest point on curve script and node i mentioned is different from the one you'll find under create deformers - point on curve.

That particular point on curve deformer uses the curvePointConstraint command, which is basically constraining cvs (or partial cvs) to a locator.

My script/node will do the opposite - a locator/transform can be constrained to the curve, so if you have the curve skinned/deforming/moving, you can have something stick to the curve. again, its the same as the closestPointOnSurface node, but it works on curves.

anyway, just wanted to clarify since a couple people asked :)

Labels: , , ,

Tuesday, May 19, 2009

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.


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 :)

Labels: , ,