So I wrote the following MEL script that instances and places a set of objects at the positions of the bodies in the rigid body array so you can render or export or do whatever with them.
It does not handle any animation.
It's simply for using a rigid body array to place objects in a scene.
Hopefully it will be useful to someone else too,
enjoy:
Code: Select all
//This tool instances and places a set of ordered object(s) at the positions of the bodies in a Dynamica rigid body array
//first select the rigid body array SHAPE, then select objects that you want instanced at the positions of the bodies
//in the rigid body array
global proc string[] bakeRigidBodyArraySnapshot()
{
string $sel[] = `ls -sl`;
if (`objectType $sel[0]` != "dRigidBodyArray")
error "The Dynamica rigid body array SHAPE must be selected first.";
int $bodyCount = `getAttr ( $sel[0] + ".numBodies")`;
int $variantCount = `size $sel`;
if ($variantCount < 2)
error "Select the rigid body SHAPE first, then select geometry variants that you want instanced at the positions of the rigid bodies.";
int $i = 1; //skip the rigid body itself
for (; $i < $variantCount; $i++)
{
if (`objectType $sel[$i]` != "transform")
{
string $shapeList[] = `listRelatives -path -s $sel[$i]`;
for($shape in $shapeList)
{
if ( `nodeType $shape` != "mesh" && `nodeType $shape` != "surface")
error "Select the rigid body SHAPE first, then select geometry variants. The objects that followed were not detected to be geometry.";
}
}
}
string $newObjects[];
int $currentVariant = 1;
for ($i = 0; $i < $bodyCount; $i++)
{
string $newObj[] = `instance $sel[$currentVariant]`;
float $t[] = `getAttr ($sel[0] + ".position[" + $i + "]")`;
float $r[] = `getAttr ($sel[0] + ".rotation[" + $i + "]")`;
move -absolute $t[0] $t[1] $t[2] $newObj[0];
rotate -absolute $r[0] $r[1] $r[2] $newObj[0];
$newObjects[`size $newObjects`] = $newObj[0];
$currentVariant ++;
if ($currentVariant >= $variantCount)
$currentVariant = 1; // 0 is the rigidBodyArray
}
return $newObjects;
}
select `bakeRigidBodyArraySnapshot`;