Updating from eto.forms dialog

I am very new to using eto.forms, but am trying to test something simple out before I go to far with it.

The goal is to have eto dialog pop up so that others can use a grasshopper script and select which geometry they are working with without getting too far into it, with variables being passed into the dialog from my grasshopper script.

I have got it working with a dropdown list being populated from grasshopper, but cannot get variables to update without closing the dialog box, is it possible? Or am I barking up the wrong tree?

I know that I can use something like Human or Synapse, but would rather solve it without another plugin if I can avoid it.

Here is my cobbled together code:

import Rhino
import scriptcontext
import System
import Rhino.UI
import Eto.Drawing as drawing
import Eto.Forms as forms

class SlopeWindow(forms.Dialog[bool]):
    def __init__(self):
        super(SlopeWindow, self).__init__()

        self.Title = 'Controls for slopes'
        self.Padding = drawing.Padding(15)
        self.Resizable = False

        # list box label
        self.m_label = forms.Label()
        self.m_label.Text = 'Which curve?'

        # list box - which curve
        self.m_listbox = forms.ListBox()
        self.m_listbox.DataStore = x
        self.m_listbox.SelectedIndex = 0

        # ok button to select the curve
        self.DefaultButton = forms.Button()
        self.DefaultButton.Text = 'OK'
        self.DefaultButton.Click += self.OnOKButtonClick

        # Create the abort button
        self.AbortButton = forms.Button()
        self.AbortButton.Text = 'Cancel'
        self.AbortButton.Click += self.OnCloseButtonClick

        # Try to bring it together
        layout = forms.DynamicLayout()
        layout.Spacing = drawing.Size(5, 5)
        layout.AddRow(self.m_label, self.m_listbox)
        layout.AddRow(None) # spacer
        layout.AddRow(self.DefaultButton, self.AbortButton)

        # Set dialog content
        self.Content = layout

    # Listen to the window
    def GetListValue(self):
        return self.m_listbox.SelectedValue

    def GetListIndex(self):
        return self.m_listbox.SelectedIndex

    # Close button click handler
    def OnCloseButtonClick(self, sender, e):
        self.Close(False)

    # OK button clicker
    def OnOKButtonClick(self, sender, e):
        if self.m_listbox.SelectedValue == "":
            self.Close(False)
        else:
            global a
            global b
            a = self.GetListValue()  # Update the variable 'a'
            b = self.GetListIndex()
            self.Close(True)

def GetLine():
    dialog = SlopeWindow()
    rc = dialog.ShowModal(Rhino.UI.RhinoEtoApp.MainWindow)
    if rc:
        print("Selected curve:", a)

if __name__ == "__main__":
    x = ["c1", "c2", "c3"]  # just for testing - gets passed in from grasshopper otherwise
    a = None
    b = None
    GetLine()

Hi, @Jacob_Wood

You are not on the wrong track with Eto, but I would change the window type.

Right now you are using a modal dialog:

rc = dialog.ShowModal(Rhino.UI.RhinoEtoApp.MainWindow)

That is the main reason you only get the updated values after closing the window. A modal dialog blocks the script until it closes. The controls inside the dialog can change, but your Grasshopper component will not finish solving and push new output values until ShowModal() returns.

For this kind of “live control panel”, use a modeless Form instead of a Dialog, listen to the ListBox.SelectedIndexChanged event, store the current selection somewhere persistent, and expire the Grasshopper component so it recomputes.

For example, in a GhPython component with inputs x and show, and outputs a and b:

import Rhino
import scriptcontext as sc

import Eto.Drawing as drawing
import Eto.Forms as forms


class SlopeWindow(forms.Form):
    def __init__(self, items, component, state_key, form_key):
        super(SlopeWindow, self).__init__()

        self._component = component
        self._state_key = state_key
        self._form_key = form_key
        self._updating = False

        self.Title = "Controls for slopes"
        self.Padding = drawing.Padding(15)
        self.Resizable = False

        self.m_label = forms.Label()
        self.m_label.Text = "Which curve?"

        self.m_listbox = forms.ListBox()
        self.m_listbox.SelectedIndexChanged += self.OnSelectedIndexChanged

        self.close_button = forms.Button()
        self.close_button.Text = "Close"
        self.close_button.Click += self.OnCloseButtonClick

        layout = forms.DynamicLayout()
        layout.Spacing = drawing.Size(5, 5)
        layout.AddRow(self.m_label, self.m_listbox)
        layout.AddRow(None)
        layout.AddRow(None, self.close_button)

        self.Content = layout
        self.Closed += self.OnClosed

        self.UpdateItems(items)

    def UpdateItems(self, items):
        self._updating = True

        items = list(items or [])
        old_value = self.m_listbox.SelectedValue

        self.m_listbox.DataStore = items

        if old_value in items:
            self.m_listbox.SelectedIndex = items.index(old_value)
        elif items:
            self.m_listbox.SelectedIndex = 0
        else:
            self.m_listbox.SelectedIndex = -1

        self._updating = False
        self.StoreSelection()

    def StoreSelection(self):
        sc.sticky[self._state_key] = (
            self.m_listbox.SelectedValue,
            self.m_listbox.SelectedIndex
        )

    def OnSelectedIndexChanged(self, sender, e):
        if self._updating:
            return

        self.StoreSelection()

        ghdoc = self._component.OnPingDocument()
        if ghdoc:
            ghdoc.ScheduleSolution(
                1,
                lambda doc: self._component.ExpireSolution(False)
            )

    def OnCloseButtonClick(self, sender, e):
        self.Close()

    def OnClosed(self, sender, e):
        if sc.sticky.get(self._form_key) is self:
            del sc.sticky[self._form_key]


component_key = str(ghenv.Component.InstanceGuid)
form_key = component_key + "_slope_window"
state_key = component_key + "_slope_state"

items = list(x or [])

if show:
    form = sc.sticky.get(form_key)

    if form is None or form.IsDisposed:
        form = SlopeWindow(items, ghenv.Component, state_key, form_key)
        form.Owner = Rhino.UI.RhinoEtoApp.MainWindow
        sc.sticky[form_key] = form
        form.Show()
    else:
        form.UpdateItems(items)
else:
    form = sc.sticky.get(form_key)

    if form is not None and not form.IsDisposed:
        form.Close()


a, b = sc.sticky.get(state_key, (None, -1))

The important changes are:

class SlopeWindow(forms.Form):

instead of:

class SlopeWindow(forms.Dialog[bool]):

and:

form.Show()

instead of:

dialog.ShowModal(...)

If all you want is to return one final value after the user presses OK, then a modal dialog is fine. But if you want Grasshopper outputs to update while the window remains open, the modeless form approach is the right one.

Best,
Lucas N.G.