Synchronize display scale across slice viewers

Great, thank you @lassoan . That’s exactly what I was looking for.

I just added a tiny bit of additional functionality, namely to toggle zoomSync on and off.

slicer.updatingSliceNodes = False
slicer.zoomSync = False

slicer.sliceNodes = [slicer.app.layoutManager().sliceWidget(viewName).mrmlSliceNode()
    for viewName in slicer.app.layoutManager().sliceViewNames()]

def sliceModified(caller, event):
    if slicer.updatingSliceNodes:
        # prevent infinite loop of slice node updates triggering slice node updates
        return
    slicer.updatingSliceNodes = True
    fov = caller.GetFieldOfView()
    for sliceNode in slicer.sliceNodes:
        if sliceNode != caller:
            sliceNode.SetFieldOfView(*fov)
    slicer.updatingSliceNodes = False

def toggleZoomSync():
    if slicer.zoomSync: 
        # zoom sync is on already, therefore observer function is alread hooked, should be unhooked
        for sliceNode in slicer.sliceNodes:
            sliceNode.RemoveObserver(sliceNode.zoomChangeObserverTag)
        slicer.zoomSync = False
    else: 
        # zoom sync is off --> toogle to on (register observer function)
        for sliceNode in slicer.sliceNodes:
            sliceNode.zoomChangeObserverTag = sliceNode.AddObserver(vtk.vtkCommand.ModifiedEvent, sliceModified)
        slicer.zoomSync = True

toggleZoomSync()

After running this code snippet once, calls to

toggleZoomSync()

toggle slice zoom synchronization on/off.

1 Like