Accessing ghenv from Python module

I have taken a bunch of code I use in multiple GH Python components and put them in a Python module I store in Plug-ins\IronPython\Lib. The code is organized into a number of classes, I can call the code from any GH Python component, everything works fine, with one exception. Ghenv is not defined, so I can’t call ghenv.Component.AddRuntimeMessage() from within the code. I’ve seen a number of posts referring to the need to replace ghenv.Component by self when using the SDK, but if I’m not mistaken that refers to Rhino 6, and even then would probably not work within my classes.
Is there anyway to reference ghenv from within this module, or do I need to send along ghenv or the component as a parameter when I instantiate the class?

This is called encapsulation and is a basic concept in OOP. A module does not know about the existence of other modules, unless it’s made aware.

Also, in this regard ghenv is a variable in the GhPython nameless scope. So, you can just pass it as an argument to other functions (A) that need it. You could also store it as a variable in another module.

Example for A in Ghpython:

import othermodule
othermodule.something(ghenv)

in othermodule:

def something(ghenv):
    ghenv.Component.AddRuntimeMessage()

Thanks,

Giulio


Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Dear Giulio,

Thank you, I will do so.

Best,

Rudi

1 Like