# Convert vtkArray -\> vtkPoints?

**URL:** https://discourse.vtk.org/t/convert-vtkarray-vtkpoints/10609
**Category:** Support
**Tags:** python
**Created:** [February 3, 2023, 7:58pm UTC](https://discourse.vtk.org/t/convert-vtkarray-vtkpoints/10609 "2023-02-03T19:58:03Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![rexthor](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rexthor/32/6481_2.png) [@rexthor](https://discourse.vtk.org/u/rexthor)
#### Post date: [February 3, 2023, 7:58pm UTC](https://discourse.vtk.org/t/convert-vtkarray-vtkpoints/10609/1 "2023-02-03T19:58:03Z")

</div>

It seems (to me) that the data in vtkArray should be similar to vtkPoints. If I read in data, as a numpy array, and convert it to vtk format like:

```python
pd = vtk.vtkPolyData()
g = numpy_to_vtk(my_nice_array)
pd.SetPoints(g)

```

This doesn’t work. I can use something like:

```python
...
pts = vtk.vtkPoints()
for x, y, z in my_nice_array:
    pts.InsertNextPoint(x, y, z)

```

But I was wondering if I could somehow convert the output of `numpy_to_vtk` to `vtkPoints`?

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [February 5, 2023, 2:12pm UTC](https://discourse.vtk.org/t/convert-vtkarray-vtkpoints/10609/2 "2023-02-05T14:12:31Z")

</div>

Hello,

> [@rexthor](#):
>
> This doesn’t work.

Can you, please, be more specific? “Does’t work” can mean a lot of different unexpected results when it comes to computer graphics.

regards,

Paulo

---

<div class="post-metadata">

### Author: ![marcomusy](https://discourse.vtk.org/user_avatar/discourse.vtk.org/marcomusy/32/95_2.png) [@marcomusy](https://discourse.vtk.org/u/marcomusy)
#### Post date: [February 5, 2023, 3:28pm UTC](https://discourse.vtk.org/t/convert-vtkarray-vtkpoints/10609/3 "2023-02-05T15:28:42Z")

</div>

I guess you can do

```python
pd.GetPoints().SetData(numpy_to_vtk(my_nice_array))

```

---

<div class="post-metadata">

### Author: ![rexthor](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rexthor/32/6481_2.png) [@rexthor](https://discourse.vtk.org/u/rexthor)
#### Post date: [February 6, 2023, 1:21pm UTC](https://discourse.vtk.org/t/convert-vtkarray-vtkpoints/10609/4 "2023-02-06T13:21:29Z")

</div>

First, thanks for the replies.

@Paulo_Carvalho, here is the error that I got:

```auto
TypeError: SetPoints argument 1: method requires a vtkPoints, a vtkTypeFloat64Array was provided.

```

@marcomusy, this is the error that statement generates:

```auto
AttributeError: 'NoneType' object has no attribute 'SetData'

```

Here is the code I ran to get that last error:

```python
import vtk
import numpy as np
from vtk.util.numpy_support import numpy_to_vtk
pd = vtk.vtkPolyData()
g = numpy_to_vtk(np.zeros(5))
pd.GetPoints().SetData(g)

```

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [February 6, 2023, 2:31pm UTC](https://discourse.vtk.org/t/convert-vtkarray-vtkpoints/10609/5 "2023-02-06T14:31:26Z")

</div>

What do you get when you run

```python
pd = vtk.vtkPolyData()
g = numpy_to_vtk(my_nice_array)
pd.SetPoints(g)

```

?

---

<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 6, 2023, 2:45pm UTC](https://discourse.vtk.org/t/convert-vtkarray-vtkpoints/10609/6 "2023-02-06T14:45:03Z")

</div>

When you create a vtkPolyData, it is empty, so it has no points:

```python
>>> pd = vtk.vtkPolyData()
>>> pd.GetPoints()
None

```

And you can’t call `SetPoints()` with a vtkDataArray, since they’re different types. So what you have to do is create a vtkPoints object from the vtkDataArray, and then call `SetPoints()`.

```python
>>> pd = vtk.vtkPolyData()
>>> points = vtk.vtkPoints()
>>> points.SetData(numpy_to_vtk(my_nice_array))
>>> pd.SetPoints(points)

```

Also note that the vtkDataArray must have 3 components or else this will fail. So make sure the array has the correct shape.

---

<div class="post-metadata">

### Author: ![marcomusy](https://discourse.vtk.org/user_avatar/discourse.vtk.org/marcomusy/32/95_2.png) [@marcomusy](https://discourse.vtk.org/u/marcomusy)
#### Post date: [February 6, 2023, 2:48pm UTC](https://discourse.vtk.org/t/convert-vtkarray-vtkpoints/10609/7 "2023-02-06T14:48:48Z")

</div>

i think the problem is that your polydata is still empty, this works:

```python
import vedo
msh = vedo.Mesh("https://vedo.embl.es/examples/data/apple.ply")
pd = msh.polydata()
pts = msh.points()

import vtk
import numpy as np
from vtk.util.numpy_support import numpy_to_vtk
#pd = vtk.vtkPolyData()
g = numpy_to_vtk(np.zeros_like(pts)+pts*2) # whatever..
pd.GetPoints().SetData(g)

#vedo.show(pd)

```

you need to set it with a `vtkPoints` object.

---

<div class="post-metadata">

### Author: ![rexthor](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rexthor/32/6481_2.png) [@rexthor](https://discourse.vtk.org/u/rexthor)
#### Post date: [February 6, 2023, 3:01pm UTC](https://discourse.vtk.org/t/convert-vtkarray-vtkpoints/10609/8 "2023-02-06T15:01:44Z")

</div>

I agree with you guys, that the “points” was empty - it just seemed to me like the vtkPoints is conceptually just an array, I was wondering if there was some way to use the vtk.util function `numpy_to_vtk` to directly load points instead of having to iterate over the rows in the array to add the coordinates … but it looks like the only way to do it is to iterate over the rows.

---

<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 6, 2023, 3:05pm UTC](https://discourse.vtk.org/t/convert-vtkarray-vtkpoints/10609/9 "2023-02-06T15:05:11Z")

</div>

No, it’s not necessary to iterate over the rows. Both my answer and Marco’s answer demonstrate how to create vtkPoints from a numpy array. It just takes two steps.

---

<div class="post-metadata">

### Author: ![rexthor](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rexthor/32/6481_2.png) [@rexthor](https://discourse.vtk.org/u/rexthor)
#### Post date: [February 6, 2023, 3:06pm UTC](https://discourse.vtk.org/t/convert-vtkarray-vtkpoints/10609/10 "2023-02-06T15:06:39Z")

</div>

Ok - sorry, I see that now, thanks!

---

<div class="post-metadata">

### Author: ![michaelhogg](https://discourse.vtk.org/user_avatar/discourse.vtk.org/michaelhogg/32/4613_2.png) [@michaelhogg](https://discourse.vtk.org/u/michaelhogg)
#### Post date: [October 31, 2023, 4:46am UTC](https://discourse.vtk.org/t/convert-vtkarray-vtkpoints/10609/11 "2023-10-31T04:46:10Z")

</div>

I like to wrap the polydata object first

```auto
import numpy as np
from vtkmodules.numpy_interface import dataset_adapter as dsa

polydata = dsa.WrapDataObject(vtk.vtkPolyData())
polydata.Points = np.random.random(30).reshape(-1,3)

```
