Sunday, January 15, 2012

Duplicate Inkscape Layer Across Multiple SVGs

Note: I use Vim for this tutorial. Possibly, your editor or your favourite scripting language could serve as a suitable alternative.

The goal of this tutorial is to show one way of inserting a new layer into many SVG files. The SVG with the layer we want to copy is called the source layer in this tutorial. The term target layer is used here to identify the layer in the destination SVGs under which we want to paste the source layer.

Here is my source SVG containing the source layer, called "red star":

 

Here are two destination SVGs:



In this workflow, we've added a layer with a new object to one of several similar designs. Rather than duplicate that effort across all three SVG files, it would be better to duplicate the layer instead.  Let's do that.

Open the SVG with your source layer in Vim and search for "red star"

  /red star

Save the whole <g ...> tag in register a:

  "ayat

Edit your first target SVG:

  :e vi.svg

Find the target layer. In my files, that's the layer called text:

  /label.*text

Move to the beginning of the <g ...> tag:

  vato<esc>

Paste the star:

  "aP

Save:

  :w

And repeat for all destination SVGs.

Of course, you could open many files at once with:

  :args *.svg

and then:

  :wn

To save and move to the next file.

Seeing the Results

Open the files again in Inkscape (or use File | Revert if they were already open in a session) and you should see your source layer dutifully copied to each of the destination files.



This is a kludge. It is. Hand editing XML files is always a kludge.  This could be better done with a script, possibly an Inkscape plugin.  I might even write it. About time I revisited that corner of the world again. I'll let you know if I do. Until then... hack away on those XML files by hand. :-/