Sub topic 16
Automating Booleans and Cleanup with Python
This chapter assumes you can navigate Blender, switch modes, select mesh components, and add or apply modifiers. It also assumes basic Python syntax. Blender scripts use the bpy module to control scenes and the bmesh module to edit mesh data.
Python is useful when you repeat the same boolean and cleanup steps across many objects. The script below expects an active target object and an object named Cutter.
Script the Boolean modifier
A Boolean modifier needs a target mesh, an operation, and an operand object. This example subtracts the cutter from the active object.
import bpy
target = bpy.context.active_object
cutter = bpy.data.objects["Cutter"]
if target is None or target.type != 'MESH':
raise RuntimeError("Select a mesh target object.")
if cutter.type != 'MESH':
raise RuntimeError("Cutter must be a mesh object.")
mod = target.modifiers.new(name="Boolean", type='BOOLEAN')
mod.operation = 'DIFFERENCE'
mod.operand_type = 'OBJECT'
mod.object = cutter
mod.solver = 'EXACT'
cutter.display_type = 'WIRE'
cutter.hide_render = True
print(f"Boolean added to {target.name}")The operation property can be DIFFERENCE, UNION, or INTERSECT. Use EXACT when accuracy matters, especially with overlapping or complex meshes. Check the object names before running the script. A missing name raises a KeyError.
Apply and clean the result
Applying a modifier converts its evaluated result into mesh data. Keep the modifier unapplied while you are still adjusting the cutter. Apply it only when you need permanent geometry or want to continue with direct mesh editing.
import bpy
import bmesh
target = bpy.context.active_object
if target is None or target.type != 'MESH':
raise RuntimeError("The active object must be a mesh.")
with bpy.context.temp_override(object=target, active_object=target):
bpy.ops.object.modifier_apply(modifier="Boolean")
bm = bmesh.new()
bm.from_mesh(target.data)
bmesh.ops.remove_doubles(bm, verts=bm.verts, dist=0.0001)
loose = [vertex for vertex in bm.verts if not vertex.link_edges]
if loose:
bmesh.ops.delete(bm, geom=loose, context='VERTS')
bmesh.ops.recalc_face_normals(bm, faces=bm.faces)
bad_edges = [edge for edge in bm.edges if not edge.is_manifold]
print(f"Non-manifold edges remaining: {len(bad_edges)}")
bm.to_mesh(target.data)
bm.free()
target.data.update()remove_doubles merges nearby vertices. The distance is measured in Blender units, so choose it carefully. A value that is too large can collapse intended details. The cleanup removes loose vertices and recalculates face normals. It reports non-manifold edges but does not automatically fill every hole.
| Task | Python mechanism | Main risk |
|---|---|---|
| Add a cut | Boolean modifier | Wrong object name or operation |
| Make geometry permanent | `modifier_apply` | Losing an editable modifier |
| Merge duplicates | `remove_doubles` | Excessive merge distance |
| Check topology | `edge.is_manifold` | A count does not identify the cause |
Common scripting errors
- The active object is not the intended target.
- The cutter has a different name, including different capitalization.
- The Boolean modifier has already been applied, so
modifier_applycannot find it. - Object transforms were not applied before the Boolean was created.
- Cleanup is run before the modifier is evaluated.
- A non-manifold count is treated as proof that the mesh is unusable. Some models intentionally contain open borders.
Exercise
Why should a script leave the Boolean modifier unapplied while you are still positioning the cutter?
Show answer
An unapplied modifier keeps the cutter relationship editable, so you can change its position, scale, or shape without rebuilding the cut.