This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Python Plugin: Brush Animator

Tags: None
(comma "," separated)
saedjubarin
Registered Member
Posts
58
Karma
0

Python Plugin: Brush Animator

Sat Mar 16, 2019 6:25 pm
How I use this plugin:
Config:
I bind r key to next and f key to new.

1. I create a new file
2. I press f for new layer and paint on it (Has Onion Skin like opacity fading of layers below)
3. Repeat #2 until enough layers.
4. I press r to skip to bottom layer (automatically skips "Background") and paint on it
5. I press r to rotate to next layer and paint on it.
6. Repeat #5 until the brush is finished. My personal approach is to do multiple passes and only change brush when all layers are done with the one in hand.

Example .kra file containing the layers for animated brush I made in a small amount of time.

brush_animator.py:
Code: Select all
 
from krita import Extension,  Krita

class BrushAnimator(Extension):

    def __init__(self, parent):
        #Always initialise the superclass.
        #This is necessary to create the underlying C++ object
        super().__init__(parent)
        self.current = "init"
        self.doc = app.activeDocument()

    def setup(self):
        pass

    def createActions(self, window):
        action_next = window.createAction("ba_next", "Brush Animator: Next","tools/scripts")
        # parameter 1 =  the name that Krita uses to identify the action
        # parameter 2 = the text to be added to the menu entry for this script
        # parameter 3 = location in Krita menu to put this script entry
        action_next.triggered.connect(self._next)
        action_start = window.createAction("ba_new", "Brush Animator: New Layer","tools/scripts")
        action_start.triggered.connect(self.new)
       
    def _next(self):
        print("Brush Animator: Next")
        self.doc = app.activeDocument()
        self.toplevel = self.doc.topLevelNodes()
        if self.current == "init":
            self.start()
        else:
            n_nodes = len(self.toplevel)
            self.toplevel[self.current%n_nodes].setVisible(False)
            self.current = (self.current + 1) %n_nodes
            self.toplevel[self.current].setVisible(True)
            self.toplevel[self.current].setOpacity(255)
            self.doc.setActiveNode(self.toplevel[self.current])
            self.doc.refreshProjection()
            app.activeWindow().activeView().setVisible()
        if self.doc.activeNode().name() == "Background":
            if len(self.toplevel) > 1:
                self._next()

    def start(self):
        print("Brush Animator: Start")
        self.doc = app.activeDocument()
        self.toplevel = self.doc.topLevelNodes()
        self.current = 0
        for node in self.toplevel:
            node.setVisible(False)
        self.toplevel[self.current].setVisible(True)
        self.toplevel[self.current].setOpacity(255)
        self.doc.setActiveNode(self.toplevel[self.current])
        self.doc.refreshProjection()
        app.activeWindow().activeView().setVisible()
        if self.doc.activeNode().name() == "Background":
            if len(self.toplevel) > 1:
                self._next()

    def new(self):
        print("Brush Animator: New")
        self.doc = app.activeDocument()
        nodes = self.doc.topLevelNodes()
        new_node = self.doc.createNode("PyLayer %s"%len(nodes), "paintLayer")
        root = self.doc.rootNode()
        root.addChildNode(new_node, None)
        self.doc.waitForDone()
        nodes = self.doc.topLevelNodes()
        idx = 0
        for node in nodes[::-1]:
            if node.name() != "Background":
                node.setOpacity(max(0,255-(255//5)*idx))
            idx += 1
        self.doc.refreshProjection()
       
# And add the extension to Krita's list of extensions:
app=Krita.instance()
extension=BrushAnimator(parent=app) #instantiate your class
app.addExtension(extension)


__init__.py:
Code: Select all
from .brush_animator import *


brush_animator.desktop:
Code: Select all
[Desktop Entry]
Type=Service
ServiceTypes=Krita/PythonPlugin
X-KDE-Library=brush_animator
X-Python-2-Compatible=false
Name=Brush Animator
Comment=Layer Traversion For Animated Brush Creation


ba_next.action:
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<ActionCollection version="2" name="Scripts">
    <Actions category="Scripts">
        <text>Brush Animator</text>

        <Action name="ba_next">
        <icon></icon>
        <text>Next</text>
        <whatsThis></whatsThis>
        <toolTip></toolTip>
        <iconText></iconText>
        <activationFlags>10000</activationFlags>
        <activationConditions>0</activationConditions>
        <shortcut>ctrl+alt+shift+p</shortcut>
        <isCheckable>false</isCheckable>
        <statusTip></statusTip>
        </Action>
    </Actions>
</ActionCollection>


ba_new.action:
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<ActionCollection version="2" name="Scripts">
    <Actions category="Scripts">
        <text>Brush Animator</text>

        <Action name="ba_new">
        <icon></icon>
        <text>New</text>
        <whatsThis></whatsThis>
        <toolTip></toolTip>
        <iconText></iconText>
        <activationFlags>10000</activationFlags>
        <activationConditions>0</activationConditions>
        <shortcut>ctrl+alt+shift+i</shortcut>
        <isCheckable>false</isCheckable>
        <statusTip></statusTip>
        </Action>
    </Actions>
</ActionCollection>


Bookmarks



Who is online

Registered users: Bing [Bot], claydoh, Evergrowing, Google [Bot], rblackwell