Improve performance of Remove Empty Groups extension. Thanks to Cyrille
for commit by email
This commit is contained in:
parent
5020596ab0
commit
6db9acabfc
@ -5,32 +5,40 @@ import inkex
|
|||||||
"""
|
"""
|
||||||
Extension for InkScape 1.0
|
Extension for InkScape 1.0
|
||||||
|
|
||||||
This extension is totally minimal. It will just clean the whole document from groups without content (dangling groups). That usually happens if you have a group but remove it's paths for example. The group will possibly stay in the XML tree. This also applies for layers because layers are just special types of groups. This effect applies to the whole document ONLY!
|
This extension is totally minimal. It will just clean the whole document from groups without content (dangling groups). That usually happens if you have a group but remove its paths for example. The group will possibly stay in the XML tree. This also applies for layers because layers are just special types of groups. This effect applies to the whole document ONLY!
|
||||||
|
|
||||||
Author: Mario Voigt / FabLab Chemnitz
|
Author: Mario Voigt / FabLab Chemnitz
|
||||||
Mail: mario.voigt@stadtfabrikanten.org
|
Mail: mario.voigt@stadtfabrikanten.org
|
||||||
Date: 19.08.2020
|
Date: 19.08.2020
|
||||||
Last Patch: 01.04.2021
|
Last Patch: 18.04.2022
|
||||||
License: GNU GPL v3
|
License: GNU GPL v3
|
||||||
|
|
||||||
|
Thanks to Cyrille
|
||||||
"""
|
"""
|
||||||
|
|
||||||
class RemoveEmptyGroups(inkex.EffectExtension):
|
class RemoveEmptyGroups(inkex.EffectExtension):
|
||||||
|
|
||||||
def effect(self):
|
def effect(self):
|
||||||
while True:
|
# gets all group elements in document, at any/all nested levels
|
||||||
groups = self.document.xpath('//svg:g',namespaces=inkex.NSS)
|
groups = self.document.xpath('//svg:g',namespaces=inkex.NSS)
|
||||||
oldLen = len(groups)
|
|
||||||
#leave the loop if there are no groups at all
|
# end if there are no groups
|
||||||
if len(groups) == 0:
|
if len(groups) == 0:
|
||||||
break
|
return
|
||||||
#loop trough groups. we have minimum of one to check for emptyness
|
|
||||||
for group in groups:
|
# used to track if we make changes
|
||||||
if len(group.getchildren()) == 0 and group.getparent() is not None:
|
made_changes = False
|
||||||
group.getparent().remove(group) #deletes the deepest empty group
|
|
||||||
continue #we found minimum of one element to delete. so we should run another cycle to check if the parent of this group is empty after deletion
|
# loop through groups
|
||||||
newLen = len(self.document.xpath('//svg:g',namespaces=inkex.NSS))
|
for group in groups:
|
||||||
if newLen == oldLen: #found no more empty groups. Leaving the loop
|
|
||||||
break
|
# checks if item is empty leaf, and if so prune up branch
|
||||||
|
while len(group.getchildren()) == 0:
|
||||||
|
# this group is empty, delete it
|
||||||
|
parent = group.getparent()
|
||||||
|
parent.remove(group)
|
||||||
|
# see if we should delete the parent too, recursively
|
||||||
|
group = parent
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
RemoveEmptyGroups().run()
|
RemoveEmptyGroups().run()
|
Reference in New Issue
Block a user