Help with copying images / nodes

I’ve been working on this for a few days with little success, so maybe some of you guys could help me out here

I’m trying to make a plugin to easily convert animations into sprite sheets (and could possibly be used for anything that could be separated into frames and needs to be layed out in a grid)
here’s my current code (minus a few irrelevant things), which works, except i think the pixel data messes up when you have transparent pixels, (which i would like to keep)

from krita import *

#how big to make the final sprite sheet
framesX = 2
framesY = 2

srcDoc = Krita.instance().activeDocument()

spriteDoc = Krita.instance().createDocument(srcDoc.width(), srcDoc.height(), “UnnamedSprite”, srcDoc.colorModel(), srcDoc.colorDepth(), srcDoc.colorProfile(), srcDoc.resolution())

spriteDoc.resizeImage(0,0, srcDoc.width() * framesX, srcDoc.height() * framesY)

#for each layer, copy each frame to the new document
for node in srcDoc.rootNode().childNodes():

frame = 0
posX = 0
posY = 0

while frame <= srcDoc.animationLength():
spriteDoc.setCurrentTime(frame)
print(frame)

  if node.hasKeyframeAtTime(frame):
  	newNode = spriteDoc.createNode(node.name() + " - frame " + str(frame), "PaintLayer")
  	newNode.setPixelData(node.pixelDataAtTime(node.bounds().left(), node.bounds().top(), node.bounds().width(), node.bounds().height(), frame), node.bounds().left(), node.bounds().top(), node.bounds().width(), node.bounds().height())
  	spriteDoc.rootNode().addChildNode(newNode, None)
  	
  	newNode.move(posX, posY)
  
  frame = frame + 1
  posX = posX + srcDoc.width()
  if posX >= srcDoc.width() * framesX:
  	posX = 0
  	posY = posY + srcDoc.height()

Krita.instance().activeWindow().addView(spriteDoc)
spriteDoc.refreshProjection()

right now im copying the pixel data at a specific frame, and setting the pixel data of a new layer to that pixel data, but unless there aren’t any transparent pixels, it messes everything up…
i’ve tried clone() and duplicate() but i cant figure out how to get them to work, i did get it to work once, but that also gives me all the frames of that layer and i only want one (also it looks like it made all the frames the same? or something weird like that)
(also, i tried to clone() the document, if i wanted to instead merge down the frames instead of copying them to new layers, and it looked like it worked fine, untill i added it as a view, then it would make krita crash… but i decided i didn’t want to do it that way anyway)

are there any other good ways of copying things? or a way to get the transparency to not freak out (and preferably that works well with animation, and doesn’t copy all the frames)?

attached image of a layer with transparency after going through setPixelData() / pixelDataAtTime() (just a green brush stroke)

Have you checked out an existing sprite sheet exporter. It looks like it does a similar thing with taking an animation and breaking it up into a sprite sheet file.

aww man… i was looking for a while if anyone else had already done it, and couldn’t find anything… i was so proud of myself that i thought of something nobody else had done, and was reasonably within my skill level, which I could contribute…

well thanks, just from a quick glance i cant quite figure out exactly whats going on, but it looks well commented, and like a good reference to learn from… or maybe I’ll just use it instead of making my own, I don’t know

1 Like