Getting active document without ghenv?

ghenv is very obscure to me. Not only it’s undocumented, it also has no autocompletion hints.

For example, I tried accessing components outside of the cluster from the inside of the cluster.

Old forum posts suggested using either direct approach,

doc = ghenv.Component.OnPingDocument().Owner.OnPingDocument()

or accessing it through recursion,

doc = ghenv.Component.OnPingDocument()

while doc.Owner is not None:

    doc = doc.Owner.OnPingDocument()

Since I didn’t understand how exactly it worked, I tried searching for it in the API to no avail.

Eventually I found this, which seems to have the same output while inside the cluster:

doc = Grasshopper.Instances.ActiveDocument.Owner.OwnerDocument()

However, it only works outside of the cluster if put like this:

doc = Grasshopper.Instances.ActiveDocument

Would these approaches be identical in speed? What could be the caveats of using either one of the above? Maybe there exists a properly documented method I just didn’t manage to find?

I usually use:

import Grasshopper as gh
doc = gh.Instances.ActiveCanvas.Document

But again, things get fucky inside clusters.

Edit: This might also be relevant, depending on which level document you’re trying to get:

It’s been a few years since I’ve done anything in Grasshopper with Python, and all my stuff was mainly for the old Ghpython IronPython2 components.

But checking my code, I vaguely remember the best way round this I found, was instead of the simple Python script style components, to use the class based one (containing a custom subclass of component *).

Then within RunScript, after Grasshopper has initialised an instance of your class, any attributes of ghenv.Component (e.g. .Params) can be replaced with attributes of self (from the parent class).

(*) with the boilerplate: from ghpythonlib.componentbase import executingcomponent as component

For ghdoc (e.g. in library code that doesn’t contain it as an injected magic variable), I just naughtily used sc.doc after double checking the user hasn’t changed it to Rhino.RhinoDoc.ActiveDoc, and made sure it’s an instance of either RhinoCodePlatform.Rhino3D.Languages.GH1.Legacy.ProxyDocument (if available) or GhPython.DocReplacement.GrasshopperDocument.

Wow, I barely understand a single word out of this, and I have very little experience with classes, so I don’t think I quite follow. Something like this?

from ghpythonlib.componentbase import executingcomponent as component

class MyComponent(component):

    def __init__(self):

        super().__init__()   

RunScript looks like it belongs to C# though?

You also mention it’s been years since you interacted with Python in Grasshopper. Did you switch to C# entirely? If so, was it due to performance? I’m looking into rewriting my whole project in C# to save on resources, but only if there’s a real need to…

Thank you.

I haven’t tested this in Rhino 9, but if you manage to create a GHPython component (neither a CPython 3, not the new IronPython 2 components), then just switch the editor into class mode from the menu, and it auto populates all that boiler plate for you, including the RunScript method, and go from there.

You can put custom stuff in __init__ (overriding it simply to call super().init is pointless, and also that’s Python 3 onlt syntax), but note that __init__ only runs when a component is first placed, or when the work book is first loaded.

I do really like C# in Rhino & GH but no, I never switched to it. Our project needed some custom components to be generated at run-time based on external classes, (to self upgrade the plug-in, and perhaps even make it zero maintenance), and I’ve no idea how to go about that in any static compiled language. With Python, it was mostly a matter of just bundling our own devops code in the plug-in itself, letting the user run it to ‘upgrade’, and having a launcher component be able to access its own source code as text strings.

Thank you for a detailed response. I wasn’t able to properly figure it out, though. That’s as far as I had gotten:

Anyhow, I don’t believe GHPython would be of much use to me. It’s editor is far less convenient than such of a modern Python component.

There really is no need to complicate things (e.g. implement SDK mode):



261015_GetGhDoc_00.gh (7.7 KB)

These methods should also work with the new CPython, IronPython and C# scripting components (i.e. they use the Grasshopper.Instances namespace) :man_shrugging:

Thank you, that’s what I needed.

So… sorry to stir this cauldron once again, but I’ve got another problem. I was using your GHPython suggestion, but once I tried to implement it in regular Python 3 I’ve stumbled upon this:

Maybe you have an idea what can be done about it?

I also found the solution through document count to be quite unreliable, since its output is based on the amount of opened scripts:

And even if I somehow manage to import this module, it breaks elsewhere:

It might be a IronPython vs CPython import issue. If you import Grasshopper like so it appears to work:

Ah yes, I can imagine. In that case it might be better to iteratively move up the document levels.

Actually, this was the issue, - the attribute “Document” missing from DocumentServer despite being highlighted in the autocomplete suggestions.

Sounds like another CPython regression/issue. Probably one for @eirannejad