Krita 5 - Python API: Annotations

Hi

@halla
I’m testing annotations, an I have 2 remarks

The first one

I think it’s a bug.
But maybe I use annotation in an incorrect way but…

Here the code of my test:

from krita import *

fileName="/home/grum/Temporaire/test.kra"

doc = Krita.instance().createDocument(300, 300, "Test", "RGBA", "U8", "", 300.0)
#Krita.instance().activeWindow().addView(doc)

for i in range(3):
    text=f"{i}={i*' * '}"
    doc.setAnnotation(f'test{i}', f"description {i}", QByteArray(text.encode()))

print("-set------------")
for annotation in doc.annotationTypes():
    print("annotation: ", annotation, "description: ", doc.annotationDescription(annotation))
    ba=doc.annotation(annotation)
    print(bytes(ba).decode())
    
doc.saveAs(fileName)
doc.close()


doc=Krita.instance().openDocument(fileName)

print("-loaded------------")
for annotation in doc.annotationTypes():
    print("annotation: ", annotation, "description: ", doc.annotationDescription(annotation))
    ba=doc.annotation(annotation)
    print(bytes(ba).decode())

doc.close()

Result is:

-set------------
annotation:  test0 description:  description 0
0=
annotation:  test1 description:  description 1
1= * 
annotation:  test2 description:  description 2
2= *  * 
-loaded------------
annotation:  test2 description:  description 2
2= *  * 
annotation:  test1 description:  description 1

annotation:  test0 description:  description 0

As you can see:

  • Annotations are OK when just created
  • Annotations are KO after document is loaded
    – annotation identifier is OK
    – annotation description is OK
    – annotation data are KO; only the last one is loaded

Looking kra file, everything is OK (maindoc.xml + all annotations files contains expected data)

So it’s seems problem is during file loading, only the last annotation content is loaded.

=> Do I open a bug? as it’s still in prealpha…

The second one

Open a file
Set and/or remove annotation

File is considered as not modified/
Then close the file, all annotations changes are lost

I think it’s not possible to add annotation in undo/redo stack, but at least when annotations are modified, file status should be changed to “modified”, otherwise:

  • that’s sure, some plugin annotations will be lost
  • plugins developers will try dirty and tricky things to force file to be in “modified” status…

Otherwise, it’s very simple to use :slight_smile:

Grum999

2 Likes

Thx ! really useful, I like it.

Yes, please do open a bug. I had almost missed your message here :slight_smile:

Ah thanks :slight_smile:
I didn’t bumped the topic because I wasn’t considering this in high priority for now :sweat_smile:

Bug 434244 has been created
Bug only refers to first point (the bug)

The second point is more an improvement than a bug I think, so I didn’t opened a bug for it.
What is the best thing to do for it?

Grum999

Please add it to the report as a comment – if I touch this code again, I might as well look into both things.

Ok, comments added to bug

Thanks :slight_smile:

Grum999