Get multiple objects plus list option

I’m missing something essential here - I tried a bunch of ways and I’m not getting it…

I want to be able to select several objects but also have a list toggle on the command line. As soon as I change the option, it finishes. There’s supposed to be a loop in there somewhere that I can break out of but I can’t figure it out… :frowning: TIA, --Mitch

import Rhino
def TestGetObjects_OptionList():
    go = Rhino.Input.Custom.GetObject()
    go.SetCommandPrompt("Select objects")

    listValues = ["DoThis", "DoThat", "DoNothing"]
    opList = go.AddOptionList("List", listValues, 2)
    go.GetMultiple(1,0)
    if go.CommandResult()!=Rhino.Commands.Result.Success:
        return go.CommandResult()
    ids = [go.Object(i).ObjectId for i in range(go.ObjectCount)]
    listIndex = go.Option().CurrentListOptionIndex
          
    print ids
    print "Chosen option = {}".format(listIndex)

TestGetObjects_OptionList()

How does the code below work? I’m no Python expert and have not tested this, but the same control flow works for me in C#. Note that with an existing selection prior to running the the command, it will complete immediately. Use go.EnablePreselect(False) to prevent this.

import Rhino
def TestGetObjects_OptionList():
    go = Rhino.Input.Custom.GetObject()
    go.SetCommandPrompt("Select objects")

    listValues = ["DoThis", "DoThat", "DoNothing"]
    opList = go.AddOptionList("List", listValues, 2)
    while True:
    	getResult = go.GetMultiple(1,0)
    	if getResult == Rhino.Input.Cancel:
    	    return Rhino.Commands.Result.Cancel
    	elif getResult == Rhino.Input.Option:
    	    listIndex = go.Option().CurrentListOptionIndex
	    print "Chosen option = {}".format(listIndex)
	elif getResult == Rhino.Input.Object:
	    ids = [go.Object(i).ObjectId for i in range(go.ObjectCount)]		
            break #break from while loop after objects have been selected
	
TestGetObjects_OptionList()

I think adding the EnablePreSelect(False) like menno suggests plus a couple of small tweaks does what you want:

def TestGetObjects_OptionList():
    go = Rhino.Input.Custom.GetObject()
    go.SetCommandPrompt("Select objects")
    go.EnableClearObjectsOnEntry(False);
    go.EnableUnselectObjectsOnExit(False);
    go.DeselectAllBeforePostSelect = False;

    listValues = ["DoThis", "DoThat", "DoNothing"]
    opList = go.AddOptionList("List", listValues, 2)
    while True:
    	getResult = go.GetMultiple(1,0)
    	if getResult == Rhino.Input.GetResult.Cancel:
    	    return Rhino.Commands.Result.Cancel
    	elif getResult == Rhino.Input.GetResult.Option:
    	    go.EnablePreSelect(False, True)
    	    listIndex = go.Option().CurrentListOptionIndex
	    print "Chosen option = {}".format(listIndex)
	elif getResult == Rhino.Input.GetResult.Object:
	    ids = [go.Object(i).ObjectId for i in range(go.ObjectCount)]		
            break #break from while loop after objects have been selected
    print [str(o.ObjectId) for o in go.Objects()]

As explained in this sample

@menno, @Alain Thanks guys!!! --Mitch