How to duplicate a vtkMRMLScalarVolumeNode using Python?

Hi everyone,

I am trying to duplicate a vtkMRMLScalarVolumeNode:
node_CopyVolume = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLScalarVolumeNode')

What can I do to copy the volume data from node_OriginVolume to the node_CopyVolume, so that node_CopyVolume can be displayed in the 3D view when node_OriginVolume is changed or removed?

Thank you so much in advance!

Hi Steve,

Thank you so much for your help!

I tested two ways of cloning the ScalarVolume:

  1. Following the way you showed me:
volumesLogic = slicer.modules.volumes.logic()
node_Copy= volumesLogic.CloneVolume(slicer.mrmlScene, node_Origin, "Cloned volume")
  1. The method to copy any type of node:
shNode = slicer.vtkMRMLSubjectHierarchyNode.GetSubjectHierarchyNode(slicer.mrmlScene)
itemIDToClone_X  = shNode.GetItemByDataNode(node_Origin)
clonedItemID_X = slicer.modules.subjecthierarchy.logic().CloneSubjectHierarchyItem(shNode, itemIDToClone_X)
node_Copy = shNode.GetItemDataNode(clonedItemID_X);    node_Copy.SetName("Cloned volume")

However, after I got node_Copy , neither of the two copied ScalarVolume can be displayed under a transform hierarchy. I was only able to display them in 2D window with Axial plane setting, however, never be able to put them under a Linear Transform.

Could you please help me solve the problem, and let me know how to show the cloned ScalarVolume in 3D?

The CloneVolume method at least should give you a normal volume node that should work the same as the source. I haven’t tried the subject hierarchy version but it should be the same as cloning with the context menu in the Data module so you could experiment with that to see if you get the same kind of node with code that you get manually.

Generally, it’s best if you can provide the shortest possible snippet that replicates any issues you see using public data (using the SampleData module). Something that anyone can paste in the the python console to replicate. Then we can see what’s happening and give you advice.

Thank you, @pieper !

Yes, I can make the node_Copy display in the 3D view when toggling it in one of the three 2D windows (red, green, yellow).

However, ever since the 2D window changed the view, the ScalarVolume will disappear.

How can I display all more than 3 ScalarVolumes in the 3D window?
Say if I would like to display 10 ScalarVolumes at the same time in 3D slicer, all duplicated and transformed from the original one, is that possible?

You may need to review some of the tutorials and examples to see what’s available currently built in to Slicer. If it’s not available you may need to write custom code to display the 10 volumes the way you want them.