How to take the default command line option as the chosen option?

I’m looking at the python script on this page which I literally just copied here since it shows what I’m wondering about. https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Input_Custom_GetBaseClass_AddOptionList_1.htm

So it asks for an object and then there is an option in the commandline to change the display mode for that object. When I click on that option in the menu and choose the display mode all works fine.

The problem is:
image
I would assume, based on how rhino commands work, that if I press enter here that it chooses the “Wireframe” default option. But instead, it now just exits the script without changing the display mode, as if you would press esc. How do I take the default input from this option when pressing enter and still cancel when pressing esc instead?

import Rhino
import scriptcontext

def ObjectDisplayMode():
    filter = Rhino.DocObjects.ObjectType.Brep | Rhino.DocObjects.ObjectType.Mesh
    rc, objref = Rhino.Input.RhinoGet.GetOneObject("Select mesh or surface", True, filter)
    if rc != Rhino.Commands.Result.Success: return rc;
    viewportId = scriptcontext.doc.Views.ActiveView.ActiveViewportID

    attr = objref.Object().Attributes
    if attr.HasDisplayModeOverride(viewportId):
        print "Removing display mode override from object"
        attr.RemoveDisplayModeOverride(viewportId)
    else:
        modes = Rhino.Display.DisplayModeDescription.GetDisplayModes()
        mode = None
        if len(modes) == 1:
            mode = modes[0]
        else:
            go = Rhino.Input.Custom.GetOption()
            go.SetCommandPrompt("Select display mode")
            str_modes = []
            for m in modes:
                s = m.EnglishName.replace(" ","").replace("-","")
                str_modes.append(s)
            go.AddOptionList("DisplayMode", str_modes, 0)
            if go.Get()==Rhino.Input.GetResult.Option:
                mode = modes[go.Option().CurrentListOptionIndex]
        if not mode: return Rhino.Commands.Result.Cancel
        attr.SetDisplayModeOverride(mode, viewportId)
    scriptcontext.doc.Objects.ModifyAttributes(objref, attr, False)
    scriptcontext.doc.Views.Redraw()


if __name__=="__main__":
    ObjectDisplayMode()

Hi Siemen,
this sample is bad style since it does not set the display mode to the displayed option value.
Actually it removes any display mode override if no option has been selected.
You could fix that (and emulate the behavior of the command _SetObjectDisplayMode) by adding the default option Mode=UseView.
Jess

Check if the attached helps a bit…SetObjectDisplayMode.py (1.5 KB)

Jess

1 Like

This seems to work when I test it quickly, thanks! I’m gonna go through this later to see if I fully understand what you did there.

Thanks a lot @Jess! I see AcceptNothing was what I was looking for.