Dec 05

We were talking at my work the other day how one could create a MEL command with many arguments without using tokens. So a previous example I gave of MEL in how it compares to Python needs to be updated. While creating this version I have to say it is much more error prone than creating a simple Python object like I show here:

Maya Python vs MEL data storage

It is simply much easier to manage and write more complex data in Python. Regardless here is a MEL test I made to show a more complex and robust version. This is for those who would rather use MEL or are simply curious to see what I am talking about.

//This is what the node data will look like
//"-node"
//"-name" "name"
//"-parent" "name"
//"-childs" "name" "name" "name" "name"

// Recurse a hiearchy and create the nodes array
global proc rtCreateNodes( string $node, string $nodes[] )
{
	// Get the parent and children
	string $parent[] = `listRelatives -parent $node`;
	string $childs[] = `listRelatives -children $node`;

	//  Add to the array data for this node//

	// Node
	$nodes[size($nodes)] = "-node";

	// Name
	$nodes[size($nodes)] = "-name";
	$nodes[size($nodes)] = $node;

	// Parent
	$nodes[size($nodes)] = "-parent";
	$nodes[size($nodes)] = $parent[0];

	// Childs
	$nodes[size($nodes)] = "-childs";

	for($child in $childs)
		$nodes[size($nodes)] = $child;

	// Recurse children
	for($child in $childs)
		rtCreateNodes( $child, $nodes );
}

// Give this proc the name of the node you want in the nodes array
// and rtReturnNode will return the node and its data
global proc string[] rtReturnNode( string $node, string $nodes[] )
{
	string $nodeData[];

	//Loop through the nodes to find $node
	for($i = 0; $i < size($nodes); $i++)
	{
		//Found a node
		if($nodes[$i] == "-node")
		{
			string $inode[] = {};

			//Go to the next peice of data
			$i++;

			//Get Data
			for($n = $i; $n < size($nodes); $n++)
			{
				int $next = 0;

				switch( $nodes[$n] )
				{
					case "-name":

						$n++;
						$inode[size($inode)] = "-name";
						$inode[size($inode)] = $nodes[$n];
						break;

					case "-parent":

						$n++;
						$inode[size($inode)] = "-parent";
						$inode[size($inode)] = $nodes[$n];
						break;

					case "-childs":

						$n++;
						$inode[size($inode)] = "-childs";

						//Get the children
						for($c = $n; $c < size($nodes); $c++)
						{
							if($nodes[$c] != "-node")
								$inode[size($inode)] = $nodes[$c];
							else
							{
								$n = ($c - 1);
								break;
							}
						}
						break;

					case "-node":

						$next = 1;
						$i = ($n - 1);
						break;
				}

				//If the next node was found stop looking for data
				if($next == 1) break;
			}

			if($inode[1] == $node)
			{
				$nodeData = $inode;
				break;
			}
		}
	}

	return $nodeData;
}

// Do something with the above procs //
string $getNodes[];
string $sel[] = `ls -sl`;
if(size($sel) == 1) rtCreateNodes $sel[0] $getNodes;

// This will print the data of the nodes in your hierarchy
print $getNodes;

// Change nodeName with a node you are looking for in $getNodes
string $data[] = `rtReturnNode "nodeName" $getNodes`;

// I look for joint2 and get this result
// Result: -name joint2 -parent joint1 -childs joint3 joint6 //

So with the above you can create some nodes in Maya like several joints and run this to get return data on that hierarchy. I admit this is much easier than tokenizing and much more flexible for when adding additional flags. My main complaint is the amount of sorting through the array you still need to do and it takes much more code to sort it all out.

Have fun,
-RyanT

Leave a Reply