# Rotate volume and get the Quaternion (or Euler)

**URL:** <https://discourse.slicer.org/t/rotate-volume-and-get-the-quaternion-or-euler/4186>\
**Category:** Development\
**Tags:** extensions-manager\
**Created:** [September 25, 2018, 2:12pm UTC](https://discourse.slicer.org/t/rotate-volume-and-get-the-quaternion-or-euler/4186 "2018-09-25T14:12:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Niels](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@Niels](https://discourse.slicer.org/u/Niels)\
**Post date:** [September 25, 2018, 2:12pm UTC](https://discourse.slicer.org/t/rotate-volume-and-get-the-quaternion-or-euler/4186/1 "2018-09-25T14:12:11Z")

</div>

Hello all,

In the transform hierarchy of my scene I added a LinearTransfromNode to rotate my volume to its correct pose. In my Python script I would like to get the value of the rotation in quaternion format. I need the value to save it to file and use it in another program.

I added the following code in python to get the matrix:

transformNodeID = inputVolume.GetTransformNodeID()  
if transformNodeID :  
transformNode = slicer.mrmlScene.GetNodeByID(transformNodeID )  
matrix = transformNode .GetMatrixTransformToParent()

Now I have the vtkMatrix4x4, but I can not find any feature to convert it to a rotation I can use. I prefer quaternions.

---

<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:** [September 25, 2018, 5:33pm UTC](https://discourse.slicer.org/t/rotate-volume-and-get-the-quaternion-or-euler/4186/2 "2018-09-25T17:33:37Z")

</div>

You can convert using [vtkQuaternion](https://www.vtk.org/doc/nightly/html/classvtkQuaternion.html).

---

<div class="post-metadata">

**Author:** ![Niels](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@Niels](https://discourse.slicer.org/u/Niels)\
**Post date:** [September 26, 2018, 9:39am UTC](https://discourse.slicer.org/t/rotate-volume-and-get-the-quaternion-or-euler/4186/3 "2018-09-26T09:39:34Z")

</div>

Thanks Andras, I will get the rotation part of the 4x4 and place them in a 3x3 and to see if I can get the quaternion from it.

---

<div class="post-metadata">

**Author:** ![Niels](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@Niels](https://discourse.slicer.org/u/Niels)\
**Post date:** [September 27, 2018, 10:08am UTC](https://discourse.slicer.org/t/rotate-volume-and-get-the-quaternion-or-euler/4186/4 "2018-09-27T10:08:14Z")

</div>

For those interested, this is what I now use to get the quaternion from the volume transformNode.

```
if inputVolume.GetTransformNodeID():
  transformNode = slicer.mrmlScene.GetNodeByID(inputVolume.GetTransformNodeID())      
  transfromMatrix = vtk.vtkMatrix4x4()
  transformNode.GetMatrixTransformToWorld(transfromMatrix)
  
  rotationMatrix = [[1,0,0],[0,1,0],[0,0,1]]
  for i in range(3):
    for j in range(3):
      rotationMatrix[i][j] = transfromMatrix.GetElement(i,j)
  
  rotationQuat = list(imageRotation)
  vtk.vtkMath.Matrix3x3ToQuaternion(rotationMatrix, rotationQuat);
  
  imageRotation = tuple( rotationQuat )
```
