How to write selected Mesh to an .obj file?

Hi Rolf,

as far as i understand, only a few plugins support access (using COM and IDispatch) through the plugin object, eg RhinoScript or Grasshopper. The “OBJ Export” plugin does not support it i guess. But even if it would support access, i don’t think you could export unbaked GH geometry as OBJ with it, as it was made to export geometry placed in the document. For this, in V5, you could just script the _Export command.

The new FileObj class in the Wip does appear to be the thing to use but again the help file mentions “Write an obj file based on the contents of a RhinoDoc”.

Below seems to work to export the contents of a document:

import Rhino
import scriptcontext

def ExportAsObj():
    
    filepath = r"D:\Temp\TestExport.obj"
    
    write_options = Rhino.FileIO.FileWriteOptions()
    write_options.WriteSelectedObjectsOnly = False
    
    obj_options = Rhino.FileIO.FileObjWriteOptions(write_options)
    obj_options.UseSimpleDialog = True

    rc = Rhino.FileIO.FileObj.Write(filepath, scriptcontext.doc, obj_options)
    if rc == Rhino.PlugIns.WriteFileResult.Success:
        print "Success, file saved:", filepath
    else:
        print "Error saving file:", filepath

ExportAsObj()

If your document contains meshable objects, eg. a nurbs sphere, the “Polygon Mesh Options” dialog comes up. Note that above exports the whole scene. To export only the selection, set True in the write_options.

@pascal, there seems to be a bug in the WIP with the OBJ file import. If a file is re-imported, the imported file remains in use by the process and cannot be overwritten, eg. with above code.

_
c.