Python script to 'Select Sublayer Objects'

I am trying to script the ‘Select Sublayer Objects’ command from Layers but so far I don’t even know where to start looking for this command. Is there someone willing to provide slight guidance or a head start on how I could approach this?!

you may iterate through the layer table and find the layer you want.
each layer object will have methods to get parents/children if only a certain sub layer can be selected
then you can do the find by layer on the object table to get all the objects of that layer

1 Like

Hi @aaa1,

below is a quick example which tries to select objects on sub-layers of a layer named “Default”:

import scriptcontext
import rhinoscriptsyntax as rs

def SelectSublayerObjects(parent_layer_path):
    index = scriptcontext.doc.Layers.FindByFullPath(parent_layer_path, -1)
    if index < 0: return
    sub_layers = scriptcontext.doc.Layers[index].GetChildren()
    if not sub_layers: return
    sub_layer_objs = []
    for sub_layer in sub_layers: 
        rh_objs = scriptcontext.doc.Objects.FindByLayer(sub_layer)
        if rh_objs: 

# call the function    

if your parent layer is itself a sublayer of another layer, you can define it like this:


btw. to only select on a single sublayer, you can use rhinoscriptsyntax which is much shorter eg:

rs.ObjectsByLayer("Default::MySublayerName", select=True)



Sorry for the confusion. ‘Select Sublayer Objects’ actually selects the objects on the current highlighted layer, and the layers below it if it a parent itself. So the actual request, rephrased for added clarity, should have been: how to select objects on the current highlighted layer.
If it’s not too much to point me in that direction, otherwise (and also) thanks for the script, I will see if I can modify somehow.

Hi @aaa1, below selects all selectable objects on layers which are selected in the layer panel:

import scriptcontext
import rhinoscriptsyntax as rs

def SelectObjectsOnSelectedLayers():
    rc, indices = scriptcontext.doc.Layers.GetSelected()
    if not rc: return
    rh_objs = []
    for index in indices:
        layer = scriptcontext.doc.Layers[index]
        layer_objs = scriptcontext.doc.Objects.FindByLayer(layer)
        if layer_objs: