.comment-link {margin-left:.6em;}

melGeek

Wednesday, April 06, 2005

George's Pizza Shop

Our buddy George gave me an idea for demoing the next two topics I wanted to bring up. For loops and the Random Function. First run this...


{
curve -name pizzaCurve -d 3 -p 0 0.0310559 0 -p -4.875776 0.0621118 0 -p -10.093168 0.0621118 0 -p -14.813665 0 0 -p -19.906832 0.0621118 0 -p -20.900621 1.987578 0 -p -19.968944 4.875776 0 -p -18.074534 3.136646 0 -p -17.919255 2.018634 0 -p -14.875776 2.049689 0 -p -10 2 0 -p -5 2 0 -p 0 2 0 -k 0 -k 0 -k 0 -k 1 -k 2 -k 3 -k 4 -k 5 -k 6 -k 7 -k 8 -k 9 -k 10 -k 10 -k 10 ;
revolve -n pizzaGEO -ch 1 -po 0 -rn 0 -ssw 0 -esw 360 -ut 0 -tol 0.01 -degree 3 -s 8 -ulp 1 -ax 0 1 0 "pizzaCurve";
DeleteHistory pizzGEO;
delete pizzaCurve;

polyCylinder -name pepperoni_geo -r 1 -h 0.25 -sx 8 -sy 1 -sz 1 -ax 0 1 0 -tx 1 -ch 1;
setAttr pepperoni_geo.ty 2.25;

duplicate -rr;
for ($i=1; $i<50; ++$i)
duplicate -rr -st;
;
}


There isnt anything special about the above code, I just drew a curve and revolved it, made a cylander and duplicated it 49 times. I did this with echo all commands on and used the important bits from the script editor.

First lets get random.

There is a way to setAttr for an object randomly. Its called the rand command, here is an example. Make a poly cube and then run this...


{
int $random = `rand -10 10`;
setAttr "pCube1.translateX" $random;
}


The rand -10 10 allows you to set the attribute to any value between those two. Pretty sneaky sis.

Anyway lets get to the for loop. Now if you look at the above code theres a nasty little bit of for loopery happening here for ($i=1; $i<50; ++$i) .

We address what that means later. There is a much easier way to do a for loop. Maya has a for in loop. We are going to use that to move about all 50 peperonis randomly. Heres how it goes.


{
select -r "pepperoni_*";
string $peps[] = `ls -sl -exactType transform`;

for ($each in $peps)
{
float $randomX = `rand -15 15`;
float $randomZ = `rand -15 15`;

setAttr ($each + ".tx") $randomX;
setAttr ($each + ".tz") $randomZ;
}
}


So the for in loop is pretty sassy, its saying in english "For each one of you in ths array... do something". You can declare the $each variable, although you do not need to.

5 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home