Conditional Actions


Conditional statements let you execute a statement or grouping only if the test condition is true when evaluated as a type int. This test condition must be enclosed in parentheses, and the resulting value must be an int, float, or vector. The test condition value is then converted to an int type to represent either true or false.


If the test condition has a value of type int or float, the test condition is true only if the value, converted to type int, is not 0.



if (0) print("true\n"); // False

if (1) print("true\n"); // True

if (-17.4) print("true\n"); // True

if (0.9) print("true\n"); // False

if (-0.7) print("true\n"); // False



In MAXScript, the conditional expression must evaluate to a BooleanClass value, either True or False. Integers and Floats are NOT automatically converted to BooleanClass values and always require a logical comparison operator to return a BooleanClass result. To match MEL's behavior, you would always use the expression N!=0 where N is the number (Float or Integer). The above examples would look like:

if 0!=0 do print true -- False

if 1!=0 do print true -- True

if -17.4!=0 do print true -- True

if 0.9!=0 do print true -- False

if -0.7!=0 do print true -- False



If the test condition has a value of type vector, the test condition is true only if the magnitude of the vector, converted to type int, is not 0.



if (<<0.9, 0.3, 0.3>>) print("true\n"); // False

if (<<.8, 0.3, 0.7>>) print("true\n"); // True

if (<<0, 0, 0>>) print("true\n"); // False

if (<<0, 4, -37>>) print("true\n"); // True

if (<<0, 1, 0>>) print("true\n"); // True


In MAXScript, the vector must be explicitly converted to a Float and logically compared to another value to return a BooleanClass value. The above examples would look like

if length [0.9, 0.3, 0.3] != 0 do print true -- False

if length [.8, 0.3, 0.7] != 0 do print true -- True

if length [0, 0, 0] != 0 do print true -- False

if length [0, 4, -37] != 0 do print true -- True

if length [0, 1, 0] != 0 do print true -- True


The test condition can include any variables or operators (even the assignment operator), but the resulting value must be of type int, float, or vector.


In MAXScript, the test conditions can contain any expression, but the value of the evaluation must be either True or False. If a variable already contains a BooleanClass value (True or False), it can be used directly in the condition:

if theObject.renderable do format "% will be rendered!\n"





The if conditional statement has the following format:

if ( test condition )



If test condition is true, statement executes.



if (9836 % 27 > 13)

print("Wow, that's interesting!\n");

if (3743 % 17 < 8)


print("These are...");

print(" useful results.\n");




In MAXScript, the conditional statement must be followed by either do or then.

if condition_expression do true_expression

if condition_expression then true_expression


The reason for the existence of two different syntax forms is the following:

As you will see below, the else statement can only be used together with then but not with do.

When typing commands directly into the MAXScript Listener window, typing if condition then tells MAXScript to expect a following else expression - the evaluation will be on hold until the else expression has been entered.

If you don't intend to use an else expression, typing in the form if condition do expression will execute immediately after pressing the Numeric Enter without waiting for another expression.


In regular scripts, both then and do can be used interchangeably, but it is a better style to use do unless there is a need for an else expression.




The else statement works only with the if conditional statement. It has the following format:

if ( test condition )





If test condition is true, statement1 executes, otherwise statement2 executes. The statement1 and statement2 can be any valid statements.



In MAXScript, as explained above, else can only be used when the if condition uses a then statement. It will cause an error if used together with a do statement.

if condition_expression then







if (rand(10) < 5)

print("That was random.\n");


print("No it wasn't.\n");

if (rand(2))

print("It takes one to get here\n");



print("This is just...");

print(" less than one");




Here is the same example expressed in MAXScript:

if random 0 10 < 5 then

print "That was random."


print "No it wasn't."

if random 0 2 != 0 then

print "It takes one to get here\n"



print "This is just..."

print " less than one"




else if


The else if statement works only with the if else conditional statement. It has the following format:

if ( test condition1 )


else if ( test condition2 )



If test condition1 is true, statement1 executes. Otherwise, if test condition2 is true, statement2 executes. If neither test condition is true, neither statement will be executed. The statement1 and statement2 can be any valid statement.



float $random = rand(2);

if ($random < 1)

print("That was random.\n");

else if ($random < 2)

print("Sure was.\n");


print("That's impossible...\n");


In the above example:


       If the random variable value is less than 1, only "That was random.\n" prints.

       If the random value is between 1, inclusive, and two, exclusive, only "Sure was.\n" prints.

       If the value of random is exactly 2, only "That's impossible...\n" prints.


The rand(2) call would rarely return a value that was exactly 2.



In MAXScript, there is no explicit else if statement defined, but you can nest as many if()then()else() expressions as you want to achieve the exactly same effect.


rand = random 0.0 2.0

if rand < 1 then

print "That was random."

else if rand < 2 then

print "Sure was."


print "That's impossible..."



Note the differences!

       random() is the MAXScript function equivalent to the rand() function in MEL. Since user variables are not prefixed by a special character in MXS as in MEL, using the function name random as a user variable would cause an error. That's why the variable was renamed to rand in this example.

       MAXScript does not provide an explicit type declaration, the variable rand is type-free as any other MXS variable. The random() function will create random values of the same type as the start and end values provided as argument. When both are Integers, the result will always be an Integer. In the above example, the range values were provided as floating point values to force a result as a Float.




float $crow = rand(10);

if ($crow < 5)

