Simulating Bin of Objects with Mel in Maya
In an advanced animation class I worked on an animated short that required a model of a bin of teddy bears. Placing the bears individually seemed like an arduous task, so I wrote a simple script to automate the process. Similar techniques could easily be applied when generating any heap of objects such as piles of trash. The result of simulation is generally more realistic looking than what is achievable by the average hand, or at the very least a good base to work from/tweak
The code here is commented clearly. Reading it should outline the basic process.
$num = 30; //number of bear duplicates to generate;
//define the reference bear to duplicate
string $ref_bear = "bear0";
//for each bear to be duplicated
for ($i = 1; $i < $num; $i++)
{
select($ref_bear);
duplicate - rr;
select - d;
//move the bear to a random spot above the bin
select("bear" + $i);
$x = rand(-10, 10);
$y = rand(18, 65);
$z = rand(-10, 10);
move $x $y $z;
//rotate the bear into a random configuration
$xrot = rand(-90, 90) + "deg";
$yrot = rand(-90, 90) + "deg";
$zrot = rand(-90, 90) + "deg";
rotate - r $xrot $yrot $zrot;
select - d;
//make the shape of the bear (just the body part, not the small eyes etc.) a rigid body
select("bear" + $i + "Shape");
rigidBody - n - active;
select - d;
}
//select all the rigid bodies to add a gravity field
$rigidBodies = `ls - type rigidBody`;
select($rigidBodies);
delete ($rigidBodies);
//user still has to add gravity manually after all the rigid bodies are selected
//animate the simualtion. Then go to solvers -> initial state -> set all dynamic
//then select all rigid bodies again, and delete them. This helps with exporting the finished thing
$rigidBodies = `ls - type rigidBody`;
select($rigidBodies);
delete ($rigidBodies);
Comments