Bug 446811

Summary: setChildNodes misorders nodes
Product: [Applications] krita Reporter: JulesLetters+kde
Component: Layer StackAssignee: Krita Bugs <krita-bugs-null>
Status: CONFIRMED ---    
Severity: normal CC: halla, sergey
Priority: NOR    
Version First Reported In: 5.0.0-beta2   
Target Milestone: ---   
Platform: Microsoft Windows   
OS: Microsoft Windows   
Latest Commit: Version Fixed/Implemented In:
Sentry Crash Report:
Attachments: Long Crashlog included for completeness. The last few entries are from the provided script.

Description JulesLetters+kde 2021-12-11 03:23:01 UTC
Created attachment 144441 [details]
Long Crashlog included for completeness. The last few entries are from the provided script.

SUMMARY


STEPS TO REPRODUCE
1. Open Krita
2. Create a new document (I've used a custom document, 600 x 800)
3. Open the scripter via Tools > Scripts > Scripter, and use the following:

```
from krita import Krita, Document

class MyBug:
    @staticmethod
    def log_info(text: str) -> None:
        print(text)

    @staticmethod
    def remove_layer_if_exists(document: Document, layer_name: str) -> None:
        layer = document.nodeByName(layer_name)
        if layer:
            layer.remove()

    def do_a_thing(self) -> None:
        self.log_info("Doing the thing!")
        active_document = Krita.instance().activeDocument()

        temp_layer_name = "MY_LAYER"
        cloned_layer_name = f"{temp_layer_name}_CLONE"
        if active_document:
            self.log_info("A")
            self.remove_layer_if_exists(active_document, temp_layer_name)
            self.remove_layer_if_exists(active_document, cloned_layer_name)
            self.log_info("B")
            temp_layer = active_document.createGroupLayer(temp_layer_name)
            self.log_info("C")
            active_document.rootNode().addChildNode(temp_layer, None)
            self.log_info("D")
            child_nodes = [active_document.createNode("01", "paintLayer"),
                           active_document.createNode("02", "paintLayer"),
                           active_document.createNode("03", "paintLayer")]
            self.log_info("E")
            temp_layer.setChildNodes(child_nodes)
            self.log_info("F")

            cloned_layer = temp_layer.clone()
            self.log_info("G")
            cloned_layer.setName(cloned_layer_name)
            additional_child_nodes = [active_document.createNode("04", "paintLayer"),
                                      active_document.createNode("05", "paintLayer")]
            nodes = cloned_layer.childNodes() + additional_child_nodes
            self.log_info(str([n.name() for n in nodes]))
            self.log_info("H")
            cloned_layer.setChildNodes(nodes)
            self.log_info("I")
            active_document.rootNode().addChildNode(cloned_layer, None)
        self.log_info("Exiting Doing the thing function.")

MyBug().do_a_thing()
```

OBSERVED RESULT
Either:
1) A Crash after 'C' is logged to the console
2) The resulting Node tree is: MY_LAYER_CLONE (05, 04, 01, 03, 02), MY_LAYER (03, 02, 01)

EXPECTED RESULT
The resulting Node tree is: MY_LAYER_CLONE (05, 04, 03, 02, 01), MY_LAYER (03, 02, 01)

SOFTWARE/OS VERSIONS
Windows: 10.0.19043 Build 19043.1348
KDE Plasma Version: ???
KDE Frameworks Version: ???
Qt Version: ???
Comment 1 Sergey Morozov 2022-08-31 02:22:20 UTC
I can reproduce this bug on 5.1.0(on arch linux) with simpler example.

current_document = Krita.instance().activeDocument()
current_node = current_document.activeNode()
parent = current_node.parentNode()
children = parent.childNodes()
print([child.name() for child in children])
parent.setChildNodes(children)
children = parent.childNodes()
print([child.name() for child in children])

Example output:

['Selection Mask', 'Background', 'Paint Layer 1', 'diffusion 0', 'diffusion 1']
['Background', 'Paint Layer 1', 'diffusion 0', 'diffusion 1', 'Selection Mask']
Comment 2 Halla Rempt 2022-09-05 09:31:57 UTC
Are you really using Krita 5.0.0-beta2? If so, please update to the latest version, 5.1.0.
Comment 3 Halla Rempt 2022-09-05 09:36:47 UTC
I cannot reproduce any crashes with 5.1.0, though.
Comment 4 Sergey Morozov 2022-09-05 09:47:44 UTC
(In reply to Halla Rempt from comment #3)
> I cannot reproduce any crashes with 5.1.0, though.

No crashes on 5.1.0, but nodes are still misordered(it feels like they shift by one node every time I do setChildNodes)

Should I file a new bug?
Comment 5 Halla Rempt 2022-09-05 09:51:51 UTC
No, this is fine, we've narrowed down the problem and I've confirmed the issue. I'll just edit the title.