Get layer paths/names of only selected layers (Python)

Is there a way to get the layer names of the active selected layers within rhino and store them in an array?

I’m trying to batch rename layers, but having to either iterate over a list of ALL layer names within my file or select one through dialogue is pretty painful. Is there a way to just grab the active selection and then use those? Please see the attached image as an example of the specific layers I want to grab.

I can’t check whether the layer contains a certain string either as I’m renaming my sub-layers which share the same name as other sublayers under different parents.Selection

something like this maybe?

import Rhino as r
import random

selectedLayers = r.RhinoDoc.ActiveDoc.Layers.GetSelected() # get the currently selected layers
counter = random.randint(10,25) # some random int to use for layer naming
for l in selectedLayers[1]: # array of indices of selected layers
    lname = r.RhinoDoc.ActiveDoc.Layers.FindIndex(l) # use index to find layername
    print lname.Name #layername property
    if lname.GetChildren() != None: #check if it has child layers
        for childName in lname.GetChildren(): #for each child layer in returned array
            print childName # do something with childlayer name
    lname.Name = "Bay " + counter.ToString() #change selected parent layer name
    counter +=1 # increase random number by 1
    lname.CommitChanges() # commit changed to document

Hi Chris,

Thanks so much! The first line was super helpful in getting what I wanted.

Here’s my finished script for reference to increment the ‘Bay’ indexes by a single digit.

import Rhino as r
import random


def RenameSelectedLayers():
    # get the currently selected layers
    selectedLayers = r.RhinoDoc.ActiveDoc.Layers.GetSelected()

    # array of indices of selected layers
    for layer in reversed(selectedLayers[1]):
        # use index to find layername
        layerName = r.RhinoDoc.ActiveDoc.Layers.FindIndex(layer)

        # check layer name has a space before attempting to split and increment
        if "Bay " in layerName.Name:
            # remove "Bay " from layer name so we're just left with the str "001"
            numberIndex = layerName.Name[4:]
            # increment number index
            numberIndexInt = int(numberIndex) + 1
            # declare str variable to use for final layer name
            newNumberIndexStr = str(numberIndexInt)

            # check length of layerNumber before appending
            if not len(newNumberIndexStr) == 0:
                if len(newNumberIndexStr) == 1:
                    # Add "0" to number index "01"
                    newNumberIndexStr = "00" + numberIndexInt.ToString()

                if len(newNumberIndexStr) == 2:
                    # Add "0" to number index "01"
                    newNumberIndexStr = "0" + numberIndexInt.ToString()

                # change selected parent layer name
                layerName.Name = "Bay " + newNumberIndexStr

    # commit changed to document
    layerName.CommitChanges()


RenameSelectedLayers()

Looks good - thanks for sharing!
You can simplify your number naming section (the if statements above) to just:
newNumberIndexStr = numberIndexInt.ToString().zfill(3)
The zfill() method adds leading zeros if needed

1 Like

cool! Glad you got what you needed!

1 Like

Just saw this, sorry Graham. Thanks, that’s a really useful tip!

You’re very welcome
A few more methods here :wink:
https://dancergraham.github.io/blog/any-way-the-wind-blows/

1 Like

@Dancergraham, that’s nice. But note that dir is a reserved keyword.

_
c.

1 Like

Thanks for taking the time to look @clement -i’ll Fix it