Python Scripting: Hook onto brush event and invoke tool on image possible?

I was thinking about if I could implement a similar behavior to this one

as a Krita Python plug-in. However, I couldn’t find a brush stroke event I could connect to and even if I could it seems to me that there is no way to invoke a tool like the color picker at a specific place of the image.

What I basically wanted to do is to hook onto the brush stroke event and invoke the color picker just before the stroke is happening to sample the current color at that coordinate, so it will be used as a foreground color for the next stroke.


fun on_brush_stoke(self, args):

I wonder if what I try to do is even possible. From what I can see from the docs, it doesn’t seem to go that deep and is pretty spare with signals/events.

EDIT: The more I look into the API documentation the more I think that it’s not possible to implement this as a python plug-in. I played around with the API and can do all sort of things but not connecting to such low level events like when a tool is used, and overwrite the default behaviour. Maybe I’m just not seeing it. If someone knows more about this, I would appreciate any advice on this.


Any info about possibility of invoking scripts in other ways, than keyboard shortcut, and user-made dockers (ideally invoking them on krita events) are very much appreciated.

I’m also afraid, that krita scripting is rather for creating actions by combining existing ones, and accessing some of the most common variables.
Krita in general seems to be event based though, so I suppose, that once you decide to change the source itself and compile krita on your own, all those things start to be doable. Probably I would have to try it someday too…

1 Like

Yes. I already tried to connect to existing actions like

# No actual code. writing this from memory

on activating the extension. However it does not really work. Although it doesn’t throw any errors, the connected function is never called, when I change the tool (like in this case) to the brush tool. Even if it would work, it’s of limited use to activate a tool like the Brush Tool or the Color Picker when you can’t do much with it after that.

This is true and I’m already looking into the actual code. It’s been a while since I programmed C/C++ (mostly use C# and Python for a few years now) but I think I can maybe do this.

I already found the classes where the brush presets are defined and the UI is build, however I couldn’t find the class where the brush presets are actually applied or the stroke is generated. Krita is just so much code, I’m so lost x3

The example you show is quite interesting - I haven’t tried using it this way. So sad you didn’t manage to make it work.
I had a lot of fun recently with creating my extension (hope I’ll manage to publish it soon), as I’m really a python guy, but I already feel like I’m missing a lot of possibilities, without full access to krita elements.
Good to hear I’m not the only one overwhelmed by the documentation - I already get those most basic classes like canvas, view, document… but there is so much more code that that, and it is probably impossible to reach without messing with source code.
Good luck with your brush engine problems. Let me know if you manage to make any progress.

So, basically there isn’t. We haven’t gotten around to designing api for doing anything at all on the canvas, there’s no undo stack control either and there’s still a lot of missing elements in terms of control over what is going on in the layers and control over the resources.

In particular the canvas control is tricky because even in the cpp code we’re not happy with it, but it is what it is to deal with all sorts of input system weirdness.


Thanks a lot for your response. Good to know it all for sure

1 Like

I’m currently skimming through Kritas code and think about implementing my feature directly instead of an extension.

I already found the classes in krita/plugins/paintops/colorsmudge and where the paintop settings are implemented in kis_colorsmudgeop_settings_widget.cpp and so on. However I failed to find the class and method were the brush strokes are generated and processed. I found KisColorSmudgeOp::paintAt() however if I’m not mistaken this is for each dab, but I need to pick colors at the beginning and end of strokes.

Can you give me a hint in were to find this? I’m a bit overwhelmed by the huge project. I read something about a brush stroke factory somewhere in the docs but can’t find the code.

1 Like

I think you will need to look at the freehand brush tool and look in the stroke start and stroke end functions.