Center a volume in 3D scene

Hi, I want to center a certain volume in the 3D view. I tried
layoutManager = slicer.app.layoutManager()
threeDWidget = layoutManager.threeDWidget(0)
threeDView = threeDWidget.threeDView()
threeDView.resetFocalPoint()
in the scripted repository, but I have more than one nodes in the scene and I don’t want to delete them, so it doesn’t work. Does anyone have any idea how to do this?

Thanks for your time!

You don’t need to delete the nodes that you don’t want to include in the center computation, just temporarily hide them (node.GetDisplayNode().SetVisibility(False)).

Hi Andras,
Thanks for your answer! I tried hiding them but it is still not at the center on the scene.
If I have more than one volumes, even if I hide the others, the volume still won’t be at the center on the scene.
What I want to know is how to center one specific volume while other volumes are still there.
Thanks!

You can center the camera on a node by basic VTK method calls. Something like this should work:

node = getNode('CTChest')
threedView = slicer.app.layoutManager().threeDWidget(0).threeDView()

# Get volume center
bounds=[0,0,0,0,0,0]
node.GetRASBounds(bounds)
nodeCenter = [(bounds[0]+bounds[1])/2, (bounds[2]+bounds[3])/2, (bounds[4]+bounds[5])/2]

# Shift camera to look at the new focal point
renderWindow = threedView.renderWindow()
renderer = renderWindow.GetRenderers().GetFirstRenderer()
camera = renderer.GetActiveCamera()
oldFocalPoint = camera.GetFocalPoint()
oldPosition = camera.GetPosition()
cameraOffset = [nodeCenter[0]-oldFocalPoint[0], nodeCenter[1]-oldFocalPoint[1], nodeCenter[2]-oldFocalPoint[2]]
camera.SetFocalPoint(nodeCenter)
camera.SetPosition(oldPosition[0]+cameraOffset[0], oldPosition[1]+cameraOffset[1], oldPosition[2]+cameraOffset[2])