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.
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
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