Aligned faces pyramid worm
February 6th, 2009More random walkies. In this one, the faces are aligned, but the corners are not.
Turns out there’s no shortcut to find the center of a face via MEL. The interface does it all the time — there’s a dot in the middle of each face, and lots of snap tools, so I know it’s buried in there somewhere… but I couldn’t find it. So I took a procedure from Seth Hall and cleaned it up.
MEL code follows:
//find the center of a face
global proc vector centerOfFace(string $face) {
// get locations of all vertices in an array
float $pos[] = `xform -q -ws -t ($face)`;
//get length of the array: # vertices * 3
int $posSize = size($pos);
//put the average of all vectors into 1st vector
for ($i = 0; $i < 3; $i++){
for ($j = $i + 3; $j < $posSize; $j += 3){
$pos[$i] += $pos[$j];
}
$pos[$i] /= ($posSize / 3);
}
vector $averagePos = <<$pos[0], $pos[1], $pos[2]>>;
return $averagePos;
}
proc doit() {
//make a 3-sided cone and select it
select `polyCone -r 1 -h 1.414214 -sx 3 -n "pCone1"`;
//move its pivot point to the bottom face
xform -rp 0 -0.707107 0;
//move to origin and freeze xforms
xform -t 0 0.707107 0;
makeIdentity -apply true -t 1;
//turn off constraint warnings
cycleCheck -e off;
for ($i=0; $i<500; ++$i) {
//select cone
$cone = `ls -sl`;
//select a random face
int $r = rand(4);
$sel = ( $cone[0] + ".f[" + $r + "]" );
select $sel;
//find center of face
vector $fc = centerOfFace($sel);
//instance cone
select `instance $cone`;
$new = `ls -sl`;
//move instance to face
xform -a -t ($fc.x) ($fc.y) ($fc.z) $new;
//align instance to face with a temporary normal constraint
string $tmpConst[] = `normalConstraint -aim 0 1 0 $sel $new`;
delete $tmpConst[0];
//animate visibility
setKeyframe -attribute "visibility" -v 0 -t 0 $new;
setKeyframe -attribute "visibility" -v 1 -t ($i+1) $new;
};
cycleCheck -e on;
}
doit();
« previously: Random walk pyramids | Home | next: Sloppy pyramid worm »