You can use the following arithmetic operator symbols to add, subtract, multiply, compare, and perform other actions with variables.
|
Symbol |
Meaning |
Used with these data types |
|
+ |
plus |
int, float, string, vector, matrix |
|
- |
minus or negation |
int, float, vector, matrix |
|
* |
for int, float, matrix: multiply for vector: dot product |
int, float, vector, matrix |
|
/ |
divided by |
int, float, vector |
|
% |
remainder of division |
int, float, vector |
|
^ |
cross product |
vector |
Additionally, the matrix type can be used with the modulus (%) and divide (/) operators. However, these operations must have an integer or float as the right-hand operand. For more information about these exceptional operations, see Matrices.
MAXScript supports the operators +, -, * and / in the same way MEL does.
The % operator is not supported by MAXScript, instead, the mod function is used to perform the modulus operation.
Note the difference!
· In MAXScript, the operator ^ is used to calculate the first operand to the power of the second.
· In MEL, ^ is used for vector cross product.
· In MAXScript, vector cross product is calculated using the cross() function.
For integer and floating-point attributes and variables, the arithmetic operators work according to the rules of basic math. The modulus (%) operator is an operator commonly available in programming languages. It calculates the remainder of division as shown in the examples below.
Example
int $card = 7 % 3; // Result: 1
This assigns card the value 1, the remainder of 7 divided by 3. The number 7 divided by 3 equals 2, with a remainder of 1.
float $bus = 0.5 % 3; // Result: 0.5
This assigns bus the value 0.5, the remainder of 0.5 divided by 3. The number 0.5 divided by 3 equals 0, with a remainder of 0.5.
In MAXScript, the modulus operation is performed using the mod function, followed by the two arguments. The result of the mod function in MXS is always a Float:
card = mod 7 3 --result is 1.0
bus = mod 0.5 3 --result is 0.5
For details on how to use the + operator with strings, see Strings.
For operations between vector variables, the ^ operator calculates the cross product of the vectors, and the * operator calculates the dot product. For all other operators, each component of one vector is operated on by its counterpart component in the other vector.
Examples
vector $Vo = <<3, 9, 5>> + <<9, 2, 3>>; // <<12, 11, 8>>
vector $Vu = <<2, 3, 4>> - <<1, 2, 3>>; // <<1, 1, 1>>
vector $me = <<2, 3, 2>> / <<1, 2, 5>>; // <<2, 1.5, 0.4>>
vector $rf = <<12, 3, 9>> % <<4, 2, 5>>; // <<0, 1, 4>>
In MAXScript, you can perform the first 3 operations. There is no built-in way to perform a modulus operation between two vectors.
Vo = [3, 9, 5] + [9, 2, 3] -- [12, 11, 8]
Vu = [2, 3, 4>> - [1, 2, 3] -- [1, 1, 1]
me = [2, 3, 2>> / [1, 2, 5] --[2, 1.5, 0.4]
rf = [mod 12 4, mod 3 2, mod 9 5] --using mod directly is not possible.
The dot product returns a float. The dot product is often conceptualized as the product of the lengths of the two argument vectors multiplied by the cosine of the angle between these vectors.
Example
vector $V1 = <<4, 6, 8>>;
vector $V2 = <<2, 2, 2>>;
float $F1 = $V1 * $V2; // 36 (dot product)
The result of the dot product (*) operator can be calculated with the following equation:
$F1 = ($V1.x * $V2.x) + ($V1.y * $V2.y) + ($V1.z * $V2.z);
In MAXScript, the vector dot product is calculated using the functions dot, followed by the two vectors as arguments. There are no corresponding operators to perform this function.
V1 = [4, 6, 8]
V2 = [2, 2, 2]
F1 = dot V1 V2 -- 36.0 (dot product)
Note the difference!
In MAXScript, using the * operator between two vectors will return a new vector whose components are the multiplication of the corresponding elements of the two operands:
[10,20,30] * [30,40,50] -- returns [300,800,1500]
Although most operations between a vector and an int or float implicitly convert the int or float to a vector, * operations are different. For * operations between a vector and an int or float, each component of the vector is multiplied by the integer or floating-point number.
Examples
vector $tak = 3.2 * <<1, 3, -5>>; // <<3.2, 9.6, -16>>
vector $sak = <<0, 1.2, 9.1>> * 5; // <<0, 6, 45.5>>
The same applies to MAXScript multiplication of a vector and a Float or Integer:
tak = 3.2 * [1,3,-5] -- returns [3.2, 9.6, -16]
sak = [0, 1.2, 9.1] * 5 -- returns [0,6,45.5]
The ^ operator works only with the vector data type. It represents the cross product operator, which returns a vector.
Example
vector $V3 = <<2, 0, 0>>;
vector $V4 = <<0, 3, 0>>;
vector $V5 = $V3 ^ $V4; // <<0, 0, 6>> (cross product)
The cross product vector is in a direction that is normal to both of the argument vectors. The magnitude of this vector is the product of the lengths of the two argument vectors, multiplied by the sine of the angle between the two argument vectors. You can calculate the resulting cross product vector with the following equation:
$V5 = <<(($V3.y * $V4.z) - ($V3.z * $V4.y)),
(($V3.z * $V4.x) - ($V3.x * $V4.z)),
(($V3.x * $V4.y) - ($V3.y * $V4.x))>>;
In MAXScript, the vector cross product is calculated using the function cross followed by the two vectors as arguments. There are no corresponding operators to perform this function.
V3 = [2, 0, 0]
V4 = [0, 3, 0]
V5 = cross V3 $V4 -- [0, 0, 6] (cross product)
Note the difference
In MAXScript, using the * operator between two vectors will return a new vector whose components are the multiplication of the corresponding elements of the two operands:
[10,20,30] * [30,40,50] -- returns [300,800,1500]
You can add and subtract only between corresponding elements of each matrix. The matrices must have the same number of rows and columns.
Examples
matrix $Va[1][4] = <<2, 0, 0, 2>>;
matrix $Vb[1][4] = <<6, 3, 7, 5>>;
matrix $Vc[1][4] = $Va + $Vb; // <<8, 3, 7, 7>>
matrix $Vd[1][4] = $Va - $Vb; // <<-4, -3, -7, -3>>
When you multiply matrices, the number of columns of the left-hand side matrix must be equal to the number of rows of the right-hand side matrix. The resulting matrix has the same number of rows as the left matrix and the same number of columns as the right matrix.
In MAXScript, you can perform addition, subtraction and multiplication between matrix3 values. BigMatrix values support only addition and only if the two matrices are of the same size.
Example
matrix $Ve[2][4] = <<4, 1, 1, 2; 3, 4, 5, 8>>;
matrix $Vf[4][3] = <<7, 6, 0; 7, 5, 4; 2, 1, 6; 2, 0, 2>>;
matrix $Vg[2][3] = $Ve * $Vf; // <<41, 30, 14; 75, 43, 62>>
When the right-hand side operand is a scalar, the modulus and division operators operate on each element of the matrix using the scalar as the right-hand side operand. The examples below illustrates this.
Examples
matrix $Vh[1][4] = <<4, 9, 5, 2>> % 3; // <<1, 0, 2, 2>>
matrix $Vi[1][4] = <<1, 2, 8, 4>> / 2; // <<0.5, 1, 4, 2>>
These operations on matrices are not directly possible in MAXScript.
Often in scripting you will want to assign a variable the value of itself operated on by some other value. The shortcut operations give you a quick and simple way to do this.
You can use shorthand assignment operators with int, float, and vector data types. These operators provide no additional functionality or execution speed, but they might save you some typing time.
For example, instead of writing:
$bus = $bus + 3.5;
You could write the following line instead:
$bus += 3.5;
Both statements do the same thing, but the second requires less typing. For any arithmetic operator, you can abbreviate the format:
variable = variable operator value
to this format:
variable operator= value
The following table shows all of the shortcut operators of this form, their expanded syntax, and their values. These operators are defined only for the int, float, and vector types. However, the += operator is also defined for the string type (see Strings).
|
Shortcut Syntax |
Expanded Syntax |
Value |
|
variable += value; |
variable = variable + value; |
variable + value; |
|
variable -= value; |
variable = variable - value; |
variable - value; |
|
variable *= value; |
variable = variable * value; |
variable * value; |
|
variable /= value; |
variable = variable / value; |
variable / value; |
|
variable %= value; |
variable = variable % value; |
variable % value; |
Examples
float $mice = 25.3;
$mice -= 6; // $mice = $mice - 6;
$mice %= 4; // $mice = $mice % 4;
$mice /= 3; // $mice = $mice / 3;
vector $kick = <<2, 3, 4>>;
$kick += <<7, 5, 1>>; // $kick = $kick + <<7, 5, 1>>
MAXScript supports the first 4 of the above 5 shortcuts, as it does not feature the operator %.
mice = 25.3
mice -= 6
mod mice 4 -- % is not supported as operator in MXS
mice /= 3
kick = [2, 3, 4]
kick += [7, 5, 1] -- result is [9,8,5]
There is also a ^= operator that is only defined for a vector. This operation calculates a cross product on the left-hand side vector and the right-hand side vector operand.
Example
vector $ice = <<1, 2, 3>>;
$ice ^= <<2, 4, 4>>; // $ice = $ice ^ <<2, 4, 4>>
This shortcut is not available in MAXScript.
There are shortcut operators that can be used for incrementing and decrementing floats and ints by 1. The following table shows these operators, their expanded syntax, and their values.
|
Shortcut Syntax |
Expanded Syntax |
Value |
|
variable++;
|
variable = variable + 1;
|
variable;
|
|
variable--;
|
variable = variable - 1;
|
variable;
|
|
++variable;
|
variable = variable + 1;
|
variable + 1;
|
|
--variable;
|
variable = variable - 1;
|
variable - 1;
|
When the increment or decrement shortcut operator precedes the variable, think of the increment or decrement as occurring before the statement is executed. However, when the operator is after the variable, think of the increment or decrement as occurring after the statement is executed.
Examples
float $eel = 32.3;
float $crab = $eel++; // $crab = 32.3; $eel = 33.3;
$crab = $eel--; // $crab = 33.3; $eel = 32.3;
$crab = --$eel; // $crab = 31.3; $eel = 31.3;
$crab = ++$eel; // $crab = 32.3; $eel = 32.3;
Warning
If more than one increment or decrement shortcut operator acts on the same variable in the same statement, the order of evaluation of the operators is unpredictable. Try to avoid this situation.
MAXScript does not support this type of increment and decrement by 1. Instead, you can use the shortcut described earlier on this page and use
variable +=1 and variable -=1.
Because MAXScript is an expression-based language, the expression will ALWAYS be evaluated BEFORE the result of the expression can be used in other expressions with lower priority. Thus, there is no way shortcut to achieve the same effect of increment after executing the statement.
A possible syntax to achieve the same results as in the above example would be:
eel = 32.3
crab = eel; eel +=1 -- the result is crab = 32.3; eel = 33.3
crab = eel; eel -= 1 -- the result is crab = 33.3; eel = 32.3
crab = (eel -=1) -- the result is crab = 31.3; eel = 31.3
crab = (eel +=1) -- the result is crab = 32.3; eel = 32.3
Interestingly, because of the less characters needed (no type declaration, no $ for variables, no semicolon at the end), the MAXScript code is as long or even shorter than the MEL version.