Rotate 3D image

How to rotate the 3D image using phython interactor ?

This is one way

See also how ScreenCapture module rotates a 3D view:

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.

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.

Use 4.8.1. It contains many fixes and improvements.

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.

This is the correct, expected behavior. See above.

What command?

If you simply want to rotate a 3D view (not the 3D image), then follow what is done in Screen Capture module.

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


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.

Would you please help me with that…
Thanks

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

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) and look at how python is used in Slicer.

Best,
Steve

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.

You can paste the following into the python console:

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 for future reference.

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

You can either use the methods on the threeDView:

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