Inkscape.org
Creating New Extensions How can I check if the element has an id attribute? or any attribute ?
  1. #1
    blueDie blueDie @blueDie

    Hi,
    How can I check if the element has an id attribute? or any attribute ?

    I am trying to write an extension. 

    i progress finding id attribute of element. For example group elements

    But some group elements haven't got any id or etc attribute.

    So i'm taking errors

    to fix it example code i tried

    try:

       for myObject in Objects

           if (myObject.attrib['id'] !=None)

                     do it

    except:

        errormsg(" i kill you.! You are a dead man ")

     

    but it doesn't work and i keep getting the same message i kill you.!  You are a dead man, I kill you.! You are a dead man

    really not funny 

    please, can you save me from this message ?

  2. #2
    blueDie blueDie @blueDie

    i think i solve this problem

    my computer anymore don'T kill me

    if you encounter the same problem

    you can try below code

    if "id" not in myObjectList.attrib:
          do it..
    

    Thanks...

  3. #3
    inklinea inklinea @inklinea⛰️

    For checking attributes exist - generally would just use:

    https://docs.python.org/3/library/functions.html#hasattr

    or you can use try / except

    then if exists - get_id() 

    or set_id() if does not

    To get an id in general it would be my_object.get_id()

    Inkscape gui will always assign an id when opening a file from the menu or passing a file to the extension system.

    If you create an object without an id in the extension system - get_id() will usually assign a random id - but I would always assign it manually having checked with hasattr

  4. #4
    blueDie blueDie @blueDie

    I see thank you inklinea.

    I will use the information you provided to prepare new extensions.

     

Inkscape Inkscape.org Inkscape Forum Creating New Extensions How can I check if the element has an id attribute? or any attribute ?