Data type conversions

Maya is flexible in its handling of data types. If you do assignment or arithmetic operations between two different data types, Maya converts data type as necessary and doesn’t report a syntax error.

The following topics describe the conversions that occur in such instances. Understanding these details might help you troubleshoot unexpected attribute and variable values.

Unless you have programming experience, don’t intentionally convert data types. You might be confused by unexpected attribute and variable values.

Assign to a floating point attribute or variable

If you assign a vector to a floating point attribute or variable, Maya converts the vector to a floating point value according to this equation:

The x, y, and z numbers in the formula represent the three components in the vector. The resulting value is the magnitude of the vector.

Example

Ball.scaleY = <<1,2,0>>;

Maya assigns the floating point scaleY attribute the converted vector:

If you assign an integer to a floating point attribute or variable, Maya makes no conversion. None is necessary.

Example

Ball.scaleY = 1;

Maya assigns the value 1 to Ball.scaleY.

Assign to an integer attribute or variable

If you assign a floating point value to an integer attribute or variable, Maya deletes the decimal part of the number.

If you assign a vector to an integer attribute or variable, Maya converts the vector to an integer using the square root equation in the previous topic. However, it deletes the decimal component of the result.

Example

int $pi = 3.14;

Maya assigns the integer variable $pi the value 3.

int $temp = <<1,2,0>>;

Maya assigns the integer variable $temp this vector value:

It deletes the decimal component .2360607. The $temp variable receives the truncated value 2.

Assign to a vector attribute or variable

If you assign an integer or floating point value to a vector attribute or variable, Maya puts the integer or floating point value into each component of the vector.

Example

vector $speed = 1.34;

Because $speed is a vector, Maya assigns it <<1.34,1.34,1.34>>.

Use mixed data types with arithmetic operators

The following table lists how Maya converts data types when you use arithmetic operators between different types in an expression.

Operation Resulting data type
integer operator float float
integer operator vector vector
vector operator float vector

Example

Suppose you multiply a vector variable named $velocity by a floating point number 0.5 as follows:

$race = $velocity * 0.5;

If $velocity is <<2,3,0>> when the preceding expression executes, the $race variable is assigned the resulting vector value <<1,1.5,0>>.

Attention: When Maya does arithmetic operations on literal constants and variables without a declared data type, it guesses the data type based on the values present.

In the statement Ball.scaleY = 1/3;, for example, Maya treats 1 and 3 as integers because they have no decimal points. The expression divides integer 1 by integer 3. The integer result is 0 with a remainder of 1. Maya discards the remainder.

Because Ball.scaleY is a floating point attribute, Maya converts the integer 0 result to floating point 0 (which is the same value), then assigns it to Ball.scaleY.

To get the intended result of 1/3, you must type Ball.scaleY = 1.0/3.0;

Maya treats 1.0 and 3.0 as floating point numbers because they have decimal points. The number 1.0 divided by 3.0 results in 0.33333333333.