Create a Bounding Box for a Crowd Field

When lighting a scene, sometimes you want to avoid the lighting TD to directly deal with the crowd simulation and rather use Maya objects to locate / visualize where the Crowd Entities currently are. The Golaem Crowd Constraint Behavior provide a way to place Maya objects on Crowd Entities but that'll require to create as many Maya objects as they are Crowd Entities. In the following, we provide a sample code to use and keyframe Maya cubes as Bounding Boxes of the particles representing Crowd Entities.


Example of a Maya cube acting as a bounding box of the crowd

This whole process relies on Maya MEL functions. First you need to copy the following function in your Maya scripts and make sure it's sourced correctly. It takes the name of an object to track (in our case a particle system), the name of the cube to use as bounding box and if you want the bounding box vertices to be keyframed:

global proc updateCubeFromObjectBBox(string $cubeName, string $trackedObjectName, int $setKeyframe)
{
   // get the target bounding box
   float $bbox[] = `exactWorldBoundingBox $trackedObjectName`;
    
   // update the cube vertices position
   xform -a -ws -t $bbox[0] $bbox[1] $bbox[2] ($cubeName+".vtx[0]");
   xform -a -ws -t $bbox[0] $bbox[1] $bbox[5] ($cubeName+".vtx[1]");
   xform -a -ws -t $bbox[3] $bbox[1] $bbox[2] ($cubeName+".vtx[6]");
   xform -a -ws -t $bbox[3] $bbox[1] $bbox[5] ($cubeName+".vtx[7]");
   xform -a -ws -t $bbox[0] ($bbox[4]+1) $bbox[2] ($cubeName+".vtx[2]");
   xform -a -ws -t $bbox[0] ($bbox[4]+1) $bbox[5] ($cubeName+".vtx[3]");
   xform -a -ws -t $bbox[3] ($bbox[4]+1) $bbox[2] ($cubeName+".vtx[4]");
   xform -a -ws -t $bbox[3] ($bbox[4]+1) $bbox[5] ($cubeName+".vtx[5]");
    
   // keyframe if required
   if ($setKeyframe)
      setKeyframe -shape 0 {($cubeName+".vtx[0:7]")};
};

Next we can use that function to edit a cube vertices position according to a particle system containing our Crowd Entities. First create a Maya cube with only 1 subdivision on each axis and run the following command:

expression -s (updateCubeFromObjectBBox("pCube1", "particle1", 1)) -o "pCube1" -ae 1 -uc all;

It will create an expression attached to pCube1 which will make it deformed according to particle1 bounding box (do not forget to change the names depending on your objects :)). Set 1 as last parameter of updateCubeFromObjectBBox if you want pCube1 to be keyframed when the simulation is run. Once it is keyframed, you can run the simulation, delete the expression and export that Maya cube (which will be animated) for your lighting TDs.