error using set_property

edited July 18 in Python scripting

Hello I am trying to use set_property to be able to add custom attributes to shapes and cells. When I try to add to shape it throws internal error (see screenshot). And when I try to add it via a cell, the attribute never shows up in the GUI (see second screenshot with empty key value pairs)
Can anyone see what am I doing wrong ?
Regards
EDIT: After a while I noticed that shape.set_property method should not be called inside a loop. When I move it outside the loop it stops throwing error. However I still can't see the property in the GUI. (key value pair in second screenshot is still empty)

# Enter your Python code here

import pya

layout = pya.Layout()

layout.read(r'input_file.oas')

top = layout.top_cell()
cell = layout.cell('hello_polygons_layer')
cell.set_property(1,2) #####does not have any effect
layer_index = layout.layer(1,4)

iterator = top.begin_shapes_rec(layer_index)
while not iterator.at_end():
    shape = iterator.shape()
    if shape.is_box():
      print(shape)
      shape.set_property('myattribute','best_shape') #####Gives Error: 


    iterator.next()
layout.write(r'output_file.oas')`


Comments

  • Hi @Ege_Bey,

    "Cell#set_properties" has an effect for me:

    gives:

    What is your KLayout version?

    Regarding the shape properties: you should not modify the shapes in the loop as changing the property may change the order of iteration.

    Also, the recursive iterator may visit the same shape multiple times. You should use "called_cells" to collect the cells called from your initial cell and modify the properties in these cell's "shapes" container individually.

    Matthias

  • edited July 23

    Hi @Matthias

    I just downloaded version 0.29.4,
    tried the following code on the layout you see in screen shot. It didn't work unfortunately.

    pya.CellView.active().cell.set_property(2,3)
    print(pya.CellView.active().cell.name)
    print('ran')
    

    My user property is still empty

    Do you have any other recommendations?

  • edited July 23

    Hi @Matthias,

    I could reproduce this problem as follows.

    Preparations

    1. I borrowed troublesome_taper.oas from https://github.com/KLayout/klayout/issues/1793.
    2. Using KLayout 0.29.4 (source build on Linux Mint 20.3), I saved it as troublesome_taper_new.oas with the below options.

    3. Noticed that the file sizes differ.


    Using troublesome_taper.oas




    Using troublesome_taper_new.oas



    I hope this helps you debug.
    Best regards,
    Kazzz-S

  • edited July 23

    Hi @sekigawa

    I tried your steps as well with the oas files you have attached.

    1- I opened troublesome_taper.oas.
    2- Saved as troublesome_taper_mytry.oas with the settings you have provided.
    3- Opened troublesome_taper_mytry.oas and ran

    pya.CellView.active().cell.set_property(2,3)
    print(pya.CellView.active().cell.name)
    print('ran')
    

    Unfortunately user properties is still empty. Can it be platform dependent issue? I am using windows.

    Regards,
    Ege_Bey

  • edited July 24

    Hi @Ege_Bey,

    Thanks for testing.
    I tried on Windows 10 Enterprise with...




    As you can see above, the results on Linux were reproduced. Are you using Windows 11?
    I'll also try on Mac later today.

    Regards,
    Kazzz-S

  • Cont.

    On macOS Monterey with...




    The problem is reproducible.
    Regards,
    Kazzz-S

  • Hi @sekigawa,
    Thanks for testing!
    I am using windows 11 Enterprise

  • Cont.

    Lastly, on Windows 11 Home with...




    In all my environments, the behavior seems consistent.

  • edited July 25

    You're right. I can reproduce the problem with "troublesome_taper", but not with other files. Maybe it is only happening with a single cell per layout.

    I will debug the problem. I have created a ticket for that issue: https://github.com/KLayout/klayout/issues/1799

    Thanks for this nice breakdown of the problem.

    Matthias

  • edited July 25

    Thanks @Matthias

    How can I keep track of the progress of the bug fix? (Like is there a way to watch the ticket and get notified when it is solved?)
    Do you have any estimate on when it would be fixed?

    Again many thanks!
    Regards,
    Ege_Bey

  • Just watch it ... it is already fixed. It is going to go into the next minor release.

    The problem was an issue with the OASIS reader in certain cases.

    Matthias

  • Perfect thank you for handling this issue so quick!

  • Very good. Thanks! :)

    But even better: in case you need a real quick fix, you can pull the fixed sources from here: https://github.com/KLayout/klayout/tree/bugfix/issue-1799 and build a fixed version yourself. Difficult on Windows, easy on Linux :)

    Matthias

  • Hi @Matthias,

    Thanks for the quick fix.
    I have tested the https://github.com/KLayout/klayout/tree/bugfix/issue-1799 branch.
    All is OK on the three platforms. :smiley:

    Platform OASIS = troublesome_taper.oas OASIS = troublesome_taper_new.oas
    macOS
    Monterey
    Mac-11 Mac-12
    Linux
    Mint 20.3
    Win10-11 Win10-12
    Windows10
    Enterprise
    Win10-11 Win10-12

    Regards,
    Kazzz-S

  • edited July 26

    Thanks for testing it!
    A question came up to my mind. Will it also work when we run script outside of klayout IDE? I run most of my scripts outside of Klayout IDE. I just do pip install klayout and import pya to my script.

  • Hi @Ege_Bey,

    Yes, we can run our script outside of the KLayout IDE.
    However, we must install a patched *.whl (by pip) to address this issue.
    I've run the below script with the (1) current release version and (2) locally built version of *.whl.
    Please wait for the 0.29.5 release or build it by yourself.
    I have no experience in building *.whl for Windows.

    import sys
    import klayout.db as kdb
    
    usage  = "Usage: -------------------------------------------------\n"
    usage += "  ./forum2552-S.py <key> <val> <python_module_ID>\n"
    usage += "   ex:\n"
    usage += "     ./forum2552-S.py 2 3 rel\n"
    usage += "     ./forum2552-S.py 2 3 gh1799\n"
    usage += "--------------------------------------------------------\n"
    
    def SetCellProperty( inoas, key, val, mod ):
        outoas = "%s-%s" % (mod, inoas)
        layout = kdb.Layout()
        layout.read(inoas)
        topcell = layout.top_cell()
        topcell.set_property(key, val)
        layout.write(outoas)
    
    if __name__ == '__main__':
        args = sys.argv
        if len(args) != 4:
            print(usage)
            sys.exit(0)
        else:
            key = args[1]
            val = args[2]
            mod = args[3]
        SetCellProperty( "troublesome_taper.oas", key, val, mod )
        SetCellProperty( "troublesome_taper_new.oas", key, val, mod )
    

    I have rebuilt a new *.whl (GH-1799) on my macOS Monterey with MacPorts.

    Module OASIS = troublesome_taper.oas OASIS = troublesome_taper_new.oas
    Currnet
    Release
    rel-troublesome_taper rel-troublesome_taper_new
    GH-1799 gh1799-troublesome_taper gh1799-troublesome_taper_new

    Regards,
    Kazzz-S

  • Dear all,

    I have merged to master already.

    There are some wheels generated by GitHub here: https://pipelinesghubeus6.actions.githubusercontent.com/68A5NMsc4y2EbEDz38t9o9oKwhP9UxXzHEMhZ2QXBXaztmOIqM/_apis/pipelines/1/runs/1685/signedartifactscontent?artifactName=artifact&amp;urlExpires=2024-07-27T19:42:14.0211820Z&amp;urlSigningMethod=HMACV2&amp;urlSignature=qKNYAJyy+kvO6BA4QosoKQP1MwTMZyMZoxU0LfOX7Po=

    The version still is set to 0.29.4, but the code is taken from master.

    Plus there are fresh builds from the master branch here (PRM, DEB, Windows .exe): https://www.klayout.org/downloads/master/

    Matthias

  • Hi @Ege_Bey,

    KLayout 0.29.5 is out.

    I've confirmed that the above forum2552-S.py script also runs as expected with klayout-0.29.5-cp311-cp311-win_amd64.whl.

    Regards,
    Kazzz-S

  • Thanks a lot for the effort and taking the time!!

    Regards,
    Ege

Sign In or Register to comment.