I am writing my first Krita plugin. I have no experience with Krita-Plugins or Python, but am experienced in other languages.
My goal:
Grow the current selection by a given number of pixels, similar to what happens when I go through the menu “Select” => “Grow Selection”.
What I expected:
The current selection is visibly updated (by the dashed line which depicts a selection) on the canvas.
What I observe:
The current selection looks like it was not change, but it acts changed.
For example, I manually create a selection of 100x100px. I fill it with a random color.
I run my script, to grow the selection by 10 pixels in every direction.
The visible selection (the dashed line) is still only 100x100px
But I can draw or fill 10px around it, so the actually selected area seems to be 120x120px now.
If I change the selection manually afterwards, for example by adding a small piece using the rectangular selection tool, then the “invisibly selected” area seems to be dropped.
When I activate the option “Show Global Selection Mask”, the selection mask shows the expected area of 120x120. But when I go back to the paint layer, I still see a visible selection of 100x100.
What I have tried:
Added “doc.refreshProjection()”, but it seems to make no difference.
Am I missing a step, or am I missing a point about the functionality here? Do I have to apply the selection to something else?
from krita import Krita
def grow_selection(pixels=1):
doc = Krita.instance().activeDocument()
if not doc:
print("No document found")
return
selection = doc.selection()
if not selection:
print("No selection found")
return
selection.grow(pixels, pixels)
print(f"new selection width: {selection.width()}")
print(f"new selection height: {selection.height()}")
doc.setSelection(selection)
doc.refreshProjection()
grow_selection(10)