Newbie Click OK button question

Hi this is my first post in this forum, so hello to everyone here. I’m a total newbie in this, but I like it :slight_smile:
My first script is to run gradient map filter, very simple:

application = Krita.instance()
currentDoc = application.activeDocument()

application.action('krita_filter_gradientmap').trigger()
currentDoc.refreshProjection()

When I run the script, window “Filter: Gradient Map - Krita” is opened, waiting for my input. Since I just want all default settings, I would like to Click on OK button with script to skip required manual action. Is this possible to script?

Thanks in advance for any hint!

You gotta set up the filters manually, see this post:

Thanks, I’ve seen that post… Unfortunately it seems that there is no api for gradient map filter, only action. So I hoped that it is possible to “click” on OK button in active window (“Filter: Gradient Map - Krita”) from within script…

1 Like

I didn’t check them all, but the comments on the Filter.h file says:

  • Currently available filters are:
  • ‘autocontrast’, ‘blur’, ‘bottom edge detections’, ‘brightnesscontrast’, ‘burn’, ‘colorbalance’, ‘colortoalpha’, ‘colortransfer’,
  • ‘desaturate’, ‘dodge’, ‘emboss’, ‘emboss all directions’, ‘emboss horizontal and vertical’, ‘emboss horizontal only’,
  • ‘emboss laplascian’, ‘emboss vertical only’, ‘gaussian blur’, ‘gaussiannoisereducer’, ‘gradientmap’, ‘halftone’, ‘hsvadjustment’,
  • ‘indexcolors’, ‘invert’, ‘left edge detections’, ‘lens blur’, ‘levels’, ‘maximize’, ‘mean removal’, ‘minimize’, ‘motion blur’,
  • ‘noise’, ‘normalize’, ‘oilpaint’, ‘perchannel’, ‘phongbumpmap’, ‘pixelize’, ‘posterize’, ‘raindrops’, ‘randompick’,
  • ‘right edge detections’, ‘roundcorners’, ‘sharpen’, ‘smalltiles’, ‘sobel’, ‘threshold’, ‘top edge detections’, ‘unsharp’,
  • ‘wave’, ‘waveletnoisereducer’]

You are right, I just missed it :slight_smile: Thank you again!

np, you can get the InfoObject parameters via the XML button when you open up the filter dialog (just in case)

Ok… I don’t know how to do this, but I will research. Thanks!

Right click on any layer, add ->filter mask. Click on Map ->gradient map, then set whatever settings you like. After there is an XML button on the top right, click it and it will display all the settings names and values in an XML. That is what you need in your infoObject settings like in that post.

1 Like

A little bit outdated (especially for halftone filter which has been entirely rewritten since I write the post) you can find here a list of filters with their parameters:

Unfortunately for gradient map, it seems I didn’t found how to use it :man_shrugging:

Grum999

Thank you @KnowZero , @Grum999 !!!

Right click on any layer, add ->filter mask. Click on Map ->gradient map, then set whatever settings you like. After there is an XML button on the top right, click it and it will display all the settings names and values in an XML. That is what you need in your infoObject settings like in that post.

That’s all I needed :smiley: Thanks!

My code works like a charm :smiley: Thanks again for support!

from krita import *

# work on current document
activeDoc=Krita.instance().activeDocument()

# get active node
activeNode=activeDoc.activeNode()

# define filter properties
configParameters=InfoObject()
configParameters.setProperties({
        'colorMode': 0
    })
    
# create filter
filter=Filter()
filter.setName('gradientmap')
filter.setConfiguration(configParameters)

# define selection on which filter will be applied (here all document area)
selection=Selection()
selection.select(0,0,activeDoc.width(), activeDoc.height(), 255)

# create filter layer
filterLayer=activeDoc.createFilterLayer("Gradient Filter layer", filter, selection)

# add it to document, above active node
activeNode.parentNode().addChildNode(filterLayer, activeNode)
1 Like

Great to hear :slight_smile:

If you ever say want to use more than just the default config and the settings of this or any other filter confuse you, just copy the xml of the filter you want here and it will output to you what to put in the infoobject:

from krita import *
from PyQt5.QtCore import *
from PyQt5.QtXml import *


xml = '''<!DOCTYPE params>
<params version="2">
 <param type="internal" name="colorMode">0</param>
 <param type="internal" name="dither/noiseSeed">846930886</param>
 <param type="string" name="dither/pattern"><![CDATA[DITH 0202 GEN ]]></param>
 <param type="internal" name="dither/patternValueMode">0</param>
 <param type="internal" name="dither/spread">1</param>
 <param type="internal" name="dither/thresholdMode">0</param>
 <param type="string" name="gradientXML"><![CDATA[<gradient type="stop" name="Unnamed">
 <stop stoptype="0" offset="0" bitdepth="U8" alpha="1">
  <RGB r="0.815686285495758" b="0" g="0" space="sRGB-elle-V2-srgbtrc.icc"/>
 </stop>
 <stop stoptype="0" offset="0.521920668058455" bitdepth="U8" alpha="0.47843137383461">
  <RGB r="0.945098042488098" b="0.458823531866074" g="0.733333349227905" space="sRGB-elle-V2-srgbtrc.icc"/>
 </stop>
 <stop stoptype="0" offset="0.816283924843424" bitdepth="U8" alpha="0.858823537826538">
  <RGB r="0.0313725508749485" b="0.0784313753247261" g="0.0431372560560703" space="sRGB-elle-V2-srgbtrc.icc"/>
 </stop>
 <stop stoptype="0" offset="0.832985386221294" bitdepth="U8" alpha="0.168627455830574">
  <RGB r="0.945098042488098" b="0.458823531866074" g="0.733333349227905" space="sRGB-elle-V2-srgbtrc.icc"/>
 </stop>
 <stop stoptype="0" offset="1" bitdepth="U8" alpha="0">
  <RGB r="0" b="0" g="0" space="sRGB-elle-V2-srgbtrc.icc"/>
 </stop>
</gradient>
]]></param>
</params>'''

props = {}

doc = QDomDocument()
if doc.setContent(xml):
    e = doc.documentElement()
    n = e.firstChild()
        
    while not n.isNull():
        e = n.toElement()
        if not e.isNull():
            if e.tagName() == "param":
                if e.attributes().contains("type"):
                    type = e.attribute("type")
                    name = e.attribute("name")
                    value = e.text()
                        
                    if type == "bytearray":
                        props[name] = QVariant(QByteArray.fromBase64(value.toLatin1()))
                    else:
                        props[name] = value
                else:
                    props[e.attribute("name")] = QVariant(e.text())
        n = n.nextSibling()
else:
    print ("Failed to set XML")
print ("props",props)

Edit: fixed typo

That was exactly the reason why I put only one parameter :smiley: Thanks a lot!