Find empty layers and delete them

Hi, I was asking that question in the Steam-Forum but I thought it would be more useful to post it here. I’m also not quiet sure if this is the right category for this kind of question.
I’m looking for a script that finds and removes empty layers. There is already a script that basically does that: How to delete all empty layers?

The problem is, this script only finds “empty” layers but skips layers that have no visible content… Yes, that sounds weird. For an easy example, a freshly generated empty layer can be found by the script but when you scribble around with the eraser tool on the empty layer the script will skip it.

I guess the soultion would be to check the value of the alpha channel instead of just checking if a layer has content?
Unfortunately, I can’t program or script. Maybe someone can help me with this?

(I also think this is a very useful feature. Maybe this could be implemented in Krita?)

1 Like

This should help.

2 Likes

This should work. Compared to the script in the linked thread, it checks exactBounds instead of extent.

from krita import *

doc = Krita.instance().activeDocument()
root = doc.rootNode()

allNodes = root.findChildNodes("", True)
for layer in allNodes:
    if layer.bounds().isEmpty():
        layer.remove()
4 Likes

I think we should add a new category in resources for scripts like these? what do you say?

6 Likes

Yes, that’s a really good Idea. I think there are plenty of useful scripts out there and a place where you can browse through them would be really cool. :+1:

1 Like

Sounds like a good idea to me, definetly better (and faster) than checking the alpha values of each pixel. But it says there is an error.

AttributeError: 'GroupLayer' object has no attribute 'findChildNodes'

In file: <string>
In function: <module> at line: 6. Line with error:

Ah, that function was added in 5.2. For 5.1, it would need some manual recursion like in the other script.

from krita import *

def removeEmptyLayers(layer):
    children = layer.childNodes()
    for childLayer in children:
        removeEmptyLayers(childLayer)
        if childLayer.bounds().isEmpty():
            childLayer.remove()

doc = Krita.instance().activeDocument()
root = doc.rootNode()
removeEmptyLayers(root)
2 Likes

Oh good to know. It works great. I will save both then and use the other one when the update comes. Thank you very much. :smiling_face: