Mel Script - Listing objects and Selection

 

 

 

When writing mel scripts, often it is important to either select objects in a specific order for a specific tool or command, or you may simply need to know then names of all meshes for instance.

 

 

 

 

Example 1 : Listing all objects within the scene [API]

The mel command ls allows you to list objects within the maya scene. We can then simply loop through an array and use the node name to perform actions upon that node. (in this case simply printing the name)

 

 

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

 

 

 

Example 2 : Determining the Node Type [API]

The mel command ls allows you to list objects within the maya scene. We can then simply loop through an array and use the node name to perform actions upon that node. (in this case simply printing the name)

 

 

 

			
	{
		// declare an array called $nodes. Call ls to list
		// the nodes in the scene
		$nodes = `ls`;
		
		// loop through the array and print each node
		for( $node in $nodes ) {

			// use the nodeType function to determine the 
			// type of the node
			$type = `nodeType $node`;
			
		
			// print the node name and it's type
			print( $node +" "+ $type +"\n" );
		}
	}
	

 

 

 

Example 3 : Listing Nodes of Type ______ [API]

ls also allows us to list nodes of a specific given type, to do this we simply use the -type flag. This is really useful for processing all node of type XXXX.

 

 

 

	
	{
		// declare an array called $nodes. Call ls to list
		// the nodes in the scene
		$meshes = `ls -type "mesh"`;
		
		// loop through each mesh
		for( $mesh in $meshes ) {
			// print the mesh name
			print( $node +"\n" );
		}
	}

 

 

 

Example 4 : Listing Selected Objects [API]

By using the -sl flag with the ls function, we are able to list all selected objects.

 

 

 

	
	{
		// declare an array called $nodes. Call ls to list
		// the nodes in the scene
		$selected = `ls -sl`;
		
		// loop through each mesh
		for( $node in $selected ) {
			// print the selected object's name
			print( $node +"\n" );
		}
	}
	

 

 

 

Example 5 : Selecting Objects [API]

The select command allows us to clear, add items to, toggle or replace the set of currently selected objects in the scene.

 

 

 


	{	
		// create some cubes
		$cube1 = `polyCube`;
		$cube2 = `polyCube`;
		xform -ws -t 3 0 0;   // move 3 units in x
		$cube3 = `polyCube`;
		xform -ws -t 6 0 0;	  // move 6 units in x
			
		// de-selects all maya objects
		select -cl;
		
		// select two objects by adding into the selection list
		select -add $cube1[0];
		select -add $cube2[0];
		
		// print selected items
		print( `ls -sl` );
		print( "\n" );
		
		// toggle cube1 selection
		select -toggle $cube1[0];
		
		// print selected items
		print( `ls -sl` );
		print( "\n" );
		
		// replace the entire selection list
		select -replace $cube3[0];
		
		// print selected items
		print( `ls -sl` );
		print( "\n" );
	}