Is it possible to get array of brush presets from tag/bundle?

One more newbie question :smiley:
Is it possible to print/get array of brushes with a tag or array of brushes from (named) bundle maybe?
I mean with python script…
Thanks in advance for any hint!

Hi

API only provides basic access to resources.
And there’s currently no information about tags.

It’s technically possible to list brushes from a bundle and/or from a tag: with Krita 5 resources are stored in a SQLite database file.

So, it’s easy to execute query to get exactly what you need, database schema is not complex.

Here a basic working example:

import os.path
import krita
from PyQt5.QtSql import (QSqlDatabase, QSqlQuery)
from PyQt5.Qt import *


dbFile=os.path.join(QStandardPaths.standardLocations(QStandardPaths.DataLocation)[0], 'resourcecache.sqlite')


db=QSqlDatabase.addDatabase("QSQLITE", "dbResources")
db.setDatabaseName(dbFile)


if db.open():
    sqlQuery=QSqlQuery(db)
    if sqlQuery.exec("""
            SELECT r.name AS brushName, s.location AS bundle
            from tags t
                 join resource_tags rt
                   on t.id=rt.tag_id
                 join resources r
                   on r.id = rt.resource_id
                 join storages s
                   on s.id = r.storage_id
            where t.name='Digital'
            """):

        while sqlQuery.next():
            print("Brush:", sqlQuery.value('brushName'), "/ Bundle:", sqlQuery.value('bundle'))
            
        sqlQuery.finish()
        
    db.close()

Result:

Brush: b)_Basic-1 / Bundle: Krita_4_Default_Resources.bundle
Brush: k)_Blender_Basic / Bundle: Krita_4_Default_Resources.bundle
Brush: k)_Blender_Blur / Bundle: Krita_4_Default_Resources.bundle
Brush: k)_Blender_Pixelize / Bundle: Krita_4_Default_Resources.bundle
Brush: k)_Blender_Smear / Bundle: Krita_4_Default_Resources.bundle
Brush: t)_Shapes_Fill / Bundle: Krita_4_Default_Resources.bundle
Brush: v)_Distort_Grow / Bundle: Krita_4_Default_Resources.bundle
Brush: v)_Distort_Move / Bundle: Krita_4_Default_Resources.bundle
Brush: v)_Distort_Shrink / Bundle: Krita_4_Default_Resources.bundle
Brush: v)_Experimental_Webs / Bundle: Krita_4_Default_Resources.bundle
Brush: v)_Sketching-1_Chrome_Thin / Bundle: Krita_4_Default_Resources.bundle
Brush: v)_Sketching-2_Chrome_Large / Bundle: Krita_4_Default_Resources.bundle
Brush: v)_Sketching-3_Leaky / Bundle: Krita_4_Default_Resources.bundle

Notes concerning SQLite database access from multiple processes

According to SQLite documentation, reading database should be OK on a Linux platform as multiple processes can have the same database file opened at the same time if you only execute SELECT statements.

In other case you may encounter some problems: (5) Can multiple applications or multiple instances of the same application access a single database file at the same time?

SELECT statement should be safe even if it’s better to use API.
In all case, never execute statement to modify database content (or if you do it, do it at your own risk, you an broke your resource database…)

Grum999

2 Likes

Thank you, @Grum999 !!! I’ve been quite proficient with sql… some 20 years ago… :smiley: so this should be really easy. Yes, I understand risk of changing relational db content, basically I just want to list brush presets. Thanks for example!