# \[proof-of-concept\] Scriptable Brush

**URL:** <https://krita-artists.org/t/proof-of-concept-scriptable-brush/46634>\
**Category:** Plugins Development\
**Created:** [August 27, 2022, 11:18am UTC](https://krita-artists.org/t/proof-of-concept-scriptable-brush/46634 "2022-08-27T11:18:08Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![jade12](https://krita-artists.org/user_avatar/krita-artists.org/jade12/32/52645_2.png) [@jade12](https://krita-artists.org/u/jade12)\
**Post date:** [August 27, 2022, 11:18am UTC](https://krita-artists.org/t/proof-of-concept-scriptable-brush/46634/1 "2022-08-27T11:18:08Z")

</div>

[![](https://krita-artists.org/uploads/default/original/3X/3/6/36ad6098d3d9e2839f5ae20a0256b14dbacee0c3.jpeg "Lua Scripted Brush for Krita") ](https://www.youtube.com/watch?v=tsJpEM-9au8)

### Explanation

This is a quick experiment that I did for fun. It demonstrates a way of making scripptable brushes via Lua.

In the video I played with drawing speed and drawing angle. I’d really wanted to use pressure, but I don’t have my tablet with my dev environement ☹

I chose Lua because it’s simpler to embed in C/C++.

I hacked the pixel engine by adding a new “dynamic sensor”, which exposes other sensor values and run a lua script to calculate its own value.

### Related Topics

> [@Node-based brush designer (including mockups)](https://krita-artists.org/t/node-based-brush-designer-including-mockups/15816):
>
> Krita has been wonderful for me in the year and a half or so I’ve been using it semi-professionally, but one of the issues I have with the software is the Brush Editor. Even though I’ve made a few brushes myself, and modified other people’s for my needs, it’s hard to get a good idea about how a brush works at a glance and it’s hard to know where to start when I have an idea for a new brush in mind. I think that offering a node-based editor for brush designing and editing would not only be usef…

> [@Brush engine: Is it possible to have a pattern blending mode like this?](https://krita-artists.org/t/brush-engine-is-it-possible-to-have-a-pattern-blending-mode-like-this/20130):
>
> I’ve been messing around with patterns in the brush engine and find I’d like to have a way to control the pattern’s contribution to the brush with pen pressure. So that at 0 pressure, no pattern would be applied to the stroke, only the brush tip as defined in the brush. Also, I’d like the pattern to be dithered in rather than blended with multiply or subtract, since those don’t look very nice on their own IMO. I’ve made a mockup of what I mean, including my proposal for how the math could work,…

> [@Are there any kind of Scriptable Brushes?](https://krita-artists.org/t/are-there-any-kind-of-scriptable-brushes/29041):
>
> Just curious - does Krita has any kind of scriptable brushes? I mean brush, that can execute script that sample colors and write them back into layer. I`m using FireAlpaca that allow Lua scripting for brushes (and this is essential for some custom workflows i use) and looking to switch into Krita

### Discussions

This demo could be relevant to node-based editors. If we fix the input as all existing sensors (pressure, speed etc.), and the output as the control options (size, opacity…), I think it should be possible to build such an editor. The nodes will be some math connecting these values. The Lua script in my demo is actually the same thing, just in a different format.

On the other hand, my approach is very limited compared with a full-fledge node editor, which would be way more difficult to implement as @halla’s [pointed it out](https://krita-artists.org/t/node-based-brush-designer-including-mockups/15816/20). Potentially this version could be a medium-small project that can support lots of use cases.

Lastly, performance may or may not be an issue, I did not test it thoroughly. My guess is it’d be OK if we just use simple math in Lua. Maybe LuaJit can help?

---

<div class="post-metadata">

**Author:** ![raghukamath](https://krita-artists.org/user_avatar/krita-artists.org/raghukamath/32/59144_2.png) [@raghukamath](https://krita-artists.org/u/raghukamath)\
**Post date:** [August 27, 2022, 3:30pm UTC](https://krita-artists.org/t/proof-of-concept-scriptable-brush/46634/2 "2022-08-27T15:30:24Z")

</div>

This seems awesome. Krita supports python scripting too, can python be similarly plugged in? Also do you plan to upstream this, there may be need to fine tune it and figure out the UX if necessary.

---

<div class="post-metadata">

**Author:** ![jade12](https://krita-artists.org/user_avatar/krita-artists.org/jade12/32/52645_2.png) [@jade12](https://krita-artists.org/u/jade12)\
**Post date:** [August 27, 2022, 3:53pm UTC](https://krita-artists.org/t/proof-of-concept-scriptable-brush/46634/3 "2022-08-27T15:53:19Z")

</div>

Hi Raghavendra, for short-term I don’t plan to upstream it yet. I may or may not be able to do it in a month or so.

For now I’d like to help push the idea, collect feedback and form a design, e.g:

- Is it useful or not?
- Technical details (e.g. lua and/or python)
- Future plans (e.g. node editor)

About Lua and Python, I chose Lua for now because:

- I vaguely remember the performance is like pypy ~= luajit \> lua \> cpython
- The Lua C API seems simpler than the Python C++ API.

But this is yet to be discussed with Krita experts. There could be other considerations as well, e.g. which language is better for brush makers. I could start a new thread if the feature seems useful.

---

<div class="post-metadata">

**Author:** ![hulmanen](https://krita-artists.org/user_avatar/krita-artists.org/hulmanen/32/25122_2.png) [@hulmanen](https://krita-artists.org/u/hulmanen)\
**Post date:** [August 27, 2022, 4:30pm UTC](https://krita-artists.org/t/proof-of-concept-scriptable-brush/46634/4 "2022-08-27T16:30:44Z")

</div>

This is nifty! I think I see why Halla said a node-based brush system would need an engine all its own, though - You’d want to be able to e.g. change the order of compositing operations so it would go well beyond “just” a custom sensor like you have here. Still, really cool idea and I’d love to get to try it out.

---

<div class="post-metadata">

**Author:** ![jade12](https://krita-artists.org/user_avatar/krita-artists.org/jade12/32/52645_2.png) [@jade12](https://krita-artists.org/u/jade12)\
**Post date:** [August 27, 2022, 5:09pm UTC](https://krita-artists.org/t/proof-of-concept-scriptable-brush/46634/5 "2022-08-27T17:09:50Z")

</div>

Thanks. I agree with you and I updated my post.

---

<div class="post-metadata">

**Author:** ![EyeOdin](https://krita-artists.org/user_avatar/krita-artists.org/eyeodin/32/2662_2.png) [@EyeOdin](https://krita-artists.org/u/EyeOdin)\
**Post date:** [August 27, 2022, 5:46pm UTC](https://krita-artists.org/t/proof-of-concept-scriptable-brush/46634/6 "2022-08-27T17:46:40Z")

</div>

The idea looks awesome!  
Not sure how it works but with a entry that exposes all the options and variables would allow greater control on the behaviour than using tiny graphs on different entries and make driver variables possible.

---

<div class="post-metadata">

**Author:** ![jade12](https://krita-artists.org/user_avatar/krita-artists.org/jade12/32/52645_2.png) [@jade12](https://krita-artists.org/u/jade12)\
**Post date:** [August 28, 2022, 11:37am UTC](https://krita-artists.org/t/proof-of-concept-scriptable-brush/46634/7 "2022-08-28T11:37:40Z")

</div>

I’ve been trying to build an Appimage, but it’s been tough 😅 (linuxdeployqt does not work in Arch Linux, [invent.kde.org](http://invent.kde.org) was down…)

But it’s running now, hopefully I will get something soon.

---

<div class="post-metadata">

**Author:** ![jade12](https://krita-artists.org/user_avatar/krita-artists.org/jade12/32/52645_2.png) [@jade12](https://krita-artists.org/u/jade12)\
**Post date:** [August 28, 2022, 1:45pm UTC](https://krita-artists.org/t/proof-of-concept-scriptable-brush/46634/8 "2022-08-28T13:45:21Z")

</div>

OK, an Appimage is availble here:

> [@\[Appimage\] Scriptable Brush v0](https://krita-artists.org/t/prototype-appimage-scriptable-brush/46716):
>
> This is a follow-up of I have made an Appimage: md5sum: e4bc668c09c77c08f0d56e07706bd7e5 sha256sum: d8a108347efb19494883dbce0ec4929b5ab49efbdfbc994193af074984866100 Important This is just a prototype to test the idea and Lua vs Python. Nothing is really fixed. Do NOT use it for production The Lua scripts may or may not be saved properly. Back up your scripts! How to Use Edit a brush with pen settings (i.e. pressure, tilt etc). You will see a new ‘Lua’ entry in pen settings, enable it …

---

<div class="post-metadata">

**Author:** ![LunarKreatures](https://krita-artists.org/user_avatar/krita-artists.org/lunarkreatures/32/231342_2.png) [@LunarKreatures](https://krita-artists.org/u/LunarKreatures)\
**Post date:** [August 28, 2022, 4:44pm UTC](https://krita-artists.org/t/proof-of-concept-scriptable-brush/46634/9 "2022-08-28T16:44:51Z")

</div>

This is interesting, its something that firealpaca/medibang also have and they also use lua. I think this can open up some interesting possibilities

---

<div class="post-metadata">

**Author:** ![Ralek](https://krita-artists.org/user_avatar/krita-artists.org/ralek/32/107058_2.png) [@Ralek](https://krita-artists.org/u/Ralek)\
**Post date:** [August 28, 2022, 4:59pm UTC](https://krita-artists.org/t/proof-of-concept-scriptable-brush/46634/10 "2022-08-28T16:59:10Z")

</div>

I definitely like the sound of this! It was fun to play around with in BlackInk. To fit in with the motif of Krita, I do think Python would be a more apt language for it but that’s just my opinion.

---

<div class="post-metadata">

**Author:** ![Takiro](https://krita-artists.org/user_avatar/krita-artists.org/takiro/32/209381_2.png) [@Takiro](https://krita-artists.org/u/Takiro)\
**Post date:** [August 28, 2022, 6:25pm UTC](https://krita-artists.org/t/proof-of-concept-scriptable-brush/46634/11 "2022-08-28T18:25:42Z")

</div>

This would be pretty great for some use cases I have

> [@Bindable brush settings - determine the value of a setting by the value of another](https://krita-artists.org/t/bindable-brush-settings-determine-the-value-of-a-setting-by-the-value-of-another/250):
>
> Okay this is a thing I am thinking about for years now. It would be cool when you could bind a setting to another of the same brush preset. Consider the following use case (i actually have) : You want to create a brush to create a fur like pattern when painted. One of the important properties would be that the stroke fades out by getting smaller over the length of the stroke. This is of course easy. You open the preset, go to size, check fade, enter a reasonable value for length, set the curve …

If this would work with any brush engine.

---

<div class="post-metadata">

**Author:** ![jade12](https://krita-artists.org/user_avatar/krita-artists.org/jade12/32/52645_2.png) [@jade12](https://krita-artists.org/u/jade12)\
**Post date:** [August 28, 2022, 6:33pm UTC](https://krita-artists.org/t/proof-of-concept-scriptable-brush/46634/12 "2022-08-28T18:33:13Z")

</div>

I guess so, althougth I did not check the code of other engines.

Which engines are you thinking of?

---

<div class="post-metadata">

**Author:** ![Takiro](https://krita-artists.org/user_avatar/krita-artists.org/takiro/32/209381_2.png) [@Takiro](https://krita-artists.org/u/Takiro)\
**Post date:** [August 28, 2022, 6:34pm UTC](https://krita-artists.org/t/proof-of-concept-scriptable-brush/46634/13 "2022-08-28T18:34:56Z")

</div>

Mostly the Smudge engine in my case.

---

<div class="post-metadata">

**Author:** ![jade12](https://krita-artists.org/user_avatar/krita-artists.org/jade12/32/52645_2.png) [@jade12](https://krita-artists.org/u/jade12)\
**Post date:** [August 28, 2022, 8:10pm UTC](https://krita-artists.org/t/proof-of-concept-scriptable-brush/46634/14 "2022-08-28T20:10:44Z")

</div>

Interestingly, I realized that it also works for smudge engine (and possibly others) in my appimage. So maybe you can give a try.

---

<div class="post-metadata">

**Author:** ![Eranthis\_stellata](https://krita-artists.org/user_avatar/krita-artists.org/eranthis_stellata/32/21721_2.png) [@Eranthis\_stellata](https://krita-artists.org/u/Eranthis_stellata)\
**Post date:** [September 20, 2022, 2:06am UTC](https://krita-artists.org/t/proof-of-concept-scriptable-brush/46634/15 "2022-09-20T02:06:23Z")

</div>

I’m only seeing this now and it’s great.

I think python is better.  
As far as I know python is very easy to learn which is a big advantage.  
Besides krita itself, like blender or other authoring programs also use python, I think using python is easier for creators to use.  
In addition, the ecology of python itself is much better, maybe you can use the existing things in python to help make brushes.

Nodes are important, even though python is easy to learn, it is still not as simple as graphical nodes.
