Creating a sequence of volumes with Python

Hi there,

I am fairly new to 3D slicer and am working on an interesting project, however I am stuck at a certain point. I want to create a 3-Dimensional Time Lapse of individual CT scans using a Python script and 3D Slicer. But for some reason, I can’t visualize the sequence in the 3D view.

I have tried the following:

  • loading the volumes in, and using volume rendering to visualize the individual volumes, and placing these visualized volume rendering nodes in a Sequence. When placing the sequence in the sequence browser, I get ‘0’ as output.

# Create model node sequence
Sequence = slicer.mrmlScene.AddNewNodeByClass(“vtkMRMLSequenceNode”)
erycina = slicer.util.loadVolume(slicer.app.slicerHome + “/Scans/result2/vol_000000.tiff”)
node = getNode(‘vol_000000’)
volRenLogic = slicer.modules.volumerendering.logic()
displayNode = volRenLogic.CreateDefaultVolumeRenderingNodes(node)
Sequence.SetDataNodeAtValue(displayNode, “0”)
slicer.mrmlScene.RemoveNode(node.GetDisplayNode())
slicer.mrmlScene.RemoveNode(node)

erycina = slicer.util.loadVolume(slicer.app.slicerHome + “/Scans/result2_2/vol_000000.tiff”)
node = getNode(‘vol_000000_1’)
volRenLogic = slicer.modules.volumerendering.logic()
displayNode = volRenLogic.CreateDefaultVolumeRenderingNodes(node)
Sequence.SetDataNodeAtValue(displayNode, “1”)
slicer.mrmlScene.RemoveNode(node.GetDisplayNode())
slicer.mrmlScene.RemoveNode(node)

# Create sequence browser node

sequencebrowser = slicer.mrmlScene.AddNode(slicer.vtkMRMLSequenceBrowserNode())
sequencebrowser.AddSynchronizedSequenceNodeID(Sequence.GetID())

ProxyNode = sequencebrowser.GetProxyNode(Sequence)
ProxyNode.SetAndObserveDisplayNodeID(ProxyNode.GetID())

  • loading the volumes in, and placing them in the Sequence, after which I tried to visualize the Sequence using volume rendering (which is how you visualize the Sequence if you just make it by hand without programming). Similarly, when I try to place the Sequence in a sequence browser, it gives me ‘0’ as output

# loading the data

erycina = slicer.util.loadVolume(slicer.app.slicerHome + “/Scans/result2/vol_000000.tiff”)
n1 = getNode(‘vol_000000’)

erycina2= slicer.util.loadVolume(slicer.app.slicerHome + “/Scans/result2_2/vol_000000.tiff”)
n2 = getNode(‘vol_000000_1’)

erycina3= slicer.util.loadVolume(slicer.app.slicerHome + “/Scans/res_na_6/vol_000000.tiff”)
n3 = getNode(‘vol_000000_2’)

# creating sequence

SequenceNode = slicer.mrmlScene.AddNewNodeByClass(‘vtkMRMLSequenceNode’, ‘Sequence’)
SequenceNode.SetDataNodeAtValue(n1, str(0))
SequenceNode.SetDataNodeAtValue(n2, str(1))
SequenceNode.SetDataNodeAtValue(n3, str(2))

# Create a sequence browser node for the new merged sequence

SequenceBrowserNode = slicer.mrmlScene.AddNewNodeByClass(‘vtkMRMLSequenceBrowserNode’, ‘TimeLapseSequence’)
SequenceBrowserNode.AddSynchronizedSequenceNode(SequenceNode)
slicer.modules.sequencebrowser.setToolBarActiveBrowserNode(SequenceBrowserNode)

# Show proxy node in slice viewers

ProxyNode = SequenceBrowserNode.GetProxyNode(SequenceNode)
slicer.util.setSliceViewerLayers(background=ProxyNode)

# volume rendering

volRenLogic = slicer.modules.volumerendering.logic()
displayNode = volRenLogic.CreateDefaultVolumeRenderingNodes(SequenceNode)

Another question I have is, are there any possibilities in saving the created time lapse? Other than screenrecording the made time lapse directly? As it would be super convenient if there is some sort of way to save it as .mpg, instead of having to record it, as I strive for the full automation of this process.
I hope you can help me out with this!

You don’t need to worry about display nodes (unless you want to modify display properties as you are replaying the animation).

You can save the rendered sequence as a video using Screen Capture module by choosing “Animation mode” -> sequence.

The suggestion how to save the video did work perfectly, thank you.

However, I still have the issue of getting the 3-D view of the sequence using python.
I want to be able to see the volumes of the sequence and want to write this in python, but everything i tried failed.

I did successfully create the sequence and place the volumes in the sequence, but can’t seem to find out how to visualize this in the 3-D view.

You can find examples of building sequences of volumes and transforms in these modules: