How can I separate a single object file into multiple objects based on closed geometry?

Let's say you have a single obj with a number of separate closed 3D geometries in it. For example's sake, imagine you have an obj called neighborhoodfirehydrants.obj that includes the model for all of the fire hydrants on a street, which themselves are closed, but as I said above, are all within the same obj file.

Now let's say you wanted to be able to process each of those fire hydrants one at a time, separately. To the user, it's very obvious to see visually that these faces can be subgrouped into separate "objects" but in the Blender world, it sees them all as one.

I'm looking for a way (script would be ideal) to iterate through a blender obj neighborhoodfirehydrants.obj and find each of the "objects" (fire hydrants) that make it up. I've been looking into using the looping tools, but that's not making sense in this context.

You could use Separate P (in Edit Mode) and select By loose parts.

The reverse operation would be Join Ctrl-J (in Object Mode) this would require all objects selected that you need to join to a single object.

In a script you could select the object by name and invoke the operator:

import bpy
object = bpy.data.objects['ObjectsName']
object.select = True
bpy.ops.mesh.separate(type='LOOSE')


After this you could set an individual origin to each separated object.

This can be accessed from the toolbar or from the menu:

Object -> Transform -> Origin to Geometry

To set the origin in a script you can select all objects and run:

bpy.ops.object.origin_set(type='GEOMETRY_ORIGIN', center='MEDIAN')

• Note that you need to be in EDIT mode for P to work correctly. In this example, you also need to have everything selected - any unselected meshes in your scene will not be evaluated by the Separate command. – Thomas Dec 13 '13 at 17:09
• Note: after this you may want to run Origin -> Origin to Geometry so each object gets its center pivot in a useful location. – ideasman42 Dec 13 '13 at 17:38