https://bugs.kde.org/show_bug.cgi?id=426349

--- Comment #9 from Dmitry Kazakov <dimul...@gmail.com> ---
After my patches the following script works fine:

===========================================================

from krita import *
from PyQt5.Qt import *
from PyQt5 import QtCore
from PyQt5.QtCore import (
        QByteArray,
        QPoint
    )
from PyQt5.QtGui import (
        QColor,
        QImage,
        QPixmap
    )


def buildQImage(color, w, h):
    """Generate a QImage to use for example"""
    img=QImage(w, h, QImage.Format_ARGB32_Premultiplied)
    img.fill(Qt.transparent)
    pxmTgt = QPixmap.fromImage(img)

    gradientb = QRadialGradient(QPointF(w/3, h/4), 2*w/3)
    gradientb.setColorAt(0, color)    
    gradientb.setColorAt(1, Qt.black)

    gradientf = QRadialGradient(QPointF(w/3, h/4), 2*w/3)
    gradientf.setColorAt(0, Qt.white)
    gradientf.setColorAt(0.15, Qt.transparent)
    gradientf.setColorAt(1, Qt.transparent)

    canvas = QPainter()
    canvas.begin(pxmTgt)
    canvas.setRenderHint(QPainter.Antialiasing)

    canvas.setPen(Qt.NoPen)

    canvas.setBrush(gradientb)
    canvas.drawEllipse(QRect(20,20,w-40, h-40));

    canvas.setBrush(gradientf)
    canvas.drawEllipse(QRect(20,20,w-40, h-40));

    canvas.end()
    return pxmTgt.toImage()

def setLayerFromQImage(layerNode, image):
    """Set QImage as layer content"""
    position = QPoint(0, 0)
    ptr = image.bits()
    ptr.setsize(image.byteCount())

    layerNode.setPixelData(QByteArray(ptr.asstring()), position.x(),
position.y(), image.width(), image.height())

def sleep(value):
    """Do a sleep of `value` milliseconds"""
    loop = QEventLoop()
    QTimer.singleShot(value, loop.quit)
    loop.exec()

dWidth=500
dHeight=500

# Cyan, Magenta, Yellow: colors used to generate layers
colors=[QColor(0,255,255), QColor(255,0,255), QColor(255,255,0)]
sleepValues=[0, 125]

image=buildQImage(QColor(255,0,0), dWidth, dHeight)

newDocument = Krita.instance().createDocument(dWidth, dHeight, "Test", "RGBA",
"U8", "", 300.0)
Krita.instance().activeWindow().addView(newDocument)


parentGroupLayer = newDocument.createGroupLayer('Group layer')
newDocument.rootNode().addChildNode(parentGroupLayer, None)

for i in range(len(colors)):
    # loop over colors and apply basics actions like plugin Newspaper (and
Channel2Layers) does
    newPLayer = newDocument.createNode(f"PaintLayer{i}", 'paintlayer')
    setLayerFromQImage(newPLayer, image)
    parentGroupLayer.addChildNode(newPLayer, None)

    infoObject = InfoObject();
    infoObject.setProperty("color", colors[i])
    selection = Selection();
    selection.select(0, 0, dWidth, dHeight, 255)

    newFLayer = newDocument.createFillLayer(f"Color{i}", "color", infoObject,
selection)
    parentGroupLayer.addChildNode(newFLayer, newPLayer)

    # mandatory as when provided to createFillLayer(), infoObject is not
applied
    # must also be applied after node has been added to parent...
    newFLayer.setGenerator("color", infoObject)
    newFLayer.setBlendingMode('add')

    newLayer = newFLayer.mergeDown()

    # returned layer from merge can't be used, so get the last one
    currentProcessedLayer = parentGroupLayer.childNodes()[-1]
    currentProcessedLayer.setBlendingMode('multiply')

===========================================================

Though other methods of Python API still have the same problems

-- 
You are receiving this mail because:
You are watching all bug changes.

Reply via email to