Button and context menu options on component through GhPython

ghpython
python
compiler

#1

Hi!
Like this component, it has an Update button, and 3 option list if you Right-Click this.
Is it possible to achieve that through GHpython after compiling into ghpy file?

3


Advanced navigation inside GH canvas
(Giulio Piacentino) #3

OK-- so, this needs a little bit of introduction.

Both are possible also in GhPython. I am going to introduce the context menu, because it really sometimes is required. But I am not going to show the button, because this deviates from the default way of doing thngs in Grasshopper and so I will leave it as an exercise if you really think it’s necessary.

The GhPython component provides a dynamic binding to the underlying AppendAdditionalMenuItems function of GH_Component*. Therefore, all you really have to do it override it. In Python this really just mean define a member with the same name.

The method we are overriding is AppendAdditionalMenuItems: http://developer.rhino3d.com/api/grasshopper/html/M_Grasshopper_Kernel_GH_Component_AppendAdditionalMenuItems.htm

As you can see, it gives us an object guaranteed to be derived from type ToolStripDropDown: https://msdn.microsoft.com/en-us/library/fs7t9bea

We just need to add a new item. In turn, the item will contain some text, optionally an image and a link to a method that needs to be called when the item is clicked.

Here is the file.
AppendAdditinalMenuItems-python.gh (3.9 KB)


*when you compile, you really derive from GH_Component, thereby having access to exactly the same class. At runtime while editing, GhPython simply binds a list of methods to its own underlying GH_Component. Because of duck-typing, this will look exactly like overriding the method, but we will be able to switch many times the implementation, which is something that is not possible in normal C# development, but rather requires a Rhino restart.


#4

Thank you Giulio,that would be very helpful!


#5

That’s awesome! Would that mean we can override DrawViewportMeshes and DrawViewportWires now? And if so, could you maybe provide a small example of how, pretty please :slight_smile:


(Giulio Piacentino) #6

Yes, both are true. An example can be found in this post.
Now that GhPython is distributed with Rhino 6 and 6 has shipped, this becomes more useful.


#7

Ah yes, of course, that takes me back. This really is terrific. Great job guys :clap:


#8

I “cannot import componentbase from ghpythonlib” - does this only work with Rhino 6?


(Giulio Piacentino) #9

Yes, this is a Rhino 6 feature.


(IVELIN PEYCHEV) #10

Come on Guilio, tell the secret how to put the button there :slight_smile:


(Giulio Piacentino) #11

EDIT: I really think it makes little sense to use time to make a button there… sorry.

Thanks,

Giulio


Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com


(IVELIN PEYCHEV) #12

Well I have v6 :wink:

Pretty please a button.
Reason being, I’m too much of a programming noob to be able to use the API properly. I end up trial and error Rhino.something() does it require an argument does it not?!


(Giulio Piacentino) #13

Why do you want to do this?


(IVELIN PEYCHEV) #14

I want to create a script acting like a switch, with button on it.
when you click on the button it cycles through the inputs

Edit: and/or the outputs, preferebly it could be two buttons one cycling through the inputs and other through the outputs (that is you have only one output variable but different data (formula) is provided(calculated) depending on the button click.)


(Giulio Piacentino) #15

This, really, can be easily done with the dropdown example above. If @DavidRutten sanctions UI change to button and provides a sample, I’ll gladly translate it to Python for you.


(IVELIN PEYCHEV) #18

Use case 2: (or more of a UC1 2nd alternative solution)

2018-05-29%2000_07_54-Greenshot

This is a thread I created not long ago about the same topic:
https://discourse.mcneel.com/t/ghpyhon-access-to-grasshopper-gui/62240


(David Rutten) #19

I can’t help you out with Python, but if you want to create objects with a custom appearance special mouse handling then you’ll need to switch from Python to Visual Studio and create a GHA project. Then you can override the attributes of your components and parameters (as per the example in the developer documentation, downloadable via the GH Help menu).


(Giulio Piacentino) #20

I think David is right; while possible in Python, if you really want to go for the button, it is better if it is done in Visual Studio with C# because of autocompletion and also because there is the tutorial in the Grasshopper SDK docs, written by him, regarding Custom Attributes (C#).

Thanks,

Giulio


Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com