How to make layout view active or visible?

I am generating a layout with a Python script. I have a couple questions that I have not been able to sort out.

  1. Is it possible to save the layer properties (.lyp) file via Python?
  2. Does the .lyp file get automatically loaded when KLayout loads a .gds file?
  3. How can I make the layout I've generated active in the Editor automatically? My script runs and creates a GDS file without any issues - I'd like to also have it visible in the Editor once I'm done.

This is the beginining of my code:

    import pya

    def SetupLayer(num, dt, name):
        if pya.Application.instance().main_window().current_view() is not None:
            li = pya.Application.instance().main_window().current_view().begin_layers()
            while not li.at_end():
                lp = li.current()
                if ((lp.source_layer == num) and (lp.source_datatype == dt)):
           = name
                    lp.visible = True
                lp =

    L = pya.Layout()
    L.visible = True
    TOP = L.create_cell("TOP")

    PartOutline = L.layer(100, 0)
    SetupLayer(100, 0, "PartOutline")
    ScribeOutline = L.layer(200, 0)
    SetupLayer(200, 0, "ScribeOutline")
    Pads = L.layer(300, 0)
    SetupLayer(300, 0, "Pads")
    Pin_Number = L.layer(400, 0)
    SetupLayer(400, 0, "Pin_Number")
    Functional_Signal = L.layer(500, 0)
    SetupLayer(500, 0, "Functional_Signal")
    Net_Name = L.layer(600, 0)
    SetupLayer(600, 0, "Net_Name")

If I run the same code with a blank layout window created, it seems to run the block of code where I set the layer name but as near as I can tell, it is not the correct layout view that it is operating against. I can't seem to make the layout view I have created visible.



  • Hi Mike,

    The core idea is to work with layout views rather than layout objects (pya.Layout). Layout objects are standalone and not connected with the views.

    1. To create a layout within a view use
    view = pya.LayoutView.current()
    L =
    1. To save the layer properties use

    Your SetupLayer should then use "view" to set the layer properties.



  • Thanks for looking at my problem. I've made some progress but it seems like I've taken one step forward and two steps back. Now my GDS loads and the layer properties save correctly but now I don't have any shapes in my layout. I am expecting a polygon on 100/0 but I have no layers at all.

    import pya
    view = pya.LayoutView.current()
    lp = pya.LayerProperties()
    lp.visible = True
    L =
    TOP = L.create_cell("TOP")
    li = pya.LayerInfo(100, 0, "PartOutline")
    lp.source_layer = 100
    lp.datatype = 0 = "PartOutline"
    lp.source_name = "PartOutline"
    view.insert_layer(view.end_layers(), lp)
    po = []
    po.append(pya.Point(0, 0))
    po.append(pya.Point(720990, 0))
    po.append(pya.Point(720990, 723456))
    po.append(pya.Point(0, 723456))
    opt = pya.SaveLayoutOptions()
    view.load_layout("test.gds", 0)

    I am clearly struggling with comprhending the layout vs view models and I am sure my mistake is probably pretty simple but I can't see it. ;-)

    What am I missing?

  • I've made some further progress this evening. In fact, for the most part what I am trying to do is working. The only issue I am running into now is when I try to run my script as batch script from the command line it doesn't generate a GDS file. If I run the same script from the Macro Development GUI it runs to completion and I end up with the GDS file.

    This is my current solution:

    import pya
    class GDSLayer:
        li = None
        lp = None
        lyr = None
        def __init__(self, num, dt, name):
            self.num = num
            self.dt = dt
   = name
        def Add(self, layout):
            self.lyr = layout.layer(self.num, self.dt)
        def View(self, view):
            if view is not None:
       = view.begin_layers()
                while not
                    self.lp =
                    if ((self.lp.source_layer == self.num) and (self.lp.source_datatype == self.dt)):
                        self.lp.visible = True
                    self.lp =
    L = pya.Layout()
    TOP = L.create_cell("TOP")
    Part_Outline = GDSLayer(100, 0, "Part_Outline")
    po = []
    po.append(pya.Point(0, 0))
    po.append(pya.Point(2700000, 0))
    po.append(pya.Point(2700000, 2700000))
    po.append(pya.Point(0, 2700000))
    if pya.MainWindow.instance() is not None:
        view = pya.LayoutView.current()
        view.load_layout("t.gds", 0)

    I am getting close. I don't see anything obvious as to why this wouldn't run in -b mode.

  • There are different batch modes available. Some have a main window, some don't. "klayout -b" will enter "bare metal" batch mode where there is no main window and "pya.MainWindow.instance()" will be None.

    I tried your script in this mode:

    klayout -z -r

    and for me I got "t.gds" and "t.lyp" files. You can also add "-nc -rx" which avoids side effects such as updating the configuration file.


  • Thanks, I can replicate the results using the same switches so that works for me!

    Last question (I hope) - can I do the same on Windows? When I use the same command I don't see any results and "klayout_app -h" on the Windows command line doesn't return anything like it does on Linux.

  • KLayout isn't compiled as a console application on Windows (as then an ugly command terminal pops up), so it won't print anything to the console.

    But if you use a bash from cygwin or MSYS it will do. Also redirection to a file should work.


  • Hi all,
    I am trying to use klayout python module.
    First thing I would like to load a dxf file and visualize it from a python script.
    I have tried with the following code (inspired by this thread):

    import pya

    Unfortunately it breaks at the second line with following error message:

    AttributeError: module 'pya' has no attribute 'MainWindow'

Sign In or Register to comment.