Customize selection in Python (or Rhinoscript)

Hello,

I am trying to find a way to select objects like using rs.GetObjects(). But some objects have already to be selected. Is there a simple way to do so or do have I to use Selection Event or something more complicated?
Because I would add this to an old module written in rhinoscript it would also nice to do it there - but I think thats not possible? Thanks a lot in advance.

Philip

I don’t really know how to do this except in the following general way:

import rhinoscriptsyntax as rs

def AddToSelection(pre_sel):
    #returns preselected plus post selected objects
    #if neither, returns None
    if pre_sel:
        msg="Select any additional objects and press Enter"
    else:
        msg="Select some objects"
    post_sel=rs.GetObjects(msg,preselect=False,select=True)
    if pre_sel:
        rs.SelectObjects(pre_sel)
        if post_sel is None: return pre_sel
    if post_sel: return pre_sel + post_sel


def TestAddToSelection():
    sel=AddToSelection(rs.SelectedObjects())
    if not sel:
        print "Nothing selected."
    else:
        print "{} objects selected".format(len(sel))
TestAddToSelection()

–Mitch

Thanks for your idea. But because that process of selecting and preselecting objects occurs very often in the workflow I still wonder if that can be done more “smooth” and with the same feeling like the rs.getobjects function - so users are not confused. I played around a bit and came up with that:

import rhinoscriptsyntax as rs

def select():    
    selected =rs.SelectedObjects()
    while True:
        curves = rs.GetObjects("Please select curves",4)
        if curves:
            for curve in curves:
                if curve in selected:
                    rs.ObjectColorSource(curve,1)
                    rs.ObjectColor(curve, (0,0,0))
                    selected[selected.index(curve)] = None
                    
                else:
                    selected.append(curve)
                    rs.ObjectColor(curve, (0,255,255))
        else:
            selected  = [item for item in selected if item != None]
            if selected:
                print selected
                [rs.ObjectColorSource(sel, 0) for sel in selected]
                rs.SelectObjects(selected)
            break

select()

But thats still not what I am looking for. I hope that - like almost always in Rhino - some cool rhino common methods exists that makes my dreams come true :smile: . But if you don’t know I am really afraid that there is no solution :frowning:

Have a nice day!

As far as I know, if pre-selecting is allowed, Rhino will always take preselected objects as the complete input and not let you post-pick additional objects to add to the selection. Only if the preselect set is empty does it allow you to post-select.

Postselect only commands will always throw out any preselected objects.

So my workaround as you saw was to simply store the preselect somewhere and add it back in later. Perhaps there is some method I don’t know about, I looked through the GetObject() methods and options in RhinoCommon, but didn’t immediately see anything that would do what you want. But I certainly don’t know everything here… Perhaps @stevebaer can give you a definitive answer.

–Mitch

1 Like

Hi. Does this sample do what you need?

1 Like

Thanks for the example Alain! --Mitch

Alain’s example is exactly how I would approach this issue. Thanks Alain

Thanks a lot for that example. I never before used Rhino.Input.Custom but it seems the way to go. I need just one modification - deselecting objects should also be possible. I will try to change the sample that way.
Have a nice day!

I am using the method from the above example, but I was still not able to figure out, if and (if it is possible) how it could be archived that it is also possible to deselect some of the preselected objects.

` from Rhino import *
from Rhino.Commands import *
from Rhino.Input.Custom import *

def RunCommand():
  go = GetObject()
  go.SetCommandPrompt("Select objects")
  go.EnablePreSelect(True, True)
  go.EnablePostSelect(True)
  go.GetMultiple(0, 0)
  if go.CommandResult() != Result.Success:
    return go.CommandResult()

  selected_objects = []
  for obj in go.Objects():
    selected_objects.Add(obj)

  if go.ObjectsWerePreselected:
    go.EnablePreSelect(False, True)
    go.DeselectAllBeforePostSelect = False
    go.EnableUnselectObjectsOnExit(False)
    go.GetMultiple(0, 0)
    if go.CommandResult() == Result.Success:
      for obj in go.Objects():
        selected_objects.Add(obj)
       obj.Object().Select(True)
  return Result.Success if selected_objects.Count > 0 else Result.Nothing


go.Get Multiple does not return allready selected objects else I could do something like:

if obj in selected_objects:
        selected_objects.remove(obj)
    else:
        selected_objects.append(obj)

Is it possible at all to deselect some of the preselected objects with GetObject()?
Thanks,

Philip