Analyze a plugin and see how Krita reads and uses them
To make things easier we are going to start with a blank docker template that has the skeleton all set up.
Download blank docker template ZIPAfter you download the zip, unzip the contents in the local pykrita folder. This is where Krita store all the local plugins.
Let's look at the files the plugin created. Krita stores its plugins in a local directory on your computer. All the plugins are stored in the pykrita folder. When Krita starts, it looks in this folder for plugins to load. If you have ever used Krita as an artist and imported a plugin, this is the location where it loaded the files to. Copy the ZIP files to the location so it looks like this...
Tip
You will only see custom plugins stored in your pykrita folder. All of the plugins that come pre-installed with Krita are inside the folder where Krita was installed.
When we first go into the pykrita folder, you will see a .desktop file and a folder with your plugin name. The .desktop file is how Krita first reads the information about the plugin. If you have other custom plugins installed, they will also have a folder and a desktop file like this.
The .desktop file is just a text file. Open it up in a text editor and examine it. There are only three fields you have to really care about.
If we look into the plugin folder, we will see a few files. The two PY files are the ones that do most of the work. Let's take a look at each one of those.
The __init__ file is what is read first. All plugins will have this file and it is always called __init__.py. Let's open that up first and see what is going on.
The first import is bringing in some classes to help our plugin work as a docker. The second import is saying that there is another file that we are going to use that has the filename "docker_template". The DockerTemplate is the class name we are going to use later.
The next few lines of code is how we setup a docker. This isn't something you really customize that much, so just know that this code is what makes our plugin a docker. The other important thing is the "DockerTemplate". This is the file that is getting used to figure out what our docker is going to do. Let's look at that next.
There isn't really much going on with this file since it is a shell. The __init__(self) is the function that runs when the plugin first starts. The other canvasChanged(self, canvas) function is called every time views are added or removed. You cannot remove this function or there will be errors.
The last part with how plugins are structured is how they are enabled. When you first create, or copy, a plugin to the pykrita folder, the plugin will be disabled by default. You can enable the plugin from the python manager in Krita. Where is this enabled state stored? That is saved in the kritarc configuration file. When you enable or disable a plugin, the kritarc file gets updated like this.
With this setting saved, Krita knows what it needs to load when it starts up.
Tip
The kritarc file is where all application settings are saved in Krita. kritarc is just a text file, so you can open it in any text editor. The "rc" at the end of this file stands for "run command".
When you start developing plugins you will almost certainly run into issues at some point. If you start Krita and your plugin isn't doing anything check the python plugin manager. Look for your plugin and see if it is greyed out. If it is, that means there was some issue loading it
If you hover over your disabled plugin, Krita will give you a message with the error and what is going on. Sometimes loading plugins will give you the error message when you load Krita. Other times you will need to check the python plugin manager to see why it is disabled. If your plugin is enabled and still not working, that means there is something wrong with your logic where it isn't being done correctly. The only way to figure that out is to analyze the code, add print statements, or re-arrange things to understand why the code isn't working
Head to the Krita artists where we have a dedicated area for plugin development and give any feedback you might have.