Mel Script - Vectors and Matrices

 Within mel scripting we also have access to two in-built data types, a vector and a matrix. These two data types are very useful for calculating transformations etc when manipulating scene objects.

 Vectors The vector data type is a 3D vector on which you can perform all manner of standard geometric operations.

 ``` { // declare a 3D vector vector \$vec = <<5.2, 1.9, 0.3>>; // print the values. To access the vector // components, use .x, .y and .z print(\$vec.x + " " + \$vec.y + " " + \$vec.z + "\n"); // a *slight* annoyance here in that if we want to assign // values to individual components, ie x,y,z; We have to // use a full assignment, since trying to assign a value // to \$vec.x will result in a an error :( // \$vec = <<\$vec.x,\$vec.y,10>>; print(\$vec.x + " " + \$vec.y + " " + \$vec.z + "\n"); // declare 2 vectors vector \$x=<<1,-0.2,0.4>>, \$y=<<0.2,1,0.9>>; // perform cross product \$z = cross(\$x, \$y); // print data print("cross product ="+ \$z.x + " " + \$z.y + " " + \$z.z + "\n"); // perform dot product and print print("dot product =" + dot(\$x,\$y) + "\n" ); // to determine the length of the vector, use mag print("length= " + mag(\$x) + "\n"); // calculate some noise using our vector print("noise= " + noise(\$x) + "\n"); } ```

 Matrices Matrices can be declared of any dimension and essentially form multi-scripted floating point arrays.

 ``` { // declare a 4x4 matrix matrix \$m1; // declare a matrix with values matrix \$m2 = <<1,0,0,0; 0,1,0,0; 0,0,1,0; 1,2,3,1>>; matrix \$m3 = <<1,0,0,0; 0,1,0,0; 0,0,1,0; 0,0,0,1>>; // assign a value to a matrix element \$m3 = 0.3; \$m3 = 0.4; \$m3 = 0.6; // multiply together \$m1 = \$m2 * \$m3; // print the matrix print( \$m1 +" "+ \$m1 +" "+ \$m1 +" "+ \$m1 +"\n"+ \$m1 +" "+ \$m1 +" "+ \$m1 +" "+ \$m1 +"\n"+ \$m1 +" "+ \$m1 +" "+ \$m1 +" "+ \$m1 +"\n"+ \$m1 +" "+ \$m1 +" "+ \$m1 +" "+ \$m1 +"\n" ); } ```