Since I love it when people resurrect this old thread (no irony!) and the JSON thingy got me really interested aswell, I threw together a really minimal example in the hope that it might help @osuire and others:
Code for Serialization:
# import RhinoCommon
import Rhino
# create serialization options
options = Rhino.FileIO.SerializationOptions()
# set RhinoVersion (optional)
if RhinoVersion:
options.RhinoVersion = RhinoVersion
# set if userdata should be written (optional)
options.WriteUserData = WriteUserData
# get json string
JSONString = Geometry.ToJSON(options)
Code for Deserialization:
# import RhinoCommon
import Rhino
import json
import System
# convert the JSON String to python dictionary using the json module
PyDict = json.loads(JSONString)
# create a new .NET dictionary
SysDict = System.Collections.Generic.Dictionary[str, str]()
# loop through python dict and fill .NET dict
for key in PyDict.keys():
value = str(PyDict[key])
SysDict.Add(key, value)
# de-serialize the geometry by creating geometry from the json dict
Geometry = Rhino.Geometry.GeometryBase.FromJSON(SysDict)
Here is also the corresponding gh file shown in the screenshot: 210119_JSON_Serialize_Deserialize.gh (9.2 KB)
A few things come to mind while doing this (ping @nathanletwory) :
-
It works, which is really cool in my opinion - have searched for an easy way to serialize rhino geometry since a bit. Thanks for this!
-
I would really appreciate if some overload could be implemented so that the
FromJSON
method accepts a pythondict
object -
To me it seems a bit strange that
ToJSON
produces a string butFromJSON
absolutely needs to have a .NETDictionary[str, str]
. In my opinion the method should be able to deal with whatever comes out ofToJSON
itself. But maybe I’m wrong? -
And last but not least something a bit off-topic: Please, McNeel, you have to realize that a lot of the developer documentation - especially regarding python - is in an absolutely horrible state since years!
There are not only missing examples but completely missing documentation items. This makes working with some parts of RhinoCommon a nightmare. I would strongly advise you to just hire one Rhino scripting nerd whose job it is to collect all the terrible loose-ends in the documentation.
Maybe I’m naive here but it can’t be that big of a problem - I’ve been thinking about this for 2+ years and often was on the edge of writing docs myself and send it to you, just because I’m so immensely tired of having to look at things like this all over the place:
Although I have a job at the moment, please do let me know if a position as ‘documentation slave’ is available. This is not a joke, I would be more than happy to do this! I have the sickest OCD which will not let me stop until everything is super nice, tidy and shiny and on top 3+ years of experience in digging through missing documentation and getting it to work in python anyway. Interested?
With that all being said I hope the provided example is useful. It could also be added to the… *cough* documentation *cough* …