Inkscape.org
Beyond the Basics actions: query and set-attribute don't use the same units!?
  1. #1
    Chameleon Scales Chameleon Scales @Chameleon_Scales
    *

    If I return the x position of an object with the query-x action, then use the returned $value in a new command with the action object-set-attribute:x,$value, the object moves!

    Is there any way to make sure these commands use the same units regardless of the document properties?

  2. #2
    inklinea inklinea @inklinea⛰️

    The query series - from the command line return the visual bounding box in pixel. 

    From an extension you would use:

    # Unit conversions
    conversions = {
        'in': 96.0,
        'pt': 1.3333333333333333,
        'px': 1.0,
        'mm': 3.779527559055118,
        'cm': 37.79527559055118,
        'm': 3779.527559055118,
        'km': 3779527.559055118,
        'Q': 0.94488188976378,
        'pc': 16.0,
        'yd': 3456.0,
        'ft': 1152.0,
        '': 1.0,  # Default px
    }

     

    and then conversion_factor = conversions[self.svg.unit]

    from the command line you would probably be better using inkex with python ( it does not require inkscape ) 

    or you could parse the document with some other xml aware program - you would still use the conversion table

  3. #3
    Chameleon Scales Chameleon Scales @Chameleon_Scales
    *

    Thanks for your answer.

    Do you know which unit is each action using between query-x and set-attribute?

    I'm not writing a python extension but a bash script and would like to keep it in one file.

  4. #4
    inklinea inklinea @inklinea⛰️

    set-attribute writes directly to the attribute

    so If you just set 20 for example that has no units. 

    However I would try to parse the file in that case to find the units. 

    this was the best I was able to do - to return units from an Inkscape .svg file.

    xmlstarlet sel -t -v "//*['sodipodi:namedview']/@inkscape:document-units" drawing.svg

  5. #5
    Chameleon Scales Chameleon Scales @Chameleon_Scales
    *

    Thank you!

    Dividing the queried x by the document unit and feeding the result to set-attribute works, although if there is a page offset (since multipage svgs were introduced), it has to be added to the result in svg unit (i.e. no unit).

    I also found out that the transform-translate action uses the same unit as query, which is great because I actually want to perform translations.

    Problem double-solved for me.

  6. #6
    Chameleon Scales Chameleon Scales @Chameleon_Scales
    *

    I spoke too fast. Turns out I can't use the query function because it's not just a unit problem. For example, the position of a text object's bounding box will differ depending on the text content, so in my case I need the svg value for x.

    ChatGPT showed me this command which seems to work fine, where text453 is my object's id:

    xmlstarlet sel -t -v "//*[@id='text453']/@x" drawing.svg

     

  7. #7
    inklinea inklinea @inklinea⛰️

    I've never used chatgpt - what did you use as a question ? 

  8. #8
    Chameleon Scales Chameleon Scales @Chameleon_Scales
    *

    Here's the prompt I used in chatGPT:

    Using xmlstarlet on an svg file, how can I query the x position of an object of which I know only the id?

Inkscape Inkscape.org Inkscape Forum Beyond the Basics actions: query and set-attribute don't use the same units!?