Krita Python Scripting Tutorial Series

Hello, everyone! I was recommended to post my recent video tutorial series on creating Python scripts with Krita here on this forum. In this series, I introduce the topic of Python scripting in the context of Krita, and go over step-by-step examples. There are currently three videos in the series at the time of this posting. Here is a link to the playlist on YouTube:

https://www.youtube.com/watch?v=uNEQ5t6YhKU&list=PLn2W-yDJ6FIVUhYus54sTD4--OKk8e2J2

The first video is an introduction that shows off some of the basic parts of Krita Python scripting, the second video goes over making plugins, and the third video is about creating extensions. Based on my research, this is the only video tutorial series about this topic currently available.

Hopefully these videos will be of help to you! If you like them, please consider subscribing to my YouTube channel for more updates. I have at least three more videos in this series I intend to put out in the coming days.

Thanks!

37 Likes

Welcome to the forum :heart:

2 Likes

I’ve been looking forward to something like this for a while!

The problem is that I simply don’t know what I want to script, not how. In Blender there are some use cases that I can see easily being useful, but not to Krita. Guess I don’t know how my workflow could be drastically improved :smiley:

Regardless of that, thank you for this!

1 Like

Hi and thank you for those tutorials. I should have started scripting in krita long ago, but it was this tutorial that finally helped me to actualy get started - thank you so much for that.

Here are my first day impressions from scripting in krita.

During this first day I managed to write an insanely simple switch between my most frequently used brush opacities:

(CLICK) Switching opacity between 50% and 100% with one key
from krita import *

for view in Krita.instance().views():
	print(view.paintingOpacity())
	if view.paintingOpacity() == 1.0:
		view.setPaintingOpacity(0.5)
	else:
		view.setPaintingOpacity(1.0)

and the same for switching between my two brush blending modes

(CLICK) Switching between normal and overlay mode
from krita import *

for view in Krita.instance().views():
[print([a.objectName(), a.text()]) for a in Krita.instance().actions()]
if view.currentBlendingMode() == "normal":
    Krita.instance().action("Select Overlay Blending Mode").trigger()
else:
    Krita.instance().action("Select Normal Blending Mode").trigger()

It’s really convenient for me to use them that way, as I spend vast majority of my time in canvas-only mode.

For a long time I was wondering why switching blending modes with shortcuts never worked on my computer. Once I used:

[print([a.objectName(), a.text()]) for a in Krita.instance().actions()]

it turned out, that the actions like “Select Normal Blending Mode” (probably due to those spaces in their names), were accidentally translated in Polish translation (normally only method description should be translated). So krita tried to use methods like “Wybierz normalny tryb przesłaniania”, that is obviously not implemented.
So I guess I have found both a bug and its origin in my first day of scripting in krita :smiley:

I also tried to make a bit more complicated script that would allow me to have reference image and the artwork in separate views (as I tend to work), but make the reference image automatically set up to the coordinates - be the same size, rotation, on the same height, just like you would have a normal reference image, but once you zoom deep in the artwork, the reference is zoomed the same way - so you have both of them zoomed to the exact same spot.
Most of those (mirror, rotation, scale(sort of :wink:) already work as I found those in the View class, although I couldn’t find how to translate the canvas inside the view - I guess this variable is somewhere deeper as I haven’t found anything like that in View class (or any of the most basic ones).

(CLICK) not finished "smart reference" script
from krita import *

for window in Krita.instance().windows():
	activeView = window.activeView()

for view in Krita.instance().views():
	if view != activeView:
		reference = view

activeCan = activeView.canvas()
referenceCan = reference.canvas()
activeDoc = activeView.document()
referenceDoc = reference.document()

referenceCan.setMirror(activeCan.mirror())
referenceCan.resetRotation()
referenceCan.setRotation(activeCan.rotation())

scale = activeDoc.width()/referenceDoc.width()
print(activeDoc.width(),referenceDoc.width())
referenceCan.resetZoom()
referenceCan.setZoomLevel(activeCan.zoomLevel()/scale)

My biggest scripting dream in krita was to get the “show color selector” popup work on a modifier in “canvas input” instead as a usual shortcut. I already know that I can get it with:

Krita.instance().action("show_color_selector").trigger()

but it seems that making it appear on positive slope of the modifier (like pressing alt) and disappear only on negative one (I release alt) won’t be as easy with my knowledge after one day of scripting :wink:

Anyway, if someone knows how to tackle any of those problems, or have the idea where specifically I should search in the krita documentation, feel free to let me know :slight_smile:

1 Like

@wojtryb - You should be able to use some built-in PyQt stuff to be able to manage that… You could try something similar to these examples.

basic key release events

using modifier keys like Shift,Alt

3 Likes

Thank you so much. I’ll definetely dig into that.

1 Like

I should say a big thank you for these series, they are perfectly clear and understandable, stuff like that will encourage more and more people into creating plugins and scripts for krita.

1 Like

I’m looking forward to see the docker tutorial.

2 Likes

I’m back with an update! I have just released the fourth video in my series on Python scripting in Krita, which covers how to create and use custom dockers. In this video, I go over a very simple example and demonstrate how custom dockers can be useful. Direct link to the video is below:

https://www.youtube.com/watch?v=aQwPOC1tT7Q

This most recent video builds heavily on the preceding 3 videos, so make sure you watch those first to get the most out of this new one!

Hope this video is helpful! I should have the fifth and final (for now) video in this series out later on this week. Thanks for watching!

7 Likes

that’s amazing! Very usefull tutorial series

1 Like

That’s so cool! Even though I don’t code I gave some parts of it a look, and this looks amazing!
I think this will benefit the community a lot! :smile:

1 Like

I have returned to showcase the fifth video in this tutorial series! In this video, I show you how to make HTML manuals for your Krita plugins, as well as how to create customizable keyboard shortcuts for them using .action XML files. Direct link below:

https://www.youtube.com/watch?v=z5LzJLcmq50

I hope this is helpful to you! I think this will be the final video in this series for now. Thank you all for your kind words in this thread!

7 Likes

If krita had a blender market-like mall, hopefully there would be more and more plug-ins.

3 Likes

Thanks chief. This is amazing

3 Likes