11

I made a vector graphic in Inkscape, including layers and sub-layers for further use in Processing. I named all the layers in the UI, and realized that the final SVG only creates an inkscape:label attribute with that name, but id remains generic:

<svg:g id="layer1" inkscape:label="My custom label">

I know I can manually edit the labels in the XML editor, but is there a setting somewhere to automatically use the layer name as id?

hoijui
  • 3,615
  • 2
  • 33
  • 41
birgit
  • 1,121
  • 2
  • 21
  • 39
  • That would not be possible in general. `id` attributes have restrictions on which characters can be used in them. You couldn't just let people type in anything for the `id` value - without having to encode, in some way, the ilegal characters.. – Paul LeBeau Aug 10 '17 at 09:12

3 Answers3

5

I recently came across this question, as I was looking for the same topic. As it turned out, Inkscape (v0.92) has functions for that purpose now.

You can set IDs, and Labels in the Inkscape GUI in Object Properties menu, and they will be applied to the XML code then.

Example

Inkscape GUI

  • Draw a yellow rectangle and select it
  • Click on Object -> Object Properties...
  • In the menu set ID to yellow_rect and Label to #yellow_rect
  • Apply changes by a click on Set
  • To complete this example, repeat the steps above to create red_rect, set Label and ID
  • Eventually, group both rectanbles and set identifiers for the group as well.

enter image description here

XML Code

When I open the SVG file, Inkscape put my identifiers to the appropriate XML tags.

    <g
       id="rect_group">
      <rect
         rx="0.11797347"
         y="250.69791"
         x="5.0270834"
         height="18.785416"
         width="30.427082"
         id="yellow_rect1"
         style="fill:#f4ff00;fill-opacity:1;stroke:#000000;stroke-width:0.52916667;stroke-linejoin:round;stroke-miterlimit:3.79999995;stroke-dasharray:none;stroke-opacity:1" />
      <rect
         rx="0.11797347"
         y="258.89999"
         x="24.606249"
         height="16.933332"
         width="33.602081"
         id="red_rect1"
         style="fill:#f40000;fill-opacity:1;stroke:#000000;stroke-width:0.52916664;stroke-linejoin:round;stroke-miterlimit:3.79999995;stroke-dasharray:none;stroke-opacity:1" />
    </g>
Kiwi
  • 513
  • 5
  • 6
3

I have the same requirement when I am creating a svg for Fritzing, because fritzing doesn't refer to the inkscape:label. In such circumstance, I can make sure that label holds the legit value for id. So I make a script to do saving myself out of the dirty and heavy job.

Please note that the script ONLY READ 'Plain SVG' format. https://gist.github.com/TerrenceSun/972ef4eea97f331af1e6abfcafb7c6e5

TerrenceSun
  • 433
  • 3
  • 15
  • I am not a programmer as good as I need to be, so I respectfully ask you @terrencesun to update this script so it can work on Inkscape SVG files. I have a file with 82 layers and changing each one of them is error prone. I tried to save my file into plain SVG but all the layer grouping was lost. My final goal is to export each layer into a separate PNG file thru command line. If you can help me, I'll really appreciate it! – LobaLuna Nov 12 '20 at 20:25
  • sorry to be so unfamiliar with perl but I can't see where to set the url for the file here. – Stacey Reiman May 04 '23 at 21:31
  • @StaceyReiman The diamond operator can read from stdin or any file specified as command line argument. – TerrenceSun May 07 '23 at 01:11
-1

I don't know about a setting to automatically use the layer name as id. But why not do it the other way round: if you remove the inkscape:label attribute, then the layer name automatically becomes the id of the svg:g in the inkscape UI. The attribute inkscape:groupmode=layer is enough to make the svg:g a layer element.

hoijui
  • 3,615
  • 2
  • 33
  • 41
Stonecrusher
  • 184
  • 1
  • 2
  • 14