Inkscape.org
Creating New Extensions Given a selected element, how and what information can we retrieve about it?
  1. #1
    serialc serialc @serialc

    If I have a selected element, perhaps retrieved using:

    for elem in self.svg.selection:
        # what can I find out about the elem?

    What can I find out about the elem, potentially without querying all possible attributes?

    I can for example:

    • elem.get('id') to get the id (or other attribute)
    • elem to get the element type (e.g., g, rect)
    • len(elem) to see how many children it has (0 if none)

    What other information can we get?
    Is there a way of listing all the attributes of the element?

  2. #2
    inklinea inklinea @inklinea⛰️
    *

    The api reference is here: https://inkscape.gitlab.io/extensions/documentation/source/index.html

    However for a general python method to do it:

    def get_attributes(self):
        for att in dir(self):
            try:
                inkex.errormsg((att, getattr(self, att)))
            except:
                None

    Where self is any object you want to feed in.

  3. #3
    serialc serialc @serialc

    Hi inklinea,

    This is really helpful, thanks.
    I realize now my question was a bit vague but I was looking for the functionality of elem.attrib, which lists the SVG attributes.

    However, your response has really helped me better understand what is possible with each element as your function reveals all the object's attributes and methods.
    I struggled finding the necessary information in the reference documentation before as I was unsure what type of the element I was dealing with.
    Using the python attributes listing, using dir(), really helps.

    Thanks for your help!

Inkscape Inkscape.org Inkscape Forum Creating New Extensions Given a selected element, how and what information can we retrieve about it?