print("The crow has landed.\n");

else if ($crow < 7)


print("The crow is between five...");

print(" and seven.\n");



Here is the same example in MAXScript:


crow = random 0.0 10.0

if crow < 5 then

format "The crow has landed.\n"

else if crow < 7 do


format "The crow is between five..."

format " and seven.\n"







A switch statement executes one of several pieces of statements based on the value of the control value. This control value can be an int, float, string, or vector data type. The body of the switch is enclosed in braces and is comprised of one or more blocks of statements called cases. Cases are labeled by the word case followed by a value of the same type as the control value, then a colon.


The control value determines which case statement gets executed. If a case statement value is equal to the control value statement, that case statement is executed.


The switch statement has the following general form:

switch (control value)


case value1:



case value2:



case value3:










In MAXScript, the equivalent conditional function is called case.

Its full form is

case (control_expression) of


CaseValue1: expression1

CaseValue2: expression2

CaseValue3: expression3


CaseValueN: expressionN

default: expression1



The case is followed by the control expression whose value can be of any type, including Integer, Float, String, Point3, Color, Name, basically any value that can be used in a logical comparison. The control expression is followed by the keyword of and a block defined by parentheses containing the case values without any keyword, followed by colon, space and the expression to be evaluated if the case value is equal to the control expression's value.

If the control expression's value does not equal to any of the provided cases, and the optional default keyword is used as case, its expression will be evaluated instead.

The case expression will return the result of the expression that was evaluated, or undefined if none was evaluated. This means that the result of the case function can be assigned to variables or used in other expressions directly

For example

theResult = case of


 "Box01": "This is the first box"

 "Box02": "This is the second box"

 default: "This is a box I donít care for"


will assign one of the 3 strings to the variable theResult depending on the name of the object stored in the variable theBox.




After a case statement, you may have any statement including flow control. To prevent the MEL execution from continuing on to the next case, you can use a break statement. Within a switch, a break statement causes the rest of the switch statement to be ignored. For more information on the break statement see, break.


Example: break statement

int $sway = rand(3);

switch ($sway)


case 0:

print("Case 0\n"); // Executed only if $sway = 0


case 1:

print("Case 1\n"); // Executed only if $sway = 1


case 2:

while (rand(10) < 7) // These statements

print("I say!\n"); // are executed only

print("Case 2\n"); // if $sway = 2






Although the last case in a switch statement does not need a break statement since the switch is at its end, it is still a good idea to add the break statement. If you add more cases to the switch statement, the break statement is already there.


If you want the switch execution to continue through the next case statement, you can omit the break statement. However, this can lead to problems when the code is modified at a later time.



int $argo = rand(2);

switch ($argo)


case 0:

print("Food\n"); // Executed if $argo is 0.

case 1:

print("Fight\n"); // Executed if $argo is 0 or 1.





MAXScript does not require an equivalent to the break statement as it will never evaluate expressions whose cases do not match the condition.




You may want more than one case statement to execute the same block of MEL statements. For example, if you wanted both the control values of "a" and "A" to execute some statements, you could write something similar to the following example:



string $mt = rand(2) < 1 ? "a" : "b";

string $mp = rand(2) < 1 ? "A" : "B";

string $XXz = rand(2) < 1 ? $mt : $mp;

switch ($XXz)


case "a":

case "A":

print("Apple\n"); // Executed if "a" or "A"


case "b":

case "B":

print("Banana\n"); // Executed if "b" or "B"




In this example, the variable $mt could be either "a" or "b" and variable $mp could be either "A" or "B". Additionally, the variable $XXz could be either variable. This means that $XXz and the control value could be either "a", "b", "A", or "B".


This form of case statement is not directly possible in the MAXScript syntax.

To perform the same, you would define a function to be executed in both cases and call the function from the case expressions for "a" and "A", "b" and "B".


mt = if random 0 2 < 1 then "a" else "b"

mp = if random 0 2 < 1 then "A" else "B"

XXz = if random 0 2 < 1 then mt else mp

fn apple = print "Apple" -- Executed if "a" or "A"

fn banana = print " Banana" -- Executed if "b" or "B"

case XXz of


case "a": apple()

case "A": apple()

case "b": banana()

case "B": banana()



Because of the simpler case syntax without break statements, the MAXScript code is 2 lines shorter, while introducing two new functions to handle the cases.



To specify a block of statements to execute only if none of the case values match the control value, you can use the default label. This label may be located anywhere in the switch statement, but it usually appears after all of the case statements. If there is no default label and none of the case values match the control value, the switch statement does nothing.


Example: default label

vector $mgb = <<1, 1, 0>>;

switch ($mgb)


case <<0, 1, 1>>:

print("Who\n"); // Executed if $mgb is <<0, 1, 1>>


case <<1, 0, 1>>:

print("What\n"); // Executed if $mgb is <<1, 0, 1>>



print("Why\n"); // Executed if $mgb is not

break; // <<0, 1, 1>> or <<1, 0, 1>>



In the above example, the default case is executed and "Why\n" prints.


MAXScript behaves exactly the same way.


mgb = [1, 1, 0]

case mgb of


[0, 1, 1]: print "Who" -- Executed if mgb is [0, 1, 1]

[1, 0, 1]: print "What" -- Executed if mgb is [1, 0, 1]

default: print "Why" -- Executed if mgb is not [0, 1, 1] or [1, 0, 1]