# Link slices views by default?

**URL:** <https://discourse.slicer.org/t/link-slices-views-by-default/1934>\
**Category:** Support\
**Created:** [January 25, 2018, 6:17pm UTC](https://discourse.slicer.org/t/link-slices-views-by-default/1934 "2018-01-25T18:17:54Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![gcsharp](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/gcsharp/32/863_2.png) [@gcsharp](https://discourse.slicer.org/u/gcsharp)\
**Post date:** [January 25, 2018, 6:17pm UTC](https://discourse.slicer.org/t/link-slices-views-by-default/1934/1 "2018-01-25T18:17:54Z")

</div>

Every time I load a close a scene or restart slicer, the slice views  
become unlinked. Is there a way to make them linked by default?

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [January 25, 2018, 9:01pm UTC](https://discourse.slicer.org/t/link-slices-views-by-default/1934/2 "2018-01-25T21:01:49Z")

</div>

You could put something like this in your .slicerrc.py

```auto
for node in getNodesByClass('vtkMRMLSliceCompositeNode'):
  node.SetLinkedControl(1)

```

[https://www.slicer.org/wiki/Documentation/Nightly/Developers/Python\_scripting#How\_to\_systematically\_execute\_custom\_python\_code\_at\_startup\_.3F](https://www.slicer.org/wiki/Documentation/Nightly/Developers/Python_scripting#How_to_systematically_execute_custom_python_code_at_startup_.3F)

---

<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:** [January 26, 2018, 3:58pm UTC](https://discourse.slicer.org/t/link-slices-views-by-default/1934/3 "2018-01-26T15:58:42Z")

</div>

In addition to Steve’s code snippet above, to make all _new_ slice viewers linked as well, you can change the default composite node properties like this:

```
defaultSliceCompositeNode = slicer.vtkMRMLSliceCompositeNode()
defaultSliceCompositeNode.SetLinkedControl(1)
slicer.mrmlScene.AddDefaultNode(defaultSliceCompositeNode)

```

This code has only effect on latest trunk version, as I’ve just fixed slice composite node creation to use scene’s [CreateNodeByClass](http://apidocs.slicer.org/master/classvtkMRMLScene.html#a147b4127ffcbbf0dbc5ed894bd4c92a4) method instead of the node’s New method.

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [January 26, 2018, 4:23pm UTC](https://discourse.slicer.org/t/link-slices-views-by-default/1934/4 "2018-01-26T16:23:51Z")

</div>

Nice Andras! 👍

If this feature is widely useful we could look into adding it as a settings option.

---

<div class="post-metadata">

**Author:** ![gcsharp](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/gcsharp/32/863_2.png) [@gcsharp](https://discourse.slicer.org/u/gcsharp)\
**Post date:** [January 26, 2018, 7:39pm UTC](https://discourse.slicer.org/t/link-slices-views-by-default/1934/5 "2018-01-26T19:39:34Z")

</div>

Out of sight! This works great!!

---

<div class="post-metadata">

**Author:** ![Fernando](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fernando/32/5640_2.png) [@Fernando](https://discourse.slicer.org/u/Fernando)\
**Post date:** [June 20, 2018, 10:57am UTC](https://discourse.slicer.org/t/link-slices-views-by-default/1934/6 "2018-06-20T10:57:47Z")

</div>

I think it would be useful to add the option as a setting. I consistently link the slice views as soon as I load an image.

---

<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:** [June 20, 2018, 1:40pm UTC](https://discourse.slicer.org/t/link-slices-views-by-default/1934/7 "2018-06-20T13:40:13Z")

</div>

It would be quite simple to implement this (just need to add it to qSlicerSettingsViewsPanel.cxx and qSlicerSettingsViewsPanel.ui), but since it can be [set using .slicerrc file](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Set_all_slice_views_linked_by_default) and not that many people have asked for this, the priority of implementing this is low. I would be happy to help you implementing this if you decide to give it a go.

---

<div class="post-metadata">

**Author:** ![Alex\_Vergara](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alex_vergara/32/15205_2.png) [@Alex\_Vergara](https://discourse.slicer.org/u/Alex_Vergara)\
**Post date:** [August 5, 2020, 9:21am UTC](https://discourse.slicer.org/t/link-slices-views-by-default/1934/8 "2020-08-05T09:21:18Z")

</div>

I am having issues with the following code

```auto
def makeSlicerLinkedCompositeNodes():
    # Set linked slice views in all existing slice composite nodes and in the default node
    sliceCompositeNodes = slicer.util.getNodesByClass(
        "vtkMRMLSliceCompositeNode")
    defaultSliceCompositeNode = slicer.mrmlScene.GetDefaultNodeByClass(
        "vtkMRMLSliceCompositeNode")
    if not defaultSliceCompositeNode:
        defaultSliceCompositeNode = slicer.mrmlScene.AddNewNodeByClass(
            "vtkMRMLSliceCompositeNode")
        slicer.mrmlScene.AddDefaultNode(defaultSliceCompositeNode)
    for sliceCompositeNode in sliceCompositeNodes:
        sliceCompositeNode.SetLinkedControl(True)
    defaultSliceCompositeNode.SetLinkedControl(True)

def setSlicerViews(backgroundID, foregroundID):
    if not utils.checkTesting():
        makeSlicerLinkedCompositeNodes()

        slicer.util.setSliceViewerLayers(background=str(
            backgroundID), foreground=str(foregroundID), foregroundOpacity=0.5)

        slicer.app.layoutManager().setLayout(
            slicer.vtkMRMLLayoutNode.SlicerLayoutFourUpView)
        slicer.util.resetSliceViews()
        slicer.app.processEvents()

```

It creates extra composite nodes.

Is there a better way to set background and foreground in a forced linked views scheme?

---

<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:** [August 5, 2020, 5:35pm UTC](https://discourse.slicer.org/t/link-slices-views-by-default/1934/9 "2020-08-05T17:35:59Z")

</div>

It is not necessary to add default nodes to the scene, so instead of `AddNewNodeByClass` you can use `CreateNewNodeByClass`. See complete example: [https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Set\_all\_slice\_views\_linked\_by\_default](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Set_all_slice_views_linked_by_default)

---

<div class="post-metadata">

**Author:** ![Alex\_Vergara](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alex_vergara/32/15205_2.png) [@Alex\_Vergara](https://discourse.slicer.org/u/Alex_Vergara)\
**Post date:** [August 6, 2020, 10:12am UTC](https://discourse.slicer.org/t/link-slices-views-by-default/1934/10 "2020-08-06T10:12:32Z")

</div>

Thanks, that solved the issue!

---

<div class="post-metadata">

**Author:** ![Alex\_Vergara](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alex_vergara/32/15205_2.png) [@Alex\_Vergara](https://discourse.slicer.org/u/Alex_Vergara)\
**Post date:** [August 11, 2020, 1:18pm UTC](https://discourse.slicer.org/t/link-slices-views-by-default/1934/11 "2020-08-11T13:18:28Z")

</div>

the example in [https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Set\_all\_slice\_views\_linked\_by\_default](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Set_all_slice_views_linked_by_default)  
contradicts the memory management in [https://www.slicer.org/wiki/Documentation/Nightly/Developers/Tutorials/MemoryManagement](https://www.slicer.org/wiki/Documentation/Nightly/Developers/Tutorials/MemoryManagement)

it says that after `slicer.mrmlScene.GetNodesByClass` it must follow an unregister call.

```auto
nodes = slicer.mrmlScene.GetNodesByClass('vtkMRMLLinearTransformNode')
nodes.UnRegister(slicer.mrmlScene) # reference count is increased by both the factory method and the python reference; unregister to keep only the python reference
...

```

if I do

```auto
nodes = slicer.util.getNodesByClass("vtkMRMLScalarVolumeNode")
nodes.UnRegister(slicer.mrmlScene)

```

Then Slicer complains as `AttributeError: 'list' object has no attribute 'UnRegister'`

Why does the linked views example works? What is the correct way then?

---

<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:** [August 11, 2020, 1:29pm UTC](https://discourse.slicer.org/t/link-slices-views-by-default/1934/12 "2020-08-11T13:29:19Z")

</div>

One method returns a Python list, the other returns a VTK collection.

For Python scripts, it is simpler to use `slicer.util.getNodesByClass` because it returns a Python list, which is easier to iterate through and UnRegister has been already called internally.
