Mobile app version of vmapp.org
Login or Join
Deb5748823

: Save multiple objects from vector file to separate files using Inkscape/OS/Freeware I have a downloaded vector file (AI format). The file contains simple vector images of various dog breeds.

@Deb5748823

Posted in: #Inkscape #Vector

I have a downloaded vector file (AI format). The file contains simple vector images of various dog breeds. I've loaded the file in inkscape,ungrouped them and would like a way to save out each dog vector as a separate file. Is there a way to automate this with Inkscape or some other OS/Freeware tool? Otherwise I'll have to select each and Save as. Any help is most appreciated!

UPDATE Thanks to JSBueno for the advice on editing the actual SVG file. I didn't realize that SVG was just an XML file. I should be able to easily walk through the file and export out each dog as a separate file. This, by the way, will be way faster than doing it through the interface which was the goal of this post. Thanks to all for the advice.

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Deb5748823

2 Comments

Sorted by latest first Latest Oldest Best

 

@Dunderdale640

There are two solutions that I have found for exporting multiple objects from Inkscape as SVGs. These methods have the benefit of being scripts that can be run automatically and don't need editing of SVGs in text editors.


svg-objects-export


The svg-objects-export script works by exporting all objects based on regular expressions. In your case it would work by exporting all objects with a particular ID. To use it first clone or download the script from Github.

In Inkscape you need to change the ID of all the objects you want exporting to have the same filename pattern. To do this open the Object Properties dialogue by going to Object > Object Properties or pressing Ctrl+Shift+O. In the ID field change them all to have the same basename ID e.g. Dog_1, Dog_2, Dog_3 etc (objects cannot have the same ID).



With the file saved and the script downloaded, in a terminal run the following command

python svg-objects-export.py --pattern 'Dog_*' /path/to/your/file.svg

This will export all of the objects with the basename ID "Dog_".


inx-exportobjects


The inx-exportobjects inkscape extension will simply export all selected objects. To use it first clone or download the extension from Gitlab. To install it copy the files export_objects.inx and export_objects.py to .config/inkscape/extensions/. In Inkscape you'll have an option called "Objects to Files..." in Extensions > Exports. Select the objects you want to install and select this option.



In this dialogue box simply change the Export Directory to the place where you want to save your files and change the Basename to the new name of the files and then press Apply.



The files will be exported to your specified directory.

Each of the solutions have many other options, such as animated gif export, which you should definitely experiment with.

10% popularity Vote Up Vote Down


 

@Cofer715

Depending on the precondition, that all your objects are grouped and not combined into bigger groups themselves, here is how to do it. (I used cats in the example, because dogs were not available).


In the lower right you see a highlighted cat, object id 5358, which is a single path, so I added a green floor later which changed it to object 5364 which will occur later, so don't let confuse you. :)

You get the xml-editor by hitting button 4 from the right (arrow 1).

I changed g5358 to g-cat-5358 (field 4) to make it easy for a texttool to pick these objects which works for most elements which don't collide with xml-svg-names (color, meta, id, ...).

In the left window you see, that there is already a renamed entity, g-cat-5312 close to the top at the same indentation level.

Inkscape organizes unique ids for groups, but they don't need to be of the form gNUMBER, you can mixin other characters. Just don't pick an already existing identifier.

After changing the identifier, hit 'setzen (set)' (5) to confirm your change. You see a german interface here, YMMV.

Save the svg (as demo-cats.svg). We now switch to scripting.

for cat in $( grep g-cat- demo-cats.svg )
do
id=${cat//[^0-9]/}
echo $id
(head -n 53 demo-cats.svg; sed -n "/$cat/,/^ </g>/p" demo-cats.svg; echo "</svg>" ) > cat-${id}.svg
done


What does it do? It greps (search with grep) for the "g-cat-" pattern which is the start of our interesting section.
id=${cat//[^0-9]/} extracts the id in bash from the expression. Echo prints it to the screen which should help to find the bug if something goes wrong.
head -n 53 demo-cats.svg takes the first 53 lines from the svg, which contains all the svg-boilerplate. Depending on your settings with different svg-formats (inkscape, compressed, normal svg, inkscape-compressed) this might vary.
With nl demo-cats.svg | less you can numerate the lines, to find the right cutting point in your case.

</metadata>
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1">
<g


^Up to this part, the lines should be included.

The next command is done with the stream editor sed: sed -n "/$cat/,/^ </g>/p" demo-cats.svg; says (-n) no printing (but) searche for the cat group, up to the closing tag g at indentation level 2 which is 4 spaces before the </g> if you didn't change Inkscape preferences, which you might remember if you did. Then add the closing tag for svg: echo "</svg>". Write the output to the file 'cat-$id.svg' (this overwrites silently existing files).

So I end up with two files: cat-5312.svg and cat-5364.svg .

A more elegant solution should be possible with xmlstarlet or similar tools, which are made to search and edit xml-files. Unfortunately I'm not used to it and did't get it working after reading 10 minutes of documentation. You wouldn't need to rename the groups in the beginning.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme