Skip to content

Instantly share code, notes, and snippets.

@carlosbaraza
Forked from hyOzd/export_all_stl.py
Last active February 11, 2026 21:24
Show Gist options
  • Select an option

  • Save carlosbaraza/8c91b6dd6f1c977912fec59b3f4a0cd2 to your computer and use it in GitHub Desktop.

Select an option

Save carlosbaraza/8c91b6dd6f1c977912fec59b3f4a0cd2 to your computer and use it in GitHub Desktop.
FreeCAD macro to export all visible parts as STL. Include all groups children.
#
# Export Root Bodies to STL
#
# This is a FreeCAD script to export all visible root bodies in STL mesh format.
# Files will be named as "documentname_bodylabel.stl".
#
import FreeCAD
import os.path
doc = FreeCAD.activeDocument()
# Check if the document is saved (i.e., has a file name)
if not doc.FileName:
FreeCAD.Console.PrintError("Document has not been saved. Please save the document first.\n")
exit()
base_filename = os.path.splitext(doc.FileName)[0]
for obj in doc.Objects:
# Check if the object is a root object (not used by any other objects)
if not obj.InList:
# Check if the object has the "Shape" attribute and it's visible
if hasattr(obj, 'Shape') and obj.ViewObject.Visibility:
# Remove any non-allowed characters from the label for file naming
sanitized_label = ''.join(e for e in obj.Label if e.isalnum() or e in ['_', '-'])
filename = base_filename + "_" + sanitized_label + ".stl"
try:
obj.Shape.exportStl(filename)
FreeCAD.Console.PrintMessage(f"Exported {filename}\n")
except Exception as e:
FreeCAD.Console.PrintError(f"Error exporting {filename}: {str(e)}\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment