Added Filter to Layer

This commit is contained in:
Mario Voigt 2020-09-03 00:16:04 +02:00
parent 433caa5aed
commit 80a28a8d59
2 changed files with 106 additions and 0 deletions

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
<name>Filter To Layer</name>
<id>fablabchemnitz.de.filter_layer</id>
<param name="type" type="optiongroup" gui-text="Filter to current layer:">
<option>Add</option>
<option>Remove</option>
</param>
<param name="help-info" type="description">Filter from selected item: Apply only for the future draws and for selected item. Problem: you can't add more live effects to elements.</param>
<effect needs-document="no">
<object-type>all</object-type>
<effects-menu>
<submenu name="FabLab Chemnitz">
<submenu name="Groups and Layers"/>
</submenu>
</effects-menu>
</effect>
<script>
<command location="inx" interpreter="python">filter_layer.py</command>
</script>
</inkscape-extension>

View File

@ -0,0 +1,85 @@
#!/usr/bin/env python3
'''
This extension adds or removes filters to current layer
Copyright (C) 2012 Jabiertxo Arraiza, jabier.arraiza@marker.es
Version 0.3
TODO:
Comment Better!!!
CHANGE LOG
0.1 Start 30/07/2012
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
'''
import inkex
import sys
import re
class FilterAndLiveEffectsLayer(inkex.Effect):
def __init__(self):
inkex.Effect.__init__(self)
self.arg_parser.add_argument('--type', default = 'Add', help = 'Add or remove filters to current layer')
def selectTop(self):
selected = []
selectedSorted = None
if self.svg.selected is not None:
for id, node in self.svg.selected.items():
selected.append(id)
for node in self.document.getroot().iter():
idNode = node.get("id")
if idNode in selected:
selectedSorted = node
return selectedSorted
def effect(self):
svg = self.document.getroot()
typeOperation = self.options.type
xpathStr = '//sodipodi:namedview'
namedview = svg.xpath(xpathStr, namespaces=inkex.NSS)
idLayer = namedview[0].get('{http://www.inkscape.org/namespaces/inkscape}current-layer');
xpathStr = '//svg:g[@id="'+idLayer+'"]'
layer = svg.xpath(xpathStr, namespaces=inkex.NSS)
if typeOperation == "Add":
element = self.selectTop()
if element is not None:
if element.get('style'):
matchObj = re.search( r'filter:url\(#.*?[^\)]\)', element.get('style'), re.M|re.I)
if matchObj:
filter = matchObj.group()
element.set('style',element.get('style').replace(filter,"").replace(";;",";"))
if layer[0].get('style'):
matchObj = re.search( r'filter:url\(#.*?[^\)]\)', layer[0].get('style'), re.M|re.I)
if matchObj:
element.set('style',element.get('style').replace(matchObj.group(),"").replace(";;",";"))
style = layer[0].get('style')+ ";" + filter;
layer[0].set('style',style.replace(";;",";"))
else:
layer[0].set('style',filter)
else:
inkex.utils.debug("Nothing selected")
else: #Remove action
if layer[0].get('style'):
matchObj = re.search( r'filter:url\(#.*?[^\)]\)', layer[0].get('style'), re.M|re.I)
if matchObj:
layer[0].set('style',layer[0].get('style').replace(matchObj.group(),"").replace(";;",";"))
if __name__ == '__main__':
FilterAndLiveEffectsLayer().run()