Besides strings, you can enter other types of data into MAXScript. MAXScript not only recognizes numeric characters as numbers, it can also perform mathematical operations with them. As with strings, if you just enter a number by itself, MAXScript echoes it back to you; it evaluates the number and displays its value. Mathematical operations are more interesting.
MAXScript has many built-in functions that let you use MAXScript as a calculator.
Enter 36.5 * 2, for example, to see what a certain dimension needs to be if you want to double it.
MAXScript returns the result of the calculation. This is useful for quick calculations where loading an external calculator program doesn’t seem worth the trouble.
MAXScript also recognizes several mathematical constants. Enter pi.
MAXScript knows the value of pi and returns it.
You can use this value in more complex operations. For example, if you want to know the volume of a sphere with a 2.5-inch radius, multiply the cube of the radius by pi, then multiply by 4 and divide by 3. Enter 4/3 * pi * 2.5^3 .
You can also perform some simple mathematical operations with strings. For example, if you have defined a="MAXScript" and b=" is fun!", entering a+b returns "MAXScript is fun!"
For more string operations, see the String Values topic in the MAXScript reference.
MAXScript is able to perform many mathematical operations, including most trigonometric functions (sin, cosh, atan) and transcendental functions (exp, log, sqrt). This topic introduces two of the most useful operations: random numbers and incrementing. For a complete list of the operations supported by MAXScript, see the Number Values topic in the MAXScript reference.
One very useful mathematical operation in MAXScript is the random number function. It returns a pseudo-random number selected inclusively between two user-specified arguments.
random 1 100
returns a random integer between 1 and 100. The return value type (Float or Integer) is the same as the type of the first argument in the function, therefore if you enter
random 1.0 100
MAXScript returns a random float between 1 and 100.
For reasons beyond the scope of this tutorial, the random command will generate the same ‘random’ numbers each time a script is run. This happens if you restart the software and run the script, but not if you run the script over and over. If you want the values created by the random function to change each time you start the software, you can use the seed command:
Another useful mathematical operation that MAXScript provides is incrementing. This is a shorthand form of assignment that can be used to modify a value in place, as shown in the following long form examples:
x = x + 1
There are assignment operators corresponding to the four math operations (+,-,*, and /) that apply the operation to the assignment’s destination with the result. Their syntax is:
Using this shorthand form of assignment, the previous example can be written as:
x += 1
Drawing a Box with MAXScript