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[4][4];

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