intermediate lesson 3

Extensions

Build other functionality into your plugin that is not a docker

Non-docker plugins

Dockers are great plugins, but there are a lot of things that you might need that is more than a single docker. You might want to create a series of entries in the main menu that relate to your workflow. You also might want to create multiple windows and dialogs with one plugin for managing something. Extensions are great for this. Extensions are also plugins like dockers so the anatomy and structure of an Extension is very similar.

To start with a blank template with an extension, you can start off with this blank extension template.

Download blank extension template ZIP

Unzip these contents and put it in the pykrita folder like the previous lesson went over. The extension will appear in the python plugin manager when you start Krita. Enable it to make sure it runs on the next Krita restart.

The files for this template are named slightly different, but otherwise this template is almost pretty identical. The setup is a bit different if you go into the __init file.

from .extension_template import ExtensionTemplate

Krita.instance().addExtension(ExtensionTemplate(Krita.instance()))

This just says that we need to add an extension and point to the class that will have the logic (extension_template.py)

Main extension template class

An extension is a class like a Docker, but has a couple different functions that it uses by default. Let's open up our extension_template.py and see what it contains.

from krita import *

class ExtensionTemplate(Extension):

    def __init__(self, parent):
        super().__init__(parent)

    # Krita.instance() exists, so do any setup work
    def setup(self):
        pass

    # called after setup(self)
    def createActions(self, window):
        pass

Both the setup() and createActions() methods are called automatically while Krita is starting up. The setup() happens slightly before the createAction() happens, but both have the krita.instance() available for you to use to start working. The __init__ is called when the extension first loads. It is required that it has super().__init__(parent). If you try to remove that line you will notice your plugin will stop loading

Adding actions to your extension

Let's add an action item to our extension. This will add a main menu item in our Tools > Scripts area that shows the version of Krita we are using.

from krita import *
from PyQt5.QtWidgets import QWidget, QAction, QMessageBox

class ExtensionTemplate(Extension):

    def __init__(self, parent):
        super().__init__(parent)

    def setup(self):
        pass

    def system_check(self):
        # QMessageBox creates quick popup with information
        messageBox = QMessageBox()
        messageBox.setInformativeText(Application.version())
        messageBox.setWindowTitle('System Check')
        messageBox.setText("Hello! Here is the version of Krita you are using.");
        messageBox.setStandardButtons(QMessageBox.Close)
        messageBox.setIcon(QMessageBox.Information)
        messageBox.exec()

    def createActions(self, window):
        action = window.createAction("", "System Check")
        action.triggered.connect(self.system_check)
< Previous Lesson
Anatomy of a plugin
Next Lesson >
Learning from other plugins

Have questions, need help, or find a typo?

Head to the Krita artists where we have a dedicated area for plugin development and give any feedback you might have.

;