PDA

View Full Version : randomly select children?


JoelOtron
09-29-2005, 01:43 PM
Is there a function, plugin, coffee expression or xpreso setup in existance that will randlmly select a percentage of children in a heirarchy? Something I need immediately for a project I'm wrapping up.

Thanks!

Geespot
09-29-2005, 07:02 PM
Here's a simple 9.5 script :)

// Get the active object
var op = doc->GetActiveObject();
if(!op) return;

// De-select all objects first
CallCommand(12113);

// Percentage 0.0 = 0%, 1.0 is 100%
var perc = 0.5;

// Get te first child object of active object
var child = op->GetDown();

// Start random generator
var r = new(Random);
r->Init(GeGetSysTime()->second);
var n;

// Count the children
while(child)
{
// Generate a number between 0.0 and 1.0
n = r->Get01();

// Is this number less than our percentage?
if(n < perc)
{
// Select object
child->SetBit(BIT_AOBJ);
// Update object
child->Message(MSG_UPDATE);
}

// Move onto next object
child = child->GetNext();

}

// Update document
doc->Message(MSG_UPDATE);

Just add it to the Script Manager, select a parent object and execute the script the usual method.

Per-Anders
09-29-2005, 07:13 PM
if you're using 9.5 you can whip up a little coffee expression in no time

it should be something like:


//--------------------------
//recursive select routine
Rselect(op,rnd) {
//randomly select (make active or not)
if (rnd->Get11()>0.0) op->SetBit(BIT_AOBJ);
else op->DelBit(BIT_AOBJ);

//recurse through hierarchy
var nchild=op;
for (nchild=nchild->GetDown();nchild;nchild=nchild->GetNext()) Rselect(nchild,rnd);
}

//--------------------------
//main function routine
main(doc,op) {
var child=op->GetDown();
var random_seed=123456;
var rnd=new(Random);
//init random number generator with seed
rnd->Init(random_seed);

for (child=op->GetDown();child;child=child->GetNext()) Rselect(child,rnd);

return TRUE;
}


oops, geespot beat me to it :D

anyhow, this can work as a coffee tag too in theory (though it may slow down everything to hell)

JoelOtron
09-29-2005, 08:29 PM
hehe
Thanks guys.

I figured it would be a 9.5 coffee thing.

(copy -paste, copy paste) :)

I'm in 9.1 at the moment--but once I install I see this shouldnt be a problem. And also thanks to the guys who PM'd me as well.

CGTalk Moderation
09-29-2005, 08:29 PM
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.