ghenv.LocalScope.TryGetVariable() problem

I am using ghenv.LocalScope.TryGetVariable(). This worked fine in rhino7 grasshopper, but when I switched to rhino8 grasshopper python3, it gives me error.

is_setup_done = ghenv.LocalScope.TryGetVariable("setup_done")

This worked fine in rhino7, but in rhino8, it gives me error:
“1. Error running script: ‘ProxyScriptEnv’ object has no attribute ‘LocalScope’ [32:1]”

Then I tried this:

is_setup_done =ghenv.DataAccessManager.LocalScope.TryGetVariable("setup_done")

And now it gives me an error:
“1. Error running script: ‘NoneType’ object has no attribute ‘LocalScope’ [33:1]”

Does anyone know how to fix this issue?

@Yiran Would you mind sharing an example script? Why not just use setup_done inside the script? If that is an input parameter on the component it will be available in the script scope.

I am trying to understand what the use case is for accessing ghenv.LocalScope. In Python 3, the scope is very different that python 2 so I have removed this property from ghenv. I can bring it back if there is a specific use case

1 Like

I am trying to use MQTT inside Grasshopper. I use trigger to call this component, and I only want to create one MQTT client at the beginning. In my script, setup_done is defined later, so I used TryGetVariable.
Here is part of my script:

is_setup_done = ghenv.LocalScope.TryGetVariable("setup_done")
if setup and not (is_setup_done[0] and is_setup_done[1]):
    try:
        # reinitialise client
        print("Client reinitialized")
    except:
        # create client
        print("Creating new client")
    # connect client and set up callback function
    setup_done = True
    print("Starting")
elif setup:
    pass
elif is_setup_done[0] and is_setup_done[1]:
    # disconnect client
    setup_done = False
    print("Client disconnected")

You can use globals() or the scriptcontext.sticky that are better for this purpose:

Rhino_vt5memGn6q

See attached GH file
PersistentScope.gh (7.2 KB)