How to add custom icon to actions via python scripting?

:joy: sometimes its fun to experiment or in my case aesthetic .

You would have to make it into a plugin to load it automatically.

As for how to make it now show text:

from krita import *
qwin = Krita.instance().activeWindow().qwindow()
pobj = qwin.findChild(QToolBar,'mainToolBar')
for child in pobj.findChildren(QToolButton):
	if child.text() == 'Levels':
		child.setIcon( Krita.instance().icon('animation_play') )
		child.setToolButtonStyle(0)

Also since this is an action button a more language agnostic version can search for the action instead:

from krita import *
qwin = Krita.instance().activeWindow().qwindow()
pobj = qwin.findChild(QToolBar,'mainToolBar')
for child in pobj.findChildren(QToolButton):
	if child.defaultAction() == Krita.instance().action('krita_filter_levels'):
		child.setIcon( Krita.instance().icon('animation_play') )
		child.setToolButtonStyle(0)

That said, if your goal is only to change icons the action way is easier than coding

Also another option might be this:

It’s an incomplete plugin, but it is capable of creating more than the default 2 toolbars in Krita and customize icons. It can also do other space saving things like program different commands for right click and left clicking icons

@KnowZero
Thanks. You said the action is easier than coding, but I see from the Krita manual about how to make a plugin requires more coding. It’s tough to know what to do though because it has too much information.

Although they do have a simplified section, they say “in your Krita resources/pykrita directory create a folder called myplugin”
Except I do not have a resources folder. Am I supposed to create that too? Well I did, i created all the files necessary. My plugin does not show up in the Plugin Manager.

So you never used Krita before? Cool! What for do you need extra buttons, if you don’t use Krita? :thinking:
Sorry for joking, but if you ever run Krita only one time successful, you will have a resources-folder!

‘‘Settings’’ >> ‘‘Manage Resources…’’ >> ‘‘Open Resource Folder’’, there you’ll find the folder “pykrita” as well as the folder “actions”, and in actions you theoretically should find a few *.action-files, at least if you have installed a few plugins it is very likely that there will be some.
And if you take such an action-file and open it with your editor, helpful would be an editor with code-indentation and code-highlighting, but notepad will do too, then you’ll see something like this (it is the pick_color.action-file):

<?xml version="1.0" encoding="UTF-8"?>
<ActionCollection version="2" name="Scripts">
    <Actions category="Scripts">
        <text>Pick Color</text>

        <Action name="PickColor">
        <icon></icon>
        <text>Pick color from current layer with a simple keypress</text>
        <whatsThis></whatsThis>
        <toolTip></toolTip>
        <iconText></iconText>
        <activationFlags>0</activationFlags>
        <activationConditions>0</activationConditions>
        <shortcut>x</shortcut>
        <isCheckable>false</isCheckable>
        <statusTip></statusTip>
        </Action>
    </Actions>
</ActionCollection>

and this modified to your needs could look like this:

<?xml version="1.0" encoding="UTF-8"?>
<ActionCollection version="2" name="Scripts">
    <Actions category="Scripts">
        <text>Kracov's Button (a short descriptive text)</text>

        <Action name="Kracov's action (e.g., give it the name you've defined before↑)">
        <icon>name of the icon you have made known to Krita</icon>
        <text>Info about the function of your new button</text>
        <whatsThis>if you want: more info: bla bla</whatsThis>
        <toolTip>if you want a tooltip</toolTip>
        <iconText>if you like more bla bla</iconText>
        <activationFlags>0</activationFlags>
        <activationConditions>0</activationConditions>
        <shortcut>HERE WILL BE SHOWN A SHORTCUT IF YOU'VE SET ONE IN KRITA'S KEYBOARD-SHORTCUTS OR YOU PUT IN ONE DIRECTLY BUT THINK OF COLLISIONS</shortcut>
        <isCheckable>false</isCheckable>
        <statusTip></statusTip>
        </Action>
    </Actions>
</ActionCollection>

If you’ve put in at least the action name and the name of your icon the important things are in, then save it under the name of your new action.

By the way, you wouldn’t happen to want to add an icon to already existing actions without their own icon, since they are displayed as text only and waste space in the toolbars?
Because then you would only need to look these up in an action-file in Krita’s installation-subfolders, where the existing actions are defined.

Michelist

Okay. Well could you point me to a specific subfolder where the actions are located?

In case of Windows and a standard installation:
C:\Program Files\Krita (x64)\share\krita\actions\

In “krita.action” you’ll find most existing actions.

Michelist

it must have changed with a new build. I’m not seeing a Share folder, even in Roaming

Do you have a standard Krita installation on Windows, so no portable Krita?
If so, then please open Explorer and write into the address-bar from Explorer
"cd C:\Program Files\Krita (x64)\share\krita\actions"
without the quotation marks and press enter on your keyboard. This should open the folder, if it exists, and I’m astonished that it shouldn’t be in your installation, even Krita 5.1.5 has it.

You don’t happen to have Krita 4 in use? And what version of Windows are you using?
BTW, did you find the resource folder with my description from above?

Michelist

I have lots of actions in the C:\Program Files\Krita (x64)\share\krita\actions folder.

For the portable .zip package, that is the same folder but in the portable .zip container folder.

I apologize. I was inside the bin folder since I had that up all day. Okay, I’ve found actions. So am I to assume that all I need to do now is replace the tags with the appropriate ones, such as Levels, and it’ll change? I don’t see my action in the toolbar list. Can you be a little more elaborate on how to use actions? Here’s what I have so far:

