Data Types (Variables) session 1



If you filled any form at the DMV or adjusted color value in art package you have used variables. Variables are temporary or persistent memory space to store data for a later use or processing. In general variable names should not start with a number or contain a space.


In computer science, an integer is a datum of integral data type, a data type which represents some finite subset of the mathematical integers. Integral data types may be of different sizes and may or may not be allowed to contain negative values. Integers are represented in a computer as a group of binary digits. Computer hardware nearly always provides a way to represent a processor register, or memory address as an integer; the set of integer sizes available varies between different types of computer. http://en.wikipedia.org/wiki/Integer_(computer_science)


Did you understand that? I did not the first time I read it.

Basically, in scripting we need to tell the program how much memory space it should reserve for our data variable. To simplify this process, smart people decide to define a set of data type and their memory size, so we wouldn't have to measure the memory size for each data type. These data types are: int (short or Int16, int or Int32, long or int64, UINT …), float (float, decimal, double ...), char, string (char array), vector (3 floats), array ( a sequence of int, char, floats, string or vectors) matrix( an array of arrays).

Int:

An int is whole number without decimals like 10, 5, 2147483647 (the max value for unsigned int 32). There are several int sizes and it can contain negative value or not

But in our situation we will refer to any number without a decimal point as an int and we let the program decide what size to use.

Later when we cover comparison in the conditions/choice session, we'll see that int comparison is more accurate than float (decimal numbers).


Here is how we tell the script that we will use int:

Maya Mel

int $red =128; // notice the $ sign before the variable and the ; at the end
int $startCount = 0;
int $last_frame = 200;


Max Script

--Max Script: (max script is type-free variables and type-safe)
red_int = 128 --max does not require type symbol before the variable
--as a good practice include the type in the name
startCount = 0 as int --converting to int
last_frame = 200


Python

#(python is fully dynamic-type system)
red = 128 # python like max does not require the type in a variable
startCount = int(0) #converting to int
last_frame = 200


Lua

--(Lua is a dynamically typed language which means that variables do not have types; only values do)
red = 128 -- global variable
local startCount = 0 -- local variable
last_frame = 200




Float:

