Automatic type conversion
 
 
 

Automatic conversion

Maya’s automatic type conversion lets you convert types without explicitly stating them. It also automatically converts the data type for you if the type specified is not acceptable.

Occasionally unexpected automatic type conversions can create a problem. Knowing the rules for type conversion can help you fix these types of errors:

The following table demonstrates the rules for automatic conversions.

Operation Resulting data type
int operator float float
float operator int float
int operator vector vector
vector operator float vector
vector operator matrix vector
matrix operator vector matrix
matrix operator string string
string operator int string
$var1 = 7 + 1.3; // Type: float (8.3)
$var2 = 7.9 + 2; // Type: float (9.9)
$var3 = 2 + <<4, 5, 6>>; // Type: vector <<6, 7, 8>>
$var4 = 0007 + " Lives!"; // Type: string ("7 Lives!")

In the last example, 0007 is an int of value 7, which is converted to a string and concatenated with “Lives!”. The result is a string which implicitly declares var4 to be of type string with value “7 Lives!”.

Explicit conversion

There are two ways to explicitly convert a value of one type to another type. The most common way is to specify the type in parentheses before the value. For example:

$Z = (vector) "<<1, 2, 3>>"; // Type: vector (<<1, 2, 3>>)
$cools = (float) 7;         // Type: float (7)
$ools = (string) 47.554;    // Type: string ("47.554")

You can also explicitly convert a value to another type by specifying the type followed by the value in parentheses. For example:

$ly = vector("<<1, 2, 3>>"); // Type: vector (<<1, 2, 3>>)
$ooly = int(3.67); // Type: int (3)