Disable gumball with Python in GH

Working on a project that makes use of the model environment through the recent Rhino components in Grasshopper. I have some randomly generated panels that i can cycle through. With some python code+trigger i check if a panel is selected, which is stored as a boolean value/key in the Object.

Now i want to try and disable the gumball whenever a panel is selected, because at that point i want to show some annotation/information. The gumball comes in use again later under different conditions, so i need to enable it again when the boolean is false.

I have this code through chatgpt, but so far no luck. Does anyone see the problem?

import Rhino
import scriptcontext as sc
import rhinoscriptsyntax as rs

# Input: isSelected (bool) -- True to turn Gumball ON, False to turn OFF

def toggle_gumball(is_selected):
    # Get the current document's GumballDisplayConduit
    gumball = Rhino.UI.Gumball.GumballDisplayConduit()
    
    # Enable or disable the Gumball based on selection state
    gumball.Enabled = is_selected
    
    # Redraw the views to update the display
    Rhino.RhinoDoc.ActiveDoc.Views.Redraw()

toggle_gumball(isSelected)


Panels_Redux.gh (14.6 KB)

Realized i was thinking too hard (or rather not hard enough). Turns out its very easy to do this with a RunScipt command.

Rhino.RhinoApp.RunScript("_Gumball Off", False)

1 Like

Hi @Calcman,

When scripting within Grasshopper but trying to access things in Rhino Document such as the Gumball, DisplayMode stuff… etc.

You need to set your script context to the Rhino Document because you will be in the “grasshopper context” otherwise and it doesn’t have knowledge of the Rhino Document typically.

import Rhino
import scriptcontext as sc

# Before trying to access something in Rhino
# Set Script Context To Rhino Doc
sc.doc = Rhino.RhinoDoc.ActiveDoc

# Do something in code here that needs to interact with/modify Rhino

# After accessing Rhino and working back with Grasshopper again
# Set Script Context To Grasshopper Doc
sc.doc = ghdoc

#End the script here or continue doing other Grasshopper related code/access

I think there is something about not using ActiveDoc on MacOS but I forget exactly and can’t find that post on the forums right now but will update here if I find it.

Cheers!

1 Like

Good to know, thanks:)

1 Like