I am trying to write some code to delete certain layers according to the layer names, and there are paths in each layer. I'd like to delete the layers and their paths. When I ran the extension, there were no error messages but nothing happened to the SVG file. Can anyone help? Attached my code, thanks!
def effect(self):
for layer in self.svg.selection:
if Layer.label == 'Polygon':
layer.self.svg.delete()
Hi Inklinea, I wonder what's difference between using all_layers = self.svg.xpath('//svg:g[@inkscape:groupmode="layer"]') and self.svg if I want to work with all layers in the documents. Thanks!
Hi Inklinea, I wonder what's difference between using all_layers = self.svg.xpath('//svg:g[@inkscape:groupmode="layer"]') and self.svg if I want to work with all layers in the documents. Thanks!
Hi Inklinea, I wonder what's difference between using all_layers = self.svg.xpath('//svg:g[@inkscape:groupmode="layer"]') and self.svg if I want to work with all layers in the documents. Thanks!
If you have opened an svg file in a text editor - it's just a lot of text :)
Programming languages can parse that text as it is, however a better approach is to load parse it into a 'model'.
For example - if you have used a web browser, html files etc are manipulated using javascript and the DOM or document object model.
self.svg is similar - but it is an svg file which uses "Element Tree (etree)" - so the svg file is converted into a useful tree.
That's why we use terms like parent / child / decedents etc. The methods .append() etc are mostly methods which come from etree. https://lxml.de/tutorial.html
Inkex, the Inkscape api also adds lots of improvements and shortcuts.
xpath is way of searching through XML documents - and etree supports xpath search terms.
So ... when you use self.svg.xpath( expression ) you are using xpath to search.
Hi,
I am trying to write some code to delete certain layers according to the layer names, and there are paths in each layer. I'd like to delete the layers and their paths. When I ran the extension, there were no error messages but nothing happened to the SVG file. Can anyone help? Attached my code, thanks!
Are you trying to work with only selected layers - or all layers in the document at the same time ?
I am working with all the layers in the document at the same time, and I'd like to delete several of them with special layer names. Thanks!
self.svg.selection gives the current selection in the gui which is passed to the extension system.
If you want to set up a loop I would avoid using 'layer', 'Layer' etc.
The word layer in your code is no different for using 'x' or 'thing'
However it could collide with the actually usage of inkex.Layer
What your code actually does is simply examine each object in the selection list
for a 'label' attribute == polygon and deletes it.
That could be anything with a label attribute, which is most objects.
layer.self.svg.delete() does not make any sense because any layers in the svg are descendents of the svg root element.
however layer.self etc puts the layer higher than the svg and self.
---------------------------------------------------------------------------
If you just want to work with all layers in the svg, I would suggest using xpath to get the layers.
The only thing you have to be careful of is nested layers, if you delete the parent then try to delete the child it will throw an error.
To select all layers in the document you could use:
all_layers = self.svg.xpath('//svg:g[@inkscape:groupmode="layer"]')
for current_layer in all_layers:
inkex.errormsg(current_layer.label)
if current_layer.get('inkscape:label') == 'polygon':
current_layer.delete()
I recently made this https://gitlab.com/inklinea/toggle-layers
Thanks, inkinea! It helps!!!
Hi Inklinea, I wonder what's difference between using
all_layers = self.svg.xpath('//svg:g[@inkscape:groupmode="layer"]')
andself.svg
if I want to work with all layers in the documents. Thanks!Hi Inklinea, I wonder what's difference between using
all_layers = self.svg.xpath('//svg:g[@inkscape:groupmode="layer"]')
andself.svg
if I want to work with all layers in the documents. Thanks!Hi Inklinea, I wonder what's difference between using
all_layers = self.svg.xpath('//svg:g[@inkscape:groupmode="layer"]')
andself.svg
if I want to work with all layers in the documents. Thanks!If you have opened an svg file in a text editor - it's just a lot of text :)
Programming languages can parse that text as it is, however a better approach is to load parse it into a 'model'.
For example - if you have used a web browser, html files etc are manipulated using javascript and the DOM or document object model.
self.svg
is similar - but it is an svg file which uses "Element Tree (etree)" - so the svg file is converted into a useful tree.That's why we use terms like parent / child / decedents etc. The methods .append() etc are mostly methods which come from etree. https://lxml.de/tutorial.html
Inkex, the Inkscape api also adds lots of improvements and shortcuts.
xpath
is way of searching through XML documents - and etree supports xpath search terms.So ... when you use self.svg.xpath( expression ) you are using xpath to search.
https://www.w3schools.com/xml/xpath_syntax.asp
The double forward slash is '//Selects nodes in the document from the current node that match the selection no matter where they are'
So for example if you wanted to find every <path> in the document you could use
paths = self.svg.xpath('//svg:path')
which would give you a list of all path elements in the document.or if you just wanted to find rectangles contained in group
That would just find the immediate children of that group which are <rect> elements
or if you have sub groups / sub layers
item.xpath('.//svg:rect')
would find all <rect> elements anywhere in the current element and below.
Thanks a lot!