Optimize expressions

 
 
 

Maya does calculations internally in centimeters, radians, and seconds. A radian is an angular unit commonly used in mathematics. It equals 180 degrees divided by pi, or roughly 57.3 degrees.

When you assign a number to an attribute whose value is a measurement unit, the expression interprets the number, by default, as the appropriate unit selected in the Settings category of the Preferences window. By default, the unit type selections are centimeters, degrees, and 24 frames per second.

If a measurement unit you’ve chosen differs from the corresponding internal unit, Maya converts the number to the appropriate internal unit to do the assignment.

Example

Suppose you’ve selected degrees from the Angular menu in the Settings part of the Preferences window. You then write this expression for an object named Ball:

Ball.rotateZ = 10;

Maya reads the 10 as being 10 degrees, then converts the value to the appropriate number of radians to make the assignment to Ball’s rotateZ attribute. The conversion happens automatically. From your standpoint, Maya is simply rotating Ball 10 degrees.

In non-particle expressions, these automatic conversions affect Maya performance. Because the expression executes slower, Maya slows when you play, rewind, or otherwise change the animation time. Saving, opening, and other file operations on the scene containing the expression are also slower.

To boost Maya performance, you can turn off conversion to internal units. If you do so, you must convert units in expression statements.

To speed expression execution:

  1. Display the Expression Editor.
  2. Select one of these Convert Units options:
    None

    Converts no units. You must assign values to attributes as centimeters, radians, or seconds, as appropriate. Execution is fastest with this option.

    Angular Only

    Converts angular units, but no others. You must assign values to attributes as centimeters, seconds, and degrees, as appropriate. (This assumes you’re using the default degree setting in the Preferences. If you’ve selected radians, you must enter radians.)

    If you’re confused by converting degrees to radians, select this option. Execution is fast with this option—unless the expression has many angular values.

To return to default conversions:

  1. Display the Expression Editor.
  2. For the Convert Units option, select All.

    This lets you enter all measurement numbers in the same units specified in the Working Units preference settings. Execution is slowest with this selection, but expression writing is simplest.

    You can set a different conversion option for each expression.

Example

Suppose, in the Preferences window, you’ve set Linear units to millimeter and Angular units to degrees. You then write the following expression:

Ball.translateX = 5;
Ball.rotateZ = 10;

All causes Maya to read 5 as millimeters and 10 as degrees.

None causes Maya to read 5 as centimeters and 10 as radians.

Angular Only causes Maya to read 5 as centimeters and 10 as degrees.

To convert units in an expression statement

  1. You must convert the units mathematically in a statement.

Examples

Suppose, in the Preferences window, you’ve set Linear units to millimeter and Angular units to degrees.

In the Expression Editor you set the Convert Units option to None and enter this expression:

Ball.translateX = 5;
Ball.rotateZ = 10;

None causes Maya to read 5 as centimeters and 10 as radians, which is not the result you’re seeking.

To assign 5 millimeters to Ball’s translateX attribute, you must convert 5 to the appropriate number of centimeters. To assign 10 degrees to Ball’s rotateZ attribute, you must convert 10 to the appropriate number of radians.

The following statements do this:

Ball.translateX = 5.0 / 10.0;
Ball.rotateZ = 10.0 / 57.3;

There are 10 millimeters per centimeter. In other words, a millimeter is a centimeter divided by 10. So 5 millimeters equals 5 centimeters divided by 10. You therefore use the operation 5.0 / 10.0.

Important

When you divide floating point attributes or variables, enter the floating point value 5.0 for an even number such as 5. This ensures Maya won’t try to convert the result to int.

There are 57.3 degrees per radian. In other words, a degree is a radian divided by 57.3. So 10 degrees equals 10 radians divided by 57.3. You therefore use the value 10.0 / 57.3.

If you need a more precise conversion to radians, divide a degree by 57.29578 instead of 57.3. You can instead use the deg_to_rad function as follows:

Ball.rotateZ = deg_to_rad(10.0);

The deg_to_rad function converts 10.0 degrees to a precise radian equivalent. See deg_to_rad for details.

Turning off unit conversion affects only expressions. It doesn’t affect other Maya commands, options, or displays. For instance, the preceding example expression assigns centimeters to translateX and radians to rotateZ. The Channel Box still displays values for these attributes in millimeters and degrees. It displays values in whatever units you select in the Settings part of the Preferences window.

You can’t turn off unit conversion for particle shape node expressions. Maya handles unit conversion differently for such expressions with little impact on performance.