Krita script to turn off all Pencil layers

When working on my comic, I create a bunch of layers named Pencils where I do my cleaner pencil drawings overtop of my layouts, usually one layer per character or important prop. When I’ve finished inking (in another layer for each object) overtop of the Pencils layers, I hide all the Pencils layers. These can be nested pretty deep, and there can be a lot of them, and I’ve been wanting to expand my Python knowledge a bit, so here’s a little script (copypaste into the Scripter plugin editor pane) to turn them all off:

from krita import *

active = Krita.instance().activeDocument()
queue = [active.rootNode()]

while len(queue) > 0:
  node = queue.pop()
  if node:
    if node.name() == "Pencils":
      node.setVisible(False)
    for child in node.childNodes():
      queue.append(child)

active.refreshProjection()