ShowColorDialog GH Python script loses output when mouse over canvas

Hi Guys,

I’ve try a couple of different code samples for this, latest one attached.

The allows the Rhino Colour Picker window to open and collect the selected color - which outputs fine to the panel.

Then after a second or two the component ‘times out’ for the want of a better description and outputs a NULL - which then loses the selected color.

This is not the way any other GH Python I’ve written behaves - these retain there output until the script is run again.

Have I missed something? Or is there something different with the ShowColorDialog that requires this behavior?

Cheers

DK

230731_GH Python Color picker.gh (5.1 KB)

OK - I have a found another symptom - as long as I leave my mouse cursor on the screen with the Rhino Window (two monitor setup) the output will remain ‘live’ in GH.

As soon as the mouse moves on to the GH canvas the output goes NULL.

It almost feels this the script has a mouse event tied to it?

Weird is the word that comes to mind.

Cheers

DK

Title updated to actual issue.

Does now mean I have a way of detecting a mouse moving onto the GH Canvas - tho not sure of an application for that…

Cheers

DK

Same bug also comes up with rs.GetColor (assume it is just calling ShowColorDialog under the hood)

Sample attached.

Cheers

DK
230731_GH Python rs.GetColor bug.gh (10.8 KB)

I was able to ‘fix’ this issue by placing the Bengesht ‘Expire Dam’ component upstream of my Python script, the only reason I think this works is the bug was related to the ‘True Only’ button issue.

Anyway, my project needs other Bengesht components - so this is an suitable fix.

Cheers

DK

Hi @kiteboardshaper,

If I understand correctly what you are after?
You can store the color value in a sticky dictionary instead and this will retain the output color until a new color is selected.

The caveat is that the sticky dict will not store this color value between GH sessions if you close and reopen your file. Hopefully someone can weigh in on this as I’m new to the scripting world.

import rhinoscriptsyntax as rs

# Check if the sticky dictionary already exists, and create it if not
if "sticky_dict" not in globals():
    sticky_dict = {}

# Check if the 'x' button is set to true
if x:
    new_color = rs.GetColor(y)
    if new_color:
        sticky_dict['a'] = new_color

# Retrieve the color from the sticky dictionary or use the default value
color = sticky_dict.get('a', y)

a = color

20230805_GH Python rs.GetColor bug_Response_01a.gh (10.5 KB)

This is one approach:

Awesome! This is very helpful @AndersDeleuran, I’ve adopted your method in the 3rd option below.

Here’s the same color picker logic with three different persistent data methods:

  1. Grasshopper Sticky (persistent within current Grasshopper session only):
import rhinoscriptsyntax as rs
import System.Drawing

# Check if the sticky dictionary already exists, and create it if not
if "sticky_dict" not in globals():
    sticky_dict = {}

# Check if the 'S' button is set to true
if S:
    new_color = rs.GetColor(Dc)
    if new_color:
        sticky_dict['C'] = new_color

# Retrieve the color from the sticky dictionary or use the default value
C = sticky_dict.get('C', Dc)

# Combine R, G, and B values into a single RGB color or return RGB string if using default color
if len(C)>1:
    combined_color = System.Drawing.Color.FromArgb(C[0], C[1], C[2])
else:
    combined_color = C

C = combined_color
  1. Rhino Document Data (persistent within Rhino Document only, does not work if new rhino doc created):
import Rhino
import rhinoscriptsyntax as rs
import scriptcontext as sc
import System.Drawing

sc.doc = Rhino.RhinoDoc.ActiveDoc

# Check if the 'S' button is set to true
if S:
    new_color = rs.GetColor(Dc)
    print(new_color)
    if new_color:
        # Store new_color as document data
        color_rgb_str = str(new_color)
        rs.SetDocumentData("MyColorData", "ColorRGB", color_rgb_str)

# Retrieve the color from the document data or use the default value
stored_rgb_string = rs.GetDocumentData("MyColorData", "ColorRGB")
if stored_rgb_string:
    C = stored_rgb_string
else:
    C = Dc

sc.doc = ghdoc

  1. Grasshopper Persistent (persistent within Grasshopper session/close/re-open regardless of Rhino Document close/open/new file):
import rhinoscriptsyntax as rs
import json

# Check if the 'S' button is set to true
if S:
    # Pick New Color With Rhino Color Picker
    new_color = rs.GetColor(Dc)

    if new_color:
        # Serialize color to JSON and store it
        new_color_json = json.dumps(str(new_color))
        ghenv.Component.OnPingDocument().ValueTable.SetValue("MyColorData", new_color_json)

# Read data from persistent Grasshopper document value table
stored_color_json = ghenv.Component.OnPingDocument().ValueTable.GetValue("MyColorData", "")

# Deserialize JSON string back to output
C = json.loads(stored_color_json)

20230805_GH Python rs.GetColor bug_Response_01d.gh (7.3 KB)

1 Like