General orientation for writing controller scripts
How
to enter a controller script?
Anim8or v0.95 does not have a user friendly interface for
scripts (least to say) and we have to follow different steps for different
controllers, also depending if the controller has already a script, or not.
If the desired controller has a script, the simplest way is to click Options
> Graph Editor and double-click the name of the controller. If the controller
does not have a script yet, do as described below.
1. Visibility
Double click the desired scene element. An editor opens displaying the
properties of the element. Click the “…” button near the “Visible”
check box. In the Key Value Editor that pops up, make sure “Expression On”
is checked, then click “Edit”. The “Script Viewer” (which is also the
script editor) opens.
If the scene element does not have a “Visible” check box (cameras don’t
have), with the element still selected you have to click Edit >
Controllers… > New. Enter “visibility” (no quote marks) for Controller
Name and click OK. Click OK once more to close the other window. Nothing visible
happens, but the element has received a visibility controller. To access it,
click Options > Graph Editor and you will see “visibility” written in the
window. Double-click it to get the Key Value Editor, then continue as described
above. If you managed to get through all these menus without loosing your
patience, learning controller scripts will be a piece of cake!
2. Position
Double click the desired scene element. An editor opens displaying the
properties of the element. Click the “…” button near the “Location”
boxes. In the Key Position Editor that pops up, make sure “Expression On” is
checked, then click the “Edit” button to enter the editor.
3. Orientation
Double click the desired scene element. An editor opens displaying the
properties of the element. Click the “…” button near the “Orientation”
boxes. In the Key Position Editor that pops up, make sure “Expression On” is
checked, then click the “Edit” button to enter the editor.
4. Scale
To create a scale script, you have to click Edit > Controllers… > New.
Enter “scale” (no quote marks) as Controller Name and follow the
instructions given above for creating a “visibility” controller script, but
applied for “scale”.
5 Color (applies
only for lights)
Double click the desired light. An editor opens displaying the properties of the
element. Click the “…” button at the right of the boxes showing the RGB
color values. In the Key Position Editor that pops up, make sure “Expression
On” is checked, then click the “Edit” button to enter the editor.
6. FOV (only for
cameras)
Double click the desired camera. The Camera Editor opens displaying the
properties of the camera. Click the “…” button at the right of the FOV. In
the Key Value Editor that pops up, make sure “Expression On” is checked,
then click the “Edit” button to enter the editor.
7. Active (only
for cameras)
Click Edit > Controllers… > New. Enter “active” (no quote marks) as
Controller Name and follow the instructions given above for creating a
“visibility” controller script, but applied for “active”.
8. Morph (only
for eobjects)
The names of the morph target controllers are already in the Graph Editor (ex.
“morph.morph01”), so you just have to double click them, check “Expression
On” and click “Edit” to open the script editor. However, for the eobject
you have to assign yourself a morph controller: With the eobject selected, click
Edit > Controllers… > New. Enter “morph” (no quote marks) as
Controller Name and follow the instructions given above for creating a
“visibility” controller script, but applied for “morph”.
NOTE:
Creating (accessing) a controller script from within Terranim8or is much
simpler:
- Select the scene element
- Click Edit > Controller Script > Visibility (or Scale, Position, etc)
A
few notes on coding controller scripts
For those who know nothing about coding, here are a few explanations needed later:
Comments
The ASL specification does not mention it, but comments enclosed between /*
*/ pairs can be inserted in a script. Anything following /* till the
first */, is considered a comment and is ignored by the ASL interpreter. This is
useful for explanations regarding the script for others, or even for yourself,
to remember after a while.
Instructions
An ASL controller script consists of one or more commands (instructions) to
be executed by Anim8or. These commands can be declarations of variables,
assignments, or various other statements. There are strict rules for the allowed
syntax of an instruction, described in the ASL specification. For example, each
instruction must end with a semicolon ‘;’.
Variables
Variables are used to temporarily store intermediate values. A variable has
a name and a type and can store a value of the declared type. Before we can use
it, we must declare a variable. Any variable declared by us (users) must have
its name beginning with the dollar sign ‘$’. Anim8or has some predefined
variables that begin with other characters.
Types
The reason for using types is not understood (usually) by beginners. Beyond
the fact that different types are stored differently in the computer, operations
on different types can show much different results. For example, dividing 5/2
(as integers) gives 2; the rest of 1 being discarded! Using floating point
numbers, 5.0/2.0 yields 2.5 as expected. Sometimes we want to perform operations
with integer arithmetic and the truncation of results is exactly what we want!
See the ASL specification for details about allowed types!
Example:
| float $ElementSize;
/* This instruction is a type declaration, which states that the variable named $ElementSize can hold floating point type values */ $ElementSize = 31.07; /* This is an assignment; the variable declared above is now initialized, receiving a value of 31.07 */ |
Each controller script must contain at least one assignment, where the controller receives a value. After all, this is the purpose of controller scripts, to assign a value to a controller. Controllers are predefined variables in ASL (actually, they are defined inside Anim8or). You can use them without declaring them, which is not allowed for other (user defined) variables! Here are the predefined types and names of controllers:
| int $visibility float $scale point3 $position quaternion $orientation point3 $color float $fov int $active float $morph |
In a controller script only a single kind of controller is allowed (ex. in a position script only $position is allowed, $scale (for example) will be rejected as undeclared variable!
Operators and
operands
Expressions contain variables and constant values together with symbols
defining operations that should be performed on them. Variables and numeric
constants are operands, while the symbols designating the operations are called
operators.
Examples:
|
$b = $a * 10; /* Operands are variable $a and the integer constant 10; operator is ‘*’ (multiplication). Further, ‘=’ is the assignment operator. Its left hand operand is $b,its right hand operand being the value of the expression $a * 10 */ $logical = !$var || $b < 3; /* This expression contains the assignment operator, the ‘!’ (NOT) operator, ‘||’ (OR) and ‘<’ (LESS THAN) operators. Except assignment, they are logical operators */ |
Logical operators compare their operands and the value of the expression reflects whether the comparison yielded true, or false. If a comparison evaluates to true, the result is 1, otherwise is 0. Zero and one are the values of logical operations. There are special constants defined by Anim8or for these values: ‘true’ stands for 1 and ‘false’ stands for 0.
Conditional
statements
The “if”, “while” and “for” statements contain conditions that
evaluate to true (1), or false (0). Depending on the result of the evaluation, a
set or other set of instructions is executed next. Conditional statements
introduce branching in the flow of computations. They are the essence of
programming!
Functions
Predefined functions are inbuilt algorithms for computing proper values from
input argument values.
Example:
| $orientation = RPYtoQuaternion(61., -127.3, 15.); /* The function ‘RPYtoQuaternion’ receives three floating point arguments as rotation angles for roll, pitch and yaw and computes a quaternion equivalent with the above mentioned rotations performed in that order. The computed quaternion is then assigned to $orientation */ |