Ghenv ghpy compilation

Hi Giulio. Thank you for your excellent work! I have similar problem as RetroPost.LT @RetroPost.LT.
Here is the code:

from ghpythonlib.componentbase import executingcomponent as component
import Grasshopper, GhPython
import System
import Rhino
import rhinoscriptsyntax as rs

class MyComponent(component):
def RunScript(self, genetic):
author = “XXX”

    gh_env = ghenv

    gh_env.Component.Name = "Fitness Function"
    gh_env.Component.NickName = "Fitness Function"

    out_fitness = None

    if genetic[0] is None:
        in_genetic = None
    else:
        in_genetic = genetic

    # ==============================================================================
    # ============================= START ========================================
    sum_val = None
    if in_genetic is not None:
        sum_val = 0
        for g in in_genetic:
            sum_val += g

    out_fitness = sum_val
    return out_fitness

When I put this component into canvas and loaded input it get red and says:

  1. Solution exception:global name ‘ghenv’ is not defined

Do you know why?

Hi @rdshan,

(I moved this post to a new discussion)

You can change the name and the title of the scripting component by right-clicking on the component, and changing the NickName. GhPython does not use that value, so you are safe.

You can get default Names for the class by defining the class name of the SDK mode to something of your choosing. Names are supposed to be very short in Grasshopper. The best way to adapt the script would be to export the compilable code, and compile that with the changes that you need.

On a user-experience level, this may not be all that great: the user of your component might want to give another NickName to your component. But you you feel you really need this, you can use self. See this discussion>

Thank you, Giulio! After replacing all the ghenv.Component with ‘self’, my problem is solved.