Problem with saving screenshots while changing layers/groups visibility

Hi, Matthias!

I tried to make multiple screenshots after changing layers/group visibility in script. Looks like LayoutView.update_content do not update view properly when group was not visible.

First screenshot taken for layer from visible group (Nested 1) is fine, but second, for layer from invisible group (Nested 2), is empty.

Script is:

import pya

main_window = pya.Application.instance().main_window()
current_view = main_window.current_view()
for index in range(1, 3):
    suffix = f'{index}'
    for layer_properties in current_view.each_layer():
        layer_properties.visible =
        print(, layer_properties.visible)
    current_view.get_image(800, 600).save(f'Screenshot.{index}.png')

It was run with KLayout 0.28.11 with attached session/layout: klayout -u Session.txt -r


  • Hi EugeneZelenko

    Add a process_events before get_image should able to solved this issue

        current_view.get_image(800, 600).save(f'Screenshot.{index}.png')
  • Hi!

    Thank your for help! It works!

    But will be good idea to reflect this in documentation (LayoutView.update_content?).

  • Dear all,

    Wasn't that an issue before somewhere?

    "process_events" is required as it triggers the repaint event that actually does the rendering.
    But basically "update_content" could imply "process_events".


  • Thanks to the test case I could debug this issue and found a way to avoid the "process_events" call. This patch will be released in the next minor release. Even "update_content()" is no longer required.


  • Hi, Matthias!

    Thank you for quick fix! It'll really simplify end-user code.

  • I believe so :)

    It's actually released yesterday. There is some background processing happening and the single-threaded synchronous get_image etc. methods now take care of finishing these tasks before they deliver the image data.

    Thanks for reporting the problem and the nicely prepared test case.


  • Hi, Matthias!

    I tried to do such screenshots for multiple views opened in KLayout (with similar layer properties for each of them) and suggested workaround for 0.28.9 (pya.Application.instance().process_events()) and your fix in 0.28.12 work only for current view. As workaround I preserve current view, iterate through all views, set new current view, make screenshots and restore current view at the end.

    However, it's not obvious why LayoutView.get_image() behavior depends on view's activity status.

  • Hi Eugene,

    Thanks for bringing this up. I created a new ticket for this:

    From the code perspective I think I understand why non-active views don't get updated. Maybe the following is a workaround:

    1. call get_image once, discard the result
    2. call update_content
    3. call get_image a second time. This time the image should be correct even for inactive views.


  • Hi, Matthias!

    Thank you for help! Probably, issue could be not of high priority, because two workarounds are available.

Sign In or Register to comment.