This works similar as the first example, you just need to get the parent layer from the object’s layer:
import Rhino
import scriptcontext
import rhinoscriptsyntax as rs
import System
def SelectObjectParentLayer():
obj_id = rs.GetObject("Select Object", False, False)
if not obj_id: return
rh_obj = rs.coercerhinoobject(obj_id, True, True)
layer = scriptcontext.doc.Layers[rh_obj.Attributes.LayerIndex]
parent_layer_id = layer.ParentLayerId
if parent_layer_id != System.Guid.Empty:
parent_layer = scriptcontext.doc.Layers.FindId(parent_layer_id)
scriptcontext.doc.Layers.Select([parent_layer.LayerIndex], True)
else:
print "{} has no parent layer".format(layer.FullPath)
SelectObjectParentLayer()
It will print an error if the current object’s layer has no parent layer.
You can find layer visibility in the layer properties, eg. layer.Visible(False) would hide it. Note that you cannot hide a layer if it is the current layer. To find out if a layer is current or not, compare it’s layer index with the index returned by scriptcontext.doc.Layers.CurrentLayerIndex
Same goes for the collapsed/expanded state of a layer, eg. use layer.IsExpanded(False) to collapse it’s sub layers.
I was referring to layers, Rhino 8 WIP provides a search bar, but it’s not “fuzzy” (“key plan”, won’t find “key-plan”), it’s also impossible to assign a shortcut to it. Being able to search and “jump to”/highlight a layer in the dialog (without hiding the rest) would be a massive productivity boost.
You might try to use wildcards when entering text in the searchbar, eg. when entering “key*plan” it should find and highlight “key plan” and “key-plan”. I have not tested this in Rhino 8 WIP but i guess these default filtering rules should be working:
* = match zero or more characters
? = exactly one character
# = exactly one numeric (0-9) character
& = exactly one alpha (a-z, A-Z) character
They are located in the help under Filters. You could do the same via code by iterating all layers and comparing layer.Name with a regular expression.
No, space has a different meaning in that context. Note that when using regular expressions there are different rules. In case of your example, you could use this:
import Rhino
import scriptcontext
import rhinoscriptsyntax as rs
import re
def DoSomething():
expression = re.compile("key.plan")
indices = set()
for layer in scriptcontext.doc.Layers:
if expression.findall(layer.Name):
indices.add(layer.LayerIndex)
if indices:
scriptcontext.doc.Layers.Select(indices, True)
DoSomething()
This will highlight “key-plan” and “key plan” or “key1plan” but not “Key-Plan”, so it is case sensitive…
Message: expected string for parameter 'text' but got 'NoneType'
Traceback:
line 12, in DoSomething, "C:\Users\daniel.krajnik\AppData\Local\Temp\TempScript.py"
line 18, in <module>, "C:\Users\daniel.krajnik\AppData\Local\Temp\TempScript.py"
I’ve tried a few regexes (regices/regex expressions) and the same error popped out each time.
I will have a look at it soon (it’s 10pm in Europe now), but it would be amazing to have it work.
@clement thanks for checking, this problem occurred only as of December 2022 - I haven’t tried it since then. Glad to hear that it’s fixed now and exciting to hear that a native _HighlightObjectLayers to do that was added.
Hi! Thank you for this share. Before I try it, how would you advise to change it so that when you run the script it selects all the objects on the layers of the current selection?!
So if I have an object A on layer ONE and object B on layer TWO selected, then when I run the script it would select all objects on layer (and maybe sublayers) ONE and TWO.
Honestly Rhino 8 I believe solved half of this problem but I still can;t find a native command to select all objects on a layer and sublayer, which also I can’t begin to understand why,