Data-type Conversions

 

Data-type conversion lets you convert the value of one type to the value of another type. For example, when you convert a float with a value of 1.4 to an int, its value becomes 1.

 

The table below is a summary of how each data type is converted to a different data type.

 

 

Conversion To

 

int

float

string

vector

matrix

int ($i)

perfect

perfect

perfect

<<$i, $i, $i>>

none

float ($f)

without fraction

perfect

 

perfect

 

<<$f, $f, $f>>

none

string

without fraction if starts with number, else 0

perfect if starts with number,

else 0

perfect

 

perfect if starts with vector or floats with remaining elements 0

none

 

vector

 

length of vector without fraction

length of vector

 

3 floats separated by a space

 

perfect

 

perfect for [1][3] matrix, else none

 

matrix

for [1][3] matrix or smaller, length of matrix without fraction

for [1][3] matrix or smaller, length of matrix

none

 

for [1][3] matrix or smaller, perfect with remaining elements 0

 

perfect

 

 

 

A scalar type is a non-array type. There is no conversion from array to scalar types or from scalar to array types.

 

Examples of type conversion

int $ski = 1.8; // Assigned: 1

vector $crads = 1.7; // Assigned: <<1.7, 1.7, 1.7>>

int $wild = " 3.44 dogs"; // Assigned: 3

vector $wrd = " 8 2.2 cat"; // Assigned: <<8, 2.2, 0>>

int $my = <<1, 2, 3>>; // Assigned: 3

string $oo = <<6, 2.2, 1>>; // Assigned: "6 2.2 1"

matrix $so[1][3] = $wrd; // Assigned: <<8, 2.2, 0>>

float $mole = <<0.3, 0.4>>; // Assigned: 0.5

vector $ole = <<2, 7.7>>; // Assigned: <<2, 7.7, 0>>

 

 

MAXScript does not provide this form of conversion, because the left-hand side is type-free, and the type of the variable is always dictated by the right-hand side. What You Set Is What You Get.

In all above examples, the variable on the left (which cannot have an explicit type declaration in MXS) will be assigned exactly the right-hand side value without any changes:

ski = 1.8 -- Assigned: 1.8

crads = 1.7 --Assigned: 1.7

wild = " 3.44 dogs" -- Assigned: " 3.44 dogs"

wrd = " 8 2.2 cat" -- Assigned: " 8 2.2 cat"

my = [1, 2, 3] -- Assigned: [1,2,3]

oo = [6, 2.2, 1] -- Assigned: [6, 2.2, 1]

so[1][3] = wrd -- Assigned: " 8 2.2 cat" to the 1,3 element assuming so is already an array, otherwise ERROR

mole = [0.3, 0.4] -- Assigned: [0.3, 0.4]

ole = [2, 7.7] -- Assigned: [2, 7.7]

 

 

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. The Examples of type conversion are all automatic type conversions. However, occasionally unexpected automatic type conversions can create a problem. Following are the rules concerning automatic type conversion.

 

There are five main rules for automatic type conversion. In these rules, the word operand is used instead of value since operand stands for the value being operated on.

 

* Strings dominate all other types.

* Vectors dominate floats.

* Floats dominate ints.

* If one operand is an int type and the other is a float type, MEL converts the int to a float.

* Between vector and matrix types, the type on the left-hand side dominates.

* In assignment, the type on the left-hand side dominates (see Declaring and assigning variables).

 

In an assignment operation, the type of the right-hand side is converted to the type of the left-hand side. The first four rules apply for sub-expressions during the computation of the right-hand side; a final conversion takes place when assigning to the left-hand side.

 

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

 

 

Examples

$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!".

 

 

MAXScript provides similar type conversions. It has to deal with more types than MEL (incl. Point2, Point4, RGB, FRGBA etc.), but does not support all conversion rules, for example Integers and Floats do not convert directly into Point3 vectors.

 

var1 = 7 + 1.3 -- Result 8.3

var2 = 7.9 + 2 -- Result 9.9

var3 = 2 + [4, 5, 6] -- ERROR. Does not convert Integer to Point3 vector.

var4 = 0007 + " Lives!" -- ERROR. Does not convert Integer to String. Requires explicit conversion.

 

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)

 

 

The explicit type conversion in MAXScript is preformed using the AS reserved keyword. String values cannot be converted into other values using the AS method though, and require the use of the Execute method (equivalent to the eval command in MEL).

 

Z = "[1, 2, 3]" as Point3 -- ERROR, requires Execute

Z = execute "[1, 2, 3]" -- Classof Z returns Point3

cools = 7 as Float -- Result is 7.0, Classof cools returns Float

ools = 47.554 as String -- Result is "47.554", Classof ools returns String

 

Some MAXScript specific conversions include:

 

Point3 as Color

Converts a Point3 vector to RGB Color. Values outside the 0-255 8bpc range are NOT clamped.

For example

[100,255,30] as color -- Result is (color 100 255 30)

[100,500,30] as color -- Result is (color 100 500 30)

 

Color as Point3

Converts the RGB(A) color value to a Point3 vector, keeping values in 8bpc 0-255 range. If Alpha component is present in the RGBA value, it is lost.

For example

(color 100 255 30) as Point3 -- Result is [100,255,30]

(color 100 255 30 15) as Point3 -- Result is [100,255,30]

 

Color as Point4

Converts the RGB(A) color value to a Point4 value representing Floating Point Colors. If Alpha component is missing, assumes opaque alpha:

For example

(color 100 255 30) as Point4 -- Result is [0.392157,1,0.117647,1]

(color 100 255 30 15) as Point4 -- Result is [0.392157,1,0.117647,0.0588235]

 

Point4 as Color

Converts a Point4 value to RGB Color. Values outside the 0-255 8bpc range are NOT clamped.

For example

[0.392157,1,0.117647,1] as color -- Result is (color 100 255 30)

[0.392157,1,0.117647,0.0588235] as color -- Result is (color 100 255 30 15)

[0.392157,2,0.117647,0.0588235] as color -- Result is (color 100 510 30 15)

 

Any value as String

All MAXScript values can be converted to String, including Integers, Floats, Vectors, Colors, Matrices, Bitmap values etc. The back conversion from String to the original value using Execute must not necessarily work in all cases, for example converting a Bitmap value to String and Back does not yield the original Bitmap, but this works for typical values like Integers, Floats, Vectors and Matrices.

 

String as StringStream

Converts a String value into a StringStream value, allowing the sequential read access using methods similar to the ones available for ASCII File Access. Useful for parsing complex strings as "virtual files".

 

ObjectSet as Array

Another typical MAXScript conversion turns a system object set or objects collection into an array of Scene Nodes (Scene Objects).

For example,

selection as array --returns an array of the currently selected objects

$Box* as array --returns an array of all objects whose name starts with Box

Lights as array --returns an array of all scene Lights

Geometry as array --returns an array of all Geometry objects in the scene