TypeError: Could not find a suitable VTK type for object in programmable filter

Dear all,

consider the following python script which I execute in the Programmable Filter:

from paraview import vtk, numpy_support
import numpy as np


input_array1 = inputs[0].PointData['displacement']
input_array2 = inputs[1].PointData['displacement']

# Convert input arrays to numpy arrays
array1_np = numpy_support.vtk_to_numpy(input_array1)
array2_np = numpy_support.vtk_to_numpy(input_array2)

# Calculate the difference using numpy
difference_np = array1_np - array2_np

# Create a new VTK array for the differences
difference_array = numpy_support.numpy_to_vtk(difference_np)
difference_array.SetNumberOfComponents(input_array1.GetNumberOfComponents())
difference_array.SetName('DifferenceDisplacement')

# Add the difference array to the output
output.PointData.append(difference_array, 'DifferenceDisplacement')

The purpose of this script is to compute the difference between the “displacement” point data set of the two input arrays. This displacement field has three components since it is a vector in 3d

I am getting the following error:


Traceback (most recent call last):
  File "<string>", line 24, in <module>
  File "<string>", line 2, in RequestData
  File "<string>", line 21, in <module>
  File "/opt/paraview-5.11.0/lib/python3.9/site-packages/vtkmodules/numpy_interface/dataset_adapter.py", line 748, in append
    arr = numpyTovtkDataArray(copy, name)
  File "/opt/paraview-5.11.0/lib/python3.9/site-packages/vtkmodules/numpy_interface/dataset_adapter.py", line 148, in numpyTovtkDataArray
    vtkarray = numpy_support.numpy_to_vtk(array, array_type=array_type)
  File "/opt/paraview-5.11.0/lib/python3.9/site-packages/vtkmodules/util/numpy_support.py", line 146, in numpy_to_vtk
    vtk_typecode = get_vtk_array_type(z.dtype)
  File "/opt/paraview-5.11.0/lib/python3.9/site-packages/vtkmodules/util/numpy_support.py", line 69, in get_vtk_array_type
    raise TypeError(
TypeError: Could not find a suitable VTK type for object
Traceback (most recent call last):
  File "<string>", line 24, in <module>
  File "<string>", line 2, in RequestData
  File "<string>", line 21, in <module>
  File "/opt/paraview-5.11.0/lib/python3.9/site-packages/vtkmodules/numpy_interface/dataset_adapter.py", line 748, in append
    arr = numpyTovtkDataArray(copy, name)
  File "/opt/paraview-5.11.0/lib/python3.9/site-packages/vtkmodules/numpy_interface/dataset_adapter.py", line 148, in numpyTovtkDataArray
    vtkarray = numpy_support.numpy_to_vtk(array, array_type=array_type)
  File "/opt/paraview-5.11.0/lib/python3.9/site-packages/vtkmodules/util/numpy_support.py", line 146, in numpy_to_vtk
    vtk_typecode = get_vtk_array_type(z.dtype)
  File "/opt/paraview-5.11.0/lib/python3.9/site-packages/vtkmodules/util/numpy_support.py", line 69, in get_vtk_array_type
    raise TypeError(
TypeError: Could not find a suitable VTK type for object

The error is associated with the last line of the script, but I am not able to figure out what is the actual problem.

I know that for the simple example above, I could simply do
output.PointData.append(input_array1 - input_array2, 'DifferenceDisplacement')
which works fine. But this is just an example and for my real code, I have to create a temporary vtk object.

Unfortunately, I can not upload a state file since I am a new user.
I hope the problem reveals by inspecting the above script!

Thank you for your help!

which line is causing the error ?

The last line, i.e.,

output.PointData.append(difference_array, 'DifferenceDisplacement')

Does that work then ?

output.PointData.append(difference_np, 'DifferenceDisplacement')

This works.

Can you please explain me why?

I never thought of passing the numpy array directly to the append() function.

is working directly with numpy object and expect numpy object. If you want to work with vtk object, you have to use the old API, output.VTKObject.GetPointData().Append().