Save a Mesh into OBJ file

Hello,

I am trying to save a Mesh into an OBJ file, but I guess I am doing something wrong here… here is my code:

Rhino.RhinoDoc rc = Rhino.RhinoDoc.Create(“”);
rc.Objects.AddMesh(meshToSave);
Rhino.FileIO.FileObjWriteOptions options = new Rhino.FileIO.FileObjWriteOptions(new Rhino.FileIO.FileWriteOptions());
options.MapZtoY = true;
Rhino.PlugIns.WriteFileResult res = Rhino.FileIO.FileObj.Write(“PATH_TO_FILE/fromRhino.obj”, rc, options);
if (res == Rhino.PlugIns.WriteFileResult.Success)
{ MessageBox.Show(“File saved successfully”); }
else
{ MessageBox.Show(“Something went wrong :(”); }

When I create the new RhinoDoc, it is overriding the current ActiveDoc and Rhino is kind of reset. This same code works perfectly if I run it inside a C# node in Grasshopper.

Any ideas?

Thanks in advance!

Hi @Ayoub,

Are you using Rhino 6 or the WIP? Are you running this code from a Rhino command or from a Rhino.Inside host?

– Dale

hi @dale,
Thanks for your reply. I am using Rhino 6 on Windows. I run the command from a WPF User Control that is loaded as Panel in Rhino.

@stevebaer - I don’t see anyway of doing this in Rhino 6. Am I confused?

I’m sorry but I’m not following. What are you asking about?

In Rhino 6 - newing up a new document, adding a mesh, writing it using FileObj.Write.

From what I can tell, RhinoDoc.Create always closes the open document and replaces it with what is newed up.

– D

Gotcha; that is all new functionality in the Rhino 7 WIP and can’t be added to Rhino 6. It was a pretty big project that required a good number of changes to Rhino.

Can RhinoDoc.CreateHeadless from WIP do the job? Haven’t tried that.

CreateHeadless is exactly what I was referring to.

@stevebaer @dale
Thanks for the replies. I am a bit confused here as well, there is no way of exporting a mesh to obj from Rhino 6? What is the use case of FileObj.Write then?

Just passing by here and thinking out loud… Do you need a new doc? Why not just use the active doc and set the FileWriteOptions to use the selected objects only option to export your mesh. Just delete the mesh after?

Writing an obj file by hand is like 10 lines of code, check the file format specification & you’ll see it is literally a set of XYZ coordinates, one on each line. Followed by a set of indices, one line per face (including ngons).

See https://en.wikipedia.org/wiki/Wavefront_.obj_file

using(TextWriter tw = new StreamWriter("path/to/file.obj"))
{
  foreach(var v in mesh.Vertices) 
  {
    tw.WriteLine($"v {v.X} {v.Y} {v.Z}");
  }
  foreach(var f in mesh.Faces)
  {
    tw.Write($"f {f.A} {f.B} {f.C}");
    if (f.IsQuad) tw.Write($" {f.D}"};
    tw.WriteLine();
  }
}
2 Likes

This can absolutely be done in Rhino 6, but only with the activedoc.

I was referring to the fact that you couldn’t create a temporary document that doesn’t replace the activedoc in Rhino 6 (which is something you can do in Rhino 7).

:slight_smile: ok there’s a little more than that in our obj file writer, but this may suffice for what is needed.