Help with auto grouping a selected active node

I need a hotkey that places an active layer node into new nested groups at the active layer’s position. The following code does not work entirely, as it creates the new parent nodes, but it does not move the active node into the group I need it to be in.

from krita import Krita

app = Krita.instance()
doc = app.activeDocument()
root = doc.rootNode()


active_node = doc.activeNode()


group_node_image = doc.createGroupLayer("< image />")
group_node_house = doc.createGroupLayer("(Image Cropper)")
group_node_DI    = doc.createGroupLayer("DI: Crop")
group_node_DI.setBlendingMode("destination-in")


root.addChildNode(group_node_house, None)
group_node_house.addChildNode(group_node_image, None)
group_node_house.addChildNode(group_node_DI, None)


group_node_image.addChildNode(active_node, None)

Create a duplicate of the active node and add it to the group. The original active node can be deleted. How about this method?


from krita import Krita

app = Krita.instance()
doc = app.activeDocument()
root = doc.rootNode()


active_node = doc.activeNode()
active_node_d = active_node.duplicate()### add


group_node_image = doc.createGroupLayer("< image />")
group_node_house = doc.createGroupLayer("(Image Cropper)")
group_node_DI    = doc.createGroupLayer("DI: Crop")
group_node_DI.setBlendingMode("destination-in")


root.addChildNode(group_node_house, None)
group_node_house.addChildNode(group_node_image, None)
group_node_house.addChildNode(group_node_DI, None)



active_node.remove()### add
group_node_image.addChildNode(active_node_d, None)###

1 Like

This works for me, thank you!

Is there any explanation as to why the original activeNode by itself doesn’t work well as a reference?

I’m new to coding so I don’t know the details, but I think … is probably because activeNode is already added to the layer stack, and addchildNode() is a function that targets a node that has not yet been added to the stack. Or it could be that addchildNode() is a function for “adding” layers, not “moving” them.

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