<?xml version="1.0" encoding="UTF-8"?>
<ActionCollection version="2" name="Scripts">
    <Actions category="Scripts">
        <text>Kracov's Button (a short descriptive text)</text>

        <Action name="levels">
        <icon>krita_filter_levels</icon>
        <text>Info about the function of your new button</text>
        <whatsThis>if you want: more info: bla bla</whatsThis>
        <toolTip>if you want a tooltip</toolTip>
        <iconText>if you like more bla bla</iconText>
        <activationFlags>0</activationFlags>
        <activationConditions>0</activationConditions>
        <shortcut>HERE WILL BE SHOWN A SHORTCUT IF YOU'VE SET ONE IN KRITA'S KEYBOARD-SHORTCUTS OR YOU PUT IN ONE DIRECTLY BUT THINK OF COLLISIONS</shortcut>
        <isCheckable>false</isCheckable>
        <statusTip></statusTip>
        </Action>
    </Actions>
</ActionCollection>

@Michelist
I’m still trying to use google and the krita manual, but it is so unspecific about how to use actions. Mostly it’s a complex collection of codes.

This is actually nothing more than a simple text markup language similar to HTML, it really couldn’t be much simpler, the secrets behind it should be fathomable by comparing a few of these files.

As you can see in the file commented by me not much is mandatory, you need the name of the action, and the name of the icon made known to Krita before.
Just in case you create several of these files over time, I myself would enter a short name here, to be able to distinguish all this later:

<text>Kracov's Button (a short descriptive text)</text>

likewise here:

<text>Info about the function of your new button</text>.

and I would also fill the tooltip with meaningful information that will be displayed when you move the mouse over it.

So, now you save this as “Kracov’s 1st Action-File.action” (or whatever.action) in Krita’s resource folder “actions” (not in the installation-subfolder “actions”!), this way it will survive the next Krita update and is in the folder that is meant for user-made resources.
Now, the next time you start Krita you go via ‘‘Settings’’ >> ‘‘Configure Toolbars…’’ to the configuration for toolbars and there you search for the name of your action, here it would probably be your “levels”, and you drag that into the toolbar of your choice. That’s it.

Michelist

By the way, if you want to change actions like “Ten Brushes” or “Ten Scripts”, i.e. assign a self-made icon to them, then take their original action file from Krita’s installation folder, put only your icon there and then, AND THIS IS IMPORTANT, save it to Krita’s resource folder in the user-generated actions, but leave the original unchanged in the installation folder, actions in Krita’s resource folder have priority, the original will then be ignored.

And for actions from the file “krita.actions”, and the other big action collections, you do the same, except that you copy out the section belonging to the respective action, NOT CUT IT, and create a new action file from it.

The things above have the advantage that if you make a mistake, you can simply delete the corrupted file from the resource-folder “actions” and Krita will work again as before. If you modify the files directly in the installation folder, you can also break your Krita, so it is better to work with the copies in the area meant for users.

Michelist

I’ve already explained that I saved the levels.action file in the resource folder and that it doesn’t show up in Settings-Configure Toolbars list. There must be something I’m missing. I even tried removing the default Levels button and re-adding it just in case. Anyway, the toolbar list does not show “Kracov’s Button”

It can not be found as “Kracov’s Button” if the given “Action name” is “levels”.
“Kracov’s Button” can be seen as a human-readable hint. It is good to have such hints, i.e., if one would give its plugin to other users, so other users can get an idea of the file they are looking at.

Okay, you must have made something wrong. Did you make your icon known to Krita with the procedure described by @KnowZero? You can check if Krita knows your icon by taking an existing action and insert the name of your icon into it, just to see if Krita knows your icon. Don’t forget to make a backup copy of the file you want to test this with beforehand.

Michelist

Add/Edit: And don’t forget to restart Krita after every of these changes in an action-file, Krita has to read in the new instructions before it can use them.

@Michelist
duplicated the gradient.action, gave it an icon, named it kracov.action. still not showing up in the toolbar list.

Because you created a concurrent instruction, at least it sounds like it right now, at least your statement of having saved this test file as kracov.action suggests this. You should have saved the file as gradient.action, unless you removed the gradient.action file from its ancestral directory for the test, but I guess there are now both files in one actions directory at the same time.

Michelist

@Michelist
Okay. So I tried editing the gradientmap.action without making a copy. It did not change the icon, which is krita_filter_levels and this is part of the default krita icons. Perhaps you could try editing the code you gave me and try it yourself?

Theoretically, the linked file should now also display an icon for the Gradient Map filter, which can be found under Gradient Map in the toolbar settings.
But, for reasons I don’t understand, I can’t give this file an icon, neither the one you gave me, because I couldn’t find it among the 953 icons for Krita that can be found in Krita’s icon library at

https://scripting.krita.org/icon-library

nor one of the definitely existing icons from the library there. So I tried it with the Ten Scripts, and inserted an icon from the list of Krita icons that I know for sure works, and lo and behold it became light! :rofl: Meaning, there it worked as expected.
Why this doesn’t want to with gradientmap.action is beyond me, but I’m not a programmer either and rely on the good old trial and error method with all these things, with my own changes and icon assignments this has never happened to me.

If you want to test my statement about the ten scripts, make a backup of your own tenscripts.action and put the one downloadable from the link below for it. Then you will see that this is the right way, only everything else will have to be explained to us by people with programming knowledge:

https://ufile.io/0yk9b4lu

Michelist

Alright. I can confirm that the icon works for the action you provided. I guess this probably means that we cannot add an icon to certain actions, which is why the bulk of people here recommended scripts and plugins. If that’s the case, I have to backtrack and try to figure out how to work with the scripts because I still don’t quite understand much. Also, you didn’t tell me how to make an action to control the levels, because there’s no levels action.