The float type can represent numbers as large as 3.4E+38 (positive or negative) with an accuracy of about seven digits, and as small as 1E-44. The float type can also represent NaN (Not a Number), positive and negative infinity, and positive and negative zero. It is stored as a four-byte, single-precision (about 7 decimal digits), floating-point number.
(http://msdn.microsoft.com/en-us/library/ayazw934.aspx)

The term floating point refers to the fact that the radix point (decimal point, or, more commonly in computers, binary point) can "float"; that is, it can be placed anywhere relative to the significant digits of the number. The floating-point format needs slightly more storage (to encode the position of the radix point), so when stored in the same space, floating-point numbers achieve their greater range at the expense of precision. (http://en.wikipedia.org/wiki/Floating_point)

Division of number is a bit tricky and requires some attention. If we divide two int like 3/2 we will not get the expected result of 1.5 but will get 1. It is a good practice to convert all the number which will undergo mathematical operation to floats.

Here is how we tell the script that we will use float:

Maya Mel

//In Maya a float refers to a double (about 16 decimal digits) and stored in 64 bits.
float $level = 0.28; //$ sign before the variable and ; at the end
float $timeOfDay = 0.5;
float $last_speed = 200.45;


Max Script

--(The Number types can be freely intermixed and MAXScript will convert as necessary)
level = 0.28 -- max does not require type symbol before the variable
-- as a good practice include the type in the name

timeOfDay = 0.5 as float -- converting to float (or as double)
last_speed = 200.45


Python

#(python is fully dynamic-type system)
Level = 0.28 # python like max does not require the type in a variable
timeOfDay = float(0.5) #converting to float
last_speed = 200.45


Lua

--(a dynamically typed language which means that variables do not have types; only values do)

level = 0.28 -- global variable
local timeOfDay = 0.5 -- local variable
last_speed = 200.45



Array:

Array is a series of objects all of which are the same size and type (although they may have different values) and stored contiguously in memory (that is, there are no gaps between elements). Each object in an array is called an array element. Arrays can have more than one dimension. A vector is one-dimensional array and a matrix is a two-dimensional array. (http://www.webopedia.com/TERM/A/array.html)

Array is like the railway cars of the same size and carrying the same cargo of different amounts. You access the car by their relative position from the first car. We use open and closed bracket with a digit in between to reference array element or to define the size of array. Did you notice that [] lock like a top view of a railway car.

We need to remember that array is an object and when we say myList is an array and yourList = myList we are referencing myList and any change to the element of yourList will change the element of myList.

Here is how we tell the script that we will use array:

Maya Mel

int $scores[]; //Maya dynamically increase the size of the array as needed
float $testGrades[] = {5.25, 6.6, 1.3}; //an array of 3 floats
string $grocery_list[] = {"milk", "eggs", "cheese", "bread"};
float $oneTestGrade = $testGrades[1]; //refer to the second element


Max Script

--(An array is a variable length indexable sequence of values. The values in an array can be of any type. Array values are mapable.)
scores = #() --declaring an empty array
testGrades = #(5.25, 6.6, 1.3)
grocery_list = #("milk", "eggs", "cheese", "bread")
oneTestGrade = testGrades[1]; --refer to the first element
a = #(10,#(1,2,3),30,"test") --create a mixed type array


Python

import array

My_charArray = array.array('c',['e','g','g']) #first param id element type
a = [66.25, 333, 333, 1, 1234.5]
grocery_list = list(["milk", "eggs", "cheese", "bread"])#special array of objects

http://docs.python.org/library/array.html?highlight=array#array

Lua


--Tables can be used to hold arrays of information. Table constructors can contain a comma separated list of objects to create an array. The array elements can be accessed using square brackets, table[index]. Note that we are not --restricted to storing one type of data in an array. We can insert numbers, strings, functions, or other tables.
scores = { 1,1,2,3,5,8,13 } -- indexing into the array starts at 1, not at zero. scores [0] has the value nil
testGrades = {}
testGrades [1]=80.5 testGrades [2]=60.0 testGrades [3]=20.5
tableAsDictionary = { apple="green", orange="orange", banana="yellow" }
tableAsDictionary.apple = "red"




Vector: (like position and normals)

Vectors are a kind of sequence container. As such, their elements are ordered following a strict linear sequence. Vector containers are implemented as dynamic arrays. Their elements can be accessed not only using iterators but also using offsets on regular pointers to elements.
http://www.cplusplus.com/reference/stl/vector/

Scientists often use the word vector to mean a magnitude and direction. The vector is expressed as a point; for example, [0.5, 0.5, 0.7]. The length is the distance from the origin to that point, and the direction is similarly from the origin to (and through) the point. Most scripting languages have defined special arithmetic for vectors such as vector addition, multiplication and length.

Maya Mel

//In Maya a vector is a triple of floating point numbers (usually representing X, Y, and Z). It's convenient to have a triple-float data type in MEL because so many operations in 3D involve manipulating X,Y,Z values.

vector $myLocation = <<3.0, 7.7, 9.1>>;//use "<<" & ">>" to define vec val
vector $vert_normal = <<0.5, 0.789, 0.12356>>;
float $x_val = $myLocation.x;
float $y_val = $myLocation.y;
float $z_val = $myLocation.z;
vector $k = << 10,10,10>>;
vector $m = <<20, 20 ,20>>;
vector $sumOfknm = $k*$m;
float $d = $k.x*$m.x+$k.y*$m.y+$k.z*$m.z;
float $prodOfknm = $k * $m;


Max Script

--In 3ds Max, vectors have three values and describe positions in three-dimensional space. They can also represent percent scaling in X, Y, and Z; and (more abstractly) describe locations in RGB color space.

myLocation = [10, 20, 30]
my_destination = [100, 30.5, 41.3]
distance myLocation my_destination -- 91.3123 , distance is a max function


Python

#In Python Vector properties are similar to vectors used in science and engineering.

myLocation = vector( 10, 2.6, 32)
k_normal = vector(1.0, 0.5, 0.3)
n_length = sqrt(pow(k_normal.x, 2) + pow(k_normal.y, 2) + pow(k_normal.z, 2))


Lua

--Provided that a programmer has set up lua vector handling in game code and defined vector3 as an array of 3 doubles, you could set values as follow:

myLocation = vector3(10,20,30)
myLocation.x, myLocation.y, myLocation.z = 100, 200, 300




Matrix:

A m x n matrix is a two-dimensional array with m rows and n columns. Matrices are often used to store transform data of a 3D object.

Maya Mel

//In Maya a matrix is a two-dimensional table of floating point values. Unlike arrays, you must specify the size of a matrix when you create it
//To create a matrix with 2 rows of 3 columns, and assigns it to variable $a3. Each element of the matrix is initially filled with zeros.


matrix $m23[2][3]; //a 2D array of 2rows and 3 columns
matrix $m34[3][4] = << 2.05, 4.5, 3.25, 8.05;
1.12, 1.3, 9.52, 5.21;
7.23, 6.6, 2.34, 4.67 >>;
$m23[1][0] = 9; //setting the second row first column value to 9

//Unlike arrays, you cannot expand the size of a matrix. If you try to set a value outside the range of the matrix MEL will signal an error:
$m23[2][0] = 7; // ERROR: Element doesn't exist


Max Script

--In Max a Matrix3 class implements a 4x3 3D transformation matrix object. The transform matrix is a 3D homogeneous matrix typically used to hold object coordinate systems and transformations

myTransform = $Teapot01.transform

--When working with object transformation matrix3 values, the rows and their sub-elements cannot be modified directly.
--To change a row or an element of the row, assign the matrix3 value to a variable first, modify the matrix3 rows by assigning to the
--variable and then assign the variable back to the .transform property of the original object


myTransform.row4 = [10.0,20.0,30.0]
$Teapot01.transform = myTransform --$Teapot01 identifies a teapot objects by pathname



Python

#There are several ways to create a matrix based on the loaded module. Here we use nested list to create a matrix.

m23 = [[1.0 2.0,6.6],[ 3.0, 4.0,10.1]]


Lua

--One of the main way to represent a matrix in Lua is to use an array of arrays, that is, a table wherein each element is another table

N=2
M=3
mtNM = {} -- create the matrix
for i=1,N do
mt[i] = {} -- create a new row
for j=1,M do
mt[i][j] = 0
end
end



String:

A string is traditionally a sequence of characters and is often implemented as a byte array that stores a sequence of elements, using some character encoding. String elements may be mutated and/or its length changed.

A string variable may either have a dynamically or statically allocate memory storage depending on the programming language and/or precise data type used.

In scripting we use a null-terminated string which is a character string stored as an array containing the characters and terminated with a null character ('\0', called NUL in ASCII).

If a string variable contains the following character sequence "Frank", its length is 5 characters, but it occupies 6 bytes. Characters after the terminator may be either part of another string or just garbage.
http://en.wikipedia.org/wiki/String_(computer_science)


Each scripting language has several string operations that assist in processing its content. Some of these functions are:





Refer to the specific scripting language for a complete list and definition of string operations.

Maya MEL: http://download.autodesk.com/us/maya/2011help/Commands/cat_Language.html#Strings

MaxScript: http://docs.autodesk.com/3DSMAX/14/ENU/MAXScript%20Help%202012/files/GUID-A6A60FC7-6206-4FFC-80E2-0EF8544BE2C-465.htm

Python: http://docs.python.org/library/stdtypes.html

Lua: http://lua-users.org/wiki/StringLibraryTutorial



String Declarations:

Maya Mel



string $oneObject = "mySphere";
string $selectedObjectsList[] = `ls –sl`; //using a ls function to list selected


Max Script



oneObject = "mySphere"
selectedObjectList = selection as array


Python



oneObject = 'mySphere'
print len(oneObject) # results in 8


Lua

Lua: (Strings can be defined using single quotes, double quotes, or double square brackets)

oneObject = 'mySphere'
string.len(oneObject) //results in 8