From 80a28a8d5964d2b95fca5b74d1be5e2879c91426 Mon Sep 17 00:00:00 2001 From: Mario Voigt Date: Thu, 3 Sep 2020 00:16:04 +0200 Subject: [PATCH] Added Filter to Layer --- extensions/fablabchemnitz/filter_layer.inx | 21 ++++++ extensions/fablabchemnitz/filter_layer.py | 85 ++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 extensions/fablabchemnitz/filter_layer.inx create mode 100644 extensions/fablabchemnitz/filter_layer.py diff --git a/extensions/fablabchemnitz/filter_layer.inx b/extensions/fablabchemnitz/filter_layer.inx new file mode 100644 index 00000000..94b21d91 --- /dev/null +++ b/extensions/fablabchemnitz/filter_layer.inx @@ -0,0 +1,21 @@ + + + Filter To Layer + fablabchemnitz.de.filter_layer + + + + + Filter from selected item: Apply only for the future draws and for selected item. Problem: you can't add more live effects to elements. + + all + + + + + + + + \ No newline at end of file diff --git a/extensions/fablabchemnitz/filter_layer.py b/extensions/fablabchemnitz/filter_layer.py new file mode 100644 index 00000000..d1a62eb2 --- /dev/null +++ b/extensions/fablabchemnitz/filter_layer.py @@ -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() \ No newline at end of file