[vtkusers] Add scalars to vtk file (vtk 3.0 legacy) in Python

Ruggiero Guida ruggiero.guida at gmail.com
Tue Feb 17 01:25:26 EST 2015


Hi

Sorry for the crossposting. I have posted this message also on
http://vtk.1045678.n5.nabble.com/ but it says that it has been rejected.



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? Thanks a lot!!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20150217/4326146e/attachment-0001.html>


More information about the vtkusers mailing list