I am trying to make a copy of a layer and do some work on it.
I ran into an issue similar to the one discussed in this thread where the activeNode is not updated after layer manipulation.
The thread suggests that this is a timing issue; however, after playing a bit with it it’s starting to seem like it’s not.
Here is my first attempt: duplicate the current layer, then print the name of the active node:
from krita import *
app = Krita.instance()
doc = app.activeDocument()
app.action('duplicatelayer').trigger()
print(doc.activeNode().name())
I am running this in a document with a single layer called “v1”.
Expected result: “Copy of v1”
Actual result: “v1”
Ok, now let’s throw everything I found that would help with concurrency:
from krita import *
from PyQt5 import QtTest
app = Krita.instance()
doc = app.activeDocument()
app.action('duplicatelayer').trigger()
QtTest.QTest.qWait(100)
doc.waitForDone()
qApp.processEvents()
print(doc.activeNode().name())
Result: still “v1”. Setting the wait time to 10 seconds doesn’t help either.
However, based on the previous forum’s solution I tried this - create a dummy selection in between:
from krita import *
from PyQt5 import QtTest
app = Krita.instance()
doc = app.activeDocument()
app.action('duplicatelayer').trigger()
newSelection = Selection()
newSelection.select(100, 100, 32, 32, 255)
doc.setSelection(newSelection)
print(doc.activeNode().name())
Result: “Copy of v1”!!!
Somehow creating a selection gets the activeNode unstuck.
Note that it only works if there is no Selection Mask already present!
If I run the above code when some selection is already active, I get “v1” again.
So there’s something in setSelection that, if there’s no selection already, properly updates the activeNode.
Is this a bug or is there something that I could call explicitly?