Yes. It works on my PC too.
It seems to be the case on a slower PCs.
I tested my script on a Virtual Machine (that is significantly slower).
I think that is the case. When PC is slow operations are finished in a bit different order. On a fast PC it just has enough time to finish it.
The only way to fix it (as an idea) is to crate custom QDialog and use exec_() method (that should block computations enough).
Thanks for validating the code on your side
I think I will move on by myself now.
Just want to say that there is no reason to use setActiveLayer when using addChildNode as the new layer will automatically become the active layer. If anything, it seems to sometimes get a bit buggy when you do that.
2nd, what you may want to run is doc.waitForDone() after creating a new layer.
Actually I played a bit longer. And it still acts kinda weirdly. First it applies changes then creates new layer.
With or without doc.saitForDone. And even with custom Dialog window.
Hm… I just checked. Theoretically… Could this one be an issue.
The thing is that I always keep my PC hibernated. And it is indeed can make some application work strangely (I have seen it in other cases). Could be the case with Krita too.
I just checked with virtual machine that started fresh and the code works properly.
doc.setSelection(newSelection)
and
view.setForeGroundColor(mColor)
works perfectly, it does make selection and change the foreground color,
but on my potato laptop sometimes its end up with a blank paint layer like you
after some testing, i guess it was because of the child node didn’t get added first, addChildNode() is likely slower than app.action() and setSelection()
try add QTest.qWait() and setActiveNode() before painting
it’ll be something lke this :
from PyQt5 import QtTest
app = Krita.instance()
doc = app.activeDocument()
root = doc.rootNode()
view = app.activeWindow().activeView()
layer = doc.createNode("Paint", "paintlayer")
root.addChildNode(layer, None)
QtTest.QTest.qWait(100) #___wait 100ms for the child node to be added first
newSelection = Selection()
newSelection.select(100, 100, 32, 32, 255)
doc.setSelection(newSelection)
mColor = ManagedColor.fromQColor(QColor(255, 0, 0))
view.setForeGroundColor(mColor)
doc.setActiveNode(layer) #___prevent interruption from interrupted by clicking/selecting another layer
app.action('fill_selection_foreground_color').trigger()
or using @ KnowZero suggestion
it’ll be like this, using additional lambda :