I’ve been able to remove nodes with the RemoveNode() function, but I can’t find any corresponding function for non-Node objects, such as folders. Is there a function to remove folders, or a similar method?
It turns out that if you convert folders to “subject”
you can delete them with
# Get the MRML scene
scene = slicer.mrmlScene
# Get the folder node you want to remove. Replace 'FolderName' with your folder's name.
folderNode = scene.GetFirstNodeByName('MyFolder')
# If the node exists, then remove it
if folderNode:
scene.RemoveNode(folderNode)
else:
print("Folder 'MyFolder' not found")
````
The preferred way seems to be:
shNode = slicer.mrmlScene.GetSubjectHierarchyNode()
exportFolderItemId = shNode.CreateFolderItem(shNode.GetSceneItemID(), "MyNewFolder")
# do something with the folder
# later ...
shNode.RemoveItem(exportFolderItemId)
2 Likes