# Apply Transformation to a model

**URL:** <https://discourse.slicer.org/t/apply-transformation-to-a-model/5327>\
**Category:** Development\
**Tags:** transforms\
**Created:** [January 10, 2019, 4:10pm UTC](https://discourse.slicer.org/t/apply-transformation-to-a-model/5327 "2019-01-10T16:10:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![siaeleni](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@siaeleni](https://discourse.slicer.org/u/siaeleni)\
**Post date:** [January 10, 2019, 4:10pm UTC](https://discourse.slicer.org/t/apply-transformation-to-a-model/5327/1 "2019-01-10T16:10:16Z")

</div>

Hi,

I want to set and apply a transformation matrix using Python.  
Here is my code:

> ```
> transform = slicer.vtkMRMLTransformNode()
> transform.SetName('Transformation')
> slicer.mrmlScene.AddNode(transform)
> matrix = vtk.vtkMatrix4x4()
> matrix.SetElement(0, 0, 0.67)
> matrix.SetElement(0, 1, 0.45)
> matrix.SetElement(0, 2, 1)
> matrix.SetElement(0, 3, 23)
> 
> matrix.SetElement(1, 0, 0.34)
> matrix.SetElement(1, 1, 1)
> matrix.SetElement(1, 2, 0)
> matrix.SetElement(1, 3, -11)
> 
> matrix.SetElement(2, 0, 0.5)
> matrix.SetElement(2, 1, 0.1)
> matrix.SetElement(2, 2, 1)
> matrix.SetElement(2, 3, -33)
> 
> matrix.SetElement(3, 0, 0)
> matrix.SetElement(3, 1, 0)
> matrix.SetElement(3, 2, 0)
> matrix.SetElement(3, 3, 1)
> 
> transform.SetMatrixTransformFromParent(matrix)
> model = slicer.util.getNode('model')
> 
> model.SetAndObserveTransformNodeID(transform.GetName())
> model.SetAndObserveTransformNodeID(transform.GetID())
> 
> ```

Unfortunately, I get some strange results when I check the output at Transformation module, why that is happening and what am I doing wrong here?

Thanks

---

<div class="post-metadata">

**Author:** ![adamrankin](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/adamrankin/32/155_2.png) [@adamrankin](https://discourse.slicer.org/u/adamrankin)\
**Post date:** [January 10, 2019, 4:51pm UTC](https://discourse.slicer.org/t/apply-transformation-to-a-model/5327/2 "2019-01-10T16:51:43Z")

</div>

This line is not needed:

> model.SetAndObserveTransformNodeID(transform.GetName())

What is the strange results?

---

<div class="post-metadata">

**Author:** ![siaeleni](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@siaeleni](https://discourse.slicer.org/u/siaeleni)\
**Post date:** [January 10, 2019, 6:16pm UTC](https://discourse.slicer.org/t/apply-transformation-to-a-model/5327/3 "2019-01-10T18:16:18Z")

</div>

When I check the transformation matrix at **Transforms** module and pick the “Transformation”, the displayed matrix is not the same as I have set at the script, thus when I apply the transformation is not correct. Do you know why it is happening?

---

<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 10, 2019, 6:18pm UTC](https://discourse.slicer.org/t/apply-transformation-to-a-model/5327/4 "2019-01-10T18:18:14Z")

</div>

You would normally set transform **to** parent and that is displayed on the GUI. In your example above, you set transform **from** parent, so on the GUI you saw inverse of that matrix.

```auto
matrix = vtk.vtkMatrix4x4()
matrix.SetElement(...)
transformNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLTransformNode', 'Transformation')
transformNode.SetMatrixTransformToParent(matrix)

modelNode = slicer.util.getNode('model')
modelNode.SetAndObserveTransformNodeID(transformNode.GetID())

```
