# Trying to implement custom scalar volume node

**URL:** <https://discourse.slicer.org/t/trying-to-implement-custom-scalar-volume-node/20713>\
**Category:** Development\
**Created:** [November 21, 2021, 4:14pm UTC](https://discourse.slicer.org/t/trying-to-implement-custom-scalar-volume-node/20713 "2021-11-21T16:14:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [November 21, 2021, 4:14pm UTC](https://discourse.slicer.org/t/trying-to-implement-custom-scalar-volume-node/20713/1 "2021-11-21T16:14:54Z")

</div>

Hi,

As I don’t fully understand how custom nodes should be written I describe my progress and probably if someone has an advice I would appreciate.

I’m working on custom node inherited from `vtkMRMLScalarVolumeNode`.

As an example I use `vtkMRMLAstroVolumeNode`. I’m able to display my node but it can’t be saved. When I click on `Save` button I can see my node in the table but in fact the node is not saved (the appropriate file is not created). I guess responsible for that is `vtkMRMLCustomStorageNode`?

Now I need to add some information to the node (for example the path to the file where the original data resides and the parameters that were applied when data was loaded) that should be seen in `Data module -> Node information` and also this should be saved when saving the scene (I guess using `vtkMRMLDisplayableNode::WriteXML`) and read when loading the scene. Is it ok if I add this data to the node `Attributes`?

To display this information I should reimplement `PrintSelf()` method (there should not be much problems)

To write/read I should reimplement `WriteXML()` and `ReadXMLAttributes()`. Is it the information about my node that should be written to the `Scene.mrml` file?

[My custom node can be found there](https://github.com/tierra-colada/Seismic/tree/main/SeisLoad/MRML)

---

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [November 21, 2021, 9:42pm UTC](https://discourse.slicer.org/t/trying-to-implement-custom-scalar-volume-node/20713/2 "2021-11-21T21:42:51Z")

</div>

Apparently `PrintSelf()`, `WriteXML()` and `ReadXMLAttributes()` are pretty simple to implement using vtkMRML macros. The task is solved.

The only thins has left to do is to save the binary (scalars) data from node.

---

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [November 22, 2021, 5:54pm UTC](https://discourse.slicer.org/t/trying-to-implement-custom-scalar-volume-node/20713/3 "2021-11-22T17:54:13Z")

</div>

To fix the issue with saving volume node inherited from `vtkMRMLScalarNode` I just needed my custom storage node to inherit from `vtkMRMLVolumeArchetypeStorageNode` instead of more general `vtkMRMLStorageNode`.

I’m able to save/load custom volumes now

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [November 22, 2021, 6:52pm UTC](https://discourse.slicer.org/t/trying-to-implement-custom-scalar-volume-node/20713/4 "2021-11-22T18:52:22Z")

</div>

Volume nodes are quite generic, so most likely you don’t need to create custom variants. If you want to save into a new file format then you need a custom storage node. For storing special display properties you can use custom node attributes in existing volume display node classes, but for some sophisticated features adding a new display node class might be necessary.

---

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [November 22, 2021, 7:06pm UTC](https://discourse.slicer.org/t/trying-to-implement-custom-scalar-volume-node/20713/5 "2021-11-22T19:06:48Z")

</div>

Yes thank you, for now I just created custom scalar volume and added some information (attributes) that I write to XML file and PrintSelf.

I don’t need to use new file formats and thus I just inherited from VolumeStorage and VolumeDisplay nodes mostly to get experience of registering nodes, get known with read/write functions. I think this will help me to better understand Slicer. My custom storage node and display node works exactly the same way as regular volume storage and volume display nodes

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [November 22, 2021, 9:17pm UTC](https://discourse.slicer.org/t/trying-to-implement-custom-scalar-volume-node/20713/6 "2021-11-22T21:17:50Z")

</div>

It can be useful to create this custom node as a learning exercise. If you don’t end up needing anything else than storing custom attributes then you can revert to using a regular scalar volume node and use Get/Set Attribute. All custom attributes are stored in the scene.

---

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [November 22, 2021, 9:25pm UTC](https://discourse.slicer.org/t/trying-to-implement-custom-scalar-volume-node/20713/7 "2021-11-22T21:25:25Z")

</div>

Have not thought that I can simply add attributes via set attribute, that probably would be enough.
