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
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?
You can use this function to convert a raw pointer to a Python list:
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):
A bit more elegant, but probably not much faster or simpler method is to use DeepCopy method:
This file has been truncated.
"""Return vtkMatrix4x4 or vtkMatrix3x3 elements as numpy array.
The returned array is just a copy and so any modification in the array will not affect the input matrix.
To set VTK matrix from a numpy array, use :py:meth:`vtkMatrixFromArray` or
from vtk import vtkMatrix4x4
from vtk import vtkMatrix3x3
import numpy as np
if isinstance(vmatrix, vtkMatrix4x4):
matrixSize = 4
elif isinstance(vmatrix, vtkMatrix3x3):
matrixSize = 3
raise RuntimeError("Input must be vtk.vtkMatrix3x3 or vtk.vtkMatrix4x4")
narray = np.eye(matrixSize)
Note that in Slicer’s Python environment you can simply use
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).