title: Export layers as obj from Rhino3d
body: <p><strong>The task</strong></p>
  
  <p>Export layers as obj files from Rhino.</p>
  
  <p><strong>The issue</strong></p>
  
  <p>When using Python scripting, I run an export command. Instead of exporting the model, a dialog is presented in the interface. If I click the export interface, it works fine. However it brings up the dialog box for every layer. I have many many layers though and I would like to automate the entire export process.</p>
  
  <p><em>The end goal is to display the obj files in webGL via three.js.</em></p>
  
  <p>I am new to Python and Rhino, but know PHP and JavaScript, so I understand the concepts well enough and have watched a few tutorials on Python so I can begin working on this script.</p>
  
  <p><strong>What I have tried</strong></p>
  
  <p>I am using Rhino, Atom/Python.</p>
  
  <pre><code>import scriptcontext
  import rhinoscriptsyntax as rs
  from Rhino.Geometry import Point3d
  
  def layerNames(sort=False):
      rc = []
      for layer in scriptcontext.doc.Layers:
          if not layer.IsDeleted: rc.append(layer.FullPath)
      if sort: rc.sort()
      return rc
  
  
  rs.EnableRedraw(False)
  
  strPath = rs.DocumentPath()
  strName = rs.DocumentName()
  
  arrLayers = layerNames(False)
  for layerName in arrLayers:
      objs = scriptcontext.doc.Objects.FindByLayer(layerName)
      rs.Command("_-Export "+layerName+".obj", False) 
  </code></pre>
  
  <p><strong>Notes</strong></p>
  
  <p>I was thinking of using python native file saving (open("layername.json", "a"). The thought is to somehow get the mesh from the objects in each layer, convert that to <a href="https://github.com/mrdoob/three.js/wiki/JSON-Geometry-format-4" rel="nofollow">three.js json</a> and use that instead. But I don't know how to get a mesh from a layer. I have imported Rhino.Geometry to see if it was helpful. I don't know how to find a mesh to convert, or if I can somehow use the native export command in an automated fashion and just use obj files.</p>