global variable python

Hi,

Is there an easy way to refer to a variable created inside a chop exec? eg- inside op(‘chopexec’) I’ve made a variable: cue1 = ‘bubbles’ ? I want to access this from outside op(‘chopexec’). Or is there a specific way to create a global variable?

thanks.

Hi.
You can always store and fetch variables inside any operator.

In the chopexec you could include a line such as:

me.store(‘cue1’, ‘bubbles’)

Then you can access it from any operator with:

v = op(‘chopexec1’).fetch(‘cue1’)

You can supply the fetch call with a default argument if none are found:

v = op(‘chopexec1’).fetch(‘cue1’, ‘bubbles’)

By default, fetch will search chopexec1, then each of its parents until a match is found.

This way you can include variables in a top level node, and redefine it in lower branches as necessary.

For more information see:

derivative.ca/wiki088/index. … ss#Storage

You can also set a global in python like this:

counter = 0

def increment():
    global counter
    counter += 1

If that was in an op called “countDAT”, you could access it (from a node in the same network) by saying:

currentcount = mod.util.countDAT.counter

The variable is only global to that module, which is why you need to reference the module when accessing it.

There are advantages to either way. Note that using store and fetch saves op variable values in your .toe file, while using a python global will reset the variable on each run. I have seen some strange behavior when storing program objects (like operator references or delayed run commands) using ‘store’ and ‘fetch’. Some of that may be fixed in newer versions, but be aware that your store/fetch data will try to persist when you save and reload your network.

thanks for the useful infos. I hadn’t selected auto-notify so took a while to realise I had a response.

How do variables work within a CHOP Execute dat? I defined a variable px1 in the “def offToOn” section, then when I try to call it in the “def whileOn” section, I get: global name ‘px1’ is not defined"

The rules are the same as any python code. You don’t have access to a variable declared in another function. You can make it module wide be declaring it outside the functions though.

Ah, thanks! Guess I need to learn some more about Python itself.

I’ve been trying to use this method (declaring and using globals in the scope of the script) but the issue I’m finding is that every time I access the script from an external node (with mod()) it causes the script to cook, which resets the global variable. If I let the script cook at least once and then set the node to either bypass or lock it doesn’t cook any more but that doesn’t seem like a good way of addressing the issue.

Has anyone else used this method? How have you overcome the cooking issue?

In general, can anyone comment on the most performant way to store a moderate amount of data (lists and dicts of 10K-100K in size) in a script such that it can be accessed by all of the functions in the script?

Thanks,
Clay Budin
NYC

It’s true that if the script recompiles it will reset your variable. This should only happen if you change the text in the script. A more reliable way is to create an extension. Those variables will be reset each time the extension initializes (again when you change the text of the extension). The most stable way to keep data is in storage. Extensions have the StorageManager feature, or you can simply access an operator’s storage member (directly or with the store and fetch functions). Putting things in storage will save the data state in the .toe file, so some things won’t work. For example, OP objects won’t save properly, but you can save the paths as strings. All builtin Python objects will save properly.