Model serialization


Is it possible to serialize the whole Rhino 3D model object (the RhinoDoc class I guess) into an string (XML, json, or any format) so that then you can deserialize the string to recover the original object?
(without using WriteFile functions)

Thank you,

Hi @victormunoz,

Rhino does not have a native text file format; the 3dm file format is binary. The object serializer in RhinoCommon calls into our native openNURBS code, which produces binary data.

That is said, it is possible to write and read text a file format of your own choosing. We already do this in Rhino, which supports text formats such as STEP, IGES, DWF, OBJ, and other.

I have seen a few initiatives to write JSON. But the goal is generally just mesh information, not Brep, Annnotations, etc.

What is the end goal of producing a text file version of a Rhino document?

– Dale

you can use this: Get an obj object from mesh but for the moment I don’t know how to load the obj to mesh

It has not necessarily to be a text format, it can actually be a binary file.
So, how can I get the binary data of an object without using the WriteFile function? And how can I load an object from the binary data (without using the Open function)?
The reason for not using the WriteFile and Open functions is that I want to avoid having the file available in the disk at any time.
The plugin save function will save the file encryptedly, so that no one can use it. The only way of using the file would be with the plug in load function, that will be able to decrypt the file, and then load it.

Thank you

This solution is a good starting point. Does it save the whole RhinoDoc object?
How would the loading function look like?

Yes, it save all RhinoDoc object, with a small modification it can save only 1 item that the user can select, I’m working on a loading function but I have a small problem here is the topic: Obj string to valid mesh

Hi @victormunoz

See the attached sample.

TestVictorCommand.cs (3.0 KB)

– Dale


I translated @dale code into python, hoping I can use it in ghpython component. Conversion back and forth between rhinoobject and bytes seems to work but when I write the Array[Byte] to file and read it back, I get a str type. Any ideas how I can read it back as Array[Byte]?
(evidently the python bytearray type isn’t acceptable either)

1 Like

In (Iron)Python I’d use pickle to serialize the data. You can then encrypt that data before saving to disk.

import scriptcontext as sc
import pickle
import Rhino.Geometry as rg
import Rhino.DocObjects as rd

for o in sc.doc.Objects:
	sg = pickle.dumps(o.Geometry, -1)
	sa = pickle.dumps(o.Attributes, -1)
	og = pickle.loads(sg)
	at = pickle.loads(sa)
	if og.ObjectType==rd.ObjectType.Brep:
		print(og.Faces.Count, og.Edges.Count)
	if og.ObjectType==rd.ObjectType.Mesh:
		print(og.Faces.Count, og.Vertices.Count)
	if og.ObjectType==rd.ObjectType.Extrusion:
		print(og.CapCount, og.ProfileCount)

The output I get with the attached 3dm is:

spherical mesh
(100, 92)
cool extrusion
(2, 1)
Teh Name
(1, 2)

pickletest.3dm (72.4 KB) (542 Bytes)

1 Like

Definitely the shortcut I’m looking for. Thank you much!