import rhinoscriptsyntax as rs
def ExportSTL():
filepathname=rs.DocumentPath()
if not filepathname:
# doc has not been saved
filepathname=rs.SaveFileName("Save STL","STL Files |*.stl||")
if not filepathname: return
filepathname=filepathname[:-4] #remove the extension
arrLayers=rs.LayerNames()
for layer in arrLayers:
rs.UnselectAllObjects()
#selects objects if present
objs=rs.ObjectsByLayer(layer, True)
if objs:
#path needs to be enclosed in quotes in case there are
#spaces in either the file name or the layer name
fullpath='"'+filepathname+"-"+layer+".stl"+'"'
rs.Command("_-Export "+fullpath+" _Enter _Enter")
ExportSTL()