March 2019: ewertb.soundlinker.com is getting relocated to ewertb.mayasound.com, please update your bookmarks.

MEL How-To #73

Back · Previous · Next Maya

How do I determine the length of a NURBS curve?

Using 'arclen'

The ‘arclen’ MEL command can be used to query the total length of a NURBS curve:

string $curve = `curve -d 1 -p -2.1 0.0 -2.0 -p 0.5 1.0 -3.2 -p 1.4 0.3 -1.2 -k 0 -k 1 -k 2`;
// Result: curve1 //

float $curveLength = `arclen $curve`;
// Result: 5.335323 //

If the ‘-constructionHistory’ flag is specified the command creates a "curveInfo" node downstream of the curve. This offers a persistent node which you can use to track changes to the curve.

string $curveInfo = `arclen -ch true $curve`;
// Result: curveInfo1 //

getAttr ( $curveInfo + ".arcLength" );
// Result: 5.335323 //

move -r -os -wd 0 0 1.0 ( $curve + ".cv[1]" );

getAttr ( $curveInfo + ".arcLength" );
// Result: 4.309423 //

Using 'arcLengthDimension'

Create an "arcDimension" node and attach it to the end CV for the curve, then read the ‘.arcLength’ attribute.

string $curve = `curve -d 3 -p -5.4 0.0 -5.5 -p -6.0 0.0  4.1
                            -p  3.4 0.0 -4.7 -p  7.8 0.0 -0.7
                            -p  0.3 0.0  2.3
                            -k -2 -k -1 -k 0 -k 1 -k 2 -k 3 -k 4`;

// Determine the U range for this curve.
float $maxU = `getAttr ( $curve + ".maxValue" )`;

// Create an arcLengthDimension node, attached downstream of this curve.
string $arcLD = `arcLengthDimension ( $curve + ".u[" + $maxU + "]" )`;

float $arcLength = `getAttr ( $arcLD + ".arcLength" )`;
// Result: 1246.701327 //

NOTE: This will return the arc length in centimeters regardless of your UI Unit Preference.

This method has the added advantage for querying the length along the curve at any parameter value.

setAttr ( $arcLD + ".uParamValue" ) 1.0;
$arcLength = `getAttr ( $arcLD + ".arcLength" )`;
// Result: 784.586627 //

setAttr ( $arcLD + ".uParamValue" ) 0.7071;
$arcLength = `getAttr ( $arcLD + ".arcLength" )`;
// Result: 539.611324 //

The "arcLengthDimension" command returns the shape node. If you want to delete the ‘arcLengthDimension’ node when you are finished you should pop $arcLD from its shape and delete the transform.

string $transform[] = `listRelatives -parent $arcLD`;
delete $transform[0];

Acknowledgements


Related How-To's

Wednesday, October 10, 2001