You may want to refer to the ewc_extendedLayerManager_16.mel
file on the companion disc to view the finished script with the
proper indentation.
6.4.4 Populating the Layer List:
ewc_gprocLoadCurrentLayers()
The layer manager can now create layers, but nothing is being dis-
played in the layer list. A procedure must be set up to query the
layers in a scene and load them into the textScrollList. This same
procedure will be called from the interface procedure when the
script is run. It can also be called from the Refresh Layer List
menuItem in the File menu, and from the pop-up menu assigned to
the layer textScrollList. This procedure will not take any arguments.
Start with the pseudocode to outline the flow of the procedure:
//==========================
// PROCEDURE TO LOAD THE LAYERS
global proc ewc_gprocLoadCurrentLayers()
{
// Query the layers in a scene
// Create an array to hold defaultLayer
// Remove defaultLayer from the array
// Alphabetically sort the array
// Get the total count of items currently in the list
// If the list isn't empty, clear it
// Loop through the array and add each item to the list
// Attach commands to each item
// Update the custom help line
} // End ewc_gprocLoadCurrentLayers()
First, an array must be created that holds the names of the layers in
the scene. Use the
ls command (enclosed in back quotes) with the
-exactType flag. The arguments string is displayLayer,which
queries all the nodes that match the specified type and creates an
array called
$allLayers.
// Query the layers in a scene
string $allLayers[] = `ls -exactType "displayLayer"`;
Scene Management with ewc_extendedLayerManager
269
Chapter 6
Another array needs to be created. This array will be declared with
the string
defaultLayer.
// Create an array to hold defaultLayer
string $defaultLayer[] = {"defaultLayer"};
The defaultLayer exists in all scenes. This layer cannot be edited
and isn’t even accessible in the default layer editor in Maya. The
next step is to remove the defaultLayer from the
$allLayers array.
The
stringArrayRemove command will test the two arrays. Any
strings in
$allLayers that match any of the strings in
$defaultLayer will be removed. The result is stored in a new
array.
// Remove defaultLayer from the array
string $layers[] = stringArrayRemove($defaultLayer, $allLayers);
I want the layers to be listed alphabetically in the layer list. This
meansthearraymustbesortedbeforetheelementsareindividually
assigned to the layer list.
// Alphabetically sort the array
sort($layers);
The next step is crucial. A textScrollList does not automatically
clear itself when it refreshes. If this procedure were being called to
refresh the list, it would add the layers to the existing names (even
if they are matching), displaying duplicates in the list. The thing to
do is edit the textScrollList using the
-removeAll flag, which
removes all the items.
// Clear the textScrollList of all items
textScrollList -edit -removeAll ewc_tslLayerList;
An array has been created without the defaultLayer and is alphabeti-
cally sorted. Now a loop will iterate through each element and
append it to the textScrollList.
Chapter 6
270
Iterations
n
Theloopstartsthecountat0.Thisvalueisstoredinavariable
called
$i.
n
It will cycle while the count ($i) is less than the actual size of
the array. Array elements start their count at 0. The
size com-
mand starts the count at 1. What will happen is that if an array
has five elements, the actual count of the elements goes from 0
to 4 (
$layers[0], $layers[1],andsoon).Thesize command
will return 5. This iteration will cover all elements in the array
because the actual element ID will end at 4, even though it is
the fifth element. (It’s normal if images of Milla Jovovich pop in
your head now.)
n
The $i++ will add a value of 1 each time the loop iterates.
// Loop through the array and add each item to the list
for ($i = 0; $i < size($layers); $i++)
{
The textScrollList appends each element of the array using the
-append flag on each iteration of the loop. Three new flags are
attached to the list items.
n
-doubleClickCommand: Executes a command string when the
textScrollList is double-clicked.
n
-deleteKeyCommand: Executes a command string when the
Delete or Backspace keys are pressed.
n
-selectCommand: Executes a command string when an item is
clicked in the textScrollList.
Right now the command strings will be left empty until the proce-
dures are written.
textScrollList
-edit
-append $layers[$i]
// Attach commands to each item
-doubleClickCommand ""
-deleteKeyCommand ""
-selectCommand ""
Scene Management with ewc_extendedLayerManager
271
Chapter 6

Get Professional MEL Solutions for Production now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.