Scripting - open an existing file

Hi guys,
I have a question, I’ve read the scripting introduction of Krita (this one Krita Scripting School ) , and I dug just a bit into the classes references before asking and I couldn’t find the solution. The problem is that I would really like to open a file using one command and directly put the filename in it, instead of calling the action and than manually select the file.

Actually this is part of a more general problem where I wanted to combine automatically different layers that were previously put in different files, maybe you have a solution to this one.

If I missed something I’m really sorry, I’m not a programmer. Also, if I misused this forum (e.g. I used the wrong category or similar), I’m really sorry for that too.

Thanks in advance

By file you mean opening an image as a document?
Krita.instance().openDocument("PATH TO FILE")

Yes, that was it, also my question was indeed stupid, but I just missed it from the tutorial.

Now I have another question, I’m not opening another discussion because I think it will be short.
Now I need to copy a layer from a document and paste it to another. I tried to use “setActiveDocument” to switch document, but maybe it’s not what I should do, or I’m using it the wrong way.

Not sure about view / layer activation. (there are some confusion in how to set currently active layer)
But any how, following should help in getting something to work.

"""
https://api.kde.org/appscomplete-api/krita-apidocs/libs/libkis/html/classDocument.html
https://scripting.krita.org/action-dictionary
"""

from krita import Krita


def find_first_node(doc):
    return next(iter(doc.topLevelNodes()), None)

def find_and_activate_view(doc):
    app = Krita.instance()
    for win in app.windows():
        for view in win.views():
            if view.document() == doc:
                win.activate()
                win.showView(view)
                view.setVisible()
                return

def create_document(name):
    app = Krita.instance()
    doc = app.createDocument(1000, 1000, name, 'RGBA', 'U8', '', 120.0)
    first_node = find_first_node(doc)
    first_node.setName(f'{name}_background')
    app.activeWindow().addView(doc)
    return doc

def copy_and_paste(src, dst):
    app = Krita.instance()
    copy_action = app.action('copy_layer_clipboard')
    paste_action = app.action('paste_layer_from_clipboard')
    # copy from src
    find_and_activate_view(src)
    first_node = find_first_node(src)
    app.setActiveDocument(src)
    src.setActiveNode(first_node)
    copy_action.trigger()
    # paste to dst
    find_and_activate_view(dst)
    first_node = find_first_node(dst)
    app.setActiveDocument(dst)
    dst.setActiveNode(first_node)
    paste_action.trigger()

# create some documents
doc_original = create_document('original')
doc_a = create_document('destination_a')
doc_b = create_document('destination_b')

# copy and paste original to destinations
copy_and_paste(doc_original, doc_a)
copy_and_paste(doc_original, doc_b)

/AkiR

1 Like