Subject hierarchy throws error message when creating item in the folder

Hi,

I noticed that if I try to programmatically create new folder in subject hierarchy and add there a node an error message is sent. Though the node is added to the folder.
The error message:
GetChildByPositionUnderParent: Failed to find subject hierarchy item under parent Scene at position 2
Here are the steps to reproduce:

  1. open the data module (so the subject hierarchy treeview gets initialized);
  2. copy/paste the code below (it creates model node, folder and adds the node to the folder):
# Create and set up polydata source
box = vtk.vtkCubeSource()
box.SetXLength(30)
box.SetYLength(20)
box.SetZLength(15)
box.SetCenter(10,20,5)

# Create a model node that displays output of the source
boxNode = slicer.modules.models.logic().AddModel(box.GetOutputPort())

# Adjust display properties
boxNode.GetDisplayNode().SetColor(1,0,0)
boxNode.GetDisplayNode().SetOpacity(0.8)

#-----------------------------------------
# Subject hierarchy part starts
#-----------------------------------------
shNode = slicer.mrmlScene.GetSubjectHierarchyNode()
folderID = shNode.CreateFolderItem(shNode.GetSceneItemID(), 'MyFolder')

# On this last step check the log, an error should appear there
shNode.CreateItem(folderID, boxNode)

By the way there is no such error when I use GUI to create folder and drag/drop the node to the folder.

Tested on Ubuntu 20.04 with Slicer dowloaded from official site 4.13.0-2022-02-02

Unfortunately subject hierarchy sometimes logs errors that we have been unable to find the root cause for. Since there is no funding for stabilizing infrastructure (and even then, subject hierarchy has become extremely complex by now so it has a low result/effort ratio), we focus on actual bugs that arise during the common use cases. So if your code apparently works well, then I suggest ignoring this specific error.

At the same time, contributions are more than welcome to track down and fix deep infrastructure issues like this one!

1 Like

Thank you, good to know