How to add folder in scene?

Operating system: window 10
Slicer version: 5.2.1

how can i do this in python code ?

I want to know how to add a folder in the scene and add data to that folder.

Or can I add a folder that I already have locally to the slicer and set the contents of that folder to be added at the same time?

Right click in the data module and choose “Create folder” from the popup menu. Then you can drag any object loaded into the scene to this folder.

There are two things I want to know.

  1. How to add a folder to a scene

  2. How to make it belong to the folder added when loading data to 3D slicer

image

I would like to know the code to create a folderdisplaynode and put mesh data into subcategories of that folder.

Developer Guide — 3D Slicer documentation subject hierarchy section/script_repository.html#subject-hierarchy

(ref : vtkMRMLSubjectHierarchyNode Class Reference )

shNode = slicer.modules.subjecthierarchy.logic().GetSubjectHierarchyNode()

# folder create
folderNum = shNode.CreateFolderItem(3, "test")

temp = getNode('F')

shNode.CreateItem(folderNum, temp)

a -ha it’s simple , thx

shNode = slicer.modules.subjecthierarchy.logic().GetSubjectHierarchyNode()

# get current SceneID
SceneID = shNode.GetSceneItemID()

# folder create
folderNum = shNode.CreateFolderItem(SceneID, "test")

# dummy item
temp = getNode('F')

# add item to folder
shNode.CreateItem(folderNum, temp)

However, when I run the code that adds data to the folder like this, there is a phenomenon that the slicer turns off when there is a lot of data. Is there an optimization method?

Your code seems to work. A few comments:

CreateItem is not intended to use in this way, so there may be side-effects, not sure. Try shNode.SetItemParent instead.

FYI there is a simpler way to get the SH node:
shNode = slicer.mrmlScene.GetSubjectHierarchyNode()

I don’t understand this. Please elaborate.

@cpinter

The code I want to implement is:

  1. Load all markups (fiducialNode, ClosedcurvedNode, etc) used in slicer.

  2. Create a folder corresponding to each markups

  3. Distribute markups data corresponding to each folder.

#example tree
Scene
| 
|
| ㅡㅡㅡ FiducialNode(tentative named)
|        |___ node1
|        |___ node2
|
|
| ㅡㅡㅡ  ClosedCurvedNode
|
|
|ㅡㅡ etc

# Get hierarychyNode
shNode = slicer.mrmlScene.GetSubjectHierarchyNode()

# Get Current SceneID
SceneID = shNode.GetSceneItemID()

# Create folder
folderNum = shNode.CreateFolderItem(SceneID, "test")

# Set dummy data
temp = getNode('F')

# add item to folder
shNode.CreateItem(folderNum, temp)

However, in the case of proceeding as above, if there is a lot of data to put in the folder, the slicer is turned off.

I will refer to this method and try it.

edit :

# Get hierarychyNode
shNode = slicer.mrmlScene.GetSubjectHierarchyNode()

# Get Current SceneID
SceneID = shNode.GetSceneItemID()

# Create folder
folderNum = shNode.CreateFolderItem(SceneID, "test")

# Set dummy data
temp = getNode('F')

# Get the item ID of the node
nodeItemID = shNode.GetItemByDataNode(node)

# Set the parent of the node item to the folder item
shNode.SetItemParent(nodeItemID, folderItemID)

I understand what you mean.

image

Last question. What is this problem caused by?

  1. Distribute markups data corresponding to each folder.

Having data nodes under other nodes is possible in SH, but it is not something I’d consider default behavior. If you only have folders as parents of nodes, does it work?

I assume this means it crashes. If you can reproduce this with only using sample or synthetic data, please upload a scene and/or provide instructions and we’ll try to fix the crash.

Unfortunately subject hierarchy is still not bullet-proof. Sticking to the “normal” behavior it tends to work fine, but some errors do pop up. If you have cases that are not that normal (see above), probably mode. This particular warning does not indicate serious issues. If you know how to debug in C++, it would be great if you could find the cause for these.

@dsa934 I’m glad the last comment was helpful enough to set it as solution, but maybe it would help others seeing this topic later if you told us what exactly helped. Was it using folder items? Thanks :slight_smile:

I mischecked the solution checkbox.

I understood that slicer creates a tree structure automatically when you designate a parent as a viewnode after creating some data.

It’s better to use folders (or patient and study items which behave similarly) when you need a hierarchy.