Renaming layers in Python script

Sorry for newbie question. I need to rename cell layers based on external map file, so it should do what Layers | Rename command does but for all layers mentioned in map file.

Code iterate through current_view.each_layer(). However, name property is empty string and source always contain suffix that doesn't appear in GUI. I tried to assign values to name but this didn't affect values displayed in GUI. Assigning value to source duplicate layers.

Most likely I didn't mistakes in my assumptions about KLayout API, but what is right way to implement layer renaming?


  • Just a comment: Do you really need to program this? If programming is not necessary, you can consider using the user interface with "File > Reader Options" and a layer mapping description.

  • edited June 2019

    A sample code of my suggestion, from what I know, until now, regarding this tool's API:

    layout  = pya.CellView().active().layout()
    myLayer = pya.LayerInfo( 23, 5, 'myLayer')
    print layout.layer_infos()
    print layout.layer_indexes()
    for i, layer in enumerate(layout.layer_infos()) :
      new_name  = 'myNewLayer'
      layer_num = layer.layer
      layer_dt  = layer.datatype
      #- Normally I would suggest directly changing the name of the layout by this method option:
      #- Although, it seems that is disabled. = 'myNewLayer'
      #- Next, by reading Layout documentation, I would suggest the following method, ..
      #- .. which it says that replaces an index with new layer properties, but ..
      #- .. for me it fails with an error message. This is a snap of it:
      #- Internal error: ..../src/db/db/ m_layer_states [index] == Free was not true in Layout.insert_layer_at
      #layout.insert_layer_at(i, new_layer)
      #- As -a bad- workaround you could delete the layer and create a new one with the same properties and different name, ..
      #- .. which it worked for me, but in my opinion is buggy programming...
      new_layer = pya.LayerInfo( layer_num, layer_dt, new_name)
    # for
    print layout.layer_infos()
    print layout.layer_indexes()
  • @ocasta: Than you for suggestion! But in my case layer map file is Calibre CCI GDS map and it's not listed in "File > Reader Options".

  • edited June 2019

    @crizos: Thank you for help! I tried all your three suggestions, but they didn't work :-(
    1. change works in Python (like print), but GUI was not updated;
    2. Failed same way as you wrote;
    3. names remained same in GUI but changed in layout.layer_infos(), but layout.delete_layer(i) removed all layer content.

  • I found working solution for my problem:

    app = pya.Application.instance()
    current_view = app.main_window().current_view()
    new_layers = []
    for layer in current_view.each_layer():
         new_layer = pya.LayerPropertiesNode()
         new_layer = layer = <new name>
    for layer in new_layers:
         current_view.insert_layer(current_view.end_layers(), layer)
  • Hi EugeneZelenko,

    yes, that solution should work.

    I'm a bit puzzled, simply using "" should do the job. I'll check that.

    I have created a ticket for this:


  • Just in case, I used version 0.25.4.

  • Hi Eugene,

    I fixed this issue now in the master and I'm about to release a 0.25.9 version which will contain this fix among some others.

    It was an issue related to a wrong C++ signature used in the Python binding. Thanks for bringing this up.



  • Hi, Matthias!

    Thank you for quick fix!

  • Layer Name Prefix is the name you want to name the layer with.
    delay(ms) is number of milliseconds to delay each frame for
    Add '(replace)' if YES will add the string '(replace)' to very end of frame/layer name
    Only Apply to visible layers, if YES will only apply/rename to visible layers

    Hope above information will help you. For more you can also check this link:

  • Hi, Mattias!

    I verified fix in your release build and it works fine on my side with original script (assigning name property).

Sign In or Register to comment.