Help with my first plug-in please (conditional hotkeys)

This is my first attempt at doing something custom in Krita. I have had some success getting the plug-in to register, being able to trigger it with a keyboard shortcut etc but I ran in to a strange issue where, even though I have an if-else-statement in my method the action will do both of the things, regardless of the condition that I’m trying to establish. So… basically it does both the ‘if’ part as well as the ‘else’ part…

Could anyone please try out this plug-in and have a look at the code? I apologize for the tremendous redundancy in the script. I am definitely going to delete unnecessary stuff once I get it to work.

What the plug-in is supposed to do:

  • check which tool is active
  • if the freehand selection tool is active, fill selection with FG color
  • if freehand selection tool is not active, activate it (but NOT fill selection with FG color)
    (this is what I’m having problems with… when the freehand selection tool is not selected, it selects it AND fills with FG color)

Here’s the part of the code that seems to be causing problems… Or maybe it’s something else?

    if name == 'KisToolSelectOutline':
        Krita.instance().action('fill_selection_foreground_color').trigger()
    elif name != 'KisToolSelectOutline':
        Krita.instance().action('KisToolSelectOutline').trigger()

google drive link to zip of plug-in

So essentially my goal is to create the possibility to use conditional hotkeys. Depending on which tool is active, the hotkeys should change. At least that’s the idea, but I’m still in the beginning of figuring this out… And again the problem is that regardless of whether the Freehand Selection tool is active or not it always fills with FG color. It should only fill with FG color when the Freehand Selection Tool is active. Otherwise it should activate the Freehand Selection tool.

1 Like

Have you tried doing this:

        if name == 'KisToolSelectOutline':
            print("Option 1, Tool Select *is* active, filling with fg; tool: {tool}, tool.objectName: {name}, checked: {checked}".format(**locals()))
            Krita.instance().action('fill_selection_foreground_color').trigger()
        elif name != 'KisToolSelectOutline':
            print("Option 2, Tool Select is not active; tool: {tool}, tool.objectName: {name}, checked: {checked}".format(**locals()))
            Krita.instance().action('KisToolSelectOutline').trigger()

If so, what’s the result? What does it print out?

Also does the selection tool gets selected if you run this script? Maybe the problem is that the action kind of selects the selection tool first and then runs again for some reason and see that it’s the selection tool again so it fills with the color? Not sure why would that happen, though.

Also why are you using elif, is that only for debugging purposes? It makes the code less readable than if you were using just else. Readability is quite important for good quality code.

2 Likes

Hi

Yes, it’s something else :slight_smile:

Looking your code, the plugin is instancied 3 times:

  • One time in conditsh.py, line 60:
# And add the extension to Krita's list of extensions:
Krita.instance().addExtension(MyConditsh(Krita.instance()))
  • Two times in __init__.py, lines 8/9:
# Initialize and add the extension
Scripter.addExtension(MyConditsh(Krita.instance()))
Krita.instance().addExtension(MyConditsh(Krita.instance()))

As the plugin is created 3 times, when it’s executed you have 3 executions:

  • The first execution select the “freehand selection tool”
  • The second execution fill the selection
  • The third execution fill the selection

Remove instance created from conditsh.py and remove one instance in __init__.py and everything will be ok :wink:

Grum999

2 Likes

Thanks, that worked. I thought that these lines registered the extension with Krita, so they were merely redundant. I didn’t think that they would affect the triggering of the action.

I also found out that I had to re-install the extension (re-import the script) and restart Krita, because I’d tried removing those lines before I think.

When you made some modifications in code, you don’t have to uninstall/re-install plugin.
But you have to restart Krita to have change get in account as plugin code is loaded at Krita’s startup.

Grum999

I have a new question. The script above made it so I could activate the Lasso tool with a hotkey and if Lasso is active already, the same hotkey can be used to Fill with FG color.

Now I’m trying to come up with a script where, if the Brush tool is active, the B key could be used to sample the color on the canvas. However, this is different from the previous script because the sampling is an Alternate Invocation Action, and I’d have to hold down B to activate it, and releasing would deactivate it. I found this in the API KisAlternateInvocationAction Class Reference

So I understand that I need SampleColorFgImageModeShortcut from that page. But how do I trigger this action, as compared to something like simply changing a tool, as my previous script did, via

Krita.instance().action(‘fill_selection_foreground_color’).trigger()

Because the KisAlternateInvocationAction is something that has different behaviors (FG/BG, Layer, Merged color picking etc), as well as beginning and end states as I understand, I suppose instead of a simple triggering of an action like above, the code would be much more advanced? Thanks.

Hi

The KisAlternateInvocationAction is internal Krita’s API that is not available through scripting API.

I currently have no idea about how to -through scripting- simulate the alternate function and apply it.
Probably there’s a possibility to tweak the system but it might be a dirty solution…

Grum999

1 Like

Well this is a little sad. I do a lot of graphic lasso selection painting, using just the Clear and Fill commands, rather than a brush, and if there is no way to pick a color when the Freehand Selection Tool is active, that’s a real bummer. It means I have to switch to the Brush tool or Color Picker every time I pick a color and that is very repetitive.

Why not use the fill shape brush then?

1 Like

Sometimes I do but Fill Shape Brush doesn’t have the functionality of holding Ctrl to get straight lines.

Freehand Path Tool has the filling and the color picking but again, no ctrl for straight lines :confused: