Python class instances saved between sessions

Hi,

For storing strings within rhino documents and geometry attributes there is a very clear workflow:

But what about custom class instance? Is it possible to save them within a document itself?

The only workflow I know to store data between multiple python files is the “sticky”, but the contents of it disappears when the file is closed. I am wondering what are other ways to store the “persistent” public data?


import scriptcontext as rs

class my_class:
    def __init__(self):
        self.my_attribute = 5

my_instance = my_class()

rs.sticky["someName"] = my_instance
print(rs.sticky["someName"].my_attribute)
1 Like

what about pickling it and converting to base64 string, then put it into doc strings? On reading you do the inverse: decode from base64, unpickle and voila? Or maybe just simple JSONification back and forth?

I’d be a bit hesitant to do this, as things have a tendency to change, and when you unpickle a class instance - especially if it was pickled a long time ago, it may no longer make sense to your code as it is at the moment of unpickling. (unless I misunderstand Python’s pickling of class instances and this is perfectly OK).

Aye, JSONified member data is probably more resilient.

1 Like