Toggling visibility of several curves within a specific folder

I’m working on a custom module that toggles the visibility of the markups within a specific folder. For example, a user can select the option “Nodule 1”, which causes only the markups within the Nodule 1 folder to be visible. Is there a way to accomplish this? Attached is a screenshot illustrating the problem.

For reference, this is what my UI looks like.

Check out the Slicer script repository, which should have most of the pieces for what you need. For example, this section has how to control the visibility of a MarkupsCurve: https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#change-markup-point-list-display-properties

You can identify and manipulate the items in a folder using the subject hierarchy, see the various parts of this section: https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#subject-hierarchy

Controlling visibility at the folder level is slightly more complicated in that the first time you want to do it you need to explicitly create a display node for that folder as described here: https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#manipulate-subject-hierarchy-item

That should help you get started. If you have further questions or run into problems please feel free to continue the conversation here.

Patrick followed up with a message with this question (adding it here so the response stays visible and can help other people searching the discourse):

Hello, thanks for you help! I had one more question on this topic. How do you save all of your data into a specific folder? I’m aware of the saveNode() function, but I’m not sure which node I need to save in order for everything to be saved properly.

The answer depends on what you mean by saving properly.

  • If you mean saving all the created curves and all imported imaging data as well as the subject hierarchy organization so that you can reload it later, then what you want to do is save the “scene”. You can specify where everything gets saved in an interactive dialog window by clicking the “Save data” toolbar button (see https://slicer.readthedocs.io/en/latest/user_guide/data_loading_and_saving.html#save-data). The hierarchy organization is preserved in the MRML scene file (.mrml), which is semi-readable XML. The rest of the saved nodes will hold all the data, but will not themselves know anything about how they were organized into the subject hierarchy.
  • If you mean saving individual nodes to a directory structure matching the subject hierarchy structure (like saving the curve1, curve2, comment, and table relating to Nodule 1 into a directory folder named Nodule1 and saving the corresponding nodes for Nodule2 into a directory folder named Nodule2), I’m not aware of an existing automated way to do that, but it would be straightforward to write python code to do. Using the subject hierarchy node, you could traverse the children of a hierarchy folder and use saveNode() to save the node to a directory folder with a matching name. If you then want to load from that directory structure and recreate the subject hierarchy organization, you would need to write the code to do that as well (it would basically just be the reverse process: add a subject hierarchy folder for each directory folder, traverse the saved files in the directory folder, loading each node and then setting its subject hierarchy parent to the correct subject hierarchy folder).