SUMMARY The data() method in Krita's v4.x Resource Python API returns a raw QByteArray which might be useful for consumption in a script. For example, the data() method for workspace resources should return an XML string as a QByteArray, which can then be passed to QMainWindow.restoreState() to activate a specific workspace. However only a handful of resource types return a populated QByteArray, while other resource types return an empty one, including workspaces. STEPS TO REPRODUCE 1. Open the Scripter in a default installation of krita 4.3 or 4.4 2. Paste the following script and run it: ``` python # build a list of resources to test tested_resources = [ Application.resources('pattern')['01_canvas.png'], Application.resources('pattern')['Cross02.pat'], Application.resources('pattern')['DITH 0404 CLUS'], Application.resources('gradient')['Foreground to Background'], Application.resources('gradient')['GPS eye (Blue)'], Application.resources('brush')['A -2 Sparkle 3'], Application.resources('brush')['tree flowers speedpainting strokes'], Application.resources('preset')['a)_Eraser_Circle'], Application.resources('preset')['z)_Stamp_Water'], Application.resources('palette')['Swatch RGB'], Application.resources('palette')['Default'], Application.resources('workspace')['VFX_Paint'], Application.resources('workspace')['Minimal']] # retrieve their data and print its length for r in tested_resources: print(r.data().length()) ``` OBSERVED RESULT ``` 175363 0 0 0 0 0 0 24295 25746 0 0 0 0 ``` EXPECTED RESULT ``` 175363 1636 103 433 893 4139 1659 24264 25712 11165 2594 6862 7672 ``` SOFTWARE/OS VERSIONS Krita Version: 4.4.2-alpha (git 0935bb4) Languages: id_ID, id Hidpi: false Qt Version (compiled): 5.12.9 Version (loaded): 5.12.9 OS Information Build ABI: x86_64-little_endian-lp64 Build CPU: x86_64 CPU: x86_64 Kernel Type: linux Kernel Version: 5.8.0-2-amd64 Pretty Productname: Debian GNU/Linux bullseye/sid Product Type: debian Product Version: unknown Desktop: ADDITIONAL INFORMATION I obtained the expected values by patching Resource.cpp in libkis with the following diff: ``` diff --git a/libs/libkis/Resource.cpp b/libs/libkis/Resource.cpp index d773f367c7..7f4f2d04c7 100644 --- a/libs/libkis/Resource.cpp +++ b/libs/libkis/Resource.cpp @@ -107,7 +107,9 @@ QByteArray Resource::data() const if (!d->resource) return ba; QBuffer buf(&ba); + buf.open(QIODevice::WriteOnly); d->resource->saveToDevice(&buf); + buf.close(); return ba; } ``` But I'm not sure if it makes sense to continue fixing/testing/maintaining the old and creaky resource system and exposing it to python, since the new krita 5.0 resource system is upon us soon. Thoughts?
Related to bug 422949
We'll have to find a new way to implement this, since in master, Resource no longer wraps a KoResourceSP, but just the id, name, filename and thumbnail for a given resource. I don't think we'll be implementing this before Krita 5.0, since we're trying to keep the refactoring to a minimum so we can stabilize.