4
$\begingroup$

I am looking for a scripting/code/api way to do a separate by loose parts and get a list of the resulting separated objects. There doesn't seem like there is a direct way to do this?

You can do bpy.ops.mesh.separate then loop through all objects and check for new_obj.name.startswith(old_obj_name) but that could select unwanted objects in certain scenarios. You could... make a collection, set it as the active collection, do the separate, and then get all objects in the collection? I'm not sure if that would work 100% of the time?

$\endgroup$
1
  • $\begingroup$ Or check which objects are selected? $\endgroup$ Commented Jul 9 at 23:52

1 Answer 1

5
$\begingroup$

Get created objects

import bpy

old_objects = set(bpy.data.objects)
bpy.ops.mesh.separate(type="LOOSE")

new_objects = set(bpy.data.objects).difference(old_objects)

print("The new objects created are:")
for ob in new_objects:
    print(ob.name)

Get separated objects

import bpy

old_objects = set(bpy.data.objects)
original_obj = bpy.context.object
bpy.ops.mesh.separate(type="LOOSE")

##
new_objects = set(bpy.data.objects).difference(old_objects)
new_objects.add(original_obj)
# Or:
# new_objects = {ob for ob in bpy.context.selected_objects if ob not in old_objects}
# new_objects.add(original_obj)
##

print("The separated objects are:")
for ob in new_objects:
    print(ob.name)
$\endgroup$
1
  • $\begingroup$ Nice. Also, one of the separated objects will be the original object, which won't be in the new_objects set. Can you edit your answer and add original_obj = bpy.data.objects.get('object_name') new_objects.add(original_obj) at line 7. And then change the print statement to "The separated objects are:" $\endgroup$ Commented Jul 11 at 0:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .