Optimizing performance of events when clearing nodes

The code above where the qMRMLSubjectHierarchyTreeView isn’t updated upon ending the BatchProcessingState produces the following errors.

[DEBUG][Qt] 25.05.2022 18:24:45 [] (unknown:0) - Python console user input: slicer.mrmlScene.EndState(slicer.mrmlScene.BatchProcessState)
[ERROR][VTK] 25.05.2022 18:24:45 [vtkMRMLSubjectHierarchyNode (0000028BAFF77F30)] (D:\D\P\S-0\Libs\MRML\Core\vtkMRMLSubjectHierarchyNode.cxx:2114) - GetItemDataNode: Failed to find subject hierarchy item by ID 8
[ERROR][VTK] 25.05.2022 18:24:45 [vtkMRMLSubjectHierarchyNode (0000028BAFF77F30)] (D:\D\P\S-0\Libs\MRML\Core\vtkMRMLSubjectHierarchyNode.cxx:2827) - GetItemParent: Failed to find subject hierarchy item by ID 8
[ERROR][VTK] 25.05.2022 18:24:45 [vtkMRMLSubjectHierarchyNode (0000028BAFF77F30)] (D:\D\P\S-0\Libs\MRML\Core\vtkMRMLSubjectHierarchyNode.cxx:2114) - GetItemDataNode: Failed to find subject hierarchy item by ID 9
[ERROR][VTK] 25.05.2022 18:24:45 [vtkMRMLSubjectHierarchyNode (0000028BAFF77F30)] (D:\D\P\S-0\Libs\MRML\Core\vtkMRMLSubjectHierarchyNode.cxx:2827) - GetItemParent: Failed to find subject hierarchy item by ID 9

Looks like I’m possibly running into known issues as written up in https://github.com/Slicer/Slicer/issues/6006 and https://github.com/Slicer/Slicer/issues/4650.

Using the workaround mentioned in https://github.com/Slicer/Slicer/issues/6006, I can get the subject hierarchy in the Markups module to refresh.

Force subject hierarchy tree rebuild by temporarily switching to scene import mode:

slicer.mrmlScene.StartState(slicer.vtkMRMLScene.ImportState)
slicer.mrmlScene.EndState(slicer.vtkMRMLScene.ImportState)

image