Script to automate a list of layers to rename

Hi. We are using rhino to nest parts on stock material. The problem is that we have to manually change layer names before sending them to the CNC cutter. Is there anyway to automate this layer rename? I have a long list for example….Old layer name 1 = New layer name 1. Files coming to us have an established layer name coming in and going out, so this seems perfect for a script, but the examples I have seen are basically manually changing each layer “by hand”. Can anyone point me in the right direction to write this script? Best, Troy

Hi Troy,

I see this had not gotten any reply.
So I tred to come up with a simple first version that might get you going:
layer_renamer.py (835 Bytes)

Let me know if you have any questions as I do not know your skill level, but don’t hesitate to ask.

Note that this version does not check for existing layer names nor are nested layers handled very well.

HTH
-Willem

Below the content of the scriptfile

import rhinoscriptsyntax as rs


#Layer Rename Dictionary
#dictionary where 
#each key represents a current name and their value the new name
lrd = {} 
lrd['Layer 01'] = 'NEW 1'
lrd['Layer 02'] = 'NEW 2'
lrd['Layer 03'] = 'New 4'
lrd['Layer 04'] = 'New 3'
lrd['Layer 04'] = 'Test'


def rename_layers():
    
    for layer_name in rs.LayerNames(sort = True):
        #test if layer_name id in lrd dictionary
        if layer_name in lrd.keys():
            new_name = lrd[layer_name]
            rs.RenameLayer(layer_name, new_name)
            print 'RENAMED: {}->{}'.format(layer_name,new_name)
        else:
            print 'NO CHANGE: {}'.format(layer_name)
    
    
# execute when this script is running 'by itself' 
if __name__ == '__main__':
    rename_layers()

Hi Willem, Thank you!

That works great except that I have old layers that will be renamed to an existing layer and I get a Rhino warning “Layer name in use” Do you have any ideas how to allow for this?

for example:
lrd[‘HOLES DIAM 35.0 D 13.0’] = ‘Pocket 13 mm’ (works)
lrd[‘INSETS D 13.0’] = ‘Pocket 13 mm’ (doesn’t work because new layer in use)

Best, Troy

Let me see if I underatand correct:

If a new layer exists you do not want to rename the layer, but rather move the objects to that alreay existing layer and delete the old layer(that is now empty)?

Exactly!

I bookmarked this topic for Monday. Maybe I find time to build a better version.

1 Like

Hi Troy,

I updated the script to now move objects to existing layer and delete the old layer.

layer_renamer_v2.py (1.2 KB)

import rhinoscriptsyntax as rs


#Layer Rename Dictionary
#dictionary where 
#each key represents a current name and their value the new name
lrd = {} 
lrd['Layer 01'] = 'NEW 1'
lrd['Layer 02'] = 'NEW 2'
lrd['Layer 03'] = 'New 4'
lrd['Layer 04'] = 'New 3'
lrd['Layer 05'] = 'Test'
lrd['Layer 06'] = 'Test'
lrd['Layer 07'] = 'Test'


def rename_layers():
    
    for layer_name in rs.LayerNames(sort = True):
        #test if layer_name id in lrd dictionary
        if layer_name in lrd.keys():
            new_name = lrd[layer_name]
            if rs.IsLayer(new_name):
                layer_objs = rs.ObjectsByLayer(layer_name)
                if layer_objs :
                    rs.ObjectLayer(layer_objs, new_name)
                rs.DeleteLayer(layer_name)
                print 'MOVED OBJS: {}->{}'.format(layer_name,new_name)
            else:
                rs.RenameLayer(layer_name, new_name)
                print 'RENAMED: {}->{}'.format(layer_name,new_name)
        else:
            print 'NO CHANGE: {}'.format(layer_name)
    
    
# execute when this script is running 'by itself' 
if __name__ == '__main__':
    rename_layers()
1 Like

hi.
i realy like your script Willem, and i think that we also can use it for our nesting needs, i am relatively new to phyton, so my question is if it is possible to easaly change this script, so it takes surfaces that are plainer and moves to one layer, surfaces that are rational, and puts in another layer, and surfaces that are round and puts them in another layer.
how can i change this script so it does that?

Hi

It’s not a quick change and I’m limited in time right now.
You would need to

  • take input objects
  • analyse their shape ( planar , rational, etc )
  • move to kayer based on the shape found

Maybe this helps you or others to make a first version…

-Willem

A Thousand Thanks Willem! Worked like a charm. You just unblocked a huge unknown in our design workflow. OK so next level…can new layer colors be part of the renaming?

for example.
old layer name and color> new layer name + new layer color

thank you, Willem.
i have tried to cahnge the script over the weekend, and will keep on trying, but i can’t get it to work.
if you find the time one day, to further help me, i would really appreciate your help,
but thanks anyway.

Hi,

I think this untested edit should work, or at least point you in the right direction:

import rhinoscriptsyntax as rs


#Layer Rename Dictionary
#dictionary where 
#each key represents a current name and their value the new name
lrd = {} 
lrd['Layer 01'] = ( 'NEW 1', [255,0,0])
lrd['Layer 02'] = ( 'NEW 2', [255,110,0])
lrd['Layer 03'] = ( 'New 4', [255,0,80])
lrd['Layer 04'] = ( 'New 3', [0,0,0])
lrd['Layer 05'] = ( 'Test' , [200,10,5])
lrd['Layer 06'] = ( 'Test' , [255,0,0])
lrd['Layer 07'] = ( 'Test' , [255,0,0])


def rename_layers():
    
    for layer_name in rs.LayerNames(sort = True):
        #test if layer_name id in lrd dictionary
        if layer_name in lrd.keys():
            new_name  = lrd[layer_name][0]
            new_color = lrd[layer_name][1]
            if rs.IsLayer(new_name):
                layer_objs = rs.ObjectsByLayer(layer_name)
                if layer_objs :
                    rs.ObjectLayer(layer_objs, new_name)
                rs.DeleteLayer(layer_name)
                print 'MOVED OBJS: {}->{}'.format(layer_name,new_name)
            else:
                rs.RenameLayer(layer_name, new_name)
                print 'RENAMED: {}->{}'.format(layer_name,new_name)
        
            rs.LayerColor(new_name, new_color)

      else:
            print 'NO CHANGE: {}'.format(layer_name)
    
    
# execute when this script is running 'by itself' 
if __name__ == '__main__':
    rename_layers()
1 Like

Hi Willem, Works! line 36 was just missing an indent.

I also found that “layer_renamer_v2.py” also changes the color when new layer and color already exists in the design!