March 2019: ewertb.soundlinker.com is getting relocated to ewertb.mayasound.com, please update your bookmarks.

MEL How-To #10

Back · Previous · Next Maya

How do I declare a matrix whose size is determined at run-time?

MEL does not allow you to declare a matrix using dynamic variables for its size. The following does not work:

int $a = 4;
int $b = 4;
matrix $m[$a][$b];
// Error: Line 1.12: Syntax error //

To work around this, generate a string command for the declaration and use the ‘eval’ command to execute it:

int $a = 4;
int $b = 4;
string $declare = ( "matrix $m[" + $a + "][" + $b + "];" );
eval $declare;
print $m;
<< 0, 0, 0, 0;
   0, 0, 0, 0;
   0, 0, 0, 0;
   0, 0, 0, 0 >>

Note: I've had a number of e-mails reporting that this example does not work for them. It seems that, while the example does work when run from the Script Editor, it reports an error when run from a procedure:

// Error: "$m" is an undeclared variable.

I have not determined a solution for this yet. Sorry about that.

So far, the best work-around I've found is to staticly declare your matrix for the largest size you think is required, and using only the sub-matrix from this as needed. Provide an assertion in your script so the desired size does not exceed the declared size.

Friday, September 29, 2000