Search - User list
Full Version: Send Command can't find variable declared previously
Root » PDG/TOPs » Send Command can't find variable declared previously
tamte
I have a Python Command Chain
a single Send Command like this works:
a = "hello"
print(a)

however sending each line by separate Send Command TOP errors out and second one complains:
NameError: name 'a' is not defined

is this expected behavior?
I may be misunderstanding how Command Chain works, my expectation was that it starts a server and then I can use Send Command TOPs to send commands to which will execute on the server in a single environment, therefore it would know about the declared variable in previous commands

do I have to be passing my variables as Python Object work item attributes if I want to granularize my command chain?
chrisgreb
Any variables you create go into a local namespace that gets thrown away when the script ends. If you want to keep the variable around you should hang it somewhere global. For example:
a = 'hello'
hou.session.a = a
print(hou.session.a)
tamte
chrisgreb
Any variables you create go into a local namespace that gets thrown away when the script ends. If you want to keep the variable around you should hang it somewhere global. For example:
a = 'hello'
hou.session.a = a
print(hou.session.a)

thanks, but there is no hou.session in Python Server Command Chain like in the example file and I don't want any dependency on hou module or risking license consumption
what would be the best global namespace for passing variables within vanilla python session?
is something like this advisable or does it have any potential drawbacks that I may not see right now?
global a
a = 'hello'
chrisgreb
tamte
is something like this advisable or does it have any potential drawbacks that I may not see right now?
Yes, that works. You just have to watch out for not accidentally shadowing the global var with a local of the same name if you forget the global statement.
tamte
Perfect, thank you
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB