Selection.grow() is not represented on canvas

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)

Yes ants are bit lazy :ant::zzz:, but you can force them to refresh the path by calling 2 * invert_selection.

Maybe some one will do a bug report about lazy ants…

from krita import Krita

def grow_selection(pixels=1):
    app = Krita.instance()
    invert_selection = app.action('invert_selection')

    doc = app.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()

    invert_selection.trigger()
    invert_selection.trigger()
    

grow_selection(10)

/AkiR

2 Likes

Thanks for your help, I will try this as soon as possible.

In the meantime I stumbled over two other spots where the Python-API doesn’t act as expected. I’d be glad to help get it fixed, be it writing bug reports or trying to fix them (not that I have much C++ experience, but that’s a chance to learn I guess).

I see there’s a bugtracker linked in the repository, can I just open a bug-ticket there?

Yes, you can create bugs for Krita in https://bugs.kde.org/

If you search for Krita, you should see some example bugs. Use product = krita and try to be as descriptive as possible to help the developer reproduce the bug.