# python numpy VTKArray conversion in programmable filter

**URL:** https://discourse.paraview.org/t/python-numpy-vtkarray-conversion-in-programmable-filter/16901
**Category:** ParaView Support
**Tags:** python, vtk
**Created:** [July 7, 2025, 11:45am UTC](https://discourse.paraview.org/t/python-numpy-vtkarray-conversion-in-programmable-filter/16901 "2025-07-07T11:45:11Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![skaal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/skaal/32/16671_2.png) [@skaal](https://discourse.paraview.org/u/skaal)
#### Post date: [July 7, 2025, 11:45am UTC](https://discourse.paraview.org/t/python-numpy-vtkarray-conversion-in-programmable-filter/16901/1 "2025-07-07T11:45:12Z")

</div>

Hi all,

I am trying to convert a rotations tensor into a quaternion using a programmable filter. However, the actual conversion function from VTK refuses to cooperate…  
The code:

```auto
from paraview.vtk.vtkCommonCore import vtkMath, vtkArray
from paraview.vtk.numpy_interface import dataset_adapter as dsa
import numpy as np
def make_quat(TENSOR):
    quatlist = [] 
    for t in TENSOR:
        quat = np.zeros(4)
        tens = np.asarray(t)
        #tens = np.asarray([[1,0,0],[0,1,0],[0,0,1]])
        print(type(tens), tens)
        vtkMath.Matrix3x3ToQuaternion(tens, quat)
        quatlist.append(quat) 
    o = np.asarray(quatlist).view(dsa.VTKArray)
    o.DataSet = TENSOR.DataSet
    o.Association = TENSOR.Association
    return o
tensor = inputs[0].PointData["TENSOR"]
output.PointData.append(make_quat(tensor), "test")

```

What I get is the following error message and output (yes, in that order):

```auto
Traceback (most recent call last):
File "<string>", line 22, in <module>
File "<string>", line 19, in RequestData
File "<string>", line 12, in make_quat
   TypeError: Matrix3x3ToQuaternion argument 1: expected a sequence of 3 values, got -1 values
<class 'numpy.ndarray'> [VTKArray([[0.70710678, -0.70710678, 0.], [0.70710678, 0.70710678, 0.], [0. , 0. , 1.]])]

```

Using the handcrafted tensor (commented), the whole filter works and give the expected results. Using that approach `tens` is also a “pure” `numpy.ndarray`, without nested `VTKArray`.

How do I correctly convert that `VTKArray` to be accepted by the conversion function?  
Is that the actual problem or am I severely misunderstanding something else?

---

<div class="post-metadata">

### Author: ![nicolas.vuaille](https://discourse.paraview.org/user_avatar/discourse.paraview.org/nicolas.vuaille/32/5873_2.png) [@nicolas.vuaille](https://discourse.paraview.org/u/nicolas.vuaille)
#### Post date: [July 8, 2025, 10:56am UTC](https://discourse.paraview.org/t/python-numpy-vtkarray-conversion-in-programmable-filter/16901/2 "2025-07-08T10:56:01Z")

</div>

Hi,

maybe you can try to copy `t` (or `tens`) inside a new numpy array?  
Actually `VTKArray` is a subclass of `numpy.ndarray` this error looks strange…

---

<div class="post-metadata">

### Author: ![Guillaume\_Gisbert](https://discourse.paraview.org/user_avatar/discourse.paraview.org/guillaume_gisbert/32/16300_2.png) [@Guillaume\_Gisbert](https://discourse.paraview.org/u/Guillaume_Gisbert)
#### Post date: [July 9, 2025, 7:27am UTC](https://discourse.paraview.org/t/python-numpy-vtkarray-conversion-in-programmable-filter/16901/3 "2025-07-09T07:27:57Z")

</div>

Hi,

As @nicolas.vuaille mentionned, `VTKArray` is a subclass of `numpy.ndarray`. Have you tried to use `Matrix3x3ToQuaternion` directly with `t`?

---

<div class="post-metadata">

### Author: ![skaal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/skaal/32/16671_2.png) [@skaal](https://discourse.paraview.org/u/skaal)
#### Post date: [July 9, 2025, 7:56am UTC](https://discourse.paraview.org/t/python-numpy-vtkarray-conversion-in-programmable-filter/16901/4 "2025-07-09T07:56:24Z")

</div>

Then, I get exactly the same error.

```auto
Traceback (most recent call last):
  File "<string>", line 22, in <module>
  File "<string>", line 19, in RequestData
  File "<string>", line 12, in make_quat
TypeError: Matrix3x3ToQuaternion argument 1: expected a sequence of 3 values, got -1 values

```

For completeness, an example datafile and the STL file for the glyph I am testing this with:

[data.vtp](https://discourse.paraview.org/uploads/short-url/vS0NIaZ4g69B6vfYM4kCLp63lXN.vtp) (4.6 KB)  
[sphere.stl](https://discourse.paraview.org/uploads/short-url/apzFwMMW5DPFYqEawMrTXEDPaVm.stl) (26.2 KB)

---

<div class="post-metadata">

### Author: ![whereismymoney](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/w/ed8c4c/32.png) [@whereismymoney](https://discourse.paraview.org/u/whereismymoney)
#### Post date: [July 10, 2025, 5:44am UTC](https://discourse.paraview.org/t/python-numpy-vtkarray-conversion-in-programmable-filter/16901/5 "2025-07-10T05:44:15Z")

</div>

Here is how I solved the problem.

```auto
from paraview.vtk.vtkCommonCore import vtkMath, vtkArray
import vtk
import numpy as np

reader = vtk.vtkXMLPolyDataReader()
reader.SetFileName("data.vtp")
reader.Update()
point_data = reader.GetOutput().GetPointData()
specific_tensor_array = point_data.GetArray("TENSOR")
print(specific_tensor_array.GetNumberOfComponents())
for i in range(specific_tensor_array.GetNumberOfTuples()):
    print(specific_tensor_array.GetTuple(i))
    item = np.asarray(specific_tensor_array.GetTuple(i))
    print(item)
    tens = np.asarray([item[0:3],item[3:6],item[6:9]])
    print(tens)
    quat = np.zeros(4)
    vtkMath.Matrix3x3ToQuaternion(tens, quat)
    print(quat)

```

---

<div class="post-metadata">

### Author: ![whereismymoney](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/w/ed8c4c/32.png) [@whereismymoney](https://discourse.paraview.org/u/whereismymoney)
#### Post date: [July 10, 2025, 5:45am UTC](https://discourse.paraview.org/t/python-numpy-vtkarray-conversion-in-programmable-filter/16901/6 "2025-07-10T05:45:33Z")

</div>

Here is the output:

```auto
python test.py  
9
(0.7071067811865475, 0.7071067811865476, 0.0, -0.7071067811865476, 0.7071067811865475, 0.0, 0.0, 0.0, 1.0)
[ 0.70710678 0.70710678 0. -0.70710678 0.70710678 0.
  0. 0. 1. ]
[[0.70710678 0.70710678 0.]
 [-0.70710678 0.70710678 0.]
 [0. 0. 1.]]
[0.92387953 0. 0. -0.38268343]
(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0)
[1. 0. 0. 0. 1. 0. 0. 0. 1.]
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
[1. 0. 0. 0.]
(0.7071067811865475, 0.7071067811865476, 0.0, -0.7071067811865476, 0.7071067811865475, 0.0, 0.0, 0.0, 1.0)
[ 0.70710678 0.70710678 0. -0.70710678 0.70710678 0.
  0. 0. 1. ]
[[0.70710678 0.70710678 0.]
 [-0.70710678 0.70710678 0.]
 [0. 0. 1.]]
[0.92387953 0. 0. -0.38268343]
(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0)
[1. 0. 0. 0. 1. 0. 0. 0. 1.]
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
[1. 0. 0. 0.]
(0.7071067811865475, 0.7071067811865476, 0.0, -0.7071067811865476, 0.7071067811865475, 0.0, 0.0, 0.0, 1.0)
[ 0.70710678 0.70710678 0. -0.70710678 0.70710678 0.
  0. 0. 1. ]
[[0.70710678 0.70710678 0.]
 [-0.70710678 0.70710678 0.]
 [0. 0. 1.]]
[0.92387953 0. 0. -0.38268343]
(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0)
[1. 0. 0. 0. 1. 0. 0. 0. 1.]
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
[1. 0. 0. 0.]
(0.7071067811865475, 0.7071067811865476, 0.0, -0.7071067811865476, 0.7071067811865475, 0.0, 0.0, 0.0, 1.0)
[ 0.70710678 0.70710678 0. -0.70710678 0.70710678 0.
  0. 0. 1. ]
[[0.70710678 0.70710678 0.]
 [-0.70710678 0.70710678 0.]
 [0. 0. 1.]]
[0.92387953 0. 0. -0.38268343]
(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0)
[1. 0. 0. 0. 1. 0. 0. 0. 1.]
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
[1. 0. 0. 0.]
(0.7071067811865475, 0.7071067811865476, 0.0, -0.7071067811865476, 0.7071067811865475, 0.0, 0.0, 0.0, 1.0)
[ 0.70710678 0.70710678 0. -0.70710678 0.70710678 0.
  0. 0. 1. ]
[[0.70710678 0.70710678 0.]
 [-0.70710678 0.70710678 0.]
 [0. 0. 1.]]
[0.92387953 0. 0. -0.38268343]
(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0)
[1. 0. 0. 0. 1. 0. 0. 0. 1.]
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
[1. 0. 0. 0.]

```

---

<div class="post-metadata">

### Author: ![skaal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/skaal/32/16671_2.png) [@skaal](https://discourse.paraview.org/u/skaal)
#### Post date: [July 10, 2025, 6:49am UTC](https://discourse.paraview.org/t/python-numpy-vtkarray-conversion-in-programmable-filter/16901/7 "2025-07-10T06:49:21Z")

</div>

Thank you!  
While this does not work in the PV programmable filter, it gave me the right ideas. This works:

```auto
from paraview.vtk.vtkCommonCore import vtkMath, vtkArray
from paraview.vtk.numpy_interface import dataset_adapter as dsa
import numpy as np
def make_quat(TENSOR):
    quatlist = []
    shape = np.shape(TENSOR)
    for i in range(shape[0]):
        quat = np.zeros(4)
        tens = TENSOR[i, :, :]
        vtkMath.Matrix3x3ToQuaternion(tens, quat)
        quatlist.append(quat) 
    o = np.asarray(quatlist).view(dsa.VTKArray)
    o.DataSet = TENSOR.DataSet
    o.Association = TENSOR.Association
    return o
tensor = inputs[0].PointData["TENSOR"].GetArrays()
output.PointData.append(make_quat(tensor[0]), "test")

```

What threw me off is that `inputs[0].PointData["TENSOR"]` returns a weird array shape with extra leading dimensions which I could not shave off. Adding a `GetArrays()` converts the leading dimension to a list (with only one element).  
At least for me, this behavior was absolutely not intuitive.
