How to see all loaded data in Scene via Python

How can I obtain all loaded data in the scene via python, if such data exists? I need to create a conditional that only runs a certain function if the scene already has some data, otherwise the user is prompted to load data.

Also what are the different data types that can be loaded by the user? For example, scene, volume, sequence, and transforms can be loaded by the user via the slicer.util.load… function, but I do not know all the possible types/functions

See the inheritance diagram at the top of this page which shows all the descendants of vtkMRMLStorableNode:
https://apidocs.slicer.org/master/classvtkMRMLStorableNode.html
There are other nodes that can be in a scene that serve various purposes, but I guess the storable nodes are “data types” that could be saved/loaded.
Another possibility is to look at all vtkMRMLDisplayableNodes:
https://apidocs.slicer.org/master/classvtkMRMLDisplayableNode.html
which includes only the ones the user can “see” in the scene.
To summarize: Markups, models, volumes, segmentations, and transforms.

For checking if the subject hierarchy (the list displayed in your screenshot) has any items in it, I think this works:

slicer.mrmlScene.GetSubjectHierarchyNode().GetNumberOfItems()

For listing all nodes in the scene of a certain type (which includes many that don’t show up in the subject hierarchy) you can do something like

list(slicer.mrmlScene.GetNodesByClass('vtkMRMLDisplayableNode'))

for example to get a list of all the vtkMRMLDisplayableNodes

2 Likes

For this use case, I would rather observe the MRML scene for new nodes added and react to the first addition (you can also filter the type of nodes you want to consider), then you can remove the observer or simply ignore subsequent event triggers.

There are a lot of data types that the user can load, and this also depends on the extensions installed by the user. slicer.util.load.. are utility functions to load the most commonly used types, but they don’t represent the whole extent of possibilities. In the approach described (observing the MRML Scene) above you will be “notified” for any data type loaded, even data supported by other modules.

1 Like