Mel Script - Locator Nodes [API]

 

 

 

 

Locator nodes are one of the most commonly used node within Maya due entirely to their simplicity. A simple shape is parented under a single transform node.

 

 

 

Creating a locator

Creating a locator node in mel is fairly straight forward, you simply need to call the spaceLocator procedure. You can optionally provide a position for the node by using the -p flag.

 

 

		// construct the full path to a file
		$locator = `spaceLocator -p 0 10 0`;

		// read a word from the file and print
		print( "newNodeName=" + $locator + "\n" );

 

 

Accessing Locator Nodes

Getting a list of all locator nodes is performed by simply using ls as per usual. Normally there is only one thing you really need to know about a locator node, and that is what it's position in space is.

Bizarrely, you can think of a locator node as being a geometry object with a single point. The value of this point is given by the localPosition attribute of locator. This is then transformed into the correct location using the parent transform. This is not fun :( We have to transform the position by the matrix of the locator nodes transform.

 

 

		// construct the full path to a file
		string $nodes[] = `ls -type "locator"`;

		// loop through all locator nodes
		for( $locator in $nodes )
		{
			// print locator name
			print( $locator + "(" );

			// get the parent transform of the locator
			string $parents[] = `listRelatives -p $locator`;
			
			// use xform to get the word space matrix
			float $m[] = `xform -q -ws -m $parents[0]`;
			
			// get local position attribute
			float $pos[] = `getAttr ($locator+".localPosition")`;
			
			// transform position by matrix - assuming pos[3] == 1
			float $new_pos[3];
			$new_pos[0] = $m[0]*$pos[0] + $m[4]*$pos[1] + $m[8]*$pos[2] + $m[12];
			$new_pos[1] = $m[1]*$pos[0] + $m[5]*$pos[1] + $m[9]*$pos[2] + $m[13];
			$new_pos[2] = $m[2]*$pos[0] + $m[6]*$pos[1] + $m[10]*$pos[2] + $m[14];
			
			// print world space position 
			print( $new_pos[0] + " " + $new_pos[1] + " " + $new_pos[2] + ")\n" );  
		}