# Create a model/mesh in python from array of vertices and triangles?

**URL:** https://discourse.slicer.org/t/create-a-model-mesh-in-python-from-array-of-vertices-and-triangles/5541
**Category:** Support
**Tags:** python, 3d-model
**Created:** [January 28, 2019, 2:50pm UTC](https://discourse.slicer.org/t/create-a-model-mesh-in-python-from-array-of-vertices-and-triangles/5541 "2019-01-28T14:50:28Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![stephan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/stephan/32/2444_2.png) [@stephan](https://discourse.slicer.org/u/stephan)
#### Post date: [January 28, 2019, 2:50pm UTC](https://discourse.slicer.org/t/create-a-model-mesh-in-python-from-array-of-vertices-and-triangles/5541/1 "2019-01-28T14:50:28Z")

</div>

Hi,  
I am trying to create a model in python. The model data, i.e. the raw vertices, vertex normals and triangles, will come from reading a non-standard file format in this extension. (Background: The extension will eventually be able to read data exported from an electroanatomic mapping system such as [this one](http://www.bostonscientific.com/en-US/products/capital-equipment--mapping-and-navigation/rhythmia-mapping-system.html).)  
I’ve found some guidance [here](https://www.slicer.org/wiki/Documentation/Nightly/Developers/Python_scripting#Accessing_Model_data_as_numpy_array) and [here](https://vtk.org/doc/nightly/html/classvtkPolyData.html#a34a0f2c07e4464a32cfb30e946a78be2), but I am stuck.

I’ve tried:

```
cubeVerts = [[1.0, 1.0, 1.0],
             [1.0, -1.0, 1.0],
             [-1.0, -1.0, 1.0],
             [-1.0, 1.0, 1.0],
             [1.0, 1.0, -1.0],
             [1.0, -1.0, -1.0],
             [-1.0, -1.0, -1.0],
             [-1.0, 1.0, -1.0]]

cubeTris = [[0,1,2],
            [0,2,3],
            [0,1,5],
            [0,5,4],
            [0,4,7],
            [0,7,3],
            [3,2,6],
            [3,6,7],
            [1,5,7],
            [1,6,2],
            [4,5,6],
            [4,6,7]] 

modelNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLModelNode')
modelPolyData = modelNode.GetPolyData()
modelPolyData.SetVerts(cube)

```

But since the model created does not contain any points initially, `modelPolyData = modelNode.GetPolyData()` return nothing and `modelPolyData.SetVerts` fails with `AttributeError: 'NoneType' object has no attribute 'SetVerts'`.

I considered using the approach in [this example](https://www.slicer.org/wiki/Documentation/Nightly/Developers/Python_scripting#Accessing_Model_data_as_numpy_array) in the script repository and create a dummy sphere first which is then overwritten by the actual data, but here I ran into another problem: `vtkPolyData::SetVerts` needs a vtkCellArray and I could not find any member function of this type that simply takes an array of point coordinates together with another array of connectivity information.

I even thought about simply writing out a .vtk file line by line from the python script and re-load it into Slicer, but I am sure there is a more elegant solution…

Any help would be appreciated.

Thanks  
Stephan

---

<div class="post-metadata">

### Author: ![stephan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/stephan/32/2444_2.png) [@stephan](https://discourse.slicer.org/u/stephan)
#### Post date: [January 28, 2019, 7:19pm UTC](https://discourse.slicer.org/t/create-a-model-mesh-in-python-from-array-of-vertices-and-triangles/5541/2 "2019-01-28T19:19:24Z")

</div>

In the meantime I have found some very valuable additional resources [here](https://vtk.org/Wiki/VTK/Examples/Python/DataManipulation/Cube.py), [here](https://public.kitware.com/pipermail/vtkusers/2004-August/026366.html), and [here](http://vtk.1045678.n5.nabble.com/Set-vertex-normals-td5734525.html) and have been able to solve the issue.

If someone has the same question in the future: this was my solution (based on the references above).

```
def mkVtkIdList(it):
  vil = vtk.vtkIdList()
  for i in it:
    vil.InsertNextId(int(i))
  return vil

def CreateMesh(modeNode, arrayVertices, arrayVertexNormals, arrayTriangles, labelsScalars, arrayScalars):
  # modelNode : a vtkMRMLModelNode in the Slicer scene which will hold the mesh
  # arrayVertices : list of triples [[x1,y1,z2], [x2,y2,z2], ... ,[xn,yn,zn]] of vertex coordinates
  # arrayVertexNormals : list of triples [[nx1,ny1,nz2], [nx2,ny2,nz2], ... ] of vertex normals
  # arrayTriangles : list of triples of 0-based indices defining triangles
  # labelsScalars : list of strings such as ["bipolar", "unipolar"] to label the individual scalars data sets
  # arrayScalars : an array of n rows for n vertices and m colums for m inidividual scalar sets

  # create the building blocks of polydata including data attributes.
  mesh = vtk.vtkPolyData()
  points = vtk.vtkPoints()
  normals = vtk.vtkFloatArray()
  polys = vtk.vtkCellArray()
  
  # load the array data into the respective VTK data structures
  for i in range(len(arrayVertices)):
    points.InsertPoint(i, arrayVertices[i])
  
  for i in range(len(arrayTriangles)):
    polys.InsertNextCell(mkVtkIdList(arrayTriangles[i]))
  
  for i in range(len(arrayVertexNormals)):
    normals.InsertTuple3(i, arrayVertexNormals[i][0], arrayVertexNormals[i][1], arrayVertexNormals[i][2])
  
  # put together the mesh object
  mesh.SetPoints(points)
  mesh.SetPolys(polys)
  if(len(arrayVertexNormals) == len(arrayVertices)):
    mesh.GetPointData().SetNormals(normals)
  
  # Add scalars
  scalars = []
  for j in range(len(labelsScalars)):
    scalars.append(vtk.vtkFloatArray())
    
    for i in range(len(arrayVertices)):
      scalars[j].InsertTuple1(i,arrayScalars[i][j])
    
    scalars[j].SetName(labelsScalars[j])
    mesh.GetPointData().AddArray(scalars[j])
  
  modelNode.SetAndObservePolyData(mesh)
```

---

<div class="post-metadata">

### Author: ![hyncik](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/hyncik/32/67418_2.png) [@hyncik](https://discourse.slicer.org/u/hyncik)
#### Post date: [September 8, 2023, 3:43am UTC](https://discourse.slicer.org/t/create-a-model-mesh-in-python-from-array-of-vertices-and-triangles/5541/3 "2023-09-08T03:43:45Z")

</div>

Thank you for the solution, it saved me a lot of time. Ludek
