Skip to content

Instantly share code, notes, and snippets.

@corpix
Last active April 6, 2026 08:07
Show Gist options
  • Select an option

  • Save corpix/95eef0cd3ddb712ec5e7787e16fa6ca6 to your computer and use it in GitHub Desktop.

Select an option

Save corpix/95eef0cd3ddb712ec5e7787e16fa6ca6 to your computer and use it in GitHub Desktop.
freecad macro to export selected objects into stl and open in prusa slicer. MOVED into it's own repo https://github.com/corpix/freecad-slicer
import FreeCAD, FreeCADGui, Mesh, subprocess, os, tempfile, shutil, threading
# Setup for custom slicer:
## Open python console
## Run FreeCAD.ParamGet("User parameter:BaseApp/Preferences/SlicerMacro").SetString("SlicerCommand", "custom-slicer")
## Now you should be able to get 'custom-slicer' with FreeCAD.ParamGet("User parameter:BaseApp/Preferences/SlicerMacro").GetString("SlicerCommand", "")
slicers = ["prusa-slicer", "orca-slicer"]
def get_slicer_path():
param = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/SlicerMacro")
slicer_command = param.GetString("SlicerCommand", "")
for command in [slicer_command] + slicers if slicer_command else slicers:
slicer_path = shutil.which(command)
if slicer_path:
return slicer_path
FreeCAD.Console.PrintError("No valid slicer found. Please set SlicerCommand parameter (see comments in macro file).\n")
return None
def run_slicer(filepaths, temp_dir, slicer_path):
try:
subprocess.run([slicer_path] + filepaths)
except Exception as e:
FreeCAD.Console.PrintError(f"Failed to run slicer {slicer_path}: {e}\n")
finally:
try:
shutil.rmtree(temp_dir)
except OSError:
pass
def main():
slicer_path = get_slicer_path()
if not slicer_path:
return
selection = FreeCADGui.Selection.getSelection()
if not selection:
FreeCAD.Console.PrintError("No objects selected.\n")
return
temp_dir = tempfile.mkdtemp()
project_name = FreeCAD.ActiveDocument.Label.replace(" ", "-")
stl_files = []
for obj in selection:
body = None
for parent in obj.InList:
if parent.TypeId == "PartDesign::Body":
body = parent
break
part_name = (body.Label if body else obj.Label).replace(" ", "-")
filename = f"{project_name}_{part_name}.stl"
path = os.path.join(temp_dir, filename)
Mesh.export([obj], path)
stl_files.append(path)
threading.Thread(target=run_slicer, args=(stl_files, temp_dir, slicer_path), daemon=True).start()
main()
@corpix
Copy link
Copy Markdown
Author

corpix commented Apr 6, 2026

moved into it's own repo https://github.com/corpix/freecad-slicer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment