Create An Extension Skeleton

In this post you will learn the basics of a skeleton extension for Krita, and how to load and run it. See the docs! If you have a single Python file that you want to run once off or occasionally you can do that through the scripter extension. However, if you want to run it through Krita’s Tools->Scripts menu then you will need to create a Python extension. In this post you’ll learn the bare bones of getting a script noticed by Krita.

[Update March 2018: Added reference to new Manual.html file]

[Update June 2018: updated docs ref, changes to code from new createActions structure]

Set up the files and directory structure

An extension is a script with additional files packaged together. An extension comprises of:

  • a script
  • a directory holding the script
  • a file called __init__.py in the same directory
  • zero or more other files that the script relies on, usually also in that directory or its subdirectories; and, finally,
  • a file ending in .desktop that contains metadata about the script.
  • [Update:] a Manual.html file in the script’s directory

Find the right directory

In order for Krita to load your extension all of these bits need to be in the right place for Krita to read them.  In practical terms, that means that everything has to go in Krita’s resources directory. You can find Krita’s resource directory like this:

  • start Krita
  • click Settings->Manage Resources
  • this opens a dialog box with buttons down the right hand side. Click the bottom button marked Open Resource Folder.

That should open a file manager at your resources directory! That’s where you’re going to be putting your files. On Linux that directory should be in ~/.local/share/krita/pykrita

Make a File for the Metadata and a Directory for the Extension

Call your extension my_extension. In your resources directory create:

  • a (text) file with the name my_extension.desktop; and
  • a subdirectory called my_extension.

Create the Files You’re Going to Need

They’re both empty at the moment. You’ll fix that soon. In the subdirectory called my_extension create two new (empty) Python files called:

  • __init__.py; and
  • my_extension.py; and
  • Manual.html

Create the Package

In the __init__.py file put this code:

from .my_extension import *

That’s just one line. And it’s “from dot my_extension import star”. Don’t miss the dot.

Create the Metadata

In the my_extension.desktop file put:

[Desktop Entry]
Type=Service
ServiceTypes=Krita/PythonPlugin
X-KDE-Library=my_extension
X-Python-2-Compatible=false
Name=The Name of your Extension
Comment=A comment you want shown in the list of Python scripts. 

The main things you’ll change when you write other extensions are: X-KDE-Library, Name and Comment. The last two are, I hope, self explanatory.  X-KDE-Library is the name of the directory that your extension is in.

Write the Script

The bare minimum code you need to get your script into the Tools-> Scripting menu on Krita is:

from krita import Extension,  Krita

class MyExtension(Extension):

    def __init__(self, parent):
        #Always initialise the superclass.
        #This is necessary to create the underlying C++ object 
        super().__init__(parent)

    def setup(self):
        pass

    def createActions(self, window):
        action = window.createActions("name_of_action", "Menu Entry for Script","tools/scripts")
        # parameter 1 =  the name that Krita uses to identify the action
        # parameter 2 = the text to be added to the menu entry for this script
        # parameter 3 = location in Krita menu to put this script entry
        action.triggered.connect(self.action_triggered)
        
    def action_triggered(self):
        pass # your active code goes here. 

# And add the extension to Krita's list of extensions:
app=Krita.instance()
extension=MyExtension(parent=app) #instantiate your class
app.addExtension(extension) 

This Extension doesn’t actually do anything! I will analyse the script, and make it do something in a following post.

[Update]

Write the Manual

Krita parses the contents of the Manual.html file and displays it in the bottom pane of the Python Plugin Manager when you are enabling (or disabling) your scripts (see the next section).   A basic skeleton for the manual looks like this:





Title


Name of the Script

Tell people about what your script does here. This is an html document so you can format it with html tags.

Usage

Tell people how to use your script here.

Enable Your Script

Finally, you need to enable your script in the settings dialogue. Krita won’t list your scripts in the scripting menu until you have manually enabled them. See my post on enabling scripts for how to do that. When you are enabling your script you have to look for it under the name you gave it in the desktop file. In the example above you would look under “T” for “The Name of your Extension” since that’s the name in the desktop file.

A note on naming conventions

Krita uses an application framework called Qt5. It uses a camelCase naming convention. Python, on the other hand uses BiCaps for class definitions and lower_case_with_underscores for other names. Whichever convention you chose you’re going to have trouble because, sooner or later, you’re going to have to access both Qt5 functions and functions from Python’s Standard Library and you’re naming convention can’t be consistent with both of those. I think it is best to stay with Python naming conventions. At least that way you are implicitly identifying your code as Python code.

One thought on “Create An Extension Skeleton

Leave a comment