PDA

View Full Version : Error: undeclared variable


rafoarc
06-05-2008, 12:50 AM
Hello... I am working on the following script:

//part_01
int $xmax = 20;
int $ymax = 20;

//create the lattice of cells
for($x=0; $x<$xmax; $x++){
for($y=0; $y<$ymax; $y++){
polyPlane -ax 0 0 1 -w 1 -h 1 -sx 1 -sy 1 -name ("MyPlane" + $x + "x" +$y);
move $x $y 0;
}
}

//scale some of them randomly
for($x=0; $x<$xmax; $x++){
for($y=0; $y<$ymax; $y++){
$name = ("MyPlane"+$x+"x"+$y);
select -cl ;
select -r $name ;
if(rand(2) >1){
scale 0 0 0;

}
}
}

//part_02
//create a matrix that will contain the state of each cell

string $createMatrix = "matrix $cell_state[" + $ymax + "][" + $xmax + "]";
eval ($createMatrix);

//part_03
//get the state of each cell and add it to the matrix
for($x=0; $x<$xmax; $x++){
for($y=0; $y<$ymax; $y++){

//string $createMatrix = "matrix $cell_state[" + $ymax + "][" + $xmax + "]";
//eval ($createMatrix);


string $attribholder = "getAttr \"" + $name + ".scaleX\"";


$cell_state [$y][$x] = `eval ($attribholder)`;



}
}
When I try to run the whole script, I am getting the error: "$cell_state" is an undeclared variable. // However, when I run the three parts (part_1, part_2, part_3) seperately it is working fine! Any idea about what I am doing wrong? I tried to make $cell_state global with no luck and I tried to place the declaration of the matrix inside the for loop, no luck either....

Thanks!

dbsmith
06-06-2008, 04:57 AM
Yes, in common-speak, you're trying to create a matrix and give it a dynamic run-time dependent size, and then access it later on. You're also creating this matrix by putting it in a string and evaluating that string, so maya doesn't really know that the matrix exists, if that makes sense. I'm not sure why you're using eval this way.
You're basically saying "Heres a command in this string! Run the command in this string!"
where you could just run the command normally without the string or eval.


You could (probably - sorry no time to test this) solve these problems by simply declaring your matrix and its size way at the top, eg

matrix $cell_state[$ymax][$xmax];
or
matrix $cell_state[20][20];

NaughtyNathan
06-12-2008, 02:52 PM
matricies cannot be dynamically sized in MEL. Although you can "fool" it into doing this using eval and variables like you've attempted, they'd have to be global variables and this isn't recommended.

see this post for more info:

http://www.highend3d.com/boards/index.php?showtopic=244619

:nathaN

rafoarc
06-18-2008, 12:20 AM
Thank you both for your answers guys. Dbsmith, as nathaN said, matrices can not be be dynamically sized in MEL, that is the following does not work:



int $xmax = 20;

int $ymax = 20;

matrix $cell_state[$ymax][$xmax];



That is why I used the eval command, trying to get around it… but it works only partially… I guess making the variables global would be an option. I think the idea of defining a very large matrix and using just a part of it (as nathaN’s post on highend suggests) is much better. Or maybe using separate procedures and a UI (with one button defining the variables and another creating the matrix) will work… Anyway, I stoped working on that script for a while, so I am not sure yet…

CGTalk Moderation
06-18-2008, 12:20 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.