Persisting data that is dynamically generated in Python component

Hey all,
I’m working on developing a grasshopper plugin using the Rhino 6 WIP (so that I can write the plugin using python).

I am trying to work out how to persist an array of text values each time a gh definition is saved. The text values would contain information about the user who is saving the file.

By persistence, I mean that I’d like these values to be part of the file itself. Basically if the definition is opened on another computer, the values can be read, but if the file is saved using the new computer, the values will be automatically overwritten.

I found this thread from 2014:

Is the correct approach? If so, could someone elaborate on where the question asker’s code came from? Will including a write() method in a class be sufficient? Is some other approach better, if being executed in python?

Many thanks

When you say “write the plugin using python”, do you mean you’re creating a GHA assembly using python and some compiler, or are you using the Python component?

If the former, then yes you’ll need to override the Write and Read methods (however that’s done in python) and include your own data in the archive. If you mean the latter, then I don’t know why you’d need Rhino6 specifically.

The former, following the process outlined here:

I just tried this:

from ghpythonlib.componentbase import executingcomponent as component
import Grasshopper, GhPython
import System
import Rhino
import rhinoscriptsyntax as rs
from Rhino.Geometry import Point3d as p3

class MyComponent(component):

    def RunScript(self, P):
        X = reader.GetString("SomeData");
        return X

    def read(self, P):
        X = reader.GetString("SomeData");
        return X

But I’m getting an error: “solution exception: global name reader is not defined”. Is there a module that I need to import?

Sorry, that usage of python is beyond my remit. @piac do you know?

My current line of thinking is that I need to do something like:

from GH_IO.Serialization import GH_IReader as reader

at the top of the script, except that python gives this error:

Runtime error (ImportException): No module named GH_IO

This is based on some documentation that I found here:
http://developer.rhino3d.com/api/grasshopper/html/N_GH_IO_Serialization.htm

Plus what I’m sort of extrapolating from these sources:
http://ebanshi.cc/questions/5253021/writing-data-to-a-file-by-overriding-the-write-and-read-methods-of-a-base-cl

C# seems to import the required pieces automatically:

…which might just mean that the answer is for me to man up and learn some c#

HI @Matthew_Breau

I wrote an example that shows how to do this with GhPython. Overriding Read() and Write() works with compiled add-ons. Here is an example that you can compile to see in action.

Also, the second sample shows the pickle module in action, so that you can serialize easily more complex data.

read-write.gh (9.6 KB)

4 Likes

Great example, thanks.

Could you comment on this thread, I am currently using persistent data input to store states of a component. Is there a reference example I could look at showing how components (eg. TreeBranch - Maintain Paths) keep persistence. Is the read()/write() example you shared the way to go in such situations?