This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Python: Creating Transparency Masks

Tags: None
(comma "," separated)
User avatar
ren062408
Registered Member
Posts
29
Karma
0
OS

Python: Creating Transparency Masks

Sat Oct 27, 2018 7:56 pm
Good day everyone,

I am having a problem adding a transparency mask node to a paint layer. Here's my code sample:

Code: Select all
from krita import *
doc = Krita.instance().activeDocument()
root = doc.rootNode()

newNode1 = doc.createNode('groupLayer1', 'groupLayer')
root.addChildNode(newNode1, None)

newNode2 = doc.createNode('paintLayer1', 'paintLayer')
newNode1.addChildNode(newNode2, None)

newNode3 = doc.createNode('paintLayer2', 'paintLayer')
root.addChildNode(newNode3, None)

newNode4 = doc.createNode('transparencyMask1', 'transparencyMask')
#newNode3.addChildNode(newNode4, None)

The script runs fine up to the point where the script adds the 'paintLayer2' to the document. So in the end, we'll end up with, from top to bottom, 'paintLayer2', 'groupLayer1' with a child node 'paintLayer1', and then a 'Layer 1'.

But if we were to remove the comment on the last line to add a transparency mask to our 'paintLayer2':
Code: Select all
#newNode3.addChildNode(newNode4, None)

Krita will freeze up until we close the program down. Interestingly, if we do:
Code: Select all
from krita import *
doc = Krita.instance().activeDocument()
root = doc.rootNode()

newNode1 = doc.createNode('groupLayer1', 'groupLayer')
print(newNode1.name())
newNode2 = doc.createNode('transparencyMask1', 'transparencyMask')
print(newNode2.name())

the output is:
groupLayer1
effect


Don't really know what's going on, can you guys help me?
User avatar
scottpetrovic
Registered Member
Posts
520
Karma
4
OS
I don't really know why it is crashing with that. I am not sure if it is a bug...or if there is another way you are supposed to be adding transparency layers. I simplified your script a bit and it still seems to crash

from krita import *
doc = Krita.instance().activeDocument()
root = doc.rootNode()

newNode1 = doc.createNode('custom named layer', 'paintLayer')
root.addChildNode(newNode1, None)

newNode4 = doc.createNode('transparencyMask1', 'transparencyMask')

# this line crashes things
newNode1.addChildNode(newNode4, None)
User avatar
ren062408
Registered Member
Posts
29
Karma
0
OS
scottpetrovic wrote:I don't really know why it is crashing with that. I am not sure if it is a bug...or if there is another way you are supposed to be adding transparency layers. I simplified your script a bit and it still seems to crash

from krita import *
doc = Krita.instance().activeDocument()
root = doc.rootNode()

newNode1 = doc.createNode('custom named layer', 'paintLayer')
root.addChildNode(newNode1, None)

newNode4 = doc.createNode('transparencyMask1', 'transparencyMask')

# this line crashes things
newNode1.addChildNode(newNode4, None)

Thank you very much for taking the time to look at this. That last line of code is really crashing things, really hard to understand why tho. What really mystifies me to no end is when running: newNode4.name() it returns 'effect' as a name instead of 'transparencyMask1', I think it is referencing a hidden node that's named 'effect' and is causing krita to crash as we attach the new node as a child. Really don't know if it's because I am using the transparency mask wrong or it's a bug.

Also I am trying an alternative way:
Code: Select all
from krita import *

for action in Krita.instance().actions():
   print(action.text())

#Krita.instance().action('transparency_mask').trigger()

but I can't seem to find the right action parameter for transparency mask in order for the last line to work.
User avatar
scottpetrovic
Registered Member
Posts
520
Karma
4
OS
You could try this...

Krita.instance().action('add_new_transparency_mask').trigger()

I am not sure exactly how to find that ID in Python, but I know Krita stores most of its actions in .action files. If you search your krita installation or source code files you should see them all being defined.
User avatar
ren062408
Registered Member
Posts
29
Karma
0
OS
scottpetrovic wrote:You could try this...

Krita.instance().action('add_new_transparency_mask').trigger()

I am not sure exactly how to find that ID in Python, but I know Krita stores most of its actions in .action files. If you search your krita installation or source code files you should see them all being defined.

Thanks a lot. I Did a google search for:
list of Krita Actions "select_opaque"

and found myself here: https://phabricator.kde.org/file/data/z ... dpmch/file

Went to the ' <ActionCollection version="2" name="Krita">' section and found myself the treasures I've been looking for. It seems that the text() property just returns the textual content of the action based on the menus and what I am looking for is the name. Just collected the names and put it in a text file for safe keeping. Wish there was a print(action.name()) tho, so I can easily pull out a list anytime.
User avatar
scottpetrovic
Registered Member
Posts
520
Karma
4
OS
After doing a bit more playing around...it looks like you can access those names from Python. This is probably going to be a better approach then people scraping all those .action files.

Code: Select all
from krita import *

for action in Krita.instance().actions():
   print(action.objectName() + " - " + action.text())
User avatar
ren062408
Registered Member
Posts
29
Karma
0
OS
scottpetrovic wrote:After doing a bit more playing around...it looks like you can access those names from Python. This is probably going to be a better approach then people scraping all those .action files.

Code: Select all
from krita import *

for action in Krita.instance().actions():
   print(action.objectName() + " - " + action.text())

wow, this is way cooler, thanks a lot.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Sogou [Bot]