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?
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…
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.
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: