3D views synchronized using python

I am doing an VR/AR project in 3Dslicer, we open multiple 3D views, for example the black views shown in the image. We also have the blue 3D. We can now synchronize them in slicer by pressing the two interlooped circles, but I was wondering if there is a python function in the slicer library or somewhere else, to automatically synchronize and desynchronize them. I could not find anything on previously posted topics.

Pim

That would be the setSliceLink(bool) function of the slice controller.

You can have a look at this example to see how to use it (and how to retrieve the slice controller).
Your views need to be in the same viewgroup, but if it works for you with the button it should already be the case.

Thanks for your response. As far as I know this only works for slice views in slicer. But the views I am using are 3d views and indeed that is also what I see when I try to use this. It gives an error that there is no slice with the corresponding name.

Do you know how it works for 3d views?

For 3D views a different function is used (SetLinkedControl), see how it is in the source code when you press the button you mentioned:

1 Like

Yes thanks! This function works great I used it like this:

def link3dViews(self):
    lm = slicer.app.layoutManager()
    for name in ["View1", "LeftView", "RightView"]:
        widget = lm.threeDWidget(name)
        if widget and widget.mrmlViewNode():
            widget.mrmlViewNode().SetLinkedControl(True)

def unlink3dViews(self):
    lm = slicer.app.layoutManager()
    for name in ["View1", "LeftView", "RightView"]:
        widget = lm.threeDWidget(name)
        if widget and widget.mrmlViewNode():
            widget.mrmlViewNode().SetLinkedControl(False)

Only small bug that is happening, both methods are linked to a separate button, but I have to press unlink twice before the view actually unlinks, any idea why?

Accidently called both functions when trying to only call unlink, so it had to be done twice. This code should work for synchronizing 3d views!