# Creating a sequence of volumes with Python

**URL:** https://discourse.slicer.org/t/creating-a-sequence-of-volumes-with-python/10733
**Category:** Support
**Tags:** sequences, python
**Created:** [March 18, 2020, 1:19pm UTC](https://discourse.slicer.org/t/creating-a-sequence-of-volumes-with-python/10733 "2020-03-18T13:19:50Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Dootsiie](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dootsiie/32/6299_2.png) [@Dootsiie](https://discourse.slicer.org/u/Dootsiie)
#### Post date: [March 18, 2020, 1:19pm UTC](https://discourse.slicer.org/t/creating-a-sequence-of-volumes-with-python/10733/1 "2020-03-18T13:19:50Z")

</div>

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!

---

<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: [March 18, 2020, 1:23pm UTC](https://discourse.slicer.org/t/creating-a-sequence-of-volumes-with-python/10733/2 "2020-03-18T13:23:36Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Dootsiie](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dootsiie/32/6299_2.png) [@Dootsiie](https://discourse.slicer.org/u/Dootsiie)
#### Post date: [March 19, 2020, 1:43pm UTC](https://discourse.slicer.org/t/creating-a-sequence-of-volumes-with-python/10733/3 "2020-03-19T13:43:28Z")

</div>

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.

---

<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: [March 19, 2020, 8:35pm UTC](https://discourse.slicer.org/t/creating-a-sequence-of-volumes-with-python/10733/4 "2020-03-19T20:35:22Z")

</div>

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

- [https://github.com/SlicerRt/Sequences/blob/master/CropVolumeSequence/CropVolumeSequence.py](https://github.com/SlicerRt/Sequences/blob/master/CropVolumeSequence/CropVolumeSequence.py)
- [https://github.com/moselhy/SlicerSequenceRegistration/blob/master/SequenceRegistration/SequenceRegistration.py](https://github.com/moselhy/SlicerSequenceRegistration/blob/master/SequenceRegistration/SequenceRegistration.py)
