Mel Script - Nurbs Curves [API]

 

 

 

 

Nurbs Curves within Maya are geometry objects that are parented underneath a transform node.

 

 

 

Creating a NURBS Curve

To create a NURBS curve in mel, simply using the curve command using the -p flag to specify the point values.

 

 


		// Use the curve command to create a curve
		curve -p 0 0 0 -p 5 0 0 -p 5 0 5 -p 0 0 5;

 

 

 

Finding All Nurbs Curves in a Scene

fgetword simply reads the next word in the file and returns it as a text string.

 

 


		// get a list of all curves in the scene
		$curves = `ls -type "curve"`;

		// loop through each curve
		for( $curve in $curves )
		{
			// print the name of the curve
			print( $curve + "\n" );
		}
		

 

 

Accessing local Space Control Points

Nurbs Curve information should be accessed using a curve info node. Once attached to the curve this should provide you will all the information you need for the curve.

 

 


		proc getCurveCvs( string $curve )
		{
			//	attach a curve info node to the curve
			string $infoNode = `createNode curveInfo`;
			connectAttr ($curve +".worldSpace")  ($infoNode+".inputCurve");
				
			// Curve Degree
			int $degree = eval("getAttr " + $curve + ".degree");

			// Curve Spans
			int $spans = eval("getAttr "+$curve+".spans");
		
			//	output the cv count
			int $numCVs = $spans + $degree;
			print ("numCVs = "+$numCVs+"\n");
		
			//	output all cvs 
			for($i=0;$i<$numCVs;$i++)
			{
				float $cv[]   = `pointPosition -l ( $curve + ".cp["+$i+"]" )`;
		
				print($cv[0]+" "+$cv[1]+" "+$cv[2]+"\n");
			}
			// delete the curve info node
			delete $infoNode;
		}

 

 

Accessing Knot Vectors

Knot vectors in Maya are slightly different to those normally described in 3D Geometry textbooks.

 

 


		proc getCurveKnots( string $curve )
		{
			//	attach a curve info node to the curve
			string $infoNode = `createNode curveInfo`;
			connectAttr ($curve +".worldSpace")  ($infoNode+".inputCurve");
		
			// Curve Type
			int $form = eval("getAttr " + $curve + ".form");
				
			//	get the knot values
			float $Knots[] = `getAttr ($infoNode+".knots")`;
		
			//	output knots
			print( "numKnots= " + (`size($Knots)`+2) + "\n");
		
			//	insert an additional knot at the front....
			if($form == 2)
			{
				print ($Knots[0]-1);
			}
			else
			{
				print $Knots[0];
			}
			for($i=0;$i< size($Knots);$i++)
			{
				print (" "+$Knots[$i]);
			}

			//	insert an additional knot at the end....
			if($form == 2)
			{
				print (($Knots[size($Knots)-1]+1) + "\n");
			}
			else
			{
				print ($Knots[size($Knots)-1] + "\n");
			}
			// delete the curve info node
			delete $infoNode;
		}

 

 

Accessing Curve Information

Nurbs Curve information should be accessed using a curve info node. Once attached to the curve this should provide you will all the information you need for the curve.

 

 


		proc getCurveInfo( string $curve )
		{
			//	attach a curve info node to the curve
			string $infoNode = `createNode curveInfo`;
			connectAttr ($curve +".worldSpace")  ($infoNode+".inputCurve");
				
			// Curve Type
			int $form = eval("getAttr " + $curve + ".form");
			
			// Curve Degree
			int $degree = eval("getAttr " + $curve + ".degree");

			// Curve Spans
			int $spans = eval("getAttr "+$curve+".spans");
		
			// delete the curve info node
			delete $infoNode;
		}