Skip to content

Commit

Permalink
Support group layers as control image #10
Browse files Browse the repository at this point in the history
* list nested layers
* list layers in the order of the layer stack
  • Loading branch information
Acly committed Sep 23, 2023
1 parent 514471f commit a76ddf1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
19 changes: 14 additions & 5 deletions ai_diffusion/document.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Optional
import krita
from krita import Krita
from PyQt5.QtCore import QUuid
from PyQt5.QtCore import QUuid, QByteArray
from PyQt5.QtGui import QImage

from .image import Extent, Bounds, Mask, Image
Expand Down Expand Up @@ -76,7 +76,9 @@ def get_image(self, bounds: Bounds = None, exclude_layer=None):
return Image(img)

def get_layer_image(self, layer, bounds: Bounds):
return Image(QImage(layer.pixelData(*bounds), *bounds.extent, QImage.Format_ARGB32))
data: QByteArray = layer.projectionPixelData(*bounds)
assert data is not None and data.size() >= bounds.extent.pixel_count * 4
return Image(QImage(data, *bounds.extent, QImage.Format_ARGB32))

def insert_layer(
self, name: str, img: Image, bounds: Bounds, below: Optional[krita.Node] = None
Expand Down Expand Up @@ -110,12 +112,19 @@ def hide_layer(self, layer):
return layer

@property
def paint_layers(self):
return [node for node in self._doc.rootNode().childNodes() if node.type() == "paintlayer"]
def image_layers(self):
return list(_traverse_layers(self._doc.rootNode(), ["paintlayer", "grouplayer"]))

def find_layer(self, id: QUuid):
return next((layer for layer in self.paint_layers if layer.uniqueId() == id), None)
return next((layer for layer in self.image_layers if layer.uniqueId() == id), None)

@property
def active_layer(self):
return self._doc.activeNode()


def _traverse_layers(node, type_filter=None):
for child in node.childNodes():
yield from _traverse_layers(child, type_filter)
if not type_filter or child.type() in type_filter:
yield child
6 changes: 4 additions & 2 deletions ai_diffusion/ui/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,13 @@ def _notify(self):
self.changed.emit()

def update_and_select_layer(self, id: QUuid):
layers = self._model.document.paint_layers
layers = reversed(self._model.document.image_layers)
self.layer_select.clear()
index = -1
for layer in layers:
self.layer_select.addItem(layer.name(), layer.uniqueId())
index = next((i for i, layer in enumerate(layers) if layer.uniqueId() == id), -1)
if layer.uniqueId() == id:
index = self.layer_select.count() - 1
if index == -1:
self.remove_button.click()
else:
Expand Down

0 comments on commit a76ddf1

Please sign in to comment.