# How to add folder in scene?

**URL:** <https://discourse.slicer.org/t/how-to-add-folder-in-scene/29499>\
**Category:** Development\
**Created:** [May 17, 2023, 2:09am UTC](https://discourse.slicer.org/t/how-to-add-folder-in-scene/29499 "2023-05-17T02:09:51Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![dsa934](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dsa934/32/17907_2.png) [@dsa934](https://discourse.slicer.org/u/dsa934)\
**Post date:** [May 17, 2023, 2:09am UTC](https://discourse.slicer.org/t/how-to-add-folder-in-scene/29499/1 "2023-05-17T02:09:51Z")

</div>

Operating system: window 10  
Slicer version: 5.2.1

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/4/6/460e27508a41c729774dc75f727730127a628515.png)

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?

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [May 17, 2023, 2:16am UTC](https://discourse.slicer.org/t/how-to-add-folder-in-scene/29499/2 "2023-05-17T02:16:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dsa934](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dsa934/32/17907_2.png) [@dsa934](https://discourse.slicer.org/u/dsa934)\
**Post date:** [May 17, 2023, 2:20am UTC](https://discourse.slicer.org/t/how-to-add-folder-in-scene/29499/3 "2023-05-17T02:20:52Z")

</div>

> [@dsa934](#):
>
> how can i do this **in python code ?**

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](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/2/1/21dcd6d26c516e5372ea79dccf21571da977b63d.png)

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

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [May 17, 2023, 3:32am UTC](https://discourse.slicer.org/t/how-to-add-folder-in-scene/29499/4 "2023-05-17T03:32:23Z")

</div>

[Developer Guide — 3D Slicer documentation](https://slicer.readthedocs.io/en/latest/developer_guide) subject hierarchy section/script\_repository.html#subject-hierarchy

---

<div class="post-metadata">

**Author:** ![dsa934](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dsa934/32/17907_2.png) [@dsa934](https://discourse.slicer.org/u/dsa934)\
**Post date:** [May 17, 2023, 4:24am UTC](https://discourse.slicer.org/t/how-to-add-folder-in-scene/29499/5 "2023-05-17T04:24:09Z")

</div>

```auto
(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

---

<div class="post-metadata">

**Author:** ![dsa934](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dsa934/32/17907_2.png) [@dsa934](https://discourse.slicer.org/u/dsa934)\
**Post date:** [May 17, 2023, 7:51am UTC](https://discourse.slicer.org/t/how-to-add-folder-in-scene/29499/6 "2023-05-17T07:51:19Z")

</div>

```auto
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?

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [May 17, 2023, 9:24am UTC](https://discourse.slicer.org/t/how-to-add-folder-in-scene/29499/7 "2023-05-17T09:24:01Z")

</div>

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()`

> [@dsa934](#):
>
> 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?

I don’t understand this. Please elaborate.

---

<div class="post-metadata">

**Author:** ![dsa934](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dsa934/32/17907_2.png) [@dsa934](https://discourse.slicer.org/u/dsa934)\
**Post date:** [May 18, 2023, 1:36am UTC](https://discourse.slicer.org/t/how-to-add-folder-in-scene/29499/8 "2023-05-18T01:36:44Z")

</div>

@cpinter

> [@cpinter](#):
>
> I don’t understand this. Please elaborate.

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.

```auto
#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.

> [@cpinter](#):
>
> `CreateItem` is not intended to use in this way, so there may be side-effects, not sure. Try `shNode.SetItemParent` instead.

I will refer to this method and try it.

edit :

```auto
# 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](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/3/4/341d326b5d0954365b0e5c9a4dc712a60e22c05e.png)

Last question. What is this problem caused by?

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [May 22, 2023, 8:29am UTC](https://discourse.slicer.org/t/how-to-add-folder-in-scene/29499/9 "2023-05-22T08:29:13Z")

</div>

> 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?

> [@dsa934](#):
>
> if there is a lot of data to put in the folder, the slicer is turned off.

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.

> [@dsa934](#):
>
> Last question. What is this problem caused by?

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.

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [May 22, 2023, 9:31am UTC](https://discourse.slicer.org/t/how-to-add-folder-in-scene/29499/10 "2023-05-22T09:31:49Z")

</div>

@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 🙂

---

<div class="post-metadata">

**Author:** ![dsa934](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dsa934/32/17907_2.png) [@dsa934](https://discourse.slicer.org/u/dsa934)\
**Post date:** [May 22, 2023, 10:26am UTC](https://discourse.slicer.org/t/how-to-add-folder-in-scene/29499/11 "2023-05-22T10:26:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [May 22, 2023, 10:41am UTC](https://discourse.slicer.org/t/how-to-add-folder-in-scene/29499/12 "2023-05-22T10:41:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Victor\_Wayne](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/victor_wayne/32/80778_2.png) [@Victor\_Wayne](https://discourse.slicer.org/u/Victor_Wayne)\
**Post date:** [August 7, 2025, 10:10am UTC](https://discourse.slicer.org/t/how-to-add-folder-in-scene/29499/14 "2025-08-07T10:10:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [August 7, 2025, 10:41am UTC](https://discourse.slicer.org/t/how-to-add-folder-in-scene/29499/15 "2025-08-07T10:41:24Z")

</div>

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:

```auto
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

---

<div class="post-metadata">

**Author:** ![Victor\_Wayne](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/victor_wayne/32/80778_2.png) [@Victor\_Wayne](https://discourse.slicer.org/u/Victor_Wayne)\
**Post date:** [August 7, 2025, 11:59am UTC](https://discourse.slicer.org/t/how-to-add-folder-in-scene/29499/16 "2025-08-07T11:59:41Z")

</div>

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.

```python
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

```python
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.

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [August 7, 2025, 12:26pm UTC](https://discourse.slicer.org/t/how-to-add-folder-in-scene/29499/17 "2025-08-07T12:26:54Z")

</div>

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 🙂 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](https://github.com/Slicer/Slicer/blob/main/Modules/Loadable/SubjectHierarchy/Widgets/qMRMLSubjectHierarchyTreeView.h#L87-L106), which is quite comprehensive.
