Call variable from ironpython node to external python script

Hi guys,
as a simplification of my problem, I created an external file test.py with the following code:

n = aaa
if n <= 5:
    n += 1
else:
    n += 2

meanwhile, inside the Ironpython script in dynamo I have:

import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

sys.path.append("C:\\Users\\.....") # test.py location

aaa = IN[0]

from test import *

OUT = n

and the error is

global name “aaa” is not defined, beacuse inside test.py it’s not.
And now the question: is there a way to make the variable aaa readable in test.py?
Thanks in advance!

I don’t think this is possible, although I may be wrong. Is there any specific reason you’re trying to do this? If I were you, I would treat your python node in Dynamo as your main.py and import functions from test.py as if it were a module.

Thanks @cgartland for the quick answer.
I thought about it as well. But the main reason for it is that I’m going to use my test.py more than one time in different python node and the code is not so simple… so if I have to modify it, I only do it once…

For that reason I would recommend defining all of your common functions in test.py and importing them as needed. For example, if this is test.py

def test_func(var):
    var += 1
    return var

And this is your Dynamo node:

from test import test_func
a = 1
b = test_func(a)
OUT = b

You can use test_func in as many other Dynamo graphs as you need, but you don’t have to worry about global variables.

What is in test.py and could what is in test.py be put directly into a python node?

Reason i ask this is because you could just do the following, though you will have to change your input “aaa” to “IN[0]”

1 Like