'ghenv' Not defined, Python component not working after Compile

I’ve written a Python code in Grasshoppers Ghpython Script Component,
the code works fine in “Procedural Mode” but then after converting to “GH SDK mode” and compiling the component, I get this error: 'Global name ‘ghenv’ not defined.

PS. Why am i using ghenv and why?
for things like Adding Baloon Msg, Changing Name and NickName and Description of the component and …

PS II.
lines that work fine in procedural modes (but later cause problems) are like:
ghenv.Component.Name = “Mycomponent1”
ghenv.Component.NickName = ‘cmp1’
ghenv.Component.IconDisplayMode = ghenv.Component.IconDisplayMode.application
or
ghenv.Component.AddRuntimeMessage(gh.Kernel.GH_RuntimeMessageLevel.Warning, ‘Blah Blaaah’)

change ghenv to self…
example:

    def myfunction(self):
        try:
            #do something
        except:
            self.AddRuntimeMessage(Grasshopper.Kernel.GH_RuntimeMessageLevel.Warning,str(sys.exc_info()[1]))
1 Like

Hi @garsivaz

this has come up once before. I’ve noted it down here: RH-54351.

Did @chanley’s method help?
After compiling, ghenv.Component is simply self, because the script becomes the component.

In general, component Name and Nickname in SDK mode are taken from the class name of your component and from the Nickname of the GhPython component. So there’s no need to reset it later. On the other hand, are you sure that overriding your users’ choices for icon is a good idea? Adding runtime messages is sometimes needed – you should get it to work with the system above.

4 Likes

Thanks a lot @piac @chanley
Sorry if the question was repeated,

changing ‘ghenv.Component’ to ‘self’ , solved the issue perfectly.

Cheers

Hiall

I tried changing ‘ghenv.Component’ to ‘self’ and then saving it as .py in the lib folder, but it still giving the warning, this time as the “global name “self” is not defined” when trying to call it from the lib.

Any clues on how should this be done?

Thanks!

Hi, this worked for me:

from Grasshopper.Kernel import GH_RuntimeMessageLevel as RML
if Name == None:
            print("Add a Name")
            self.AddRuntimeMessage(RML.Warning,"Add a Name string")
1 Like