How to write selected Mesh to an .obj file?


#1

This should be simple, but how do I save/export individual mesh objects to an .obj file from a GrassHopper component? (C#). I’ve been looking around on the RhinoCommon pages but … it should be simple, no?

I would like to save individual Mesh objects like when using the Rhino menu for export :

bild

I’d love to se a working code example on this.

// Rolf


(Nathan 'jesterKing' Letwory) #2

For Rhino WIP you can peruse the File IO namespace. FileObj is what you are looking for I think.


#3

I already found FileObj in the documentation but there’s no such thing in RhinoCommon 5, nothing in the intellisense and it doesn’t compile. This is why I had to ask ( Rhino.FileIO.FileObj ) :thinking:

Edit: I just noticed that I had searched Rh 6 documentation. No such thing in RC 5.
// Rolf


#4

Isn’t this fantastic - I finally found the plugin, I managed to figure out that it’s not loaded (by guid), I did manage to load it (by guid), and everything looked good, and finally I tried to grab the plugin object using Find(guid) - but no plugin is returned! < slapping forehead in dispair >

Fig 1. Obviously the “OBJ Export” plugin exist (see listing):
bild

But this code fails to grab the plugin object - why? What am I doing wrong? :

        bool IsLoaded;
        bool IsLoadProtected;
        Rhino.PlugIns.FileExportPlugIn ObjExport = null;

        var guid = Rhino.PlugIns.FileExportPlugIn.IdFromName("OBJ Export");
        if ( Rhino.PlugIns.FileExportPlugIn.PlugInExists(guid, out IsLoaded, out IsLoadProtected) )
        {
            if ( !IsLoaded && !IsLoadProtected && !Rhino.PlugIns.FileExportPlugIn.LoadPlugIn(guid) )
                throw new System.ArgumentException("WARNING - Could not Load the 'OBJ Export' plugin!");
            Rhino.PlugIns.PlugIn plugin = Rhino.PlugIns.FileExportPlugIn.Find(guid);
            if ( plugin == null )
                throw new System.ArgumentException("WARNING - Could not Find the 'OBJ Export' plugin!");
            ObjExport = (Rhino.PlugIns.FileExportPlugIn)plugin;
            if ( ObjExport == null )
                throw new System.InvalidCastException("Error - Could not Find the 'OBJ Export' plugin!");
        }

Crash.

So how do I get hold of the Plugin object? I think I can say that by now I’m very interested in getting to know the black magic which I’m missing… :thinking:

// Rolf


#5

Ping.

How do I get hold of a PlugIn object (in RhinoCommon 5) which I know exist because I was able to get the guid based on its name?

The following code fails with the guid:

bild

// Rolf


#6

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.


Export stl files with C# component in GH
GH/PY bake and export mesh as obj
#7

I do have baked Rhino (mesh) objects, no problem. But I’m working with Rhino5 so FileObj isn’t available. : ( (Otherwise I would have had it working, no doubt ).

So question is how I go about saving .obj and .stl files using RhinoCommon 5?

// Rolf


#8

Select the objects, then script the _Export command.

_
c.


#9

I wrote a couple of Obj exporters in C#, here i found one that works with normals and textures coordinates;
objWriter.gh (10.8 KB)

Should work fine out of the box, the button triggers the write, not sure about Y-Up / Z - Up and X Forward… was set up for Unity, but you can edit the code - it is quite simple.

good luck

edit:
just in case - vert.Add(String.Format("v {0} {1} {2}", -in_mesh[n].Vertices[i].X, in_mesh[n].Vertices[i].Z, in_mesh[n].Vertices[i].Y));
this is the main line for vertices - remove the minus and swap X with Y in case you need it


(Pascal Golay) #10

Hi Clement - I see that, thanks.
Hm - it does not seem to affect ‘normal’ command based export> overwrite though.

-Pascal


#11

I can confirm that, it only seems to happen when you try to overwrite using above example. No matter if the obj has been opened or imported. Overwriting with the regular OBJ Export works with no problem.

_
c.