Script Controller

Main toolbar > Open Track View > Select a track in the Track View Hierarchy > Track View toolbar > Assign Controller > Script

Graph Editors > Track View > Open Track View > Select a track in the Track View Hierarchy > Track View toolbar > Assign Controller > Script

Six Script controllers are available in the software:

The Script controllers work similarly to Expression controllers. They provide a Properties dialog where a script can be entered for computing the controller value. The primary advantages of Script controllers are:

Writing Controller Scripts

The software interprets the text you type into the Script text box as the body of a MAXScript block expression. You can type as many expressions as you want on as many lines as you want, and they are evaluated in turn. The value of the last expression is taken as the controller value. This value must yield the right type for the controller: Float for float, Point3 for position, Quat for rotation, and so on.

Since the text is inside a block expression, you can declare local variables that are visible only within the script and are temporary for one evaluation. You can also declare or access global variables that are shared with all other scripts in MAXScript and hold their values from one evaluation to the next.

A controller is always evaluated by the software with respect to a specific animation time. This might be the current time slider or incrementing frame time if an animation is playing. In the case of Script controllers, the time being evaluated is used to establish an automatic "at time" context around the controller script, so any properties you access (outside of other explicit ôat timeö expressions) yield the correct values for the current controller evaluation time. This means you don't have to do anything special in your scripts to work at the correct time. You can access the evaluation time, with the standard MAXScript variable, current Time. You can also reference scene property values at other times by using "at time" expressions in your scripts, as in regular MAXScript programming.

See also

Procedure

Example: To keep an object centered relative to other objects in the scene during an animation:

  1. Assign a Script controller to the Position track of the object that should remain centered.

  2. Right-click the Position track in the Track View Hierarchy and select Properties.

  3. Type the following script in the Script Controller dialog window.

    local pos=[0,0,0]

    for o in objects where o != $foo do

    pos += o.pos

    pos / (objects.count - 1)

    This script computes the average position of all objects, except the current one (written as $foo here) by setting up a local, iterating over all objects except $foo, accumulating a total position vector, and computing the average in the last line, which is the final result of the script.

Interface

After assigning a Script controller, a Properties dialog is available by right-clicking the track in the Track View Hierarchy and selecting properties or clicking the Properties button on the Track View toolbar.

Script Controller dialog

Script: Type the script to compute the controller value here.

Result: Shows the results of an evaluation or any error messages caused by errors in your script.

Evaluate: Evaluates the controller script and prints the result in the Result box. The evaluation is computed for the current position of the time slider.

Close: Compiles and checks the controller script. If everything works, the Properties dialog is closed. Any problems result in a query box asking whether you really want to close the box with an incorrect script. If you do, the controller will yield a null value (0, [0,0,0],) when evaluated.