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++

Add Python API for input-aware brush strokes

Moritz Staudinger961b654161de
diff --git a/libs/libkis/Node.cpp b/libs/libkis/Node.cpp
index b1c1078da2..1ec66ee71a 100644
--- a/libs/libkis/Node.cpp
+++ b/libs/libkis/Node.cpp
@@ -7,6 +7,8 @@
#include <QScopedPointer>
#include <QUuid>
+#include <iterator>
+
#include <KoColorSpace.h>
#include <KoColorSpaceRegistry.h>
#include <KoColorTransformation.h>
@@ -71,6 +73,7 @@
#include "KisMainWindow.h"
#include "kis_canvas2.h"
#include "KoCanvasResourceProvider.h"
+#include <brushengine/kis_paint_information.h>
#include <brushengine/kis_paintop_preset.h>
@@ -881,6 +884,44 @@ void Node::paintLine(const QPointF pointOne, const QPointF pointTwo, double pres
helper.paintLine(pointOneInfo, pointTwoInfo);
}
+void Node::paintStroke(const QList<StrokePoint> &points, const QString strokeStyle)
+{
+ if (paintAbility() != "PAINT") {
+ dbgScript << "Script attempted to use Node::paintStroke() on an unpaintable node, ignoring.";
+ return;
+ }
+
+ if (points.isEmpty()) {
+ return;
+ }
+
+ auto paintInformation = [] (const StrokePoint &point) {
+ return KisPaintInformation(point.position(),
+ point.pressure(),
+ point.xTilt(),
+ point.yTilt(),
+ point.rotation(),
+ point.tangentialPressure(),
+ 1.0,
+ point.time(),
+ point.speed());
+ };
+
+ KisFigurePaintingToolHelper helper = PaintingResources::createHelper(d->image, node(), strokeStyle);
+
+ KisPaintInformation previous = paintInformation(points.first());
+ if (points.size() == 1) {
+ helper.paintLine(previous, previous);
+ return;
+ }
+
+ for (auto it = std::next(points.cbegin()); it != points.cend(); ++it) {
+ KisPaintInformation current = paintInformation(*it);
+ helper.paintLine(previous, current);
+ previous = current;
+ }
+}
+
void Node::paintRectangle(const QRectF &rect, const QString strokeStyle, const QString fillStyle)
{
diff --git a/libs/libkis/Node.h b/libs/libkis/Node.h
index ce63d3f1bc..b852671109 100644
--- a/libs/libkis/Node.h
+++ b/libs/libkis/Node.h
@@ -14,6 +14,7 @@
#include "libkis.h"
#include "PaintingResources.h"
+#include "StrokePoint.h"
/**
* Node represents a layer or mask in a Krita image's Node hierarchy. Group layers can contain
@@ -591,6 +592,21 @@ public Q_SLOTS:
double pressureTwo = 1.0,
const QString strokeStyle = PaintingResources::defaultStrokeStyle);
+ /**
+ * @brief paintStroke paint one continuous brush stroke with complete input
+ * information for every supplied point. Uses the current brush preset.
+ *
+ * The stroke is submitted as one undoable freehand stroke, so brush
+ * spacing, randomness, and distance-based sensors are continuous across
+ * all segments. A single point paints one dab.
+ *
+ * @param points input samples in image coordinates. Time is elapsed
+ * milliseconds from the beginning of the stroke; speed is normalized.
+ * @param strokeStyle appearance of the outline; see paintLine().
+ */
+ void paintStroke(const QList<StrokePoint> &points,
+ const QString strokeStyle = PaintingResources::defaultStrokeStyle);
+
/**
* @brief paint a rectangle on the canvas. Uses current brush preset
* @param rect QRect with x, y, width, and height
diff --git a/libs/libkis/StrokePoint.h b/libs/libkis/StrokePoint.h
new file mode 100644
index 0000000000..aa0e8d5404
--- /dev/null
+++ b/libs/libkis/StrokePoint.h
@@ -0,0 +1,71 @@
+/*
+ * SPDX-FileCopyrightText: 2026 Riki
+ *
+ * SPDX-License-Identifier: LGPL-2.0-or-later
+ */
+#ifndef LIBKIS_STROKE_POINT_H
+#define LIBKIS_STROKE_POINT_H
+
+#include <QPointF>
+
+#include "kritalibkis_export.h"
+
+/**
+ * A complete input sample for Node::paintStroke().
+ *
+ * Time is the number of milliseconds since the beginning of the stroke and
+ * speed is normalized to the range [0, 1], as it is for normal canvas input.
+ * Pressure is the final pressure passed to the brush engine.
+ */
+class KRITALIBKIS_EXPORT StrokePoint
+{
+public:
+ StrokePoint(const QPointF &position = QPointF(),
+ qreal pressure = 1.0,
+ qreal xTilt = 0.0,
+ qreal yTilt = 0.0,
+ qreal rotation = 0.0,
+ qreal tangentialPressure = 0.0,
+ qreal time = 0.0,
+ qreal speed = 0.0)
+ : m_position(position)
+ , m_pressure(pressure)
+ , m_xTilt(xTilt)
+ , m_yTilt(yTilt)
+ , m_rotation(rotation)
+ , m_tangentialPressure(tangentialPressure)
+ , m_time(time)
+ , m_speed(speed)
+ {
+ }
+
+ const QPointF &position() const { return m_position; }
+ qreal pressure() const { return m_pressure; }
+ qreal xTilt() const { return m_xTilt; }
+ qreal yTilt() const { return m_yTilt; }
+ qreal rotation() const { return m_rotation; }
+ qreal tangentialPressure() const { return m_tangentialPressure; }
+ qreal time() const { return m_time; }
+ qreal speed() const { return m_speed; }
+
+ void setPosition(const QPointF &value) { m_position = value; }
+ void setPressure(qreal value) { m_pressure = value; }
+ void setXTilt(qreal value) { m_xTilt = value; }
+ void setYTilt(qreal value) { m_yTilt = value; }
+ void setRotation(qreal value) { m_rotation = value; }
+ void setTangentialPressure(qreal value) { m_tangentialPressure = value; }
+ void setTime(qreal value) { m_time = value; }
+ void setSpeed(qreal value) { m_speed = value; }
+
+private:
+ QPointF m_position;
+ qreal m_pressure;
+ qreal m_xTilt;
+ qreal m_yTilt;
+ qreal m_rotation;
+ qreal m_tangentialPressure;
+ qreal m_time;
+ qreal m_speed;
+};
+
+#endif
diff --git a/libs/libkis/tests/TestNode.cpp b/libs/libkis/tests/TestNode.cpp
index 9a903b42d1..5f501131de 100644
--- a/libs/libkis/tests/TestNode.cpp
+++ b/libs/libkis/tests/TestNode.cpp
@@ -9,6 +9,7 @@
#include <KritaVersionWrapper.h>
#include <Node.h>
+#include <StrokePoint.h>
#include <Krita.h>
#include <KoColorSpaceRegistry.h>
@@ -254,6 +255,37 @@ void TestNode::testFindChildNodes()
}
+void TestNode::testStrokePoint()
+{
+ StrokePoint point(QPointF(12.5, 9.25), 0.25, -20.0, 15.0, 45.0, 0.75, 123.0, 0.5);
+
+ QCOMPARE(point.position(), QPointF(12.5, 9.25));
+ QCOMPARE(point.pressure(), 0.25);
+ QCOMPARE(point.xTilt(), -20.0);
+ QCOMPARE(point.yTilt(), 15.0);
+ QCOMPARE(point.rotation(), 45.0);
+ QCOMPARE(point.tangentialPressure(), 0.75);
+ QCOMPARE(point.time(), 123.0);
+ QCOMPARE(point.speed(), 0.5);
+
+ point.setPosition(QPointF(1.0, 2.0));
+ point.setPressure(0.8);
+ point.setXTilt(3.0);
+ point.setYTilt(4.0);
+ point.setRotation(5.0);
+ point.setTangentialPressure(0.6);
+ point.setTime(7.0);
+ point.setSpeed(0.9);
+
+ QCOMPARE(point.position(), QPointF(1.0, 2.0));
+ QCOMPARE(point.pressure(), 0.8);
+ QCOMPARE(point.xTilt(), 3.0);
+ QCOMPARE(point.yTilt(), 4.0);
+ QCOMPARE(point.rotation(), 5.0);
+ QCOMPARE(point.tangentialPressure(), 0.6);
+ QCOMPARE(point.time(), 7.0);
+ QCOMPARE(point.speed(), 0.9);
+}
-KISTEST_MAIN(TestNode)
+KISTEST_MAIN(TestNode)
diff --git a/libs/libkis/tests/TestNode.h b/libs/libkis/tests/TestNode.h
index cd708f437e..4c1892a0e5 100644
--- a/libs/libkis/tests/TestNode.h
+++ b/libs/libkis/tests/TestNode.h
@@ -19,7 +19,7 @@ private Q_SLOTS:
void testThumbnail();
void testMergeDown();
void testFindChildNodes();
+ void testStrokePoint();
};
#endif
-
diff --git a/plugins/extensions/pykrita/sip/krita/Node.sip b/plugins/extensions/pykrita/sip/krita/Node.sip
index f3247b4762..1783747d91 100644
--- a/plugins/extensions/pykrita/sip/krita/Node.sip
+++ b/plugins/extensions/pykrita/sip/krita/Node.sip
@@ -83,6 +83,7 @@ public Q_SLOTS:
QUuid uniqueId() const;
void paintLine(const QPoint pointOne, const QPoint pointTwo, double pressureOne = 1.0, double pressureTwo = 1.0, const QString strokeStyle = "ForegroundColor");
+ void paintStroke(const QList<StrokePoint> &points, const QString strokeStyle = "ForegroundColor");
void paintRectangle(const QRectF &rect, const QString strokeStyle = "ForegroundColor", const QString fillStyle = "None");
void paintPolygon(const QList<QPointF> points, const QString strokeStyle = "ForegroundColor", const QString fillStyle = "None");
void paintEllipse(const QRectF &rect, const QString strokeStyle = "ForegroundColor", const QString fillStyle = "None");
diff --git a/plugins/extensions/pykrita/sip/krita/StrokePoint.sip b/plugins/extensions/pykrita/sip/krita/StrokePoint.sip
new file mode 100644
index 0000000000..562c71a0ae
--- /dev/null
+++ b/plugins/extensions/pykrita/sip/krita/StrokePoint.sip
@@ -0,0 +1,31 @@
+class StrokePoint
+{
+%TypeHeaderCode
+#include "StrokePoint.h"
+%End
+public:
+ StrokePoint(const QPointF &position = QPointF(),
+ qreal pressure = 1.0,
+ qreal xTilt = 0.0,
+ qreal yTilt = 0.0,
+ qreal rotation = 0.0,
+ qreal tangentialPressure = 0.0,
+ qreal time = 0.0,
+ qreal speed = 0.0);
+ const QPointF &position() const;
+ qreal pressure() const;
+ qreal xTilt() const;
+ qreal yTilt() const;
+ qreal rotation() const;
+ qreal tangentialPressure() const;
+ qreal time() const;
+ qreal speed() const;
+ void setPosition(const QPointF &value);
+ void setPressure(qreal value);
+ void setXTilt(qreal value);
+ void setYTilt(qreal value);
+ void setRotation(qreal value);
+ void setTangentialPressure(qreal value);
+ void setTime(qreal value);
+ void setSpeed(qreal value);
+};
diff --git a/plugins/extensions/pykrita/sip/krita/kritamod.sip b/plugins/extensions/pykrita/sip/krita/kritamod.sip
index 7d4e6f59a5..d12a7338f5 100644
--- a/plugins/extensions/pykrita/sip/krita/kritamod.sip
+++ b/plugins/extensions/pykrita/sip/krita/kritamod.sip
@@ -29,6 +29,7 @@
%Include View.sip
%Include Window.sip
%Include Krita.sip
+%Include StrokePoint.sip
%Include Node.sip
%Include GroupLayer.sip