Zoom in or out via python interactor version 4.8.0 on MAC

Hi, I was wondering how can I control the zooming of the 3D view via python interactor on slicer ?
like it can be seen on the attached pictures?
thank you
35%20PM20%20PM

You can use these to zoom in/out:

camera = slicer.mrmlScene.GetFirstNodeByClass('vtkMRMLCameraNode').GetCamera()
camera.Dolly(1.2)
camera.Dolly(0.8)

I tried the provided code, but nothing has changed !!

In that case, you may have multiple 3d views and/or cameras objects.

What is the output of:

getNodes("*Camera*")

Yes, check the camera (you can also get the camera for a view as shown in examples in the script repository).

Also, the two Dolly commands in the example show how to zoom in and out. If you run them all at once then it zooms in and out, so you may not notice the difference. Make sure you execute the code snippet tline by line.

41%20PM
this is what I got when I executed getNodes(“Camera”)

@Ahmed_Soufane:

Did you try this ?

As a side note, you don’t have to generate a screemshot when sharing experiment done in the python interactor, you could copy the text and use the markdown syntax for code snippet. See Is there any reference documentation for post formatting? - support - Discourse Meta

Now, having only camera means that the code snippet posted by @lassoan is expected to work. I am running out of idea.

I executed this code snippet, line by line, but nothing has changed in the images. maybe because I am using an older version of 3D slicer 4.8.0 ? would you recommend updating to the newer version maybe this issue will be resolved ??

camera = slicer.mrmlScene.GetFirstNodeByClass(‘vtkMRMLCameraNode’).GetCamera()

camera.Dolly(1.2)
camera.Dolly(0.8)

I tested with Slicer 4.8.1 and I confirmed it worked on Linux

Would be nice if you could try both 4.8.1 and the latest preview build.

what did you execute exactly in order to zoom in or out can you show me please, because I just downloaded slicer version 4.8.1, and still there is no response from slicer and nothing changed in any of the views …

Here it is:

  1. Sample Data → MRHead
  2. Enable volume Rendering
  3. Open python interactor and copy/past the python code
  4. Then press enter, and the dolly will update
# Clear scene
slicer.mrmlScene.Clear(0)

# Download sample data
from SampleData import SampleDataLogic
mrhead = SampleDataLogic().downloadMRHead()

# Open VolumeRendering module
selectModule(slicer.modules.volumerendering)

# Get VolumeRendering widget
widget = slicer.modules.volumerendering.widgetRepresentation()

# Enable VolumeRendering for our dataset
widget.setMRMLVolumeNode(mrhead)

# Get rendering technique combobox and change to GPU technique to GPU
# XXX This is sub-optimal and brittle, we will improve the API to allow changing the technique more easily
slicer.util.findChild(widget, "RenderingMethodComboBox").currentIndex = 1

# Enable rendering
getNode("*GPU*Rendering*").SetVisibility(1)

# Get and update camera
camera = slicer.mrmlScene.GetFirstNodeByClass('vtkMRMLCameraNode').GetCamera()

# Update Dolly
camera.Dolly(0.8)


# ... 
camera.Dolly(1.2)

what does updating dolly actually means ? Is is suppose to zoom in or out on the 3D view, because even though I have used the exact same code you sent. I got no response from slicer at all, it the 3D image did not zoom in or out ?

what does updating dolly actually means ?

From the VTK documentation:

Divide the camera's distance from the focal point by the given dolly value.

Use a value greater than one to dolly-in toward the focal point, and use a value less 
than one to dolly-out away from the focal point.

The code that Jc provided works for me. Be sure to experiment by typing camera.Dolly(X) into the python console with different values of X and see what happens in the 3D view.

Hey all,
I am trying to accomplish the same thing with zooming but not with the 3D widget camera but in a slice rendering. I’ve tried SetDimension and SetFieldOfView but they distort the image.
The Dolly function is a subset of the camera and not the slice.

I am essentially trying to recreate the right mouse click and drag that allows you to zoom-in on a set point.

Thanks!

You can use SetFieldOfView but compensate for the size of the render view like is done here (so you don’t distort the aspect ratio)

Awesome, worked like a charm. I adapted it to constant value. Here is the snip code in case someone else runs into this issue.

# rescaling dimensions to zoom in using slice node's aspect ratio
x = 50
y = x * sliceNode.GetFieldOfView()[1] / sliceNode.GetFieldOfView()[0]
z = sliceNode.GetFieldOfView()[2]
sliceNode.SetFieldOfView(x,y,z)
sliceNode.Modified()