2

I am trying to add a calculated scalar to an existing VTK file.

A simplified version of my code is the following

import vtk
import os
import numpy as np

reader = vtk.vtkDataSetReader()   
reader.SetFileName(vtk_file_name)
reader.ReadAllScalarsOn()
reader.Update()
data = reader.GetOutput() #This contains all data from the VTK
cell_data = data.GetCellData() #This contains just the cells data
scalar_data1 = cell_data.GetArray('scalar1')
scalar_data2 = cell_data.GetArray('scalar2')

scalar1 = np.array([scalar_data1.GetValue(i) for i in range(data.GetNumberOfCells())])
scalar2 = np.array([scalar_data2.GetValue(i) for i in range(data.GetNumberOfCells())])

scalar3 = scalar1 - scalar2

writer = vtk.vtkDataSetWriter()

At this point I assume that I need to add a vtkArray to data by using data.SetCell

The problem is that SetCell asks for a vtkCellArray and I have not managed yet to convert my array scalar3 to a vtkCellArray.

Is this the right approach? Any suggestion?

sawa
  • 165,429
  • 45
  • 277
  • 381
Rojj
  • 1,170
  • 1
  • 12
  • 32
  • Have a look at vtk.numpy_support or paraview dataset_adapter for converting back and forth from numpy.array and vtkarrays. Actually this simple example could have been done using the calculator or the python calculator (in paraview) or vtkArrayCalculator – lib Feb 19 '15 at 08:48

1 Answers1

2

You actually need to use cell_data.AddArray() to add your array. SetCell() would actually modify the topology of your data set.

Rojj is correct about using vtk.numpy_support to convert back and forth between vtkArrays and numpy arrays. You can use something like the following:

import vtk
from vtk.util import numpy_support
...
scalar3_array = numpy_support.numpy_to_vtk(scalar3)
scalar3_array.SetName('scalar3')
cell_data.AddArray(scalar3)
Cory Quammen
  • 1,243
  • 7
  • 9
  • Thanks Cory. it works. I assume you meant `scalar3_array.SetName('scalar3')` – Rojj Feb 20 '15 at 16:23
  • Just realized though that I still need another step. How do I add the cell data to `data` so that I can use this as an input for the `vtkDataSetWriter`? – Rojj Feb 20 '15 at 18:33
  • Oops... edited my mistake. The cell_data is already a member of `data`, so you shouldn't need to do anything before passing it to the `vtkDataSetWriter`. – Cory Quammen Feb 22 '15 at 13:53
  • 1
    One caveat - you will likely be safer creating a deep copy of the output from the reader with `data = reader.GetOutput().DeepCopy()` before you modify its cell data. That way, if the reader re-executes, it won't clobber your modifications. – Cory Quammen Feb 22 '15 at 14:08
  • `vtkDataSetWriter` requires a `vtkDataObject`. The step I was missing was 'data.UpdateData()' to update `data` with the `cell_data` new content and then I can pass the updated `data` to the writer. Thanks for your help! – Rojj Feb 27 '15 at 13:33
  • Do you want to update your answer so I can accept it? – Rojj Feb 27 '15 at 13:35
  • I'm confused. `reader.GetOutput()` returns a `vtkDataSet`, but `vtkDataSet` does not have an `UpdateData()` method. – Cory Quammen Feb 28 '15 at 14:32