# Zoom in or out via python interactor version 4.8.0 on MAC

**URL:** <https://discourse.slicer.org/t/zoom-in-or-out-via-python-interactor-version-4-8-0-on-mac/2254>\
**Category:** Support\
**Created:** [March 6, 2018, 11:28pm UTC](https://discourse.slicer.org/t/zoom-in-or-out-via-python-interactor-version-4-8-0-on-mac/2254 "2018-03-06T23:28:34Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Soufane](https://avatars.discourse-cdn.com/v4/letter/a/b38774/32.png) [@Ahmed\_Soufane](https://discourse.slicer.org/u/Ahmed_Soufane)\
**Post date:** [March 6, 2018, 11:28pm UTC](https://discourse.slicer.org/t/zoom-in-or-out-via-python-interactor-version-4-8-0-on-mac/2254/1 "2018-03-06T23:28:34Z")

</div>

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%20PM](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/2/0/2012700a7c1c835acd68c9864cd963946b61121c.png)

---

<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:** [March 7, 2018, 1:05am UTC](https://discourse.slicer.org/t/zoom-in-or-out-via-python-interactor-version-4-8-0-on-mac/2254/2 "2018-03-07T01:05:45Z")

</div>

You can use these to zoom in/out:

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

---

<div class="post-metadata">

**Author:** ![Ahmed\_Soufane](https://avatars.discourse-cdn.com/v4/letter/a/b38774/32.png) [@Ahmed\_Soufane](https://discourse.slicer.org/u/Ahmed_Soufane)\
**Post date:** [March 7, 2018, 1:44am UTC](https://discourse.slicer.org/t/zoom-in-or-out-via-python-interactor-version-4-8-0-on-mac/2254/3 "2018-03-07T01:44:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [March 7, 2018, 7:36am UTC](https://discourse.slicer.org/t/zoom-in-or-out-via-python-interactor-version-4-8-0-on-mac/2254/4 "2018-03-07T07:36:36Z")

</div>

> [@Ahmed\_Soufane](#):
>
> 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:

```auto
getNodes("*Camera*")

```

---

<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:** [March 7, 2018, 1:19pm UTC](https://discourse.slicer.org/t/zoom-in-or-out-via-python-interactor-version-4-8-0-on-mac/2254/5 "2018-03-07T13:19:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ahmed\_Soufane](https://avatars.discourse-cdn.com/v4/letter/a/b38774/32.png) [@Ahmed\_Soufane](https://discourse.slicer.org/u/Ahmed_Soufane)\
**Post date:** [March 7, 2018, 8:47pm UTC](https://discourse.slicer.org/t/zoom-in-or-out-via-python-interactor-version-4-8-0-on-mac/2254/7 "2018-03-07T20:47:34Z")

</div>

![41%20PM](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/9/a/9ad018ca1d578c7fac6512a532d2f5df2003813a.png)  
this is what I got when I executed getNodes(“_Camera_”)

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [March 7, 2018, 9:09pm UTC](https://discourse.slicer.org/t/zoom-in-or-out-via-python-interactor-version-4-8-0-on-mac/2254/8 "2018-03-07T21:09:41Z")

</div>

@Ahmed_Soufane:

> [@lassoan](#):
>
> Make sure you execute the code snippet tline by line.

Did you try this ?

> [@Ahmed\_Soufane](#):
>
> this is what I got when I executed getNodes(“Camera”)

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](https://meta.discourse.org/t/post-format-reference-documentation/19197)

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

---

<div class="post-metadata">

**Author:** ![Ahmed\_Soufane](https://avatars.discourse-cdn.com/v4/letter/a/b38774/32.png) [@Ahmed\_Soufane](https://discourse.slicer.org/u/Ahmed_Soufane)\
**Post date:** [March 7, 2018, 9:43pm UTC](https://discourse.slicer.org/t/zoom-in-or-out-via-python-interactor-version-4-8-0-on-mac/2254/9 "2018-03-07T21:43:13Z")

</div>

> [@lassoan](#):
>
> camera.Dolly(0.8)

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)

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [March 7, 2018, 9:44pm UTC](https://discourse.slicer.org/t/zoom-in-or-out-via-python-interactor-version-4-8-0-on-mac/2254/10 "2018-03-07T21:44:23Z")

</div>

> [@Ahmed\_Soufane](#):
>
> 3D slicer 4.8.0

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.

---

<div class="post-metadata">

**Author:** ![Ahmed\_Soufane](https://avatars.discourse-cdn.com/v4/letter/a/b38774/32.png) [@Ahmed\_Soufane](https://discourse.slicer.org/u/Ahmed_Soufane)\
**Post date:** [March 7, 2018, 9:57pm UTC](https://discourse.slicer.org/t/zoom-in-or-out-via-python-interactor-version-4-8-0-on-mac/2254/11 "2018-03-07T21:57:15Z")

</div>

> [@lassoan](#):
>
> camera.Dolly(0.8)

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 …

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [March 7, 2018, 10:57pm UTC](https://discourse.slicer.org/t/zoom-in-or-out-via-python-interactor-version-4-8-0-on-mac/2254/12 "2018-03-07T22:57:11Z")

</div>

> [@Ahmed\_Soufane](#):
>
> what did you execute exactly

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

```auto
# 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)

```

---

<div class="post-metadata">

**Author:** ![Ahmed\_Soufane](https://avatars.discourse-cdn.com/v4/letter/a/b38774/32.png) [@Ahmed\_Soufane](https://discourse.slicer.org/u/Ahmed_Soufane)\
**Post date:** [March 7, 2018, 11:14pm UTC](https://discourse.slicer.org/t/zoom-in-or-out-via-python-interactor-version-4-8-0-on-mac/2254/13 "2018-03-07T23:14:40Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [March 8, 2018, 12:15am UTC](https://discourse.slicer.org/t/zoom-in-or-out-via-python-interactor-version-4-8-0-on-mac/2254/14 "2018-03-08T00:15:19Z")

</div>

> what does updating dolly actually means ?

From the [VTK documentation](https://www.vtk.org/doc/nightly/html/classvtkCamera.html#ac1dcd796574d6b2abcade341587838c2):

```auto
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.

```

---

<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:** [March 8, 2018, 12:31am UTC](https://discourse.slicer.org/t/zoom-in-or-out-via-python-interactor-version-4-8-0-on-mac/2254/15 "2018-03-08T00:31:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![talmazov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/talmazov/32/3966_2.png) [@talmazov](https://discourse.slicer.org/u/talmazov)\
**Post date:** [July 2, 2019, 8:37pm UTC](https://discourse.slicer.org/t/zoom-in-or-out-via-python-interactor-version-4-8-0-on-mac/2254/16 "2019-07-02T20:37:21Z")

</div>

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!

---

<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:** [July 2, 2019, 8:52pm UTC](https://discourse.slicer.org/t/zoom-in-or-out-via-python-interactor-version-4-8-0-on-mac/2254/17 "2019-07-02T20:52:38Z")

</div>

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)

> <https://github.com/Slicer/Slicer/blob/a02f79c7c788708a3dccfeb9ff4df76b6a5868be/Libs/MRML/Logic/vtkMRMLSliceLinkLogic.cxx#L370-L404>

---

<div class="post-metadata">

**Author:** ![talmazov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/talmazov/32/3966_2.png) [@talmazov](https://discourse.slicer.org/u/talmazov)\
**Post date:** [July 2, 2019, 9:54pm UTC](https://discourse.slicer.org/t/zoom-in-or-out-via-python-interactor-version-4-8-0-on-mac/2254/18 "2019-07-02T21:54:44Z")

</div>

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()
```
