How to export object position in frames and replace simple object with imported image

Hello:
Thank you very much, your code work like a charm.
One minor thing: I create an animation with 24 frames for one seconds.
How to extend to like 3 or 5 times longer, just copy and paste the first 24 frames from the time line is enough?
Again, thank you very very much!

Just expand the list variable, the more values on the list, the more frames it will create.

If your goal is to skip frames, then you need a multiplier when you do:
doc.setCurrentTime(i)

Edit: If you are still confused then here:

from krita import *

doc = Krita.instance().activeDocument()

skipFrames = 24
list = [ [0,0], [50,50] ]

img = QImage('c:/my_path_to/soccer.png')

img.convertToFormat(QImage.Format_RGBA8888)
ptr = img.constBits()
ptr.setsize(img.byteCount())

def stepper1(i):
    doc.setCurrentTime(i*skipFrames)
    Krita.instance().action('add_blank_frame').trigger()
    
    if len(list)-1 >= i+1:
        QtCore.QTimer.singleShot(1000, lambda: stepper1(i+1))
    else:
        stepper2(0)

def stepper2(i):
    doc.setCurrentTime(i*skipFrames)
    node = doc.activeNode()
    node.setPixelData( bytes(ptr.asarray()) ,list[i][0],list[i][1], img.width(), img.height() )
    doc.refreshProjection()
    
    if len(list)-1 >= i+1: QtCore.QTimer.singleShot(1000, lambda: stepper2(i+1))

newLayer = doc.createNode("Animated Item", "paintLayer")
doc.rootNode().addChildNode(newLayer,None)

QtCore.QTimer.singleShot(1000, lambda: stepper1(0))

Hello:
I found some minor issue with the following code with 24 positions in the list.
from krita import *
doc = Krita.instance().activeDocument()
list = [ [0,0], [30, 31], [60, 62], [90, 93], [120,124] , [150,155], [180,186], [210,217], [240,248], [270,279], [300,310], [330,341], [330,346], [360,315], [390,284], [420,253] , [450,222] , [480,191], [510,160], [540,129], [570,98], [600,67], [630,36], [660,0]]

view = Krita.instance().activeWindow().activeView()
img = QImage(‘D:/Videos/Animation/Soccer50px.png’)
img.convertToFormat(QImage.Format_RGBA8888)
ptr = img.constBits()
ptr.setsize(img.byteCount())

def stepper1(i):
doc.setCurrentTime(i)
Krita.instance().action(‘add_blank_frame’).trigger()
if len(list)-1 >= i+1:
QtCore.QTimer.singleShot(1000, lambda: stepper1(i+1))
else:
stepper2(0)

def stepper2(i):
doc.setCurrentTime(i)
node = doc.activeNode()
node.setPixelData( bytes(ptr.asarray()) ,list[i][0],list[i][1], img.width(), img.height() )
doc.refreshProjection()
if len(list)-1 >= i+1: QtCore.QTimer.singleShot(1000, lambda: stepper2(i+1))

newLayer = doc.createNode(“Animated Item”, “paintLayer”)
doc.rootNode().addChildNode(newLayer,None)

QtCore.QTimer.singleShot(1000, lambda: stepper1(0))

To play the animation, I can’t see any issue, so it basically works.
But if I look carefully, then I found there are a few frames, the soccer ball is gone, so they are empty frames.
Frame numbers are: #1, #3, #9, #13, #17, #19.
Except #9, the other missing frames are prime numbers, like: 3, 13, 17, 19.
I have tried to use one frame before the empty frame to duplicate the frame. But this option “Create Duplicate Frame” is gone.
Any advice?
Thanks,
I don’t know how to upload a picture to show the timeline.

Try Krita 5 beta. It is more reliable for animation as it had a lot of fixes.

Otherwise, you can select the layer with the soccer ball and rerun the script except change:

newLayer = doc.createNode(“Animated Item”, “paintLayer”)
doc.rootNode().addChildNode(newLayer,None)

QtCore.QTimer.singleShot(1000, lambda: stepper1(0))

to

QtCore.QTimer.singleShot(1000, lambda: stepper2(0))

This should backfill the missing ones

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.