Send Command can't find variable declared previously

   2366   6   1
User Avatar
Member
8524 posts
Joined: July 2007
Offline
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?
Edited by tamte - May 23, 2020 01:14:10

Attachments:
top_commandchain_python_variable_error.hip (101.2 KB)

Tomas Slancik
FX Supervisor
Method Studios, NY
User Avatar
Member
603 posts
Joined: Sept. 2016
Offline
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)
User Avatar
Member
8524 posts
Joined: July 2007
Offline
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'
Edited by tamte - May 23, 2020 14:42:24
Tomas Slancik
FX Supervisor
Method Studios, NY
User Avatar
Member
603 posts
Joined: Sept. 2016
Offline
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.
User Avatar
Member
8524 posts
Joined: July 2007
Offline
Perfect, thank you
Tomas Slancik
FX Supervisor
Method Studios, NY
User Avatar
Member
8524 posts
Joined: July 2007
Offline
the above solution using global variables worked perectly fine in h18 python2
however after a while using it in 18.5.733 python3 and the global variables are not seen in subsequent Command Send nodes in the command chain

not sure if necessarily python3 related or something has changed in PDG, but it would be great if it was possible to still do this

Attachments:
bug_top_pythonserver_python3_global_variables.hip (90.3 KB)

Tomas Slancik
FX Supervisor
Method Studios, NY
User Avatar
Member
8524 posts
Joined: July 2007
Offline
just needed this again, tested in 19.5, still doesn't seem to work (BUG#126845)

however I found it listed in docs [www.sidefx.com]

or is there any other pythonic way to access data from the command server in downstream command chain nodes without unnecessary workarounds like workitem attributes or hou.session?
Tomas Slancik
FX Supervisor
Method Studios, NY
  • Quick Links