Using python classes in several python script nodes

Is there a way to use a class in a python node that I have defined in another node before?

May I ask what class did you define ?
Basically we can do it.

I am trying to write a code that would generate architectural layouts.
A single Python Script node becomes bulky and inconvenient to use. So I want to use several nodes for diferent purposes and functionality.
Some of the classes i want to define are for “A* search” algorythm described here:
https://www.laurentluce.com/posts/solving-mazes-using-python-simple-recursivity-and-a-search/

You can pass the class to the OUT variable and use it as an input in your other nodes.

Your first node may be this:

class MyClass():
    pass

OUT = MyClass # Do not instantiate this class.

And your second node could be this:

MyClass = IN[0] # Assume you have passed the class to the first input.
my_inst = MyClass() # Instantiate the class only once we have assigned it to a variable.
1 Like

It is a very interesting algorithm. Keep up good work man. You can define your class in another python (.py file) and save all of those in one specific directory (in my example, I put in D:\ - which I marked by number 1 in the picture).
Then you can call it from python script inside dynamo (marked by number 2 in the picture).
I hope it could help.

6 Likes

Interesting method—much more pythonic too.

1 Like