Ghenv unknown when running _-RunPythonScript

When attempting to run the following script with _-RunPythonScript, ghenv is unknown. I figure this is because the GH environment is not available at runtime, as it would be inside a GHPython component. How would I write a similar function at runtime?

import Grasshopper as gh
doc = ghenv.Component.OnPingDocument()

Have tried, but get an error `OnPingDocument’ takes 1 arguement, but confused by which value give the method. I am sure I miss understanding the documentation.


from Grasshopper.Kernel import GH_Component

Hi @bpachuca1

Make sure the IronPython plug-in is enabled.

– Dale

Hello @dale IronPython is enabled when checking the PluginManager. However ghenv is still not defined when running a Python script outside of GH using _-RunPythonScript.

Is ghenv available at runtime outside of GH? If not, how would I replicate the function below to run using _-RunPythonScript ?

( doing this outside of GH, since I am calling the script from a .bat script and need to find a component by NickName.

Original function

import Grasshopper as gh
ghObj= ghenv.Component.OnPingDocument().Objects

Trying to get a list of all the components in the GH script

Hi @bpachuca1,

Accessing Grasshopper from Rhino.Python (e.g. outside of Grasshopper) is a bit different. You’ll need to add a reference to the Grasshopper plug-in using clr.AddReferenceByName. From there, you can do something like this:

import Rhino
import clr
import Grasshopper

gh = Rhino.RhinoApp.GetPlugInObject('Grasshopper')
if not gh.IsEditorLoaded():
    print 'Grasshopper editor is not loaded'
    for doc in Grasshopper.Instances.DocumentServer.Document:
        print('document name: ' + doc.DisplayName)
        i = 0
        for obj in doc.Objects:
            print('document object (' + str(i) + '): ' + obj.Name)
            i = i + 1

– Dale

Thank you @dale - worked like a charm!