PDA

View Full Version : select random object in selection



bendingiscool
12-15-2008, 08:03 AM
Hey, ok this should be dead simple, but I can't work out the syntax.

Basically I want to be able to be able to select a bunch of geometry, cubes, cylinders, etc, then go through a loop randomly instancing one of them each time through.

So far I have...

string $sel[] = `ls -sl`;

for ($i=0; $i<10; $i++)
{
select $sel[1];
instance;
}

now this is just going to select a specified object from the selection, what I want is to be able to select a random object of the selection each time it loops through.

Many thanks, Chris

Haruyuki
12-15-2008, 09:06 AM
Try this:

string $sel[] = `ls -sl`; // define selection.
int $sizeSel = size($sel); // define size of selection.

for ($i=0; $i<10; $i++) // for loop, change 10 to number of loop.
{
int $randomSel = rand ($sizeSel - 0.01); // randomize selection.
select $sel[$randomSel]; // select object based on randomized index number.
instance;
}

Haruyuki
12-15-2008, 10:03 AM
Instead of using `-sl`, you can use `-type` to list all objects of the specified type.
See maya documentation for `ls` command, there are some other flag that can be useful.