Document pixel position under cursor position

I’ve looked at previous solutions like To monitor the cursor position and I wasn’t quite satisfied. At some point, I thought that I might have to add some API methods to Krita API, but it turns out the API as of 5.2.3 provides just enough to translate cursor position to pixel position. I was able to figure this out from the following references:

Some sample code that tracks pixel position on the top left of the canvas. The code can be run in Tools -> Scripts -> Scripter.

from krita import *

inst = Krita.instance()
win = inst.activeWindow()
qwin = win.qwindow()
view = win.activeView()

# Note 'view_0' is hard coded here.
# Krita increments the name for each new document. 'view_1', 'view_2', ...
# This line can fail. 
# I recommend restarting Krita, opening a document, and then running the script.
pobj = qwin.findChild(QWidget,'view_0')
wobj = pobj.findChild(QOpenGLWidget)

def update():
    flakeToImage = view.flakeToImageTransform()
    widgetToFlake = view.flakeToCanvasTransform().inverted()[0]
    widgetToImage = widgetToFlake * flakeToImage
    gpos = QCursor.pos()
    wpos = wobj.mapFromGlobal(gpos)
    pos = widgetToImage.map(wpos)
    x, y = pos.x(), pos.y()
    label.setText(f'{x}x {y}y')

for child in wobj.children():
    if isinstance(child, QLabel):
        child._timer.stop()
        child._timer.deleteLater()
        child.deleteLater()

label = QLabel('999999x 999999y', wobj)
label.setEnabled(False)
label.setStyleSheet('background-color: rgba(0, 0, 0, 0.2); color: white;')
label.show()
timer = QTimer(label)
timer.timeout.connect(update)
timer.start(int(1000/60))

Krita throws an error when at least not opened a file. Where should it go to be enabled?


AttributeError: ‘NoneType’ object has no attribute ‘loader’

In file:

In function: module_from_spec at line: 568. Line with error:

So it works but it throws an error. Where can I find the new Script for starting? i saved it to a file.

I’m using PixelRuler, an additional software which is free for personal use. But since it is only ~ € / $5, I bought the full version because the free version misses a few features. You can adjust the length and width of the rulers, transparency too, measure distances, angles, have a color picker and a resizable zoom-window and more. You can calibrate it to your monitor, and have conversion scaling.
For me, it is more than worth its price.

Michelist