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.
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)
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.
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.
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?
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 :)
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
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 😃
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.
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
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.
Sure post it.
I will have a look
Quick question, how do you attach images to the message?
When you create a post there is a paperclip icon to the bottom left below the text box.
You can attach files :)
Oh I see it, thx, I will send the photos now
Here Is before I use the extension (Note: I had already tried renaming some of the ids myself
Here is me using the extension
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
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.
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?
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 :)
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;
}
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
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 😃