Rhino Python Script for Renaming Layers in multiple files with .XLSX tablesc

Hey all, I am new to Rhino Python and am looking for an easier way to do a painful task in Rhino. I am currently using Rhino 6 (we just upgraded from 5) to design aluminum ships. We need the ability to rename multiple layers (parent or child) to a specific name. I have been using the Python script I found from Steve Baer (FROM HERE)which works great if you only need to rename 1 layer. I have searched for days trying to piece this together and would love for someone to take a look and see if there is a better way. I have close to 500 “OldName” “NewName” instances in about 500 individual .3dm files.

Here is my current working script…

import rhinoscriptsyntax as rs
from scriptcontext import doc

def rename():
    layerName = ("OLDNAME")

    matchingLayers = [layer for layer in doc.Layers if layer.Name == layerName]

    layerToRename = None
    if len(matchingLayers) == 0:
        return
    if len(matchingLayers) == 1:
        layerToRename = matchingLayers[0]
    elif len(matchingLayers) > 1:
        i = 0;
        for layer in matchingLayers:
            print "({0}) {1}".format(
                i+1, matchingLayers[i].FullPath.replace("::", "->"))
            i += 1

        selectedLayer = rs.GetInteger(
            "which layer?", -1, 1, len(matchingLayers))
        if selectedLayer == None:
            return
        layerToRename = matchingLayers[selectedLayer - 1]

    layerName = ("NEWNAME")
    layerToRename.Name = layerName
    layerToRename.CommitChanges()
    return

if __name__ == "__main__":
    rename()

After this is done, I start it over with the next “Old / New” names.

image

I have an excel .xlsx file but can’t figure out how to get it to open, then lookup the correct old name and replace it with the correct new name.

As you can see below, our layer structure

image

Im not sure it is even possible, but would love to be able to run this one time and it opens all files in a folder, runs the script, saves, and opens the next file.

I appreciate any advice you may have,

Thanks.

Emanuel T.

You’re going to have fun on this one. The principal challenge is nested layers. As a nested layer full name is actually ParentLayer::SubLayer::Sublayer... What you need to be careful about is that if you change the name of a parent layer, it changes the name of all of the sublayers below… So if you reference the original sublayer name after that, you will throw an error, as the layer no longer exists in the layer table.

So you generally need to proceed with the lowest level of sublayers first and then work your way up the tree. It’s a bit of a bookkeeping nightmare.

It was very painful to create, but has made the task at hand a little less painful. It has worked flawlessly, which was surprising to me, with over 10,000 lines. Since I added it to a button, i can open each file, select the button to run the script, then save, and open the next file and repeat. Maybe one day a plugin will be created to edit and replace basic object attributes like Layer name, Object name, etc.