Mel Script - Display Layers [API]





Display layers in Maya are used to organise objects into specific visibility groups.





Creating a Display Layer and Adding Some Objects

To create a display layer we simply need to use the createDisplayLayer 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  displayLayer`) + 1;
		// construct the full path to a file
		$layer = `createDisplayLayer -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 Display Layers

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



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

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