Calling a function from another GHPython component

When I am coding, I sometimes break the code down in multiple files to improve readability. I am trying to do something similar in Grasshopper.
So I have one GHPython component that has a few functions.
And I have another (actually multiple) GHPython component that wants to use those functions.
Is there a way I can connect the two components and call the functions?

One way of having those functions available is to define a class for them and either store it in a sticky, or if you are working with objects pass them between gh components.

I show a short example of passing class instances and calling their methods here: (5.8 KB)


Thanks Lando for your response

I am not using classes here.
The idea is to improve modularity of code.
What do you mean by store them in a sticky?

Already started writing after re-reading your question :slight_smile:

see attached for the sticky approach (10.2 KB)

As for modularity i would advise for classes and against scripts, this way your code becomes more readable.

Let me know if the example helps


This is great. Thank you.
I wish there was a more natural way to do this though.

Maybe there is a more elegant way, but sadly i don’t know any.
I used this on some projects so I know it works at least.

Lets see if someone else has another idea :slight_smile:

1 Like

This is very useful though.

I’d advice placing your functions in a .py file/module and importing this in your GHPython scripts (old thread here). And also against using classes for structuring code. That’s not very Pythonic.

Edit: Note that defining a class in one GHPython component and sending class instances downstream through wires, is a really powerful way of OOP developing in Grasshopper I also find. Especially for dynamic systems like this one.