Mel Script - Arrays

 

 

 

Arrays allow you to contain mulitple elemements of the same type in a single variable. mel only has support for single subscript arrays, unlike many other languages such as C, C++ or Java.

 

 

 

 

Example 1 : Declaring and printing an array

arrays in mel are very similar, however mel's arrays are slightly more flexible. To access an individual element of an array, use the array operator []. We can also use the size() function to determine the number of elements in the array. Also note that array indices start from zero, not 1!!

 

 


	{
		// declare an array of 11 elements
		int $int_array[] = {10,9,8,7,6,5,4,3,2,1,0};
		
		// you can use size to determine the number of elements
		int $size = size($int_array);
		
		// loop through the array and print each value [for loop notes]
		for($i=0;$i<$size;++$i) {
		
			// access the value by providing an index into
			// the array, where the first index is 0. 
			$value = $int_array[ $i ];
		
			// print the value
			print( $value + "\n" );
		}
	}
	

 

 

 

Example 2 : Listing Scene Nodes

Within mel we can use the mel function ls to list objects in the maya scene. You may well be suprised by how many there are just in a new scene file! This example uses a lot of shorthand techniques for dealing with arrays. Generally my preference is for scripts like this because it's far easier to see the intent of the original script writer.

 

 

 

	{
		// declare an array called $nodes. Call ls to list
		// the nodes in the scene
		$nodes = `ls`;
		
		// loop through the array and print each value [for in loop notes]
		for( $node in $nodes ) {
		
			// print the node name
			print( $node + "\n" );
		}
	}