Here you go Chris, slightly modified code adding in a random number generator and applying it to the polyCube command to set the scale. Commented so people can follow it
Code:
int $i;
int $j;
// holds random value assigned each time a cube is created
float $randSize;
for ($i=0; $i<5; $i++) {
for ($j=0; $j<5; $j++) {
// generate a random float number between 0.0 and 2.0
$randSize = rand(2);
// use the random number to set the cube dimensions
polyCube -w $randSize -h $randSize -d $randSize;
move -r (2*$j)0(2*$i);
}
}
The only thing to remember though is that $i, $j and $randSize will be global variables which can be bad. The next step would be to change it into a function so that the variables are local.
Code:
// function that creates a 5x5 grid of randomly sized cubes
proc generateVariedCubes() {
int $i;
int $j;
// holds random value assigned each time a cube is created
float $randSize;
for ($i=0;$i<5;$i++){
for ($j=0;$j<5;$j++){
// generate a random float number between 0.0 and 2.0
$randSize = rand(2);
// use the random number to set the cube dimensions
polyCube -w $randSize -h $randSize -d $randSize;
move -r (2*$j)0(2*$i);
}
}
}
// call the function
generateVariedCubes();
Once you've run the function code then you wont need to rerun it for the current maya session. You can therefore run the generateVariedCubes() call whenever you want or multiple times and it will create the cubes.
As an exercise it'd be good to modify the function so that it generates another set of cubes to the right or left of the previous one. Up to you though
Simon