Returning object outside class with Modeless Forms

Hi, I am trying to create a modeless form that allows picking some objects and returns them outside the form class.

I manage to output my selection as a label in the form for example, but I cannot retrieve it outside the class to use it further in the script.

I tried using OnPushPickButton method, but that just crashes the modeless form.

Here is the code:

import Eto.Forms as forms
import Eto.Drawing as drawing
import Rhino.Input 
import Rhino.Commands
import Rhino 
import rhinoscriptsyntax as rs
import scriptcontext as sc

Objects = []
class MyForm(forms.Form):
    def __init__(self):
        self.Title = "Return Object"
        self.ClientSize = drawing.Size(200, 200)
        self.Padding = drawing.Padding(5)
        self.Resizable = True
        
        button = forms.Button()
        button.Text = "Pick Brep"
        button.Click += self.OnSelectClick
        
        self.label = forms.Label()
        self.label.Text = "Nothing selected yet"
        
        layout = forms.DynamicLayout()
        layout.AddRow(button)
        layout.AddRow(self.label)
        self.Content = layout
        
    def ReturnObj(self,obj):
        Objects.append(obj)
    def OnSelectClick(self, s, e):
        self.Minimize()
        try: 
            r, oref = Rhino.Input .RhinoGet.GetMultipleObjects("Pick brep", True, Rhino.DocObjects.ObjectType.Brep)
            if r == Rhino.Commands.Result.Success:
                self.label.Text = str(oref)
            self.ReturnObj(oref)
        except Exception as e: self.label.Text = str(e)
        
        self.BringToFront()
        
myform = MyForm()
myform.Owner = Rhino.UI.RhinoEtoApp.MainWindow
myform.Show()

Thanks!

Hi,

You could try

  1. a global variable for storing data throughout your script’s runtime.
  2. Add the object data to the document sticky for session-based memory.
  3. Use documentusertext to persistently store data across sessions.

Hope this helps,
Farouk

Hi @CosminP,

This seems to work in Rhino 7 and Rhino 8 (Python2).

test_pushpickbutton.py (1.7 KB)

Does this help?

– Dale

1 Like

Hey @dale, thanks for the quick reply!

Unfortunately this definition creates a dialog that is Modal, I want to create a Modeless From. Using PushPickButton crashes the Modeless form for me (even when trying to hide the form), and so far I did not find other method that allows me to return the selected object outside the class and pass it further in the script. Is this even something that makes sense using Eto?

I am attaching the code with Modeless Form + PushPickButton that I tried and crashed:

import Eto.Forms as forms
import Eto.Drawing as drawing
import Rhino.Input 
import Rhino.Commands
import Rhino 
import rhinoscriptsyntax as rs
import scriptcontext as sc


objs = []
class MyForm(forms.Form):
    def __init__(self):
        self.Title = "Return Object"
        self.ClientSize = drawing.Size(200, 200)
        self.Padding = drawing.Padding(5)
        
        button = forms.Button()
        button.Text = "Pick Brep"
        button.Click += self.OnPushPickButton
        
        self.Content = button
        
    def ReturnObj(self,obj):
        Objects.append(obj)
        
    def OnSelectClick(self, s, e):
        try: 
            rc, oref = Rhino.Input .RhinoGet.GetMultipleObjects("Pick breps", True, rh.DocObjects.ObjectType.Brep)
            if rc != Rhino.Commands.Result.Success: 
                return
            self.label.Text = str(oref)
            self.ReturnObjects(oref)
        except Exception as e: self.label.Text = str(e)
        
    def OnPushPickButton(self, sender, e):
        self.Minimize()
        Rhino.UI.EtoExtensions.PushPickButton(self, self.OnSelectClick)
        self.BringToFront()
        
def Test():
    myform = MyForm()
    myform.Owner = Rhino.UI.RhinoEtoApp.MainWindow
    myform.Show()

if __name__ == "__main__":
    Test()



Thanks again!

Hey @farouk.serragedine, thanks for the suggestion!

I tried using globals and sticky, but without success. I think it has to do with the fact that I work with modeless forms and I cannot implement PickPushButton without crashing Rhino

Hey @CosminP,

If your form is modeless, you don’t need to use PushPickButton. Modeless forms don’t block the main Rhino window. So just start an object getter.

The reason for PushPickButton is that when in a model form is visible, the main Rhino window is disabled - you cannot click outside of the form. PushPickButton temporarily hides the model form and enables the main Rhino windows allowing you do pick objects.

Make sense?

I’ve attached a sample modeless form for you to review

test_modeless_form.py (2.1 KB)

– Dale