3

I have a simple script which performs Boolean union on multiple meshes. I can manually trace modifier errors in the console and exclude meshes which leads to union artifacts. Here's the script:

import bpy
wm = bpy.context.window_manager
act_obj = bpy.context.active_object
union_list = []
for sel_obj in bpy.context.selected_objects:
    if sel_obj.type == 'MESH':
        if sel_obj != act_obj:
            bpy.ops.object.modifier_add(type="BOOLEAN")
            union_list.append(sel_obj)

k=0
union_list_len = len(union_list)
wm.progress_begin(0, union_list_len)

for mod in act_obj.modifiers:
    if mod.type == "BOOLEAN":
        print(k,union_list_len,union_list[k].name)
        wm.progress_update(k)
        mod.operation = "UNION"
        mod.object = union_list[k]
        bpy.ops.object.modifier_apply(modifier=mod.name)
        k=k+1
act_obj.select = False
bpy.ops.object.delete()
wm.progress_end()

Is there a way to catch Boolean modifier error in python and skip meshes which produce those errors?

someonewithpc
  • 12,381
  • 6
  • 55
  • 89

1 Answers1

1

Well the easy way would be to use a try-except catch

This would skip the meshes where errors happen print their names and reselect them after deleting the rest:

import bpy
wm = bpy.context.window_manager
act_obj = bpy.context.active_object
union_list = []
passed = []
for sel_obj in bpy.context.selected_objects:
    if sel_obj.type == 'MESH':
        if sel_obj != act_obj:
            try:
                bpy.ops.object.modifier_add(type="BOOLEAN")
                union_list.append(sel_obj)
            except:
                print("\n"+sel_obj.name)
                passed.append(sel_obj)
                sel_obj.select = False

k=0
union_list_len = len(union_list)
wm.progress_begin(0, union_list_len)

for mod in act_obj.modifiers:
    if mod.type == "BOOLEAN":
        print(k,union_list_len,union_list[k].name)
        wm.progress_update(k)
        mod.operation = "UNION"
        mod.object = union_list[k]
        try:
            bpy.ops.object.modifier_apply(modifier=mod.name)
        except:
            print("\n"+sel_obj.name)
            passed.append(sel_obj)
            sel_obj.select = False
        k=k+1
act_obj.select = False
bpy.ops.object.delete()
wm.progress_end()
for obj in passed: obj.select = True

hope it helps, Teck-Freak

Teck-freak
  • 496
  • 3
  • 11