Iterating through layers with particular name

I need to iterate all shapes on layers with particular name (or name pattern). Currently I do this in such way:

for layer_index in range(layout.layers()):
   layer_info = layout.get_info(layer_index)
   if == layer_name:
     # Do something

but is empty. There is but there is no method(s) in LayoutView to find layer properties by layer/datatype available from LayerInfo.

My question is: are and supposed to be same? If so, probably there is bug in implementation of (I tried 0.25.4 and 0.26.4). If not, it'll be reasonable to have find methods in LayoutView for LayerProperties like Layout has for LayerInfo.


  • Hi Eugene,

    I need to explain the concept of "database layers" and the "layer views" in the layer list.

    The database (the object you see as "layout") is a representation of your layout file. It lives outside the graphical user interface. It does not know what layers you draw and how you draw them or how you named them in the layer list.

    The layer list is a kind of instruction set how to draw layers: which color, style, animation and so on. Plus it gives a layer a "display identity". This is what you specify when you use "rename" in the layer list.

    The database also has a layer name, but this is usually empty. GDS files do not supply layer names, only numbers. Other formats like DXF or CIF only supply names. You can assign layer names in the database using "Edit/Layers/Edit Layer Specification" or by specifying a layer mapping file in the reader options. Once a layer is given in the database, it becomes visible in the "name" attribute of "layer_info".

    I assume you just renamed the layer in the layer list. That will not change the database layer specification. It will just tell KLayout what you want to read instead of "117/0" (just an example).

    You can iterate the display layers this way:

    lv = pya.LayoutView.current()
    # utilizes the "layer iterator" to traverse all 
    # leaf nodes of the layer tree
    li = lv.begin_layers()
    while not li.at_end():
      node = li.current()
      # this is the layout object
      ly = lv.cellview(node.cellview())
      # this is the layer index
      layer = node.layer_index()
      # this is the name you have given in the layer list
      # NOTE: it is empty if the layer was not renamed
      name =
      # important: advance to the next layer

    The iteration is complicated by the fact that the layer list can be a tree and there may be multiple layouts loaded (what you see as "@1", "@2" etc.). The former problem is solved by utilizing a depth-first iteration (with the layer iterator). The latter by fetching the layout object indicated by the "cellview index".


  • Hi, Matthias!

    Thank you for explanations! Actually I solved my problem in similar way :-)

    Part of my question was to simplify list iteration search with KLayout API, of course, in case if KLayout keep internal indexes for layer view as map/unordered map.

Sign In or Register to comment.