Python Scripting Global Variables

Python Scripting: What’s the best practices way of using global variables so that they exist as the same script is ran multiple times? Is scriptcontext.sticky the best way to implement this?

I want the global variable to persist between script executions.

1 Like

You can either make them stick by making them global or by inserting them into the sticky dictionary.

I’d say it all depends on the use case.

If you have only few of variables to keep alive, I’d make them global.

if "count" not in globals():
   count = 0

Otherwise, it’s probably easier to insert them into sticky.

import scriptcontext as sc

if "count" not in sc.sticky.keys():
    sc.sticky["count"] = 0
else:
    count = sc.sticky["count"]
3 Likes

@diff-arch - I’m trying to keep the global variables persistent between script executions. I’ve ran the following code to set the count global variable. The I comment out the first line and the variable does not persist. How do I get it to persist?

globals()['count'] = 5
print count

@diff-arch - I updated the original question so it’s a little more clear.

@diff-arch - sc.sticky definitely works. Is there a way to do it with globals or is sc.sticky the only way to go?

This establishes the count variable, in case it doesn’t exist in globals().

if "count" not in globals():
    count = 5   #  instantiation 

After that you can refer to it normally each time the script reruns, for instance:

count += 1
print count

@diff-arch - I totally understand what you are saying but “count” does not persist over different script executions. If I run you code it prints “6” every time. What I want it to do is add one every time the script is ran. So the first time it will be “6”, next time “7”, time after that “8”, etc.

If you’re using Rhino, you need to use sticky instead, I think.

Hi Mike,

Would it be an option to use DocumentUserTexts?
They will persist within the document even after closing and opening again.

rs.SetDocumentUserText()
rs.GetDocumentUserText()

@diff-arch @Willem - After some quick testing sticky is the one you want to definitely use if you want the variable to persist between Rhino documents. It will persist until Rhino is closed. rs.SetDocumentUserText / GetDocumentUserText is great for storing data in the document that you want to persist in the current document only.

Thanks for all the help!

1 Like

Hi another solution which also works for IronPython and C# is the RhinoDoc.RuntimeData property!

Its basically an extended Dictionary<object,object> → a dictionary with a generic key and value pair

Can be called Rhino.RhinoDoc.ActiveDocument.RuntimeData.Add(…)

For more information:

The advantage is that it’s not really global, but valid on the active document. Different document instances will have different state. Also great for dealing with events or passing data from one script component to another. I also like that the key can be anything and not just a string, but also a GUID or whatever you wish.

3 Likes