Mel Script - Render Layers [API]

 

 

 

 

Render layers in Maya are used to organise objects into specific different render passes.

 

 

 

 

Creating a Render Layer and Adding Some Objects

To create a render layer we simply need to use the createRenderLayer mel command. To add objects into the layer we simply need to select the objects and then call layerEditorAddObjects specifying the name of the layer we want to add them to.

 

 


		// create a couple of objects
		$obj1 =`polyCube`;
		$obj2 = `polySphere`;
		xform -t 2 0 0;
			
		// determine the number of display layers in the scene
		$num = size(`ls -type  renderLayer`) + 1;
			
		// construct the full path to a file
		$layer = `createRenderLayer -name "layer1" -number $num -empty`;

		// select the objects to add to the layer
		select -r $obj1 $obj2;

		// add the objects to the layer
		layerEditorAddObjects $layer;

 

 

Accessing Render Layers

To get access to the items contained within any given render layer we simply need to query any connections to the layer.renderInfo attribute.

 

 

		// get a list of all render layers in the scene
		$layers = `ls -type "renderLayer"`;

		// loop through each layer
		for( $layer in $layers )
		{
			print($layer + "\n");
			// get connections to the layer.renderInfo attribute
			$conns = `listConnections -d 1 -s 0 ($layer+".renderInfo")`;
			for( $c in $conns )
			{
				print("\t" + $c + "\n");
			}
		}