Export Layers Script


Does anyone know how to export layers as individual files via script?
I have several dozens layers that I need to export as individual obj/fbx files, for easier import/update into the rendering software.


I did this the other day, just for meshes in stl format but if this helps you get started try changing ‘.stl’ for ‘.obj’
You will have to set the default options first manually by saving a single object with the parameters you want, then it should retain these for the others exported
Sorry it’s half in French !

import rhinoscriptsyntax as rs
import os

def export_mesh_by_layer(maillages):
    export_path = rs.BrowseForFolder(rs.WorkingFolder(), 
        'Exporter vers quel dossier?',
        'Export stl',)
    mesh_dict = {}
    for mesh in maillages:
        obj_layer = rs.ObjectLayer(mesh)
        print obj_layer
        if obj_layer in mesh_dict:
            mesh_dict[obj_layer] = [mesh, ]
    for layer, meshes in mesh_dict.iteritems():
        layer_export_name = layer.replace(':', '_')
        if meshes:
            meshids = ''
            for mesh in meshes:
                meshids += "_SelId " + str(mesh) + " "
                filename = os.path.join(export_path, layer_export_name + ".stl")
            command_str = "-_Export " + meshids + " _Enter " + chr(34) + filename + chr(34) + " _Enter " + "_Enter"
    return None

if __name__ == '__main__':
    export_mesh_by_layer(rs.GetObjects(filter = 32)) 

Also this discussion may help

Here are a couple of scripts to try out - they work in Windows, but not sure on Mac.

One file per object - named after the file name and sequentially numbered.
Objects/layers need to be visible and unlocked.
BatchExportOBJByObject.py (2.2 KB)

One file per layer - objects/layers need to be visible and unlocked.
BatchExportOBJByLayer.py (2.1 KB)

Let me know how either one of them works… They use standard meshing settings for non-mesh objects, which you can modify in the strings at the top of the script.


Unfortunately none of the scripts above worked on Rhino for Mac… :frowning:

OK, did you get any error messages?

All comments turn to errors.
Command lines, starting with “import”, like, “import rhinoscriptsyntax as rs” prompt the Import file command.

Hello - to run the script, use RunPythonScript - these are not command line macros.

To use a Python script use RunPythonScript, or a macro:

_-RunPythonScript "Full path to py file inside double-quotes"


Thanks this workedm with @Helvetosaur BatchExportOBJByLayer.py

Although the path in which the files was exported did not work on the mac.
My Test file is saved at the desktop, but the exported layers output was "/Users/MyUserName"and the file name came in as “Desk-00//2D_lns_01.obj”

SOLVED IT, by replacing:
#e_file_name = “{}-{}.obj”.format(filename[:-4], layer_name)
e_file_name = “{}-{}.obj”.format(filename, layer_name)

OK, that’s interesting, this works as expected on the Windows platform, so something is different on Mac with how filenames/paths are set up (not surprising). I’ll need to drag my Mac out and do some testing, I haven’t had much time these last couple of weeks… I can probably put in a switch that will make it work correctly on both platforms.