Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++

Mutator: Added Mutator Python Extension to upstream Krita.

Emmet O'Neilld3489637749e
diff --git a/plugins/python/mutator/__init__.py b/plugins/python/mutator/__init__.py
new file mode 100644
index 0000000000..3a4095e9ac
--- /dev/null
+++ b/plugins/python/mutator/__init__.py
@@ -0,0 +1 @@
+from .mutator.py import *
\ No newline at end of file
diff --git a/plugins/python/mutator/mutator.desktop b/plugins/python/mutator/mutator.desktop
new file mode 100644
index 0000000000..632cc7a9df
--- /dev/null
+++ b/plugins/python/mutator/mutator.desktop
@@ -0,0 +1,8 @@
+[Desktop Entry]
+Type=Service
+ServiceTypes=Krita/PythonPlugin
+X-KDE-Library=mutator
+X-Python-2-Compatible=false
+X-Krita-Manual=mutator.html
+Name=Mutator
+Comment=Manually mutate your brush and color settings to add variation.
diff --git a/plugins/python/mutator/mutator.html b/plugins/python/mutator/mutator.html
new file mode 100644
index 0000000000..c21294a993
--- /dev/null
+++ b/plugins/python/mutator/mutator.html
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE html>
+
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head><title>Mutator User Manual</title>
+</head>
+<body>
+ <h1>Mutator:</h1>
+
+ <p>Mutator is an python extension for Krita that, when invoked, creates additional brush dynamics by randomly mutating the artist's current brush settings.</p>
+ <p>This allows the artist to occasionally inject a bit of variation (from the <i>subtle</i> to the <b>bold</b>) and unpredictability into their artwork.</p>
+ <p>Unlike the "fuzzy" options found in Krita's brush engines (which allow for automatic random variations per dab or stroke), the "Mutate" action is invoked manually by the artist whenever some brush or color variation is desired.</p>
+
+ <h2>Basic Usage:</h2>
+
+ <p>Once Mutator has been properly installed and activated. There are only two steps required to use it:</p>
+
+ <ol>
+ <li><b>Configure your Mutator settings.</b> Open the "Mutator Settings" docker (Settings > Dockers > Mutator Settings) and adjust the sliders for each property that you want to mutate. By default, sliders are set to zero (all the way to the left) which results in no change at all when mutate is called. On the other hand setting the sliders to full (all the way the right) will cause somewhat bold or extreme mutations. While each property that we can mutate has been pre-tuned to a usable range, the amount of mutation that you want will depend on your style and workflow. As such, the best way to find settings that you like is to experiment!</li>
+ <li><b>Mutate.</b> After adjusting your Mutator settings, you're ready to mutate whenever you want. There are a few ways to do this; by keyboard shortcut, by GUI button, or by menu action. Using a keyboard shortcut (Ctrl-Alt-Shift-M, by default) may be the most convenient option, as you can use it while the Mutator Settings docker is hidden, and you don't need to navigate menus.</li>
+ </ol>
+
+ <h2>Tips:</h2>
+
+ <ul>
+ <li>First off, decide whether variation is something you want: The dynamics that Mutator allows for can add nuance to otherwise perfectly flat digital colors. While some art styles and aesthetics might benefit from this, others may not.</li>
+ <li>A little bit goes a long way: If you like the idea of manually triggering some minor variation into your brush and/or colors but you're struggling to use Mutator, then you may be overdoing it. Setting all the mutation sliders to max and mashing on the mutate button may help to create some interesting effects or textures, but sometimes very subtle and occasional mutations are all you need!<li>
+ <li>Mutations add up. Too many mutations (or even a few big ones) can bring you pretty far from your original brush or color settings. Sometimes this might be what you want, but other times it can work against you. When it comes to color mutations, this is easy to mitigate - simply resample your original color with the Color Picker and continue painting and mutating. However, when it comes to brush settings, after too many mutations you may have to manually bring your size, opacity, and flow settings back to where you want them. If this bothers you, opt for very small and subtle brush mutations or avoid brush mutations altogether.</li>
+ </ul>
+
+ <h2>Contributors</h2>
+
+ <ul>
+ <li>Eoin O'Neill</li>
+ <li>Emmet O'Neill</li>
+ </ul>
+</body>
\ No newline at end of file
diff --git a/plugins/python/mutator/mutator.py b/plugins/python/mutator/mutator.py
new file mode 100644
index 0000000000..3fec4296e6
--- /dev/null
+++ b/plugins/python/mutator/mutator.py
@@ -0,0 +1,261 @@
+'''
+Licensed under the MIT License.
+
+Copyright (c) 2018 Eoin O'Neill <[email protected]>
+Copyright (c) 2018 Emmet O'Neill <[email protected]>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+'''
+
+
+import sys, math, random, colorsys
+from PyQt5.QtWidgets import *
+from krita import *
+
+
+# Global mutation settings...
+# (Typically normalized within 0.0-1.0 range.
+# Controlled via sliders within MutatorDocker GUI.)
+nSizeMut = 0.5
+nRotationMut = 1.0
+nOpacityMut = 0.1
+nFlowMut = 0.1
+nHueMut = 0.2
+nSaturationMut = 0.3
+nValueMut = 0.1
+
+
+# Usability-tuned maximum mutation values "constants"...
+# (Think of these as the *largest possible mutation* for each parameter
+# when the slider is set to 100%. Can be modified to user taste!)
+def sizeMutMax():
+ #(lowThreshold, highThreshold, scale)
+ return (10, 400, 0.25)
+rotationMutMax = 180
+opacityMutMax = 0.3
+flowMutMax = 0.3
+hueMutMax = 0.125
+saturationMutMax = 0.3
+valueMutMax = 0.25
+
+
+class Mutator(Extension):
+ ''' Mutator Class - Krita Extension
+ The Mutator Krita extension script randomly mutates some of the artist's
+ key brush and color settings by some configurable amount.
+ (When the extension is active settings can be configured in Krita's GUI using sliders in the MutatorDocker.)
+ '''
+ def __init__(self,parent):
+ super().__init__(parent)
+
+
+ def setup(self):
+ pass
+
+
+ def createActions(self, window):
+ '''
+ Adds an "action" to the Krita menus, which connects to the mutate function.
+ '''
+ action = window.createAction("mutate", "Mutate", "tools/scripting")
+ action.triggered.connect(self.mutate)
+
+
+ def mutate(self):
+ '''
+ Mutates current brush/color/etc. settings by some user-configurable amount.
+ Configurable settings are some percentage of a hard maximum amount for usability tuning.
+ Mutation is triggered *manually* by the artist via action, hotkey, or button,
+ whenever some randomness or brush/color variation is desired.
+ '''
+ window = Krita.instance().activeWindow()
+ if window == None:
+ return
+ view = window.activeView()
+ if view == None:
+ return
+ if view.document() == None:
+ return
+
+ #Brush mutations...
+ newSize = view.brushSize() + calculate_mutation(clamp(sizeMutMax()[0], sizeMutMax()[1], view.brushSize()) * sizeMutMax()[2], nSizeMut)
+ view.setBrushSize(clamp(1, 1000, newSize))
+
+ newRotation = view.brushRotation() + calculate_mutation(rotationMutMax, nRotationMut)
+ view.setBrushRotation(newRotation)
+
+ newOpacity = view.paintingOpacity() + calculate_mutation(opacityMutMax, nOpacityMut)
+ view.setPaintingOpacity(clamp(0.01, 1, newOpacity))
+
+ newFlow = view.paintingFlow() + calculate_mutation(flowMutMax, nFlowMut)
+ view.setPaintingFlow(clamp(0.01, 1, newFlow))
+
+ #Color mutations...
+ color_fg = view.foregroundColor()
+ preRGBA = color_fg.components()
+
+ HSV = list(colorsys.rgb_to_hsv(preRGBA[0], preRGBA[1], preRGBA[2]))
+
+ HSV[0] = (HSV[0] + calculate_mutation(hueMutMax, nHueMut))
+ HSV[1] = clamp(0.01, 1, HSV[1] + calculate_mutation(saturationMutMax, nSaturationMut))
+ HSV[2] = clamp(0, 1, HSV[2] + calculate_mutation(valueMutMax, nValueMut))
+
+ postRGBA = list(colorsys.hsv_to_rgb(HSV[0], HSV[1], HSV[2]))
+ postRGBA.append(preRGBA[3])
+
+ color_fg.setComponents(postRGBA)
+ view.setForeGroundColor(color_fg)
+
+
+def calculate_mutation(mutationMax, nScale):
+ '''
+ mutationMax <- maximum possible mutation value.
+ nScale <- normalized (0.0..1.0) percentage (float).
+ Returns a randomized mutation value within range from -mutationMax..mutationMax, scaled by nScale.
+ '''
+ # return random.uniform(0, math.pi * 2) * mutationMax * nScale # Linear distribution (Evenly random.)
+ return math.sin(random.uniform(0, math.pi * 2)) * mutationMax * nScale # Sine distribution (Randomness biased towards more extreme mutations.)
+
+
+def clamp(minimum, maximum, input):
+ '''
+ Clamp input to some value between the minimum and maximum values.
+ Used to keep values within expected ranges.
+ '''
+ return min(maximum, max(input, minimum))
+
+
+#GUI
+class MutatorDocker(DockWidget):
+ ''' MutatorDocker - Krita DockWidget
+ This class handles the GUI elements that assign mutation values.
+ Can be found inside Krita's Settings>Dockers menu.
+ '''
+ def __init__(self):
+ super().__init__()
+
+ self.setWindowTitle("Mutator")
+
+ # Create body, set widget and setup layout...
+ body = QWidget(self)
+ self.setWidget(body)
+ body.setLayout(QVBoxLayout())
+ body.setSizePolicy(QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred))
+
+ # Create mutation amount slides...
+ settings = QWidget()
+ body.layout().addWidget(settings)
+ settings.setLayout(QFormLayout())
+
+ sizeMutSlider = QSlider(Qt.Horizontal) # Size
+ sizeMutSlider.setRange(0, 100)
+ sizeMutSlider.valueChanged.connect(self.update_size_mut)
+ sizeMutSlider.setValue(int(nSizeMut * 100))
+ settings.layout().addRow(QLabel("Brush Size"), sizeMutSlider)
+
+ rotationMutSlider = QSlider(Qt.Horizontal) # Rotation
+ rotationMutSlider.setRange(0, 100)
+ rotationMutSlider.valueChanged.connect(self.update_rotation_mut)
+ rotationMutSlider.setValue(int(nRotationMut * 100))
+ settings.layout().addRow(QLabel("Brush Rotation"), rotationMutSlider)
+
+ opacityMutSlider = QSlider(Qt.Horizontal) # Opacity
+ opacityMutSlider.setRange(0, 100)
+ opacityMutSlider.valueChanged.connect(self.update_opacity_mut)
+ opacityMutSlider.setValue(int(nOpacityMut * 100))
+ settings.layout().addRow(QLabel("Opacity"), opacityMutSlider)
+
+ flowMutSlider = QSlider(Qt.Horizontal) # Flow
+ flowMutSlider.setRange(0, 100)
+ flowMutSlider.valueChanged.connect(self.update_flow_mut)
+ flowMutSlider.setValue(int(nFlowMut * 100))
+ settings.layout().addRow(QLabel("Flow"), flowMutSlider)
+
+ hueMutSlider = QSlider(Qt.Horizontal) # FGC Hue
+ hueMutSlider.setRange(0, 100)
+ hueMutSlider.valueChanged.connect(self.update_fgc_hue_mut)
+ hueMutSlider.setValue(int(nHueMut * 100))
+ settings.layout().addRow( QLabel("Hue (Foreground)"), hueMutSlider)
+
+ saturationMutSlider = QSlider(Qt.Horizontal) # FGC Saturation
+ saturationMutSlider.setRange(0, 100)
+ saturationMutSlider.valueChanged.connect(self.update_fgc_saturation_mut)
+ saturationMutSlider.setValue(int(nSaturationMut * 100))
+ settings.layout().addRow(QLabel("Saturation (Foreground)"), saturationMutSlider)
+
+ valueMutSlider = QSlider(Qt.Horizontal) # FGC Value
+ valueMutSlider.setRange(0, 100)
+ valueMutSlider.valueChanged.connect(self.update_fgc_value_mut)
+ valueMutSlider.setValue(int(nValueMut * 100))
+ settings.layout().addRow(QLabel("Value (Foreground)"), valueMutSlider)
+
+ # Create mutate button...
+ mutateButton = QPushButton("Mutate")
+ mutateButton.clicked.connect(self.trigger_mutate)
+ body.layout().addWidget(mutateButton)
+
+
+ # Slider event handlers...
+ # Note: Sliders range from 0-100%, but global mutation state is normalized from 0.0-1.0.
+ def update_size_mut(self, value):
+ global nSizeMut
+ nSizeMut = value / 100
+
+
+ def update_rotation_mut(self, value):
+ global nRotationMut
+ nRotationMut = value / 100
+
+
+ def update_opacity_mut(self, value):
+ global nOpacityMut
+ nOpacityMut = value / 100
+
+
+ def update_flow_mut(self, value):
+ global nFlowMut
+ nFlowMut = value / 100
+
+
+ def update_fgc_hue_mut(self, value):
+ global nHueMut
+ nHueMut = value / 100
+
+
+ def update_fgc_saturation_mut(self, value):
+ global nSaturationMut
+ nSaturationMut = value / 100
+
+
+ def update_fgc_value_mut(self, value):
+ global nValueMut
+ nValueMut = value / 100
+
+
+ def trigger_mutate(self):
+ Krita.instance().action("mutate").activate(QAction.Trigger)
+
+
+ def canvasChanged(self, canvas): # Unused
+ pass
+
+
+# Krita boilerplate.
+Krita.instance().addExtension(Mutator(Krita.instance()))
+Krita.instance().addDockWidgetFactory(DockWidgetFactory("mutatorDocker", DockWidgetFactory.DockRight, MutatorDocker))
\ No newline at end of file