[2.8] Boolean modifier issue

Hello,

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.

My blend file:
Boolean_Problem.blend (656.2 KB)

Hi,
to remove an item you can use bpy.data.remove(obj, do_unlink = True).

The suggested line of code returns the following error:

Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
AttributeError: 'BlendData' object has no attribute 'remove'

According to documentation it should be bpy.data.objects.remove(obj, do_unlink = True), but I still get no results.

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.data.objects.remove(bpy.data.objects["Cube"], do_unlink = True)

Oops, sorry for the mistake.

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(…)

If you open the initial .blend file, you’ll see that cutter’s name is “Cube”.


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?


See what I obtain from your code. It works like you wrote it.
Maybe you should reset your scene because I’m unable to reproduce your problem.

Have you tried running my code in my scene (see my .blend file ib the first post)?

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)

1 Like

Rotating is not an option, because I don’t know, which planes should be rotated and which shouldn’t. I reported it as a bug.

Thanks for your help!