Rhinoscript/Python script that select objects based on object.ID property

Hello everyone,

I am trying to make a script that will select multiple objects based on object ID. It is similar to the “SelId command” which already is in Rhino, but I want it to be able to input a list of object ID’s I want it to select.

To achieve this I first tried to recreate the SelId command however I get stuck there.
What I have is this (it is very basic):

Dim arrObjects, strId, objId

strId = Rhino.GetString("Enter object ID")

If Not IsNull(strId) Then
	objId = Rhino.GetObject(strId)
	If Not IsNull(objId) Then
		Rhino.SelectObject objId
	Else
		Rhino.Print "Object not found with that ID."
	End If
End If

However if I run it and input the object ID string, it results in “unkown command”.
I noticed that in the list of rhinoscript commands it is possible to use many different selection tools but object ID is not noted there.
But Rhino has this functionality because there is a button already for it. If I know what the code is behind that button I could create my own script that can select multiple objects based on an array of ID’s.

Can someone help me with this?

Many thanks in advance.

Best regards,

Bastiaan van Oudheusden
Naval Architect at NG Shipyards

Dim arrObjects, strId, objId

strId = Rhino.GetString("Enter object ID")

If Not IsNull(strId) Then
		Rhino.SelectObject objId
	Else
		Rhino.Print "Object not found with that ID."
	End If
End If

I think the above should work…

EDIT:
Nope, wasn’t in front of my computer…

In principle, this should work but doesn’t:

Option Explicit

Dim strId
strId = Rhino.GetString("Enter object ID")

If Not IsNull(strId) Then
	Rhino.SelectObject strId
Else
	Rhino.Print "Object not found with that ID."
End If

However, if I paste an object ID that I know exists into the command line, I get:
Unknown command: 8c590bb2-c08a-4db6-a4dc-6e702fe8fab3

This even though the ID is a string and SelectObject wants a string.

Paradoxically this does work:

Option Explicit

Dim strId

strId = Rhino.Stringbox("Enter object ID")
If Not IsNull(strId) Then
	Rhino.SelectObject strId
Else
	Rhino.Print "Object not found with that ID."
End If

So my conclusion is there is some kind of inconsistency in the way the command line interprets a pasted string with GetString(). It probably will work if the entire GUID was hand-typed in the command line.

Hi Mitch, hm it works here, even if my pasted Id has an extra space at start or end.

_
c.

Hi Clement,

Are you using that exact code that is shown above? Because I get the same error as Mitch does. I also tried typing out the number and I still got the same error.

Seems strange to me, maybe you have some settings different that makes this work?

Hi Mitch,

Yes I got the same error. Even when typing out the entire ID code.
End of yesterday I got this code in python to work, it is a very rough sketch but it was to test the principle that I could select multiple ID objects. Here I put the ID’s directly in the code for simplification.

import rhinoscriptsyntax as rs

id_list = [‘ec10904-1345-4dec-835c-eb339c5ed05a’, ‘480ca52f-53cf-40fc-a832-5419d3862e74’, ‘8c516771-cf95-46c3-840d-164a461c6c61’] # replace with your desired ID numbers
obj_list = [ ]

for id in id_list:
obj_id = rs.coerceguid(id)
if obj_id: # check if GUID was successfully coerced
rs.UnselectAllObjects()
rs.SelectObject(obj_id)
obj = rs.SelectedObjects()
if obj: # check if object was successfully selected
obj_list.append(obj)

rs.SelectObjects(obj_list)

Your code also works for me. I will see which one I will expand upon to create what I want.
Because what I’m trying to do is input a whole list of ID codes which will be selected by the script.

Anyway, mane thanks for your input!

Here is a sample scriptlet that will read a list of object ids from a file and select the objects…

import rhinoscriptsyntax as rs

def ReadIdsFromFileAndSelect():
    filter = "Text Files (*.txt)|*.txt||"
    filepath=rs.OpenFileName("File with object ID's to open?",filter)
    if not filepath: return
    #open file to read, import as a list of lines in file
    with open(filepath, "r") as file:
        line_list=file.readlines()
    to_select=[]
    #make sure only guids are in the list of objects to select
    #and that the objects that have these guids exist in file
    for item in line_list:
        obj_id=rs.coerceguid(item)
        if obj_id and rs.IsObject(obj_id):
            to_select.append(obj_id)
    #select objects - note some objects might be locked/hidden and not select
    count="No"
    if to_select:
        count=rs.SelectObjects(to_select)
    print "{} objects selected".format(count)
ReadIdsFromFileAndSelect()
1 Like

Hi @Bastiaan_van_Oudheus, yes. It also works if i have a space at the end of the Id:

Are you running it from the editor as i do or from a button ?

_
c.