Add pixelDataAtTime to Node pyKrita object
ClosedPublic

Authored by woltherav on May 27 2017, 7:39 PM.

Details

Summary

As discussed in T5882, this will allow people to get pixel data from a given layer.

This does not implement the opacity channels.

Also, it is not documented, and there's still a lot of issues with it, like the group node not giving data, however, you can make spritesheets with it :)

Test Plan

Here's an example script.

import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from krita import *

doc = Application.activeDocument()

frameWidth = doc.width()
frameHeight = doc.height()
maxFramesInRow = 9
maxFramesTotal = 72
maxRows = round(maxFramesTotal/maxFramesInRow)

spriteSheet = Application.createDocument(maxFramesInRow*frameWidth, maxRows*frameHeight, doc.name()+"_spritesheet", doc.colorModel(), doc.colorDepth(), doc.colorProfile())
file_path = QFileInfo(doc.fileName())
file_name = file_path.dir().absolutePath()+"/"+file_path.baseName()+"_spritesheet.kra"
spriteSheet.setFileName(file_name)

nodeList = doc.topLevelNodes()
spriteNodes = spriteSheet.rootNode().childNodes()
   

for node in nodeList:
   newNode = spriteSheet.createNode(node.name(), "paintlayer")
   print("node created "+newNode.name())
   x=0
   y=0
   for i in range(0, 72):
      print(str(x+(y*9)))
      ba = node.pixelDataAtTime(0, 0, frameWidth, frameHeight, x+(y*9))
      if ba.size()>0:
      	newNode.setPixelData(ba, x*frameWidth, y*frameHeight, frameWidth, frameHeight)
      x+=1
      if x>8:
         x=0
         y+=1
   spriteNodes.append(newNode)

if spriteSheet.isIdle():
   spriteSheet.rootNode().setChildNodes(spriteNodes)
   spriteSheet.refreshProjection()
   print(str(len(spriteSheet.topLevelNodes()) ))
   Application.activeWindow().addView(spriteSheet)

Diff Detail

Repository
R37 Krita
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.
woltherav created this revision.May 27 2017, 7:39 PM

Codewise, the patch looks fine, though I didn't test it :)

dkazakov accepted this revision as: dkazakov.May 30 2017, 9:46 AM

I think we can just push it

This revision is now accepted and ready to land.May 30 2017, 9:46 AM
rempt accepted this revision.May 30 2017, 9:55 AM
This revision was automatically updated to reflect the committed changes.