Expression Techniques

In 3ds Max, you can use mathematical expressions (rather than constant numbers) to express parameter values. For example, you could use the expression 24*6 to represent the number 144.

You can use mathematical expressions to control the following object properties:

Parameter wiring, the expression controller, and the numerical expression evaluator all use expressions, which are described in this topic.

Tip: To use the numerical expression evaluator, click in any standard numeric field (except caddy fields), press Ctrl+N, and enter an expression into the dialog that opens.

An expression is a mathematical function that returns a value. You can use expressions to control the following scene elements:

Scene element Calculatable property
Creation parameters Any numeric creation parameter
Transforms Position [X, Y, Z]

X Rotation

Y Rotation

Z Rotation

Scale [X%, Y%, Z%]

Modifiers Any numeric modifier parameter (including creation parameters)
Materials Colors [R, G, B]

Any numeric material parameter

Note: Expressions work only with the individual XYZ components of Euler rotation. You can't assign an expression to TCB rotation or other kinds of rotation controllers.

The following links jump to sections within this topic:

Expression Return Types

The type of value returned by an expression depends on the kind of controller:

Operators

In the following tables, p and q are any scalar value or expression, V and W are any vector value or expression. (The character "x" is used as the vector cross-product operator.)

Scalar Operators

These are the arithmetic operators for scalar values:

Operator Use Meaning
+ p+q Addition
- p-q Subtraction
- -p Additive inverse
* p*q Multiplication
/ p/q Division
^ p^q power (p to the power of q)
** p**q ^ and ** are the same operation

You can also use logical (Boolean) operators with scalar values. These operators all return 1 if true, 0 otherwise:

Operator Use Meaning
= p=q equal to
< p<q less than
> p>q Greater than
<= p<=q less than or equal to
>= p>=q Greater than or equal to
| p|q Logical OR, returns 1 if either p or q is nonzero; otherwise, returns 0
& p&q Logical AND, returns 1 if p and q are both nonzero; otherwise, returns 0
Tip: Logical operators are useful with the "if" function.

Vector Operators

For vectors that have a variable name, you can use a special component operator (.) to refer to the three scalar components of the vector:

Use Meaning
V.x first component (X)
V.y second component (Y)
V.z third component (Z)

These are the operators for vector arithmetic:

Operator Use Meaning
+ V+W Addition
- V-W subtraction
* p*V scalar multiplication
* V*p scalar multiplication
* V*W dot product
X VxW cross product
/ V/p scalar division

Operator Precedence

Expressions have eight levels of precedence. The higher the operator is on the list, the earlier it is evaluated.

Operator Level of Precedence
- + as unary operators, as in -8, +25
. the component operator, as in V.x
** ^  
X cross product
* /  
+ -  
= < > <= >=  
| &  

Parentheses are a special case. They are a grouping or subexpression operator that is provided so you can override the precedence order of the other operators.

Variables

In expressions you write for expression controllers, variables are represented by symbolic names. You create them to contain constant or variable values in your expressions. Several predefined variables are also provided. Some of these have a constant value, others can vary.

In expressions used for parameter wiring and the numerical expression evaluator, you can use predefined variables with constant values.

Predefined Variables with Constant Values

These are the predefined variables that have a constant value (variable names are case-sensitive):

Variable Name Constant Value Use
pi 3.14159 Ratio of a circle's circumference to its diameter.
e 2.71828 Base of natural logarithms.
TPS 4800 Ticks per second. The tick is the basic time unit of 3ds Max animation.

Predefined Variables with Variable Values

These are the predefined variables that have a variable, time-based value (variable names are case-sensitive).

Variable Name Meaning
F Frame number. For each frame, F equals the current frame number, counting from zero. The range of frames can vary depending on the number of frames in the active time segment.
NT Normalized time. By definition, normalized time (NT) ranges from 0 to 1 over the active time segment, regardless of how many frames are in the segment. If you base an expression on NT, its effect happens exactly once over the range. You can also multiply NT by a factor for the expression's effect to occur a certain number of times (for example, 2*NT causes the expression's effect to occur twice). Expressions based on NT speed up or slow down if you change the length of the time segment.
S Seconds (elapsed time in seconds). Elapsed time is measured from the first frame to the current frame. The range of seconds can vary depending on the total time of the active time segment.
T Ticks (elapsed time in ticks). There are 4800 ticks per second. Elapsed time is measured from the first frame to the current frame. The range of ticks can vary depending on the total time of the active time segment.

Rules for Variable Names

  • Variable names can contain as many alphanumeric characters as you like. Their length is not limited.
  • Variable names cannot contain spaces.
  • The variable name must begin with a letter. Numbers are valid within a variable name (as in "Pos1" or "M23").
  • Variable names are case-sensitive. For example, "pos", "Pos", and "POS" designate three different variables.
  • You can't create a variable with a name that duplicates another name, including the variable names that are predefined.

Functions

Following is a list of the functions provided for expressions. In this list, p, q, and r represent scalar values or scalar expressions. V and W represent vector values or vector expressions.

To use a function in an expression, enter the name of the function and appropriate arguments to it.

Trigonometric Functions

The sine, cosine, and tangent functions take an angle in degrees and return a floating-point value. The arc functions take a floating-point value and return a value in degrees.

Function Meaning
sin(p) sine
cos(p) cosine
tan(p) tangent
asin(p) arc sine
acos(p) arc cosine
atan(p) arc tangent

Hyperbolic Functions

Hyperbolic functions take a floating-point value and return a floating-point value.

Function Meaning
sinh(p) hyperbolic sine
cosh(p) hyperbolic cosine
tanh(p) hyperbolic tangent

Conversion Between Radians and Degrees

Function Meaning
radToDeg(p) takes p in radians and returns the same angle in degrees
degToRad(p) takes p in degrees and returns the same angle in radians

Rounding Functions

Function Meaning
ceil(p) smallest integer greater than or equal to p
floor(p) largest integer less than or equal to p

Standard Calculations

Function Meaning
ln(p) natural (base e) logarithm
log(p) common (base 10) logarithm
exp(p) exponential function exp(p)=e^p
pow(p,q) p to the power of q (p^q)
sqrt(p) square root
abs(p) absolute value
min(p,q) minimum returns p or q, depending on which is smaller
max(p,q) maximum returns p or q, depending on which is greater
mod(p,q) remainder of p divided by q

Conditional Functions

Function Meaning
if(p then q else if r then s) uses the standard “if/then/else if” structure. For example:

if (X_Position<0) then 0 else if (X_Position>=0 and X_Position<=10) then X_Position*3 else 100

This returns 0 if the X position is less than 0, or the X position times 3 if the X position is between 0 and 10 inclusive, or 100 if the X position is greater than 10.

The “else if” part is optional, and can be repeated to specify several different conditions if necessary.

vif(c,V1,V2) "Vector If" (Value is V1 if c is true, else V2.)

Vector Handling Functions

Function Meaning
length(V) length of V
comp(V,i) i'th component (I=0,1,2): comp([5,6,7],1)=6
unit(V) returns a unit vector in the same direction as V
Note: The comp function is an alternative to the notation V.x, V.y, V.z.

Special Animation Function

Function Meaning
noise(p,q,r) 3D noise: returns a randomly generated position

The arbitrary values p, q and r, are used as a random-generation seed. You can reuse these values to ensure that noise() returns the same value.