Skip to content

Commit

Permalink
Merge pull request #2145 from Erkhembayaar/main
Browse files Browse the repository at this point in the history
Adding solver_options and self_intersection to bool.intersection
  • Loading branch information
mikedh authored Feb 14, 2024
2 parents 83d8d1a + 082e075 commit 4f14bd7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
9 changes: 8 additions & 1 deletion trimesh/boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def union(meshes, engine=None, **kwargs):
return result


def intersection(meshes, engine=None, **kwargs):
def intersection(meshes, engine=None, solver_options=False, use_self=False, **kwargs):
"""
Compute the boolean intersection between a mesh an n other meshes.
Expand All @@ -67,12 +67,19 @@ def intersection(meshes, engine=None, **kwargs):
Meshes to be processed
engine : str
Which backend to use, i.e. 'blender' or 'manifold'
solver_options: str
Fast has some limitations
Exact is slow but handles most of the cases
use_self: Bool
Self Intersection, Do self-union or self-intersection
Returns
----------
intersection : **kwargs for a Trimesh object of the
volume that is contained by all meshes
"""
kwargs.setdefault('solver_options', solver_options)
kwargs.setdefault('use_self', use_self)
result = _engines[engine](meshes, operation="intersection", **kwargs)
return result

Expand Down
18 changes: 16 additions & 2 deletions trimesh/interfaces/blender.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,33 @@
exists = _blender_executable is not None


def boolean(meshes, operation="difference", debug=False):
def boolean(meshes, operation="difference", solver_options=False, use_self=False, debug=False):
"""
Run a boolean operation with multiple meshes using Blender.
Run a boolean operation with multiple meshes using Blenderoy.
Parameters:
- meshes: List of mesh file paths to be processed.
- operation: Type of boolean operation ("difference", "union", "intersect").
- solver_options: Solver option for the boolean operation, True == 'Exact'
- use_self: Boolean indicating whether to consider self-intersections.
- debug: If True, run in debug mode to provide additional output for troubleshooting.
Returns:
- The result of the boolean operation on the provided meshes.
"""
if not exists:
raise ValueError("No blender available!")
operation = str.upper(operation)
if operation == "INTERSECTION":
operation = "INTERSECT"

if solver_options is True:
solver_options = 'EXACT'
else:
solver_options = 'FAST'
# get the template from our resources folder
template = resources.get("templates/blender_boolean.py.tmpl")
script = template.replace("$OPERATION", operation)
script = script.replace("$SOLVER_OPTIONS", solver_options)
script = script.replace("$USE_SELF", f'{use_self}')

with MeshScript(meshes=meshes, script=script, debug=debug) as blend:
result = blend.run(_blender_executable + " --background --python $SCRIPT")
Expand Down
2 changes: 2 additions & 0 deletions trimesh/resources/templates/blender_boolean.py.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ if __name__ == '__main__':
mod = mesh.modifiers.new('boolean', 'BOOLEAN')
mod.object = other
mod.operation = '$OPERATION'
mod.solver = '$SOLVER_OPTIONS'
mod.use_self = $USE_SELF
# used mod.name instead of hard-coded "boolean"
bpy.ops.object.modifier_apply(modifier=mod.name)

Expand Down

0 comments on commit 4f14bd7

Please sign in to comment.