# vtkStringArray not being stored correctly in CellData for vtkPolyData object

**URL:** https://discourse.vtk.org/t/vtkstringarray-not-being-stored-correctly-in-celldata-for-vtkpolydata-object/10805
**Category:** Support
**Created:** [February 26, 2023, 12:41am UTC](https://discourse.vtk.org/t/vtkstringarray-not-being-stored-correctly-in-celldata-for-vtkpolydata-object/10805 "2023-02-26T00:41:57Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Robert\_Collar](https://discourse.vtk.org/user_avatar/discourse.vtk.org/robert_collar/32/6256_2.png) [@Robert\_Collar](https://discourse.vtk.org/u/Robert_Collar)
#### Post date: [February 26, 2023, 12:41am UTC](https://discourse.vtk.org/t/vtkstringarray-not-being-stored-correctly-in-celldata-for-vtkpolydata-object/10805/1 "2023-02-26T00:41:57Z")

</div>

Hi VTK community,

In the example below, a simple `vtkPolyData` object is constructed and two arrays — one numerical and another of strings — is passed to its cells. Upon subsequently retrieving the arrays, only the numerical one has any data in it (printing the string array results in `None`). Does anyone know why this is happening and how I might resolve the issue?

Best, Robert Collar

```python
import vtk
import vtk.util.numpy_support as vtknumpy

points = vtk.vtkPoints()
points.SetNumberOfPoints(4)
line = vtk.vtkLine()
lines = vtk.vtkCellArray()

# create first line segment
points.SetPoint(0, 0, 0, 0)
line.GetPointIds().SetId(0, 0)

points.SetPoint(1, 1, 1, 1)
line.GetPointIds().SetId(1, 1)

lines.InsertNextCell(line)

# create second line segment
points.SetPoint(2, 1, 1, 1)
line.GetPointIds().SetId(0, 2)

points.SetPoint(3, 2, 2, 2)
line.GetPointIds().SetId(1, 3)

lines.InsertNextCell(line)

linesPolyData = vtk.vtkPolyData()
linesPolyData.SetPoints(points)
linesPolyData.SetLines(lines)

num_ids = vtknumpy.numpy_to_vtk([0,1]) # TODO: change to be a function of number of lines
num_ids.SetName("num id")
num_ids.SetNumberOfComponents(1)
linesPolyData.GetCellData().AddArray(num_ids)

str_ids = vtk.vtkStringArray()
str_ids.SetNumberOfValues(2)
str_ids.InsertValue(0, "a")
str_ids.InsertValue(1, "b")
str_ids.SetName("str id")
str_ids.SetNumberOfComponents(1)

linesPolyData.GetCellData().AddArray(str_ids)
print(linesPolyData.GetCellData().GetArray("num id"))
print(linesPolyData.GetCellData().GetArray("str id"))

```

---

<div class="post-metadata">

### Author: ![toddy](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/t/edb3f5/32.png) [@toddy](https://discourse.vtk.org/u/toddy)
#### Post date: [February 26, 2023, 3:26am UTC](https://discourse.vtk.org/t/vtkstringarray-not-being-stored-correctly-in-celldata-for-vtkpolydata-object/10805/2 "2023-02-26T03:26:50Z")

</div>

Perhaps `str_ids.SetNumberOfComponents(1)`

---

<div class="post-metadata">

### Author: ![Robert\_Collar](https://discourse.vtk.org/user_avatar/discourse.vtk.org/robert_collar/32/6256_2.png) [@Robert\_Collar](https://discourse.vtk.org/u/Robert_Collar)
#### Post date: [February 26, 2023, 3:45am UTC](https://discourse.vtk.org/t/vtkstringarray-not-being-stored-correctly-in-celldata-for-vtkpolydata-object/10805/3 "2023-02-26T03:45:34Z")

</div>

Hi Todd, thanks for the suggestion, though adding that unfortunately doesn’t solve the problem. For future viewers, I’ve edited my initial post to include that line!

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [February 26, 2023, 4:40am UTC](https://discourse.vtk.org/t/vtkstringarray-not-being-stored-correctly-in-celldata-for-vtkpolydata-object/10805/4 "2023-02-26T04:40:08Z")

</div>

Use `GetAbstractArray()`.

The `GetArray()` is restricted to numerical data, it can only return subclasses of `vtkDataArray`. For everything else it returns NULL

So a `vtkStringArray` must be added with `AddArray()`, but retrieved with `GetAbstractArray()`.

---

<div class="post-metadata">

### Author: ![Robert\_Collar](https://discourse.vtk.org/user_avatar/discourse.vtk.org/robert_collar/32/6256_2.png) [@Robert\_Collar](https://discourse.vtk.org/u/Robert_Collar)
#### Post date: [February 26, 2023, 4:52am UTC](https://discourse.vtk.org/t/vtkstringarray-not-being-stored-correctly-in-celldata-for-vtkpolydata-object/10805/5 "2023-02-26T04:52:09Z")

</div>

This worked, thanks @dgobbi!

---

<div class="post-metadata">

### Author: ![toddy](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/t/edb3f5/32.png) [@toddy](https://discourse.vtk.org/u/toddy)
#### Post date: [February 27, 2023, 12:27am UTC](https://discourse.vtk.org/t/vtkstringarray-not-being-stored-correctly-in-celldata-for-vtkpolydata-object/10805/6 "2023-02-27T00:27:43Z")

</div>

That’s intuitive. 🤔
