# Blender API Python split body by loose parts

I'm trying to make a python script that splits alot of objects into its loose parts and i actually have gotten this to work but i encountered a bug that i don't fully understand and thus come to you to help me out!

I have the following code to manipulate each object that i need to have manipulated (and split):

for collection in bpy.data.collections:
if collection.name == "Render":
for object in collection.objects:
print("Manipulating object: {}".format(object.name))

# place at correct location and scale to the setting size
object.scale = scale

thickness = list(object.dimensions)[2]
location = [0 ,0, thickness/1000]
object.location = location
object.rotation_euler = rotation

# split the mesh
if object.type == 'MESH':
print("Splitting mesh")
bpy.ops.mesh.separate(type='LOOSE')
break # no need to check other collections


What happens is that all the objects that it iterates through are placed at the correct location and with the correct size and everything but only the last item in the list

collections.objects


is split into its loose parts but all the other objects are not.

Heres an image of the console:

Note that 5AD48 part shows up multiple times because it has been split.

My first thought was that its caused because once the body is split the collection is not the same anymore, so i tried copying the list but that just made it more buggy.

Anyone have a clue what causes this bug? Any insight will help!

Thanks in advance!

• Are you sure there are lose parts in the other objects? Try doing separate by lose parts manually. – Viplav Prakash May 6 '20 at 23:44
• @ViplavPrakash Yep, even if i make them all a copy of eachother it still only splits the last objects. My next thought is that blender actually selects the last object in the list, sees the split and just splits that object constantly. so maybe there is a way to select objects ? – cooldanietje May 6 '20 at 23:48

## 1 Answer

It seems like blender selected the last object in that list (the reason for that is likely because these meshes are imported in a piece of code infront of the code i posted) and i never actually selected the other meshes.

This code works for me and splits all bodies:

for collection in bpy.data.collections:
if collection.name == "Render":
for object in collection.objects:
print("Manipulating object: {}".format(object.name))

object.select_set(True)
# place at correct location and scale to the setting size
object.scale = scale

thickness = list(object.dimensions)[2]
location = [0 ,0, thickness/1000]
object.location = location
object.rotation_euler = rotation

# split the mesh
if object.type == 'MESH':
print("Splitting mesh")
bpy.ops.mesh.separate(type='LOOSE')
object.select_set(False)
break # no need to check other collections