Hello!
I would like my custom ghpython component to create its own input components (e.g. a toggle switch) but am struggling with hooking into the correct events in the component’s life-cycle. Ideally, the component would create the input component when it is being added to the document (and delete it when it’s being removed).
When creating a C# component which inherits from GH_Component overriding AddedToDocument seems to be the way to go, but I was unable to find the equivalent method of ExecutingComponent available in python.
Below is an example component, and I’m looking for the right place in which to call add_toggle_input.
from ghpythonlib.componentbase import executingcomponent
class MyComponent(executingcomponent):
def add_toggle_input(self):
toggle = Grasshopper.Kernel.Special.GH_BooleanToggle()
toggle.CreateAttributes()
toggle.Attributes.Pivot = System.Drawing.PointF(
self.Attributes.InputGrip.X - toggle.Attributes.Bounds.Width - 20,
self.Attributes.InputGrip.Y - toggle.Attributes.Bounds.Height / 2
)
Grasshopper.Instances.ActiveCanvas.Document.AddObject(toggle, False)
self.Params.Input[0].AddSource(toggle)
Thanks!
Chen