Persisting data that is dynamically generated in Python component


#1

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


(David Rutten) #2

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.


#3

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?


(David Rutten) #4

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


#5

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#


(Giulio Piacentino) #6

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)