0

I would like to get a world position of a specific parameter along a nurbs curve defined by a list of positions. Currently I'm creating a temporary curve in the plugin just to get an array of positions along this curve:

    # targetXforms = array of MPoints
    # knots = knots list
    # uValue = array of floats (0->1)
    #

    curveFn = om.MFnNurbsCurve()
    curveFn.create(targetXforms, knots, 3, om.MFnNurbsCurve.kOpen, False, False, nullObj)
    for i in range (numRefXforms):
        point = curveFn.getPointAtParam (uValue[i])
        print point

Is there a better way to do this (i.e. not have the overhead of creating a curve)? Some math libraries perhaps? If not how do I delete this curve so I don't have a curve created every time the plugin is evaluated (MDGModifier seems to be a bit crashy)

Also, is there a way to find length along a curve for a given parameter value. Maya 2016 Extension 2 has a function for this: MFnNurbsCurve::findLengthFromParam()

But, We don't have this extension yet. :(

Thanks in advance!

1 Answers1

0

If you provide a Nurbs curve data object to MFnNurbsCurve.create() as the parent, instead of leaving it null, then the data doesn't appear as a curve in the scene graph and therefore you don't have to remove it.

Try this:

import pymel.core as pm

pts = ( [ 0,0,0 ], [  0,10,0 ], [  0,10,10 ], [ 0,0,10 ])
knots = [0,0,0,1,1,1]

curveFn = om.MFnNurbsCurve()

dataCreator = om.MFnNurbsCurveData()
curveDataObject = dataCreator.create()

curveFn.create(pts, knots, 3, om.MFnNurbsCurve.kOpen, False, False, curveDataObject)

for i in range(11):
    point = curveFn.getPointAtParam (i/10.0)
    pm.spaceLocator(p=(point.x, point.y, point.z))

To get the arc length at a parameter without the API, you could create an arcLengthDimension node. It means you would have to create a curve in the scene and connect it up.

dimNode = pm.arcLengthDimension( 'curveShape1.u[0]' )
dimNode.uParamValue.set( 0.5 )
print(dimNode.arcLength.get()
Julian Mann
  • 6,256
  • 5
  • 31
  • 43