I try to use Boolean modifier in a loop to apply a cut for a range of objects. Here’s my code:
planes_to_cut = []
for obj in bpy.data.objects:
if "Cut_Me_" in obj.name:
planes_to_cut.append(obj)
for plane in planes_to_cut:
plane.select_set(True)
bpy.context.view_layer.objects.active = plane
print("Now cutting " + plane.name)
boolean_mod = plane.modifiers.new(type="BOOLEAN", name="Boolean")
boolean_mod.object = bpy.data.objects["Cube"]
boolean_mod.operation = 'DIFFERENCE'
bpy.ops.object.modifier_apply(apply_as='DATA', modifier=boolean_mod.name)
bpy.ops.object.select_all(action='DESELECT')
bpy.data.objects["Cube"].select_set(True)
bpy.context.view_layer.objects.active = bpy.data.objects["Cube"]
bpy.ops.object.delete(use_global=True)
Unfortunately, the script doesn’t remove my targer object (Cube) after the the end of the loop. It disappears from Outliner but I keep seeing in in a viewport as a part of modified meshes.
In your example, the cube’s name is “Cut_Me_2” and in the script only “Cube” is delete.
I try your code and as long as you have a cube name “Cube”, it works.
By the way, you don’t need to select the object you want to remove with the code bpy.data.objects.remove(…)
The problem is that “Cube” somehow gets merged with cutted meshes when the script ends, so I can’t remove it. Maybe my code produces virtual copies of this “Cube” for each cutted mesh or something like that…
Couls you possibly post a modified version of my code that works for you?
You have to rotate Cut_Me_2 and Cut_Me_3 to 180° on the z axis to fix the problem. (maybe a bug, I don’t have play that much with booleans).
But probably because your meshes is a mess, some artefacs remains. so I create the same objects to show you.
Here your scene : Boolean_Problem.blend (702.6 KB)