Export multiple groups as stl files

Hi. Is there any scrip for exporting whole groups as stl?
I am working in 3D printing domain and we usually have buildings to 3D print floor by floor.
When i design the part it’s easier to make separate layers and objects for different elements,
I group them and then i export each one as a stl. This way Prusa Slicer sees the group as a single solid.
I cound’t find a script that does this, that exports GROUPS as separate STL files. And the name of the file to be the name of the group, cuz i always name them.

Hi Mahneca,

The below script is simple version that might work for you.

Let me know if you have any questions

EDIT: updated the script to allow folders with spaces and export only selected groups
EDIT: updated the script to allow for setting stl options

export_groups.py (1.6 KB)

import rhinoscriptsyntax as rs
import os


def export_group(folder,group):
    
    rs.UnselectAllObjects()
    objects = rs.ObjectsByGroup(group)
    if not objects:
        return 
    
    rs.SelectObjects(objects)
    
    stl_file = os.path.join(folder,group+'.stl')
    
    comm_string = """
                  _-Export "{}" 
                  ExportFileAs=ASCII ExportUnfinishedObjects=Yes 
                  UseSimpleDialog=No Enter DetailedOptions 
                  JaggedSeams=No PackTextures=Yes Refine=Yes SimplePlane=Yes 
                  AdvancedOptions 
                  Angle=20 
                  AspectRatio=6 
                  Distance=0.001 
                  Density=0 
                  Grid=0 
                  MaxEdgeLength=0 
                  MinEdgeLength=0.0001 
                  SubdivisionLevel=4 
                  SubdivisionContext=Absolute 
                  EnterEnd 
                  """
    rs.Command(comm_string.format(stl_file))
    
    rs.UnselectAllObjects()


def export_groups():
    
    folder = rs.BrowseForFolder('folder for export')
    if not folder:
        return
    
    objects = rs.GetObjects('select groups to export')
    if not objects:
        return
    
    group_names = set()
    for obj_id in objects:
        group_names.update(rs.ObjectGroups(obj_id))
      
    rs.EnableRedraw(False)
    for group_name in rs.GroupNames():
        if group_name in group_names:
            export_group(folder,group_name)
        
    rs.EnableRedraw(True)

if __name__ == '__main__':
     export_groups()
1 Like

It works 90 %. It let’s me chose the location of the export but it only work when i chose to export in the folder where the script is saved, for some reason…

hmmm… It does work for another folder here.

Could it be you have no write permission on the selected folder.
Also what messages appear in the commandline, there the export of the stl’s is echoed including possible issues.

-Willem

This is the text in command bar.

Command: RunPythonScript
Command: _-Export
Save file name ( Version=7 SaveSmall=No GeometryOnly=No SaveTextures=Yes SaveNotes=No SavePlugInData=Yes Browse ): X:\Documents\13\I
File successfully saved as Rhino 7 file X:\Documents\13\I.3dm.
Command: Residence\Print
Unknown command: Residence\Print
Command: _-Export
Save file name ( Version=7 SaveSmall=No GeometryOnly=No SaveTextures=Yes SaveNotes=No SavePlugInData=Yes Browse ): X:\Documents\13\I
File successfully saved as Rhino 7 file X:\Documents\13\I.3dm.
Command: Residence\Print
Unknown command: Residence\Print
Command: _-Export
Save file name ( Version=7 SaveSmall=No GeometryOnly=No SaveTextures=Yes SaveNotes=No SavePlugInData=Yes Browse ): X:\Documents\13\I
File successfully saved as Rhino 7 file X:\Documents\13\I.3dm.
Command: Residence\Print
Unknown command: Residence\Print

For some reason it’s making a save in a Parent Folder of containing only one of the groups (I.3dm.) . But there is no stl anywhere. If i let the destination file to be the one in witch i have the scrip, it works :

Command: _-Export
Save file name ( Version=7 SaveSmall=No GeometryOnly=No SaveTextures=Yes SaveNotes=No SavePlugInData=Yes Browse ): X:\Shit\Rhino\1.stl
Choose STL option ( ExportFileAs=Binary ExportUnfinishedObjects=Yes UseSimpleDialog=No ): _Enter
Choose STL option ( ExportFileAs=Binary ExportUnfinishedObjects=Yes UseSimpleDialog=No )
Choose meshing option ( DetailedOptions PolygonDensity=50 ): DetailedOptions
Choose detailed option ( AdvancedOptions JaggedSeams=No PackTextures=Yes Refine=Yes SimplePlane=No ): PackTextures=No
Choose detailed option ( AdvancedOptions JaggedSeams=No PackTextures=No Refine=Yes SimplePlane=No )
Created a mesh with 88 points and 38 polygons.
Created a mesh with 104 points and 84 polygons.
Created a mesh with 204 points and 172 polygons.
Created a mesh with 95 points and 76 polygons.
Created a mesh with 200 points and 141 polygons.
Created a mesh with 200 points and 142 polygons.
Created a mesh with 115 points and 100 polygons.
Created a mesh with 106 points and 84 polygons.
Created a mesh with 124 points and 89 polygons.
Created a mesh with 1049 points and 1200 polygons.
Created a mesh with 152 points and 122 polygons.
Created a mesh with 200 points and 143 polygons.
Created a mesh with 110 points and 91 polygons.
Meshing… Press Esc to cancel
File successfully saved as X:\Shit\Rhino\1.stl.

Another problem is that it takes all the groups in the file. And i usually have more de 100 , and only 20 i want to export. This …examination and exporting of every group is crashing the program.

If only i could export only the selected ones or something.

Hi Mahneca,

There was a bug that did not allow folders with spaces in it.
I also added the selection of groups to export to allow for only a subset of groups to be exported
That is fixed in the edited post above:

Does this work better?
-Willen

Works like a charm now. It’s exacly what i needed.Thank you!!

1 Like

Hi,
A new small problem emerged. When exporting, it’s using a low detail level, not the default i have set. Because i print small parts, i always use a 0.001 tolerance, but the script is using a bigger one. Is there a way so change that?
Thanks.

Hi,

I see

I updated the script in the post above to set all stl export options explicitly

Check them and adjust accordingly
NOTE that each setting needs just 1 trailing space!
eg Grid=0 and not Grid=0 subtle to see but ater the first 0 there is an extra space

1 Like