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.

Revisions

  1. carlosbaraza revised this gist Sep 14, 2023. 1 changed file with 32 additions and 16 deletions.
    48 changes: 32 additions & 16 deletions export_all_stl.py
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,13 @@
    #
    # Export Root Bodies to STL
    # Export Root Bodies and Objects Inside Root Groups 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".
    # This is a FreeCAD script to export all visible root bodies and objects inside root groups in STL mesh format.
    # Files will be stored inside an "exported_YYYY-MM-DD_HH-mm-ss" subfolder and named as "documentname_groupname_bodylabel.stl" or "documentname_bodylabel.stl".
    #

    import FreeCAD
    import os.path
    import datetime

    doc = FreeCAD.activeDocument()

    @@ -15,20 +16,35 @@
    FreeCAD.Console.PrintError("Document has not been saved. Please save the document first.\n")
    exit()

    base_filename = os.path.splitext(doc.FileName)[0]
    base_path = os.path.dirname(doc.FileName)
    base_filename = os.path.splitext(os.path.basename(doc.FileName))[0]

    # Get current date and time in the desired format
    current_datetime = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")

    # Ensure the "exported_YYYY-MM-DD_HH-mm-ss" subfolder exists
    export_folder = os.path.join(base_path, f"exported_{current_datetime}")
    if not os.path.exists(export_folder):
    os.makedirs(export_folder)

    def export_object_to_stl(obj, prefix=""):
    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 = os.path.join(export_folder, base_filename + "_" + prefix + 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")

    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")
    if obj.TypeId == 'App::DocumentObjectGroup':
    # If it's a root group, export objects inside
    for sub_obj in obj.Group:
    export_object_to_stl(sub_obj, prefix=obj.Label + "_")
    else:
    # If it's a single root body
    export_object_to_stl(obj)
  2. carlosbaraza revised this gist Sep 13, 2023. 1 changed file with 23 additions and 6 deletions.
    29 changes: 23 additions & 6 deletions export_all_stl.py
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,34 @@
    #
    # Export All STL
    # Export Root Bodies to STL
    #
    # This is a small FreeCAD script to export all visible parts in STL
    # mesh format. Files will be named as "documentname_partlabel.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:
    if obj.ViewObject.Visibility:
    filename = base_filename + "_" + obj.Label + ".stl"
    obj.Shape.exportStl(filename)
    # 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")
  3. @hyOzd hyOzd renamed this gist Apr 13, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. @hyOzd hyOzd created this gist Apr 13, 2015.
    17 changes: 17 additions & 0 deletions export_all_stl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    #
    # Export All STL
    #
    # This is a small FreeCAD script to export all visible parts in STL
    # mesh format. Files will be named as "documentname_partlabel.stl".
    #

    import FreeCAD
    import os.path

    doc = FreeCAD.activeDocument()
    base_filename = os.path.splitext(doc.FileName)[0]

    for obj in doc.Objects:
    if obj.ViewObject.Visibility:
    filename = base_filename + "_" + obj.Label + ".stl"
    obj.Shape.exportStl(filename)