Samiam’s Scribble Pad

June 9, 2009

length of a curve in Maya

Filed under: Uncategorized — Tags: — admin @ 5:10 pm

I found a reasonable solutionhere:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//=================================
// curveLength v1.0 (08/2004)
// by Edvard Toth
// 
// The script is freeware. Non-commercial redistribution is permitted as long as this header remains included and unmodified.
// Your feedback is always appreciated - if you find the script useful or if you have questions, comments, suggestions, requests, 
// bug-reports, if you created an updated version, to check for updates or to make a donation please contact me at:
//
// http://www.edvardtoth.com
// 
//==================================
// INSTALLATION: Copy the script into your Maya script-directory and start it with the curveLength; command.
//
// COMPATIBILITY NOTE: Tested with Maya 4.0 - 4.5 - 5.0 - 6.0 - 7.0
//
// DESCRIPTION:
// This tiny but very straightforward utility measures the length of a curve without all the hassle that comes with 
// using Maya's implementation of the "Arc Length Tool".
//
 
global proc curveLength ()
{
 
string $CL_curve[] = `ls -sl`;
 
	if (size($CL_curve[0]) == 0)
    {
	confirmDialog -t "Oops..." -m "Please select a valid curve." -b Continue;
	return;
	}
 
	string $CL_curvea[] = `ls -l $CL_curve[0]`;
	string $CL_curveb[] = `listRelatives -f -ni -s $CL_curvea[0]`;
 
	if (nodeType($CL_curveb[0])!="nurbsCurve")
    {
	confirmDialog -t "Oops..." -m "Please select a valid curve." -b Continue;
	return;
	}    
 
        int $CL_spans = `getAttr ($CL_curve[0] + ".spans")`;
 
        string $CL_arclengthnode = `arcLengthDimension ($CL_curve[0] + ".u[" + $CL_spans + "]")`;
        string $CL_nodeparent[] = `listRelatives -p $CL_arclengthnode`;
 
        float $CL_curvelength = `getAttr ($CL_arclengthnode + ".al")`;
 
        delete $CL_nodeparent;
 
        confirmDialog -t $CL_curve[0] -m ("curvelength:  " + $CL_curvelength) -b OK;    
}

but since I hate Mel I have ported it to python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
def lengthOfCurve(dagPathToCurve):
	"""
	Assume that the Shape path is given, error checking is for upstream
	"""
	import maya
	numSpans = maya.cmds.getAttr("%s.spans" % dagPathToCurve)
	arcLengthTemp= maya.cmds.arcLengthDimension("%s.u[%s]" % (dagPathToCurve,numSpans))
 
	nodeParent = maya.cmds.listRelatives(arcLengthTemp,parent=True)
	curveLength = maya.cmds.getAttr ("%s.al" % arcLengthTemp)
	maya.cmds.delete(nodeParent)
	return curveLength
 
 
for i in maya.cmds.ls(selection=True):
	shapeNodes = maya.cmds.listRelatives(i,shapes=True)
	for shape in shapeNodes:
		if maya.cmds.nodeType(shape) == "nurbsCurve":
			print "Curve: %s is %s units long" % (shape, lengthOfCurve(shape))
		else:
			print "Wrong: %s is a %s" % (shape, maya.cmds.nodeType(shape))

As Nick so rightly pointed out the following makes a lot more sense

sometimes rtfm > google

arclen docs

1
2
3
4
5
6
7
8
9
import maya
 
for i in maya.cmds.ls(selection=True):
	shapeNodes = maya.cmds.listRelatives(i,shapes=True)
	for shape in shapeNodes:
		if maya.cmds.nodeType(shape) == "nurbsCurve":
			print "Curve: %s is %s units long" % (shape, arcLen(shape,constuctionHistory=False))
		else:
			print "Wrong: %s is a %s" % (shape, maya.cmds.nodeType(shape))

Powered by WordPress