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.

How to hide folders from the Data module subject Hierarchy? not to remove them, hide them.

The folders have a special folder display node, but it is only created when you show/hide it in the Data module (you’ll notice because the color item will appear in its row). In theory you can create it programmatically, but as I see that function is not python wrapped. This is the code:

pluginHandlerSingleton = slicer.qSlicerSubjectHierarchyPluginHandler.instance()
folderPlugin = pluginHandlerSingleton.pluginByName('Folder')
folderPlugin.createDisplayNodeForItem(folderItemID)

If you need to create the display node programmatically, there are two ways:

  1. Do in Python what this createDisplayNodeForItem function does
  2. Fix Python wrapping of the function

I have to admit that I don’t quite get what you are saying.

Here’s what I want to do.

I inspected the Data module and found out that it is using qMRMLSubjectHierarchyTreeView widget for showing the nodes in a tree view. I wanted to customize the Data module by creating my own custom widget. So I created a widget, with a combo box that selects from the list plans. The plans contains lines. I want to show the lines like a list and with all the added functionality of hide/show `eye` button, transformation tracking, color picking, and their IDs like how the Data module shows it. So I created one instance of qMRMLSubjectHierarchyTreeView, and set it like this.

self.lineDetailView = qMRMLSubjectHierarchyTreeView()
self.lineDetailView.setMRMLScene(slicer.mrmlScene)

The problem is that it shows all the nodes and folders. I just want to show the line that corresponds to the selected plan. I thought I could do the following

self.lineDetailView.nodeTypes = ['vtkMRMLMarkupsLineNode']

but this shows the folders with the lines. even if it didn’t show the folders, I don’t know how to delete the other line nodes that doesn’t belong to the current selected plan.

Any help will be appreciated. Thank you for replying to me.

OK, it turns out you want to hide them from the tree view, and not their contents from the views. This is why explaining a problem in more than a single line helps preventing misunderstandings :slight_smile: I thought this was a very simple question related to the original topic, but it’s quite different. Please open a new topic with your question if you cannot find the solution among the filtering options of the tree view, which is quite comprehensive.