Maya: Exploding a mesh

I would like to create a function that explodes a mesh but am not entirely sure how to approach this.

The first part would mean that I split the mesh into sub-meshes. I have some function already for selecting sub-meshes of a mesh so this isn’t even a problem because I can just work from that.
But once I have X amount of different meshes, I need to “explode” them so that neither mesh intersects with another. There should also be some additional padding around them so that one mesh doesn’t end up right next to another with 0 space inbetween.

I guess moving the meshes can just be done by generating random numbers and use them for the XYZ translation.
But how can I check if the meshes are intersecting or not without making the script so slow it’s unusable? I’m also wondering how to check the distance between two meshes without having to iterate through every single face.
I looked at the PyMEL mesh-class but couldn’t find any function for checking if a mesh intersects another or how to check it’s distance to another mesh.

So ignoring the randomization, my current idea is to split up the mesh list into 3 different lists (one for each axis XYZ), then iterate through each list and move the meshes along one axis only - calculating the bounding box volume of the selection and adding a padding value so that the meshes get space between them. It would look something like this:
Select all meshes in listX > Calculate bounding box X width > Add padding to that value > Move the mesh-package said value > Deselect one mesh > Repeat

Or is there any better way of doing this?

The way things explode all depends on how you want the mesh to look after the process (ie. linear along a single axis or a random point from objects center).

In any case though you could use the BoundingBox data type in Maya to do most of your heavy lifting :):. It has methods for determining intersections with either a point or another boundingbox and contains information that you can use to determine distance from one object to another.


sphere = pm.polySphere()
sphere_boundingbox = sphere.getBoundingBox() # Result dt.BoundingBox([xmin, ymin, zmin], [xmax, ymax, zmax])

cube = pm.polyCube()
cube_boundingbox = cube.getBoundingBox()

# See if the cube intersects with sphere
sphere_boundingbox.intersects(cube_boundingbox) # Return True

# Get boundingbox center
center = sphere_boundingbox.center() # Return dt.Point([x, y, z])

Yea I think I’ll go with that. Thanks!

This sounds like a job for an octree: you can use that both to accelerate the search for possible problems and find out how closely you can pack objects:

http://code.activestate.com/recipes/498121-python-octree-implementation/