6.1.7 Switch Statements
When writing the procedures for the functionality of the layer man-
ager, I keep smaller and related commands under one procedure and
assign them to individual case statements based on the control that
will be calling them. Using switch statements and if statements, we
can define exactly which part of the procedure is going to be used by
a specific control by passing a unique argument from each control.
Since most of the arguments being tested in section 6.3 will be
tested through switch statements, I want to take a moment to
review the fundamentals of a switch statement.
General Usage
A switch statement evaluates a variable and then matches the value
in the variable to a case statement that has the corresponding value
and executes the block of code inside the case statement. Your head
just exploded, so let me rephrase that. Switch statements take a
value and test it against predefined case statements for a match to
determine which block of code will be executed.
In the example below, the string “hello” is cast in the variable
held by
$s and the variable itself is placed in the parentheses of the
switch statement. The switch statement then compares the value
$s for a matching string that follows each case state-
ment. The first case statement contains the matching string, so the
print command under the case statement is executed.
string $s = "hello";
switch ($s)
case "hello":
print "Nice to meet you!";
case "bye":
print "Take care...";
print "Excuse me?";
Chapter 6
The default statement is optional and is similar to the else state-
ment used with if statements. I normally implement a switch
statement when I know that a procedure will be passed only
pre-specified values through arguments. I normally reserve if state-
ments for performing tests, especially when the value tested could
be an unknown value. This is, however, a matter of personal choice
and a subject of heated discussion amongst nerds.
Lastly, remember to include the break command to avoid fall-
ing. “Falling” occurs when the
break command is left out, which
causes MEL to continue on and evaluate the next case statement
(and so on) until reaching either a
break command or the end of the
switch statement altogether. Omit the
break command and the case
statement that is called will continue on to the next case statement
under it.
string $s = "bye";
switch ($s)
case "hello":
print "Nice to meet you!";
case "bye":
print "Take care...";
print "Excuse me?";
The Script Editor prints back:
Take care...Excuse me?
Maya doesn’t know when to stop after the second case statement
and falls right through to the default statement, executing it as well.
The last condition in a switch statement, whether it is a case or
default statement, doesn’t require a
break command, but it is good
practice to add one anyway.
Scene Management with ewc_extendedLayerManager
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.