Mobile app version of vmapp.org
Login or Join
Lee3735518

: Inkscape scripting: how to show / hide a layer and export? I have a cartoon with multiple layers; among those layers I have a German and an English layer. They have the text in their respective

@Lee3735518

Posted in: #Inkscape #Layers

I have a cartoon with multiple layers; among those layers I have a German and an English layer. They have the text in their respective languages. The idea is to hide the German layer and show the English layer and export as an English cartoon or to hide the English layer and show the German layer for a German version of the cartoon. I'd like to script that export.

Here is a part of my SVG file:

<g
inkscape:groupmode="layer"
id="layer3"
inkscape:label="English"
style="display:inline">
<text
xml:space="preserve"
<!-- .... -->
id="text3255">
<tspan id="tspan3257">I don't think</tspan>
<!-- ... -->


and

<g
inkscape:groupmode="layer"
id="layer4"
inkscape:label="German"
style="display:none">
<text
xml:space="preserve"
<!-- ... -->
id="text3284">
<tspan id="tspan3286">Ich glaube nicht,</tspan>
<!-- ... -->


I've seen stackoverflow.com/questions/9652573/inkscape-command-line-programming and tried this command (and plenty of variations to it):

inkscape -z --file=cartoon.svg --select=English --verb=LayerHideAll --select=German --verb=LayerShowAll --export-png=cartoon-de.png --export-area-drawing


But I keep getting the English text, probably because when I saved the file, the English layer was visible and the German layer wasn't.

I've also tried selecting the layers by their IDs (in the code above layer3 and layer4, respectively), and selecting the actual <text> elements; I've tried various combinations of Inkscape verbs. But no luck either.

How can I script this export, hiding or showing the language layer as needed?
Can I do this with Inkscape verbs or should I work with the XML and try setting the style attribute to display:inline or display:none?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Lee3735518

1 Comments

Sorted by latest first Latest Oldest Best

 

@YK2262411

Ok, I messed around with various XML parsing modules and ended up with the following code.

use XML::LibXML;
use XML::LibXML::XPathContext;

my $xml = XML::LibXML->load_xml(location => $file);
my $xpath = XML::LibXML::XPathContext->new($xml);
$xpath->registerNs("defNs", 'http://www.w3.org/2000/svg');
foreach my $layer ($xpath->findnodes('/defNs:svg/defNs:g[@inkscape:groupmode="layer"])) {
my $label = $layer->{'inkscape:label'};
foreach my $otherLang (@languages) {
if ($label =~ m/$other_lang$/) {
$layer->{'style'} =~ s{bdisplay:inlineb}{display:none};
}
}
if ($layerLang eq $lang) {
$layer->{'style'} =~ s{bdisplay:noneb}{display:inline};
}
}
$xml->toFile($tempFileName);


This solves my problem. Hopefully it is useful to someone else as well.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme