Basic Syntax
Spelling
Maxscript is not case sensitive.
x and X would be considered the same.
MaxScript Statements
MaxScript statements terminate at the end of line. If the expression at the end of the line is not a complete expression it will attempt to continue reading till the end of the next line.
These two statements are the same
Try typing above into listener to see what happens.
Explicit statement continuation
You can also explicitly continue a line use backslash \
For example (this is same as above):
Multiple statements on the same line
If you want to have mulitple expressions on the same line, you can separate them with semi-colons ;
Comments
comments in MaxScript start at double hyphens and lasts till end of line:
C style comments using/*
and */
are also supported
Datatypes
MaxScript variables do not have explictly declared types. The way to think about them is that they are like generic references. When you say:
a = 5
you are make a refer to an integer value so a will behave like an integer.
If later you do the following:
a = 5.0
then a will start behaving like a floating point value (think of it as throwing away the reference to the original integer and refering to a new floating point value instead)
Numeric Data types
The numerical rules for maxScript is similar to C/C++.
numbers entered without a decimal are integer and follows integer mathematical rules.
7/2 is 3 (not 3.5)
8/3 is 2 (not 2.667, note the truncation, not rounded up)
if an expression contains both integers and floating point, result is a floating point value
8.0/3 is 2.667
expressions are evaluated according to order of expression rules and propagated in the same manner. In otherwords, follow order of operator rules, results are evaluated one operator at a time.
2.0 + 8/3 is 4.0 (8/3 is 2, add 2.0 to it gives 4.0)
Numeric Operators:
operator
what it does
example (int only)
example (float/int) args
+
addition
3+2 is 5
3.2+2.0 is 5.2
-
subtraction
5-3 is 2
5.0 - 2 is 3.0
*
multiplication
6 *2 is 12
2.0 * 2.0 is 4.0
/
division
8/3 is 2
8.0/2 is 2.667
^
exponent: calculates base ^ power
2^3 is 8
4 ^ 0.5 is 2 (remember power of 0.5 is same as square root). 3 ^ 1.5 is 5, 3.0 ^ 1.5 is 5.19615
String literals:
string literals are enclosed in double quotes
strings can be concatenated with + operator
results in
Boolean literals:
Vector literals
as max deals in 3D, you will see a lot of vectors used in the code. a vector is declared as 3 comma separated values within []. For example, suppose you wanted to place an object at position x=0, y=0, z = 10, you would set the pos parameter to [0,0,10]
Reserved Words
Predefined constants/literals:
Control Structures
Control structures are used to control the flow of your program. MaxScript has the usual expected control structures like any other language but some of them do not always work as expected.
Furthermore testing out control structures in the listener can be a limited. It is probably better to write a small script file and run it each time instead.
Selection
Selection statments allow you to choose between one or more paths of script execution. In Max, the control statements return a result which can be used for further evaluation (like the ?: operator of C/C++)
if-do
The if-do form is more useful in the listener where expressions are evaluated as they are typed. Everything after the do is performed if the evaluated-expression is true
As there is no possibilty of an else with the if-do form of selection, the code typed into the listener which is processed as soon as the statement is complete will immediately evaluate the statement.
if-then
if the evaluated expression is true, evaluate statements after then. else is optional, but if included, do the code after else when evaluated expression is false.
In the case where you are typing code into the listener, an if-then statement that does not include the else will not evaluate until a statement follows it.
For example if you type in:
nothing will happen until you add another statement or an else clause.
case
Case is similar to a switch statement:
can be an a literal of some sort or it can be an expression also
NOTE: unlike C/C++ switch statments, once a match is found, the expression of the matching factor is evaluated and no other. There no break that is needed to get past the other cases like there is in C/C++
Also notice that the <evaluated-expression>
is optional. It is possible to write something like:
If this is done, the first factor expression that is true will be evaluated and the rest will be ignored
Iteration
Iterations allow you to do a set of expressions over and over again. Similar to selection statements, iteration statements also evaluate to a value. In this case it evaluates to the value of the expression of the last iteration.
while
do-while
for
The for loop is a counting loop. In max, other than standard counting loop, it also allows you to work with thing arrays or a set of objects as part of the for loop syntax. In this section we will only talk about for as a counting loop. We will look at the usage of for loops that iterate through collections in the array section of the notes
So, here are some examples of the equivalent C/C++ for loop and max for loop Example 1: simple counting loop:
Example 2: counting loop that counts by 2's
Example 3: counting loop with additional termination condition.
Note that while it is possible to break out of loops early using exit, doing so has significant performance hits.
Example 4: counting loop where statements are only done when a condition is met (but loop continues regardless)
Functions
You can write functions in MaxScript. Like C/C++ functions, MaxScript functions have a name and parameter list. However, it does not have a return type. The function returns whatever the last expression evaluates to in your function body.
You can also explicitly return a value using the return statement. Like C/C++, once a return statement is encountered, statements that follow it in the function are ignored.
Basic syntax:
The word mapped in front of the function is optional. If it exists, the function can be applied over a collection such as an array or a set of objects. More details on this will be provided in the array section of the notes
Notice first that you can spell function either as "function" or "fn". There is no difference...
functions can be recursive in nature.
The consists of the names of the parameters for the function. There are two ways to define parameter lists.
Parameter passing by Position
This is the method that you are used to for C/C++ functions. The order of the parameters in your declaration determines how values supplied in the function call are assigned. A parameter of this nature is declared by simply declaring the name of every parameter separated by space.
For example, this function divides 2 numbers
Note the lack of return statement in the above function. The return is implicitly whatever the expressions in the () evaluate to.
Parameter passing by Keyword
keyword style parameter lists also allows you to set default values for your parameters
Mixing position and keyword parameter passing
It is possible to have part of parameter list as positional and part of it as keywords. However, the positional parameters must come first before any of the keyword parameters.
Pass by Reference
As with C, parameter passing is normally pass by value. That is any changes to your parameters do not modify the a variable used by the calling function.
Furthermore, you will also need to indicate you are making a call by reference when you make the function call by prefixing the by reference variable with an &. This is different from how references work in C++ (though sort of similar to C pointers...)
Arrays
Arrays is a type of collection in max. One important difference between arrays in C/C++ and arrays in max is that max arrays can have elements of different datatypes within the same array. This is very diffrent from C/C++ in that all elements of an array must be exactly the same type.
Think of arrays as simply an indexed list, where each list item can be anything.
Furthermore arrays are indexed starting 1 and not 0 like C/C++.
Array declaration:
To declare an array use the following notation:
Array access
To access any element of an array, use arrayName[index]. For example, the following prints the first element of the array anotherArray:
Function calls and arrays
Like C/C++, arrays passed to functions are pass by reference. That is if you pass an array to a function and the function changes the array, your original array will be modified.
Mapped functions
A function may be preceded by the keyword mapped. If this keyword is present, it will allow you to apply the function through every element of a collection.
For example consider the following function:
In the above example, the functions are both mapped. Thus, when I call it using an array, it will automatically call the function using every item in the array.
For loops and arrays
When working with a collection like an array, the for loop can go through the entire collection of objects.
For example:
You can also use a for loop to collect things into an array using the collect statement. The result of the expression in the loop is added to the array:
Structs
MaxScript allows you to create structs with functions. Essentially this means you can write object oriented code if you wish to do so. Structs in many ways are similar to how they would work in C++.
Syntax
A few syntactic quirks. All members are comma separated. So once you start a struct, everything member you create in that struct is separated by every other member with a comma.
All data members can be initialized using = operator to specify default values. Data member values can be supplied using either their order or their names. When using names, you can use any ordering.
members can be accessed using (.) notation as with C++
Starting with max 2010, you are allowed to create public vs private members and their function is similar to C++. in earlier versions of max, all members are public.
Last updated