Mel: 2D Arrays

From wikinotes

Technically, Matrices are 2D arrays, but in Maya, Matrices are not scaleable. The following is a tool I reverse-engineered from Complete Maya Programming Vol. 1 for creating arrays that are scaleable in one direction. Below that, I instruct how I managed to create a 2D array that is scaleable in two directions using this method for my WP_TextureSourcer program.

2D Arrays Scaleable in One Direction

x1 x2 x3
y1 a(1) b(2) c(3)
y2 d(4) e(5) f(6)
y3 g(7) h(8) i(9)
y4 j(10) k(11) l(12)

( (y value - 1) * (# of columns) ) + x value = Array cell number

So let's say you wanted cell (2,3) (which is x2, y3)

( (y value - 1) * (# of columns) ) + x value = Array cell number ( (3 - 1) * (3) ) + 2

What you are doing is calculating the area up until the current row, and then adding your x number to it. Look at this illustration for a more visual representation of the ((3 - 1) * 3)


x1 x2 x3
y1 *a(1)* *b(2)* *c(3)*
y2 *d(4)* *e(5)* *f(6)*
y3 g(7) h(8) i(9)
y4 j(10) k(11) l(12)

then you add the x value:

x1 x2 x3
y1 *a(1)* *b(2)* *c(3)*
y2 *d(4)* *e(5)* *f(6)*
y3 * g(7)* *h(8)* i(9)
y4 j(10) k(11) l(12)


2D Arrays Scaleable in 2 directions

The obvious limitation of this is that you can extend the Y value as much as you like, but if you want to extend the X columns, you will need the script to redo the entire graph.

Notes for future usage:

In this case I was fortunate since I already had an array to work with for each Y value that only needed to be divided for every "/" I think I would take that approach again if I ever wanted to have a scaleable 2D array in 2 directions:

One initial Array for the full graph line by line:

Ygraph[0] = {"a/b/c/d"};
Ygraph[1] = {"e/f/g/h"};
etc...


Then a Second array created from that array fulfilling the same role as $arrayData[] below. Another slightly slower approach might be to use tokenize every time you want to pull an X value:

tokenize Ygraph[0] "/" $bufferB;
print $bufferB[3];


assigning the data to the graph:

In this example, $Xcolumns was already defined by finding the smallest number of directories for all selected files ($Yrows) It was unecessary for the purposes of this script to compare more than the smallest number of files since I was looking for common paths between files.

for ($i = 0; $i < size($Yrows); $i++) {
	tokenize $origPath[$i] "/" $bufferB;		//this line pulls in a path $origPath[] and splits it into array $bufferB based on where the "/" are

	for ($f = 0; $f < $Xcolumns; $f++) {
		$arrayData[$t] = $bufferB[$f];			//$arrayData[] stores the data with actual numbers (1 - 12 in the above graph example)
		$t++;
	}
}

Processing the Data in the graph

This is all very specific to the situation - but I highly recommend a gander through

WP_textureSourcer_v2.2.mel in the WP_TextureGetPath procedure