Boolean Operations on Meshes

Contents
[ ]

Boolean Operations on Meshes

Aspose.3D for Python via .NET lets you apply boolean operations on meshes through the BooleanOperator class. The supported operation kinds are defined by the BooleanOperation enum:

  • ADD — union of two meshes
  • INTERSECT — the shared volume of two meshes
  • SUB — the first mesh minus the second mesh

Each operand of a boolean operation can be an instance of Node, any entity that implements IMeshConvertible (like Mesh), or a HalfSpace. For two plain meshes the Mesh.do_boolean() static method applies the operation directly, accepting a transformation matrix for each operand.

Union of Two Overlapping Meshes

This code example calculates the union of two overlapping box meshes:

from aspose.threed.entities import Box, Mesh, BooleanOperation
from aspose.threed.utilities import Matrix4

# Create two overlapping box meshes
a = Box(1.0, 1.0, 1.0).to_mesh()
b = Box(1.0, 1.0, 1.0).to_mesh()
# Translate the second box by (0.25, 0, 0) so the two boxes overlap
transform = Matrix4(
    1.0, 0.0, 0.0, 0.0,
    0.0, 1.0, 0.0, 0.0,
    0.0, 0.0, 1.0, 0.0,
    0.25, 0.0, 0.0, 1.0)
# Union of the two boxes
union = Mesh.do_boolean(BooleanOperation.ADD, a, transform, b, None)
print("The union mesh has {} polygons".format(len(union.polygons)))

Cut a Mesh with a Half Space

A HalfSpace represents an infinite space divided by a plane, defined by its normal and a position on the plane. Cutting a mesh with a half space keeps the portion of the mesh on the positive side of the plane — the side the normal points to. Use the BooleanOperation.SUB operation with a HalfSpace operand:

from aspose.threed import Scene
from aspose.threed.entities import Box, BooleanOperator, BooleanOperand, BooleanOperation, HalfSpace
from aspose.threed.utilities import Vector3

# Create a scene with a box mesh
scene = Scene()
box_node = scene.root_node.create_child_node("box", Box(2.0, 2.0, 2.0).to_mesh())
# Define a half space whose plane passes through (0, 0, 0.5)
# and whose normal points to +Z
half_space = HalfSpace(Vector3(0.0, 0.0, 1.0), Vector3(0.0, 0.0, 0.5))
# Cut the box, this keeps the part of the box above z = 0.5
op = BooleanOperator()
op.operator = BooleanOperation.SUB
op.first = BooleanOperand.of(box_node)
op.second = BooleanOperand.of(half_space)
cut = op.to_mesh()
print("The cut mesh has {} polygons".format(len(cut.polygons)))