exec() in Python node script


Dear 3D coders,

I am making a simple script to help someone out, but running in to this: the node has a bunch of output ports, Port1, Port2, Port3 etc, and I use the exec() statement to calculate a value for the variables Port1 etc. I use a counter to name the Ports and assign the value of an array in a loop, so i am not directly assigning a value with 'Port1=whatever".

Like so:
Outcode=“Port”+str(counter+1)+" = TheArray[counter]"

If I check the value for Port1, it is fine, but I get an error:
NameError: Could not find port value for ‘Port1’ in node ‘Python’.

The whole idea is not to have to assign values to ports in the code, but to have the code still work when the user adds more ports manually following the same naming convention.

Any ideas?

Dr. Scarlett


Hello Dr. Scarlett,

   Using exec is not a good style :blush: [u]however[/u]:
   1. exec is a statement.
   2. Are you aware of the fact that you need to specify output ports as global ?
   3. Make sure to specify the scope in what you want the code to be executed.

  Kind Regards,


Oooh, goodie.

No I wasn’t aware of the need for a scope definition. Thanks a lot!

Regards, Dr. Scarlett



CODE = 'global Port1; Port1 = mywhatever'

Does that define the global port variable and assign the content in one go?

I like that snippet site you use :slight_smile:


Never mind, it works, so it does.

InkedInBlood will be happy :slight_smile: Thanks again :slight_smile:


Remember the question, well, you know what question ? ^^ Didn’t you ask about exactly that Syntax there ? :thumbsup:

Glad I could help you.
Cheers !


Heheh you are right :slight_smile:

Just because I am used to having globals defined separately of my code at the start, doesn’t mean I cannot use the same dirty trick for them as I am using for the rest.
Tricked myself, I guess :slight_smile:


And here the actual use case


This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.