3
$\begingroup$

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?

$\endgroup$
1
$\begingroup$

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

$\endgroup$
  • $\begingroup$ you have added the try in the wrong place it should be around modifier_apply() $\endgroup$ – Chebhou May 28 '15 at 18:36
  • $\begingroup$ thx Chebhou. I'll fix it $\endgroup$ – Teck-freak May 28 '15 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.