'Sort By' layers in Python


On the GUI, when I right-click on the layer list -> 'Sort By' -> 'Layout index, Layer And Datatype'
this sorts the layer,

I would like to do the same in Python,

Most commands from the right click list are part of the class LayoutView
Is there also a prepared command for it?


  • There is no direct call point for this. But there is some discussion about how to manipulate layer lists in Python safely: https://www.klayout.de/forum/discussion/comment/5919#Comment_5919


  • Hi thanks for the link, here is the code I made for sorting through layer number and datatype:

    def SortLayers(): 
      lv = pya.LayoutView.current()
      to_insert = []
      li = lv.begin_layers()
      while not li.at_end():
      to_insert.sort(key=lambda l: l.source_datatype)
      to_insert.sort(key=lambda l: l.source_layer)
      for l in to_insert:
        lv.insert_layer(lv.end_layers(), l)
Sign In or Register to comment.