How to remove the SubjectHierarchyNode (e.g. folder)and its items?

How to remove the SubjectHierarchyNode (e.g. folder)and its items?

可能是Subject Hierarchy太大了吧, 进行到一个数量就会崩掉, 所以我想分阶段, 把前面那个节段的nodes放到一个folder, 保存后删除再进行下个节段的操作, 我已经把它们装进了一个folder, 该怎么删掉这个folder呢?

It may be that the Subject Hierarchy is too large, and it will crash when it reaches a certain large amount, so I want to put the nodes of the previous section into a folder in stages, save it and delete it before proceeding to the next section. I have a folder including the nodes of the previous section, how can I delete this folder and its items?

def deleteFolder(folderID, removeDataNode=True, recursive=True):
    shNode = slicer.vtkMRMLSubjectHierarchyNode.GetSubjectHierarchyNode(slicer.mrmlScene)
    shNode.RemoveItem(folderID, removeDataNode, recursive)

I’d modernize the above snippet by:

shNode = slicer.mrmlScene.GetSubjectHierarchyNode()

Also I don’t really see the use of a function for this two-liner (that could be one), but it’s a matter of taste.

*folderID? How to get it? Why don’t use folderName?

@cpinter
@Alex_Vergara

Learn about Subject Hierarchy here
https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#subject-hierarchy

def deleteFolder(folderName, removeDataNode=True, recursive=True):
    shNode = slicer.mrmlScene.GetSubjectHierarchyNode()
    sceneItemID = shNode.GetSceneItemID()
    folderID = shNode.GetItemChildWithName(sceneItemID,folderName)
    shNode.RemoveItem(folderID, removeDataNode, recursive)

This is my code…

tow-line?what’s mean?

NNs=[]
nodes = slicer.util.getNodes("*")
for key in nodes.keys():
  if key != "12: Unnamed Series":
    modNode = slicer.util.getNode(key)
    if isinstance(modNode, slicer.vtkMRMLTransformableNode):
      NNs.append(modNode)
shNode = slicer.vtkMRMLSubjectHierarchyNode.GetSubjectHierarchyNode(slicer.mrmlScene)
newParentFolderItem = shNode.CreateFolderItem(shNode.GetSceneItemID(), "folder")
for node in NNs:
  nodeItem = shNode.GetItemByDataNode(node)
  shNode.SetItemParent(nodeItem, newParentFolderItem)

这貌似是另一个问题, 如图和code, 有一些重名的nodes没包括到folder里面,怎么整呀?@lassoan@Juicy@jamesobutler @jcfr @pieper

This seems to be another problem. As shown in the figure and code, some nodes with the same name are not included in the folder. How to fix it?
@lassoan@Juicy@jamesobutler @jcfr @pieper@cpinter@Alex_Vergara