# Vtk transform matrix as python list/tuple array

**URL:** https://discourse.slicer.org/t/vtk-transform-matrix-as-python-list-tuple-array/11797
**Category:** Development
**Tags:** vtk, transforms
**Created:** [May 30, 2020, 10:59pm UTC](https://discourse.slicer.org/t/vtk-transform-matrix-as-python-list-tuple-array/11797 "2020-05-30T22:59:22Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![talmazov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/talmazov/32/3966_2.png) [@talmazov](https://discourse.slicer.org/u/talmazov)
#### Post date: [May 30, 2020, 10:59pm UTC](https://discourse.slicer.org/t/vtk-transform-matrix-as-python-list-tuple-array/11797/1 "2020-05-30T22:59:22Z")

</div>

Hey,  
I have a model object i am exporting to PLY then trying to obtain its transform node as python list/tuple  
I am doing this in my script

> my\_matrix = transform.GetMatrixTransformFromParent()  
> matrix\_Array = my\_matrix.GetData()

where matrix\_Array should look like [[row1,…],[row2,…],[…]]

or so i thought that GetData would provide me with per  
[https://vtk.org/doc/nightly/html/classvtkMatrix4x4.html#a46a8fab5dec2c21f6379beb89730bd74](https://vtk.org/doc/nightly/html/classvtkMatrix4x4.html#a46a8fab5dec2c21f6379beb89730bd74)  
The document says it returns a raw double array, im not sure what “raw” would entail, I was hoping for it to be a native python array object but when I try to print the object it gives me a mem pointer / instance address… i’m a little confused what to do with it at this point.

I know I can try to iterate through the matrix, and I know it’s fixed 4x4, and manually create the array by pulling data via GetElement but is there a more elegant way of doing this that has already been implemented?

thanks!

---

<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: [May 30, 2020, 11:54pm UTC](https://discourse.slicer.org/t/vtk-transform-matrix-as-python-list-tuple-array/11797/2 "2020-05-30T23:54:23Z")

</div>

You can use this function to convert a raw pointer to a Python list:

> [@Centering on a segment from a script](https://discourse.slicer.org/t/centering-on-a-segment-from-a-script/6209/9):
>
> Using a custom extension is certainly a solution, but in the long term I would recommend developers to use Slicer core functions if available. Slicer-4.10 returning a swig pointer (such as \_000001bfd4897340\_p\_void) is a minor inconvenience. You can still access the values using the helper function below (in the nightly version the VTK hint is there so the position is directly returned as a Python tuple). def getListFromPointer(bufferPtr, scalarType=vtk.VTK\_DOUBLE, numberOfElements=3): """Con…

A bit more elegant, but probably not much faster or simpler method is to use DeepCopy method:

> <https://github.com/Slicer/Slicer/blob/e53e8af9c4a0b60adee28b5eca5fc1b5ff2da9ea/Base/Python/slicer/util.py#L1114-L1151>

---

<div class="post-metadata">

### Author: ![talmazov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/talmazov/32/3966_2.png) [@talmazov](https://discourse.slicer.org/u/talmazov)
#### Post date: [May 31, 2020, 8:45am UTC](https://discourse.slicer.org/t/vtk-transform-matrix-as-python-list-tuple-array/11797/3 "2020-05-31T08:45:01Z")

</div>

worked well, thanks.

---

<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: [May 31, 2020, 3:19pm UTC](https://discourse.slicer.org/t/vtk-transform-matrix-as-python-list-tuple-array/11797/4 "2020-05-31T15:19:16Z")

</div>

Note that in Slicer’s Python environment you can simply use [slicer.util.arrayFromTransformMatrix](https://slicer.readthedocs.io/en/latest/developer_guide/slicer.html#slicer.util.arrayFromTransformMatrix) to get a numpy array from a transform node (and there are similar convenience functions for getting/settings various other node contents as numpy array).
