Skip to content

Python API: Shape selection and InfoObject bugfix

Know Zero requested to merge knowzero/krita:python-api-shape-selection into master

One last API update before beta! :), well unless the beta gets delayed(hopefully not)...

Python API: Shape selection Add the ability to select and deselect vector shapes via the Python API

Bugfix: Python API InfoObject fails to return properties When the InfoObject contains a KoColor such as with a Fill Layer, the properties fails. This patch checks if any property is a KoColor and changes it to an XML output.

Edit: Tested that it works on windows

Test Plan

Run this python script:

from PyQt5 import QtCore, QtGui, QtWidgets
from krita import *
import re


def stepper(i):
    print ("STEP", i)
    doc = Krita.instance().activeDocument()
    node = doc.activeNode()
    
    if i == 0:
        print ("Creating Vector Layer")
        newLayer = doc.createVectorLayer('VLayer1')
        
        doc.rootNode().addChildNode(newLayer,None)
        print ("Done")

    elif i == 1:
        print ("Create Shape")
        newShapes=node.addShapesFromSvg('''<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"><!-- Created using Krita: https://krita.org --><svg xmlns="http://www.w3.org/2000/svg"                xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:krita="http://krita.org/namespaces/svg/krita" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" width="307.2pt" height="307.2pt" viewBox="0 0 307.2 307.2"><text id="shape0" krita:useRichText="true" krita:textVersion="2" transform="translate(8.9999996710103, 31.4999995065155)" fill="#0026ff" stroke-opacity="0" stroke="#000000" stroke-width="0" stroke-linecap="square" stroke-linejoin="bevel" font-size-adjust="0.372962" font-stretch="normal" letter-spacing="0" word-spacing="0"><tspan x="0">Top Shape2</tspan></text></svg>''')
       
        print ("Done")

    elif i == 2:
        print ("TEST: Select Shape")
        node.shapes()[0].select()
        print ("TEST: Complete")
    elif i == 3:
        print ("TEST: Print selection True")
        print( "RESULT (TRUE):", node.shapes()[0].isSelected() )
       
        print ("TEST: Complete")
    elif i == 4:
        print ("TEST: Unselect shape")

        node.shapes()[0].deselect()
        
        print ("RESULT (FALSE):", node.shapes()[0].isSelected()  )
    
    elif i == 5:
        print ("Setup fill layer")
        s = Selection();
        s.select(0, 0, doc.width(), doc.height(), 255)
        
        inf = InfoObject()
        inf.setProperties({'color': '<!DOCTYPE color>\n<color channeldepth="U16">\n <RGB b="0.854993522167206" g="0.496940553188324" space="sRGB-elle-V2-g10.icc" r="0.318547338247299"/>\n</color>\n'})
        n = doc.createFillLayer("test", "color", inf, s)
        r = doc.rootNode();
        c = r.childNodes();
        r.addChildNode(n, c[0])
        doc.refreshProjection()

        
    elif i == 6:
        print ("FILL LAYER PROPS")
        print ( "RESULT:", node.filterConfig().properties() )
    
        print ("TEST: Complete")

        print ("TEST FINISHED!")


                        
    if i < 6: QtCore.QTimer.singleShot(2000, lambda: stepper(i+1) )
        
print ("Starting Test")  
newdoc = Krita.instance().createDocument(512, 512, "Python test document", "RGBA", "U8", "", 120.0)
Krita.instance().activeWindow().addView(newdoc)
stepper(0)

Formalities Checklist

  • I confirmed this builds.
  • I confirmed Krita ran and the relevant functions work.
  • I tested the relevant unit tests and can confirm they are not broken. (If not possible, don't hesitate to ask for help!)
  • I made sure my commits build individually and have good descriptions as per KDE guidelines.
  • I made sure my code conforms to the standards set in the HACKING file.
  • I can confirm the code is licensed and attributed appropriately, and that unattributed code is mine, as per KDE Licensing Policy.
Edited by Know Zero

Merge request reports