# Appending data field to a vtk file (Python VTK)

**URL:** https://discourse.vtk.org/t/appending-data-field-to-a-vtk-file-python-vtk/3220
**Category:** Support
**Created:** [May 5, 2020, 7:33pm UTC](https://discourse.vtk.org/t/appending-data-field-to-a-vtk-file-python-vtk/3220 "2020-05-05T19:33:54Z")
**Posts on this page:** 1
**Showing post:** 4

<div class="post-metadata">

### Author: ![normanius](https://discourse.vtk.org/user_avatar/discourse.vtk.org/normanius/32/611_2.png) [@normanius](https://discourse.vtk.org/u/normanius)
#### Post date: [May 6, 2020, 12:06am UTC](https://discourse.vtk.org/t/appending-data-field-to-a-vtk-file-python-vtk/3220/4 "2020-05-06T00:06:13Z")

</div>

Thumbs up for the use of numpy containers. A pity that the vtk examples use that slow paradigm, so that the python folks are misled into writing unnecessarily slow code… I didn’t know that you can modify the returned numpy-array directly, that’s a nice!

Just want to point out that this question is about `vtkPointData` (or `vtkCellData`), as returned by `poly.GetPointData()` or `poly.GetCellData()`. Your pasted code acts on the points themselves, as returned by `poly.GetPoints()`. No big deal, just a couple of modifications required.

@Shahid My sample code simplifies to the following:

```python
    data = np.asarray(data)
    data = np.atleast_2d(data)
    nPoints = source.GetNumberOfPoints()
    assert(nPoints==len(data))
    from vtk.util.numpy_support import numpy_to_vtk
    # Setting deep=True is a bit safer, but you lose a bit of performance.
    array = numpy_to_vtk(data, deep=True)
    array.SetName("arrayName")
    dataset = source.GetPointData()
    dataset.AddArray(array)
    dataset.Modified() # As suggested by Andras

```

See [here](https://github.com/Kitware/VTK/blob/master/Wrapping/Python/vtkmodules/util/numpy_support.py) for how the utils are implemented.

---

_[View the full topic](https://discourse.vtk.org/t/appending-data-field-to-a-vtk-file-python-vtk/3220)._
