I'm looking for a command line program that can perform constructive solid geometry, aka Boolean operations on meshes. I would like to use it like this:

intersect a.stl b.stl result.stl

Blender can do this operation, but not from the command line.

MeshLab can do this from the command line, but unfortunately it resamples the mesh which increases the file size and makes the mesh look blurry. This is not the case with the implementation in Blender.

  • Welcome! Command line from what operating system? – RockPaperLz- Mask it or Casket Jul 1 '18 at 12:02
  • I'm on windows. If there is a solution for Linux, I'd be interested as well. – Toast Jul 1 '18 at 12:56

Turns out, you can run Blender from the command line:

blender -b -P intersect.py -- a.stl b.stl result.stl

And that's the python script intersect.py:

import bpy
import sys

index = sys.argv.index("--")
file_a = sys.argv[index + 1]
file_b = sys.argv[index + 2]
file_out = sys.argv[index + 3]

bpy.ops.object.select_all(action = 'SELECT')

bpy.ops.import_mesh.stl(filepath = file_a)
obj_a = bpy.context.selected_objects[0]

bpy.ops.import_mesh.stl(filepath = file_b)
obj_b = bpy.context.selected_objects[0]

bpy.context.scene.objects.active = obj_a
bpy.ops.object.modifier_add(type = 'BOOLEAN')
bpy.context.object.modifiers[0].object = obj_b

bpy.context.scene.objects.active = obj_b

bpy.context.scene.objects.active = obj_a
bpy.ops.export_mesh.stl(filepath = file_out)

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.