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.
@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?
@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.
@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.
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.