Inkscape.org
Creating New Extensions How to Set Object IDs Based on Their Positions and Z-Orders?
  1. #1
    gotanidea gotanidea @gotanidea
    *

    I want to set object IDs based on their positions (X & Y), dimensions (W & H) and Z-orders. Please see the screenshot below:

    Screenshot

    For example, based on the screenshot above, I need to change the object ID from "g1428" to "839_351_272_769_4" (the position and dimension values are rounded, whereas the last "_4" number is set because the object is the 4th topmost object (based on the Z-orders of the objects in the scene))

    Is it possible to do it with Inkscape scripting API?

  2. #2
    inklinea inklinea @inklinea⛰️

    https://inkscape.gitlab.io/extensions/documentation/source/inkex.elements._selected.html?highlight=rendering_order#inkex.elements._selected.ElementList.rendering_order

    Are you sure rewriting the ids is the best way to go ? 

    It seems simple at first but can get very complicated later. 

    The svg gets sent to the extension system then returns. If you have created duplicate ids the gui will correct for this and assign a randomish id to replace your error.
    Also ids can be referenced elsewhere in the svg and changing them can break those references. 

    I would use a transparent method that stores data and does not require an additional namespace.

    Svg already provides this functionality with the data-* attribute https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/data-*

    Also an array (collection) can be retrieve in a browser using simple javascript

    for example.

    z_order_array = document.querySelector('[data-z_order_string]')

    It's also easy to strip it away without affecting the svg.

    I've written a basic example. I've not tested it throughly.

    Zipfile attached to this post.

    Appears under Extensions>Render Order To Attribute

    For the rounding I chose Math.ceil which rounds up ( int rounds down )

    I've filtered out in the python code for just shape objects. Text is excluded.

    For reasons explained in this post https://inkscape.org/forums/extensions/issuess-with-bounding-boxes/

    Theres a list of elements you can filter by here https://inkscape.org/forums/extensions/i-am-unable-to-find-help-on-inkex/

    If you have any questions ask.


     

  3. #3
    gotanidea gotanidea @gotanidea

    @inklinea : Thank you so much for the detailed explanation and the extension!

    I plan to import the objects as image planes in Blender and use the information in the object IDs for their re-arrangement, hence the object ID rewriting

Inkscape Inkscape.org Inkscape Forum Creating New Extensions How to Set Object IDs Based on Their Positions and Z-Orders?