Apply Transformation to a model

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

This line is not needed:

model.SetAndObserveTransformNodeID(transform.GetName())

What is the strange results?

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?

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.

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

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