Select Objects With Unique Name

Hi,

I´ve tried searching around a bit but I didn´t find anything so I´m here.

Is there a way to select all objects with unique names?

Thank´s

The most obvious answer is to use the SelName command. This presents a list of names and you pick from the list.

This could be scripted if you want to pick an object, then select everything with the same name:

#Purpose: This script will select all objects with the same name.

import rhinoscriptsyntax as rs

def SelectByName():
    objects = rs.GetObjects("Select named objects", 0, True, True)
    if objects is None:
        return
    else:
        for object in objects:
            name = rs.ObjectName(object)
            if name:
                rs.ObjectsByName(name, True)
            else:
                print "The selected object is unnamed."
                rs.UnselectAllObjects()
                return

if __name__ == "__main__":
    SelectByName()

If you want to type in the name and then select everything with that name, you could do something like this:

#Purpose: This script will select all objects that match the typed name.

import rhinoscriptsyntax as rs

def SelectByName():
    name = rs.StringBox("Type the name of the objects you wish to select")
    if name is None:
        return
    else:
        rs.ObjectsByName(name, True)

if __name__ == "__main__":
    SelectByName()

Hope this helps,

Dan

Hi Dan,

first of all thank you for a fast response. I have used the SelName command so far and it worked with a reasonable number of objects. As projects grow however this method became inadequate. The “Unique” parameter is important here:

Let´s say I have a 1000 objects named “a1” to “a998”. I need to select the 996 objects with unique names and put them in one category and then select the 4 remaining objects with duplicite names and put them in another category. I have tried Grasshopper Human but I´m not trained in managing lists and same goes for my scripting (in)abilities…

Hope this explains the problem better

Human and Set will solve the question.


hi @Kral,

Here is a script that should do it:

Option Explicit

Call Main()
Sub Main()

	'get all object names
	If isnull(Rhino.AllObjects()) Then Exit Sub
	Dim arrN : arrN = Rhino.ObjectNames(Rhino.AllObjects())
	Dim i,o
	'change Null for objects with no name to empty string
	For i=0 To Ubound(arrN)
		If isnull(arrN(i))Then arrN(i) = ""
	Next
	'remove duplicates
	arrN = Rhino.CullDuplicateStrings(arrN)
	Call Rhino.UnselectAllObjects()
	Rhino.EnableRedraw False
	For i=0 To Ubound(arrN)
		If arrN(i) <> "" Then
			o = Rhino.ObjectsByName(arrN(i))
			'add to selection if only one with given name:
			If Ubound(o) = 0 Then Call Rhino.SelectObject(o(0))
		End If
	Next
	Rhino.EnableRedraw True
End Sub

Or you can save the script file below on your HD, drag-and-drop to Rhino, it will add an alias: SelUniqueNamedObjects to run this as a command:
SelUniqueNamedObjects.rvb (859 Bytes)

hth,

–jarek

Thank you Jarek this is really it and very convenient as well. I´ve tried replacing “AllObjects” with “NormalObjects” to have more controll but regarding to my scripting proficiency this surprisingly didn´t do the trick. Your script works when I isolate the objects in another file but not when I isolate them by locking/hiding other objects and blocks. Whoul you have any lead to why this might be happening?
Thank you for your time and thank you again for the script as it´s quite usefull with some workarounds.

It will take a bit more than replacing the AllObjects with NormalObjects, since ObjectsByName ignores the state (locked/hidden)… Anyway, how is it supposed to behave with hidden-locked: let’s say you have 3 objects named "A: but two of them are hidden or locked. Should it still selected the3rd “Normal” A or assume “A” is not unique?

ok, here is a version that will give you an option if the Hidden/Locked objects should be taken into account when determining the unique names:

Option Explicit

Call SelUniqueNamedObjects()
Sub SelUniqueNamedObjects()

	Dim blnIgnoreLockedAndHidden
	'setting for ignore or use not Normal objects for the unique name detection
	Dim r : r = Rhino.GetBoolean("Settings:", array("IgnoreLockedAndHidden", "No", "Yes"), array(True))
	If isnull(r) Then Exit Sub
	blnIgnoreLockedAndHidden = r(0)
	
	'get all object names
	If isnull(Rhino.NormalObjects()) Then Exit Sub
	Dim arrN : arrN = Rhino.ObjectNames(Rhino.NormalObjects())
	Dim i,o
	'change Null for objects with no name to empty string
	For i=0 To Ubound(arrN)
		If isnull(arrN(i))Then arrN(i) = ""
	Next
	
	Dim arrUnique : arrUnique = array()
	
	'remove duplicates
	arrN = Rhino.CullDuplicateStrings(arrN)
	Rhino.EnableRedraw False
	For i=0 To Ubound(arrN)
		Call Rhino.UnselectAllObjects()
		If arrN(i) <> "" Then
			o = Rhino.ObjectsByName(arrN(i), True)
			If blnIgnoreLockedAndHidden Then
				'ignore hidden/locked for name determination:
				If Not isnull(Rhino.SelectedObjects()) Then
					If Ubound(Rhino.SelectedObjects()) = 0 Then
						arrUnique = Rhino.JoinArrays(arrUnique, Rhino.SelectedObjects())
					End If
				End If
			Else 'don't ignore hidden/locked
				If Ubound(o) = 0 Then arrUnique = Rhino.JoinArrays(arrUnique, o)
			End If
		End If
	Next
	Call Rhino.SelectObjects(arrUnique)
	Rhino.EnableRedraw True
End Sub

Pure gold.

My only hope is that I´ll learn scripting well enough to be able to help people like you do.

Thanks for all the fish!

Great, glad that it works.
As for the scripting, you are on the right track already trying to modify the given code, that’s the best way!

You are probably better off with Python (what Dan used above) if you are just starting - RhinoScript is great and I learned it before Python was available but Python will give you access to many more functionalities on the advanced level when needed.

Cheers-

–jarek

1 Like

Here’s the (more or less) Python equivalent of Jarek’s script if anyone wants/needs…

SelObjsWUniqueNames.py (1.3 KB)

1 Like

Sorry, I guess I totally missed the point of the original post.

Good morning,
Can someone instruct me how to load and run this script?
If you can provide a video for this, I would be grateful.
I downloaded the script to test it, loaded it with "LoadScript, but it doesn’t appear when I call the command: Runscript.
If you can help me with this information, because in other cases, I’ve tried to use the available scripts and I couldn’t make it work.
Thanks

Python scripts are loaded and run differently from Rhinoscripts.

One relatively simple way:

Store the python script somewhere on your computer

  1. Type _RunPythonScript at the command line, then browse to the script file and open it. The script will run. This is fine for occasional use.

  2. Set up an alias or toolbar button with the following macro:
    ! _-RunPythonScript "full path to script"
    (including filename and .py extension, keep it enclosed in quotes)
    example ! _-RunPythonScript "D\Scripts\mynewscript.py"

More info here: Integrating macros and scripts into your workspace [McNeel Wiki]