Bug 487963

Summary: kis_paint_device assert when altering layer from Python
Product: [Applications] krita Reporter: IPv6 <razinkov>
Component: ScriptingAssignee: Krita Bugs <krita-bugs-null>
Status: RESOLVED INTENTIONAL    
Severity: crash CC: dimula73, halla
Priority: NOR    
Version First Reported In: nightly build (please specify the git hash!)   
Target Milestone: ---   
Platform: Microsoft Windows   
OS: Microsoft Windows   
Latest Commit: Version Fixed/Implemented In:
Sentry Crash Report:
Attachments: assert

Description IPv6 2024-06-03 14:22:25 UTC
Created attachment 170092 [details]
assert

SUMMARY

I have custom addon that replace layer byte data from python after custom processing. It worked well in Krita 5.2.+ and 5.3.0. Today i updated Krita Next to nightly build and it started to show an assert on each action. Tested both on MacOs and Windows - same assert on same action. It worked the same way well in previous nightly builds of 5.3.0

The python code that trigger assert is:
activeSel = doc.selection()
... many actions involving erode/dilate of selection ...
# restoring initial selection
doc.setSelection(activeSel) # causes assert

OBSERVED RESULT
- Assert in console SAFE ASSERT (krita): "!m_d->parent || !parent" in file /Users/gitlab/ws/builds/GZwHuM5x/0/graphics/krita/libs/image/kis_paint_device.cc, line 1195
- Alert on screen

EXPECTED RESULT
- no assert

SOFTWARE/OS VERSIONS
Windows: Windows 11
macOS: Sonoma 14.5 (23F79)
Comment 1 Halla Rempt 2024-07-27 09:14:12 UTC
Thank you for the report. Usually, safe asserts are not shown to the users, so they are not a huge deal. But I can confirm the issue.
Comment 2 Dmitry Kazakov 2024-11-07 18:57:57 UTC
Hi, IPv6!

When you modify the selection, you need to first duplicate the selection with `Selection::duplicate()` and then modify it:

```python
from krita import *
doc = Krita.instance().activeDocument()
activeSel = doc.selection()
newSel = activeSel.duplicate()
newSel.invert()
doc.setSelection(newSel)
```

Please take it into account that the selection should be a **raster** selection for this code to work correctly.