The little script (below) creates a new document, adds a paint layer, sets that layer to be the active node, enables animation on it, sets current time to 0 and then triggers the ‘add_blank_frame’ action.
My expectation is that this adds a blank frame to the paint layer at time 0 and that as a result when I right-click on that frame in the timeline the “Create Blank Frame” menu item is unavailable as the frame should already have been created and added.
That, however, is not always the case because sometimes the blank frame is added to the background layer instead of the paint layer. In fact whichever of the two layers I manually select by clicking on it in the current document will determine which layer gets the blank frame in the new document.
I wanted to log this behaviour and found that the animated() property of the background layer becomes True when it gets the blank frame. So when I create a random doc, select the paint layer in it and then run my script, everything is as it should be and the background layer of the new doc is not animated / does not have the frame created:
root: animated=False
Background: animated=False
PaintLayer: animated=True
However, when I select the background frame on a document and then run my script, the background frame of the newly created document becomes animated / gets the blank frame set:
root: animated=False
Background: animated=True
PaintLayer: animated=True
Clearly some variation of this is what @mallyj was struggling with, too, in Programmatically changing selected frame in Timeline docker (as opposed to active frame).
Would we expect the explicitly activated node to have the action applied, i.e. is this a bug?
How can I make sure my code always targets the paint layer?
Here’s my script:
inst = Krita.instance()
doc = inst.createDocument(60, 40, "TestDoc", "RGBA", "U8", "", 100.0)
inst.activeWindow().addView(doc)
newLayer = doc.createNode("PaintLayer", "paintlayer")
doc.rootNode().addChildNode(newLayer, None)
newLayer.enableAnimation()
doc.setActiveNode(newLayer)
doc.refreshProjection()
doc.setCurrentTime(0)
doc.waitForDone()
inst.action('add_blank_frame').trigger()
def printNodes(n, indent = 0):
print(4*indent*' ' + n.name() + ': animated=' + str(n.animated()))
for c in n.childNodes():
printNodes(c, indent+1)
printNodes(doc.rootNode())