Inkscape.org
Beyond the Basics How to convert Ids to label names automatically?
  1. #1
    ZV1LLE ZV1LLE @ZV1LLE

    I'm trying to find an extension or .py and .inx file that can help me name all the ids to the label names, I'm exporting one of my Svg files to Moho (A 2D Vector animation software), it comes out great, but the layer names have names like 'g4' and 'path526'. After some research I realized the Id names are being exported and not the label names, meaning I had to rename the id names to the label names. I've been racking my head around getting an automatic approach for this for a good while and searching the internet for a something that could work, but nothing worked. This is now the only I way I can try and find a solution to this issue. Any Help would be greatly appreciated, Thank You.

  2. #2
    inklinea inklinea @inklinea⛰️
    *

    Here is an answer I gave in Oct 2022:

    Labels are not unique and not mandatory - typically a created object will not have a label just an id.
    The id will be displayed in the Object>Layers and Object panel unless it has been overriden by a label you have set.
    ids should be unique and in Inkscape are mandatory (if you delete the id in the svg for that object Inkscape will assign a new id)
    If you go from label>id it can cause a conflict bear that in mind.
    The quickest way would be to loop through the selected objects in the extension system, check if it has a label and if so, replace the id with the label.
    selection_list = self.svg.selected
    for element in selection_list:
    if element.label != None:
    element.set('id', element.label)

    or you could select objects by xpath instead
    for paths only for example
    for element in self.svg.xpath('//svg:path'):
    if element.label != None:
    element.set('id', element.label)

    Also here is a working example I wrote in 2023

    https://gitlab.com/inklinea/label-to-id

  3. #3
    ZV1LLE ZV1LLE @ZV1LLE

    Hello inklinea, Thank for your answer, but I already tried your solution before making this post, but it didn't work. I could show you an example of the XML code before and after using the extension if you want.

  4. #4
    inklinea inklinea @inklinea⛰️

    Sure post it. 

    I will have a look 

  5. #5
    ZV1LLE ZV1LLE @ZV1LLE

    Quick question, how do you attach images to the message?

     

  6. #6
    inklinea inklinea @inklinea⛰️

    When you create a post there is a paperclip icon to the bottom left below the text box. 

    You can attach files :)

  7. #7
    ZV1LLE ZV1LLE @ZV1LLE

    Oh I see it, thx, I will send the photos now

  8. #8
    ZV1LLE ZV1LLE @ZV1LLE

    Here Is before I use the extension (Note: I had already tried renaming some of the ids myself

     

    Screenshot 2024 03 21 At 6.04.10 Pm
  9. #9
    ZV1LLE ZV1LLE @ZV1LLE

    Here is me using the extension

    Screenshot 2024 03 21 At 6.06.03 Pm
  10. #10
    ZV1LLE ZV1LLE @ZV1LLE
    *

    And then here are the after results (not only did it not make any changes, but also changed the id names of the head back to g21 instead of head

    Screenshot 2024 03 21 At 6.07.17 Pm
  11. #11
    inklinea inklinea @inklinea⛰️

    ids are part of the main svg file format specifications, and have to be unique

    https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/id

    If you create conflicting ids, Inkscape will try to correct to prevent you from creating a broken file.

    Labels are not part of the svg specification, they exist in the Inkscape namespace, which is a way of adding extra data to an svg file which is invisible to other programs unless they are coded to look for it.

    I don't know how Moho works. There are other ways to tag multiple items across an svg document such as the svg data-* tag or custom attributes. 

    However I have no idea how you would get Moho to pickup on that when the file is imported, or how to search for them afterwards.

  12. #12
    ZV1LLE ZV1LLE @ZV1LLE

     So, what your basically saying, is that the extension doesn't work because some of the label names are the same. if that's the case, Then I can just make the label names unique as well, will that work?

     

  13. #13
    inklinea inklinea @inklinea⛰️

    Yes that would work, you could add an underscore I suppose like body_001, body_002 etc.

    However, if you wanted to get into python scripting a better way might be to just have Inkscape automatically check all ids in the document each time and add unique number suffixes for your body_001, body_002 :)

  14. #14
    ZV1LLE ZV1LLE @ZV1LLE
    *

    I dont know how to code in python, but I have little bit of knowledge on C++, I think this is how the script would work (Note: it may be wrong, I haven't used C++ for a very long while):

    #include <iostream>
    using namespace std;
    string label

    int id
    int main() {
      if id != label

        id = label

    endl;
    }

  15. #15
    ZV1LLE ZV1LLE @ZV1LLE
    *

    I tested the code in Xcode but it had lots of errors, So I guess I will still need to relearn how to use C++, here is what I was going for by the way:

    If id does not equal label

    then set Id to label

    repeat until all ids equal label

  16. #16
    ZV1LLE ZV1LLE @ZV1LLE

    So I made the label names unique and it actually worked. All my ids now have the labels names, I cant believe I didn't think of this before. Thanks So much for your help 😃

Inkscape Inkscape.org Inkscape Forum Beyond the Basics How to convert Ids to label names automatically?