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 :
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 )
Edit: I just noticed that I had searched Rh 6 documentation. No such thing in RC 5.
// Rolf
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):
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…
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:
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.
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?
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
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.