Deletion of "vtkMRMLFolderDisplayNode"

Hi,

In my module, I create a folder using the following code:

    @staticmethod
    def create_folder_item_in_subject_hierarchy(serial_number):
        shNode = slicer.vtkMRMLSubjectHierarchyNode.GetSubjectHierarchyNode(slicer.mrmlScene)
        folder_name = serial_number + 'models'
        exportFolderItemId = shNode.CreateFolderItem(shNode.GetSceneItemID(), folder_name)
        return exportFolderItemId

Then after performing some manipulations, I delete this folder using the following code:

    @staticmethod
    def remove_folder_display_node() -> None:
       # folderDisplayNode = slicer.mrmlScene.GetFirstNodeByClass("vtkMRMLFolderDisplayNode")
        folderDisplayNode = slicer.util.getNodesByClass("vtkMRMLFolderDisplayNode")

        for node in folderDisplayNode:
            slicer.mrmlScene.RemoveNode(node)

The problem is that it doesn’t works.

It works after:

  1. Go to the Data Module.
  2. Check the “Show MRML ID” box,
  3. Toggle the visibility On then OFF then ON again
  4. Then run the code for removing

Is there a way that I can execute this without resorting to this 4 step hack?

The folder display node is only created if the user uses the show/hide function of the folder, or creates the display node explicitly (btw I’m pretty sure step 2 is not necessary).

To create the folder display node explicitly you can call this function