Hi,
I’m trying to create a button on a ghpython component, and I don’t want it as a drop down button. I’ve been trying to override the methods for Render and Layout to create the button according to similar examples in c# online, among others these guides: https://developer.rhino3d.com/guides/grasshopper/custom-attributes/
https://www.grasshopper3d.com/forum/topics/button
However nothing happens to the component. I’ve been trying to create the equivalent code for the ghpython node, but I am not sure if I have understood the c# code correctly. Is it possible to override these methods for a ghpython node to change the appearance of the node? Any help is highly appreciated. The code below is the same as the code in the attached .gh file.
from ghpythonlib.componentbase import executingcomponent as component
import Grasshopper, GhPython
import System
import Rhino
import rhinoscriptsyntax as rs
from System.Windows.Forms import MessageBox
from System.Windows.Forms import MessageBoxButtons
class Attributes_Custom(Grasshopper.Kernel.Attributes.GH_ComponentAttributes): # inherits all methods of GH_ComponentAttributes
def Layout(self): # override inherited method Layout
Grasshopper.Kernel.Attributes.GH_ComponentAttributes.Layout() # run layout before changing the definition
rec0 = Grasshopper.Kernel.GH_Convert.ToRectangle(self.Bounds) #System.Drawing.Rectangle
rec0.Height += 22
rec1 = rec0
rec1.Y = rec1.Bottom - 22
rec1.Height = 22
rec1.Inflate(-2, -2)
Bounds = rec0
self.Bounds=Bounds
ButtonBounds = rec1
self.ButtonBounds=ButtonBounds
def Render(self,canvas, graphics, channel): # Override Render method
MessageBox.Show("Render running", "Render", MessageBoxButtons.OK)
Grasshopper.Kernel.Attributes.GH_ComponentAttributes.Render(canvas, graphics, channel) # run render before changing the definition
if channel == Grasshopper.GUI.Canvas.GH_CanvasChannel.Objects:
button = GH_Capsule.CreateTextCapsule(self.ButtonBounds, self.ButtonBounds, Grasshopper.GUI.Canvas.GH_Palette.Black, "Button", 2, 0)
button.Render(graphics, self.Selected, self.Owner.Locked, False)
button.Dispose()
def RespondToMouseDown(self,sender,e):
if e.Button == System.Windows.Forms.MouseButtons.Left:
rec = self.ButtonBounds
if rec.Contains(e.CanvasLocation):
MessageBox.Show("The button was clicked", "Button", MessageBoxButtons.OK)
return Grasshopper.GUI.Canvas.GH_ObjectResponse.Handled
self.sender=sender
self.e=e
self.RespondToMouseDown(sender, e)
class MyComponent(component):
def CreateAttributes(self): # Override inherited method create attributes
m_attributes=Attributes_Custom(ghenv.Component)
def RunScript(self, x, y):
a=1
return a`
custom attributes.gh (4.8 KB)