Script Controller

GENERATE/PSCRIPT2.gif  

Five scriptable controllers are available in MAX, mirroring the five current expression controllers. They are:

The Script Controller functions in a similar way to expression controllers, providing a properties dialog in which a script can be entered that is used to compute the controller value. The primary advantages of script controllers are:

Refer to MaxScript for a complete explanation of this scripting language.

Writing Controller Scripts

3DS MAX interprets the text you type into the Script text box as the body of a MAXScript block expression, so you can type as many expressions as you want on as many lines as you want and they are evaluated in turn and 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, etc.

Since the text is effectively 'inside' a block expression, you can declare local variables which 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 MAX with respect to a specific animation time. This might be the current time slider or incrementing frame time if an animation is playing or a render is under way. 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 eval time if you need, with the standard MAXScript variable, current Time. You can also reference scene property values at other times by using explicit 'at time' expressions in your scripts, as in normal MAXScript programming.

See MAXScript.

Reference

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

The Script Controller dialog:

GENERATE/SS2.gif  

Script: You type the script to compute the controller value here. See the section below on writing controller scripts for details.

GENERATE/SR2.gif  

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

GENERATE/SSL2.gif  

Evaluate button: Cause an evaluation of the controller script to be made and prints the result in the Result box. The evaluation is computed for the current position of the MAX time slider.

Load/Save buttons: Load and save scripts to text files.

Close button: 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], etc.) when evaluated by MAX.

How 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 on the Position. track in the Track View hierarchy and select properties.
  3. Enter the following script.
  4. 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 compute the average in the last line, which is the final result of the script.

    local pos = [0,0,0]

    for o in objects where o != $foo do

    pos += o.pos

    pos / (objects.count - 1)