Adding Button with GhPython?

Is it possible to add a button with command like undo using Ghpython?

Hi Seghier,

The old GH forums have some information on how to add buttons to the Grasshopper toolbar:

I did not try to do much with it but this code adds a button to the toolbar in Python:

import Grasshopper

toolbar = Grasshopper.Instances.DocumentEditor.Controls[0].Controls[1]
toolbar.Items.Add("Hey I just got inside the toolbar")

The docs for the ToolStrip item that you will add are there:

Finally, undo methods and undo history are available through RhinoCommon for both Rhino (Rhino.RhinoDoc.ActiveDoc.Undo()) and Grasshopper (Grasshopper.Instances.ActiveCanvas.Document.Undo()).


Thank you @pierrec , i will check it and give it a try :slight_smile:

1 Like

Work fine, how to remove it again?

Solved : Got it :smiley:

This is the first step , but it need more work to add button like named view without duplicate.
how we can assign function like undo redo to tho button and how we can assign named view?

add remove (4.8 KB)

private void RunScript(bool run)
  if (!run) return;
  var toolbar = Grasshopper.Instances.DocumentEditor.Controls[0].Controls[1] as ToolStrip;
  toolbar.Items.Add("Undo", null, OnClick);
// <Custom additional code> 
private void OnClick(object sender, EventArgs e)
// </Custom additional code> 
import Grasshopper
def OnClick(sender, e):
if run:
    toolbar = Grasshopper.Instances.DocumentEditor.Controls[0].Controls[1]
    toolbar.Items.Add("Undo", None, OnClick) (4.1 KB)

1 Like

Thank you very much @Mahdiyar

This is good! But when I restart grasshopper, the button disappears again!

I don’t test it again and it is a script work when you launch it, i think you need make it like a plugin