# Rotate 3D image

**URL:** <https://discourse.slicer.org/t/rotate-3d-image/1968>\
**Category:** Support\
**Created:** [January 29, 2018, 7:05pm UTC](https://discourse.slicer.org/t/rotate-3d-image/1968 "2018-01-29T19:05:10Z")\
**Posts on this page:** 12\
**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:** [January 29, 2018, 7:05pm UTC](https://discourse.slicer.org/t/rotate-3d-image/1968/1 "2018-01-29T19:05:10Z")

</div>

How to rotate the 3D image using phython interactor ?

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [January 29, 2018, 7:50pm UTC](https://discourse.slicer.org/t/rotate-3d-image/1968/2 "2018-01-29T19:50:50Z")

</div>

This is one way

> <https://github.com/Slicer/Slicer/blob/33d2c132595e7379119a0ff1d9f453aac70e4f76/Applications/SlicerApp/Testing/Python/RSNAVisTutorial.py#L341-L342>

---

<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:** [January 29, 2018, 8:16pm UTC](https://discourse.slicer.org/t/rotate-3d-image/1968/3 "2018-01-29T20:16:06Z")

</div>

See also how ScreenCapture module rotates a 3D view:

> <https://github.com/Slicer/Slicer/blob/5d294412119bf51f14c3a19ec98f055c1cbe30fe/Modules/Scripted/ScreenCapture/ScreenCapture.py#L943-L1002>

---

<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, 2:42am UTC](https://discourse.slicer.org/t/rotate-3d-image/1968/4 "2018-03-06T02:42:08Z")

</div>

Thanks a lot for giving me the code for controlling the rotation of the 3D image. I am using MAC operating software, and the 3D slicer version I am using is is 4.8.0. The code you gave me rotates the image really slowly, as I was trying to define like this threeDView = layoutManager.threeDWidget(10).threeDView(), in order to allow more rotation to occur when running this on python interactor. The problem is the moment I execute this on python interactor, in return slicer software force quits itself immediately. Whenever I try to put any number instead of 0 in threeDWidget(number), slicer software quits unexpectedly. I was trying to manipulate the rotation from different axes as this command only allows me to control the image from I to S axes, which means in one direction. I want to know how can I rotate in all directions for the 3D image via python interactor without quitting slicer application unexpectedly.

 ![27%20PM](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/1/9/1942176317d218c1be59b3e8bc8c3c2623f51347.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 6, 2018, 3:59am UTC](https://discourse.slicer.org/t/rotate-3d-image/1968/5 "2018-03-06T03:59:37Z")

</div>

> [@Ahmed\_Soufane](#):
>
> Thanks a lot for giving me the code for controlling the rotation of the 3D image.

The code does not rotate the 3D image. Do you need to rotate the volume? Then you can apply a transform to the volume and adjust that using sliders.

> [@Ahmed\_Soufane](#):
>
> I am using MAC operating software, and the 3D slicer version I am using is is 4.8.0.

Use 4.8.1. It contains many fixes and improvements.

> [@Ahmed\_Soufane](#):
>
> The code you gave me rotates the image really slowly, as I was trying to define like this threeDView = layoutManager.threeDWidget(10).threeDView()

As you have probably only one or two 3D viewers (you can get number of 3D widgets from layoutManager), calling `layoutManager.threeDWidget(10)` is expected to crash your application.

> [@Ahmed\_Soufane](#):
>
> Whenever I try to put any number instead of 0 in threeDWidget(number), slicer software quits unexpectedly.

This is the correct, expected behavior. See above.

> [@Ahmed\_Soufane](#):
>
> I was trying to manipulate the rotation from different axes as this command only allows me to control the image from I to S axes

What command?

> [@Ahmed\_Soufane](#):
>
> I want to know how can I rotate in all directions for the 3D image via python interactor without quitting slicer application unexpectedly.

If you simply want to rotate a 3D view (not the 3D image), then follow what is done in [Screen Capture module](https://github.com/Slicer/Slicer/blob/f4a792d66b910edc54060566573d13264f3951fc/Modules/Scripted/ScreenCapture/ScreenCapture.py#L968-L983).

---

<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:12pm UTC](https://discourse.slicer.org/t/rotate-3d-image/1968/6 "2018-03-06T23:12:25Z")

</div>

I tried the code “Screen Capture module”, but I got the following:

 ![04%20PM](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/7/d/7d8651606b5d9fc610fe4119e77df77a798aea23.png)  
There are lots of parameters that has been not defined, and I do not know where to get them from or define them as the given code fragment discusses several implementations.  
 ![23%20PM](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/2/8/28a01c1e2a66cdd493f49286ecf08e678968704e.png)

Would you please help me with that…  
Thanks

---

<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:19pm UTC](https://discourse.slicer.org/t/rotate-3d-image/1968/7 "2018-03-06T23:19:31Z")

</div>

Almost all the parameters in the given code are not defined, would you please guide me or tell me how to define them in order to rotate the 3D view.  
Thank you

---

<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 7, 2018, 12:22am UTC](https://discourse.slicer.org/t/rotate-3d-image/1968/8 "2018-03-07T00:22:02Z")

</div>

Hi Ahmed -

You can’t just cut and paste the module code into the python console. Instead you will need to read through it and set the needed variables and perhaps adapt the code a bit. It takes a little thought and experience with python, but it really won’t be too difficult if you practice.

Probably first you want to get comfortable with python as a language, perhaps by doing some online tutorials (like [CodeAcademy](https://www.codecademy.com/)) and look at [how python is used in Slicer](https://www.slicer.org/wiki/Documentation/Nightly/Developers/Python_scripting).

Best,  
Steve

---

<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 12, 2018, 11:51am UTC](https://discourse.slicer.org/t/rotate-3d-image/1968/9 "2018-03-12T11:51:13Z")

</div>

Dear Pieper,  
I looked up the links you provided and tried to define the required parameters many times, but I always get many errors as if I am defining the parameters in a wrong way. Would you please help me with this as I am running out of time for the project I am working on, I would really appreciate it if you can help me with this.

Thank you.

---

<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 12, 2018, 2:51pm UTC](https://discourse.slicer.org/t/rotate-3d-image/1968/10 "2018-03-12T14:51:47Z")

</div>

You can paste the following into the python console:

```auto
layoutManager = slicer.app.layoutManager()
threeDWidget = layoutManager.threeDWidget(0)
threeDView = threeDWidget.threeDView()
threeDView.yaw()

```

The last line does the actual rotation. To do more sophisticated animation you’ll need to refer to the other code Andras, Csaba and I pointed to.

I added this [to the script repository](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Rotate_the_3D_View) for future reference.

---

<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 12, 2018, 3:36pm UTC](https://discourse.slicer.org/t/rotate-3d-image/1968/11 "2018-03-12T15:36:57Z")

</div>

This way only rotates along one axes, can I use it to rotate along a different axes ?

---

<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 12, 2018, 3:51pm UTC](https://discourse.slicer.org/t/rotate-3d-image/1968/12 "2018-03-12T15:51:06Z")

</div>

You can either use the methods on the threeDView:

[http://www.commontk.org/docs/html/classctkVTKRenderView.html](http://www.commontk.org/docs/html/classctkVTKRenderView.html)

Or you can get the active camera from the vtkRenderer (which you can get from the threeDView):

[https://www.vtk.org/doc/nightly/html/classvtkRenderer.html](https://www.vtk.org/doc/nightly/html/classvtkRenderer.html)
