# Python script to export STLs

**URL:** <https://discourse.mcneel.com/t/python-script-to-export-stls/75447>\
**Category:** Scripting\
**Created:** [November 27, 2018, 6:00pm UTC](https://discourse.mcneel.com/t/python-script-to-export-stls/75447 "2018-11-27T18:00:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![declan](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/declan/32/481847_2.png) [@declan](https://discourse.mcneel.com/u/declan)\
**Post date:** [November 27, 2018, 6:00pm UTC](https://discourse.mcneel.com/t/python-script-to-export-stls/75447/1 "2018-11-27T18:00:49Z")

</div>

Hi all,

Could someone help me tidy up this script to export a group of objects as individual STL files?

```auto
import rhinoscriptsyntax as rs

objects = rs.GetObjects("Select objects to export", 16)

for x in objects:
    name = rs.ObjectName(x)
    stlName = ((rs.DocumentName()).split('.')[0]) + "_" + name + ".stl"
    commandString = "-_Export " name "_Enter" + stlName + " _Enter " + "_Enter"
    rs.Command(commandString)

```

Thanks!!

---

<div class="post-metadata">

**Author:** ![Willem](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/willem/32/554347_2.png) [@Willem](https://discourse.mcneel.com/u/Willem)\
**Post date:** [November 27, 2018, 11:14pm UTC](https://discourse.mcneel.com/t/python-script-to-export-stls/75447/2 "2018-11-27T23:14:29Z")

</div>

Hi

This is a quick setup that seems to work:

```python
import rhinoscriptsyntax as rs
import os

objects = rs.GetObjects("Select objects to export", 16)

for obj in objects:
    name = rs.ObjectName(obj)
    stl_suffix = '_{}.stl'.format(name)
    stl_basename = rs.DocumentName().split('.')[0]
    
    stl_fullname = '{}{}'.format(stl_basename,stl_suffix)

    stl_path = rs.DocumentPath()
    
    full_filepath = os.path.join(stl_path,stl_fullname)
    
    rs.SelectObject(obj)
    command_string = '-_Export "{}" _Enter _Enter'.format(full_filepath)
    rs.Command(command_string)

```

---

<div class="post-metadata">

**Author:** ![declan](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/declan/32/481847_2.png) [@declan](https://discourse.mcneel.com/u/declan)\
**Post date:** [November 28, 2018, 1:58pm UTC](https://discourse.mcneel.com/t/python-script-to-export-stls/75447/3 "2018-11-28T13:58:17Z")

</div>

Thanks Willem! I just had to tweak “full\_filepath = …” to be non-os and it works great.

thanks again.

---

<div class="post-metadata">

**Author:** ![Helvetosaur](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/helvetosaur/32/635974_2.png) [@Helvetosaur](https://discourse.mcneel.com/u/Helvetosaur)\
**Post date:** [November 28, 2018, 2:10pm UTC](https://discourse.mcneel.com/t/python-script-to-export-stls/75447/4 "2018-11-28T14:10:42Z")

</div>

Kinda late to the party here but here are a couple of scripts from my library just in case…

Fixed STL mesh settings (adjust in the script as you want), files are named with original file name plus a number (no object names)  
[BatchExportSTLByObjWFixedSettingsPlus.py](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/c/8/c8c0bc16ee2c7f9fe83c35249a5a218a24c923c5.py) (1.9 KB)

Choice of 4 settings (coarse, medium, fine and extra-fine), numbered as above.  
[BatchExportSTLByObjWSett.py](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/a/0/a09dc547a3762de353a7d891ccaa63e827a90e32.py) (3.9 KB)
