Duplicating a Layer

In an earlier post Eric asks about how to duplicate a layer in Krita. It is possible, but how to do it is not obvious. In this post you learn how.  The trick is to remember that Krita is written in C++ and that means you need to be express about most things. In particular, you have to manually insert the duplicated node.  You can work in Scripter for this project.

1. Create a document

First, start Krita and create a new document. The document that Krita creates should have two layers in it already – called Layer 1 and Layer 2.  (If it doesn’t you’ll need to create your own layer called Layer 2 or this won’t work):

blog_180611_layer_duplicate1

2.  Start Scripter

You should know how to do this.  If not check the blog post.

3. Get a reference to the layer.

To get the layer, you’ve first got to get the document object:

doc = Krita.activeDocument()

You’re going to replicate the layer called Layer 2. In Krita, layers are called nodes, so you need to get a node with the name Layer 2. The document object has a method, nodeByName, that, given the layer’s name, returns the node of that name.

node_name = "Layer 2"
node = doc.nodeByName(node_name)

You can check you’ve got the right node, by checking its name (names are not unique, but at the moment there are only two nodes):

print(node.name())

4. Duplicate the layer

The node object has a method called duplicate that, duplicates itself. The only thing to note is that it returns a reference to the duplicated node and you have to keep the reference:

dupe = node.duplicate()

5. Rename the layer

The duplicated node has the same name as the node that was duplicated:

print(dupe.name())

Layer 2

So change the name of the duplicate:

dupe.setName("Duplicate Layer")
print(dupe.name())

Duplicate Layer

6. Insert the new Layer

This is the hard part! Not that it is difficult but, rather, it’s  knowing what to do. Duplicate just duplicates the node, it doesn’t actually attach it to the document. To do that you need to find the root node and add the duplicate as a child of that root:

root_node = doc.rootNode()
root_node.addChildNode(dupe, None)

The method takes two parameters – the node to be inserted and the node to be inserted above.

blog_180611_layer_duplicate2

You’ve now duplicated that layer. If you don’t believe me, draw on Layer 2 and run the code again, check the duplicate has the drawing you’ve just made.

7. The Full Code

Here’s the full code:

doc = Krita.activeDocument()
node_name = "Layer 2"
node = doc.nodeByName(node_name)
print(node.name())
dupe = node.duplicate()
print(dupe.name())
dupe.setName("Duplicate Layer")
print(dupe.name())
root_node = doc.rootNode()
root_node.addChildNode(dupe, None)

Be careful. Each time you run this you’ll get a new duplicate.

One thought on “Duplicating a Layer

  1. Hello!

    I try to duplicate layer and copy it to new document:

    kro = Krita.instance()
    docPre = kro.activeDocument()
    act = docPre.activeNode()
    dup = act.duplicate()
    docNxt = kro.createDocument(docPre.width(), docPre.height(), act.name(), docPre.colorModel(), docPre.colorDepth(), docPre.colorProfile(), docPre.resolution())
    root = docNxt.rootNode()
    root.addChildNode(dup, None)
    kro.activeWindow().addView(docNxt)
    docNxt.refreshProjection()

    The first time everything works fine, but after this Krita can crashes at any time.
    If I try to use it a second time, it crashes immediately.
    What am I doing wrong?

    Like

Leave a comment