Introducing Krita’s Scripter Plugin

Now it’s time to break out Scripter, or break it in, in order to do a little scripting in Python on this blog.

Enabling Scripter

If this is your first time using Scripter you may need to enable it in the scripting menu following the instructions in my earlier blog post.

What is Scripter

Scripter is a “Plugin to execute ad-hoc Python code”, or at least that’s the description it gives itself in the Python Plugin Manager section of the settings menu. You can think of Scripter as a basic development environment for Krita Python. Scripter lets you execute Python code within Krita without having to write a complete plugin. It is not an interactive console – pressing return will not execute any code. To execute your code you have to press the Play button (see below). Scripter comprises of two main sections – an editor widget (Editor) and an output widget (Output). It has one menu (File) and two buttons (Run and Debug). The Scripter window looks like this:
blog_180112_scripter_1

You type your code into the Editor press run and anything that would normally be printed is sent to Output. So, to run your first program try this:

Type:

print("Hello world")

on line 1 of the Editor. Then press Run. You should get something that looks like this:

blog_180112_scripter_2

You should note that the Editor supports Python syntax highlighting and warns you to save your script.
If you click the debug button you can step through your script one line at a time. Scripter will also show the values you have named in your script. The debug screen looks like this:

blog_180112_scripter_3

To be honest, I haven’t actually used the debugger that much.

Your script can be saved through File->Save. This will give you a dialog box where you can choose where to save your script. Scripter will remember your last used script across sessions and automatically open it for you (whether you like it or not).

Save your script now.

As at 6 February, Scripter is somewhat inconsistent in how it treats saved files. When you first start Scripter, if you have used it in the past and saved a file it will automatically load the last saved file. If you make changes to the script without saving them it will run the script with the changes. Once you save them, Scripter will only run the file. So any changes you type into the editor will not be executed until you save the file. This is not such a bad thing – if you use a different, external editor, you can make changes and save them and Scripter will run the file as changed.

These additional functions should be in Scripter by the 4.0 release (not in the first beta, but currently in master if you want to compile your own):

  • Lines should auto-indent based on the previous line.
  • Use Tab/Shift-Tab to indent/dedent the selection
  • Use Ctrl-M to toggle comment marks on a selection of lines.

Earlier versions of Scripter had a problem with Python 3.4. If you have trouble running a script and you’re using Python 3.4, update your version of Krita and it should work.

Predefined names:

The main package that provides Krita related functionality is called krita. To access the Krita Python library you can import it like this:

import krita

Spend some time investigating the krita package. Remember though that krita is providing access to wrapped C++ functions. In practical terms this means that almost all of the attributes of krita (and other objects you chance upon) are going to be methods. Don’t expect any properties. Rather, expect all values to be accessed via getters and setters. For example to get the active document in Krita you’re going to have to call the Application.activeDocument() method, rather than accessing Application.activeDocument.
The krita package provides access to an object krita.krita that is different from the object referred to by by the Krita name, but seems to have the same attributes.

There are a couple of predefined names that are always available to you:
Krita, Application.  This means you can get access to stuff related to the current instance of Krita. For example

  • Krita.activeDocument() – the active document, if any
  • Krita.extensions() – extensions loaded (typically Python scripts)
  • Krita.views() – views (often only one) currently used by Krita.

3 thoughts on “Introducing Krita’s Scripter Plugin

  1. Hello,

    I recently knew about the Scripting in Krita, I give it a try and some doubts come out with the Scripter:

    —I tried a simple thing, duplicate a selected layer and rotate it using the next code:

    from krita import *
    nodes = Krita.instance().activeWindow().activeView ().selectedNodes ()
    for node in nodes:
    node.duplicate()
    node.clone()
    node.rotateNode(5)
    # I use both because I don’t actually know the difference

    —The problem was that it didn’t duplicate the layer, I can’t even use the setVisible() seeing a visual feedback in the active view, the only one that worked for me was rotateNode() (it gives me visual feedback in the canvas)

    —are there some access limitations if I test a script in the Scripter like this?

    Regards

    Like

Leave a comment