Python equivalent for VBS Private to store variables for a session

Hi Willem,

There is actually an “official” example from Steve somewhere, but i don’t remember where it’s located - I copied it out for reference a long time ago, see below:

# The scriptcontext module contains a standard python dictionary called
# sticky which "sticks" around during the running of Rhino. This dictionary
# can be used to save settings between execution of your scripts and then
# get at those saved settings the next time you run your script -OR- from
# a completely different script.
import rhinoscriptsyntax as rs
import scriptcontext


stickyval = 0
# restore stickyval if it has been saved
if scriptcontext.sticky.has_key("my_key"):
    stickyval = scriptcontext.sticky["my_key"]
nonstickyval = 12

print "sticky =", stickyval
print "nonsticky =", nonstickyval

val = rs.GetInteger("give me an integer")
if val:
    stickyval = val
    nonstickyval = val

# save the value for use in the future
scriptcontext.sticky["my_key"] = stickyval

My own “shorthand” version:

import scriptcontext as sc

#Get
if sc.sticky.has_key("key"): value = sc.sticky["key"]
else: value = defaultValue

#Set
sc.sticky["key"] = newValue

–Mitch