This repository has been archived on 2023-03-25. You can view files and clone it, but cannot push or open issues or pull requests.
mightyscape-1.1-deprecated/extensions/fablabchemnitz/remove_empty_groups/remove_empty_groups.py

44 lines
1.4 KiB
Python
Raw Normal View History

2021-07-23 02:36:56 +02:00
#!/usr/bin/env python3
import inkex
"""
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 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!
2021-07-23 02:36:56 +02:00
Author: Mario Voigt / FabLab Chemnitz
Mail: mario.voigt@stadtfabrikanten.org
Date: 19.08.2020
Last Patch: 18.04.2022
2021-07-23 02:36:56 +02:00
License: GNU GPL v3
Thanks to Cyrille
2021-07-23 02:36:56 +02:00
"""
class RemoveEmptyGroups(inkex.EffectExtension):
def effect(self):
# gets all group elements in document, at any/all nested levels
groups = self.document.xpath('//svg:g',namespaces=inkex.NSS)
# end if there are no groups
if len(groups) == 0:
return
# used to track if we make changes
made_changes = False
# loop through groups
for group in groups:
# 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__':
2021-07-23 02:36:56 +02:00
RemoveEmptyGroups().run()