# Write mesh data (cell data) with python - it is not working?

**URL:** https://discourse.itk.org/t/write-mesh-data-cell-data-with-python-it-is-not-working/5682
**Category:** Algorithms
**Tags:** python
**Created:** [February 8, 2023, 8:42am UTC](https://discourse.itk.org/t/write-mesh-data-cell-data-with-python-it-is-not-working/5682 "2023-02-08T08:42:31Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![tutti](https://discourse.itk.org/letter_avatar_proxy/v4/letter/t/f4b2a3/32.png) [@tutti](https://discourse.itk.org/u/tutti)
#### Post date: [February 8, 2023, 8:42am UTC](https://discourse.itk.org/t/write-mesh-data-cell-data-with-python-it-is-not-working/5682/1 "2023-02-08T08:42:32Z")

</div>

Hi guys,  
I’m trying to write the mesh data with point and cell data using python (based on C++).  
This is my Python codes:

```auto
from itk import BinaryMask3DMeshSource

MeshType = itk.Mesh[itk.F, 3]
ImageType = itk.Image[itk.UC, 3]

MeshSourceType = itk.BinaryMask3DMeshSource[ImageType, MeshType]
meshSource = MeshSourceType.New()
meshSource.SetObjectValue(objectValue)
meshSource.SetInput(inputImage)

NodeNum = meshSource.GetNumberOfNodes()
CellNum = meshSource.GetNumberOfCells()
print("\tNodes =", NodeNum)
print("\tCells =", CellNum)

mesh = meshSource.GetOutput()

outputMeshFile = 'a.dat'
OutMesh = open(outputMeshFile, 'w')

points = mesh.GetPoints()
cells = mesh.GetCells()
numCells = mesh.GetNumberOfCells()

#it works for points
for i in range(points.Size()):
    p = points.ElementAt(i)
    OutMesh.write(str(p[0]) + " " + str(p[1]) + " " + str(p[2]) + "\n")
    points.SetElement(i, p)
    
    
for i in range(numCells):
    cell = cells.GetElement(i)
    numPts = cell.GetNumberOfPoints()
    point_ids = cell.GetPointIds()

    line = ""
    for j in range(numPts):
        line += str(point_ids.GetId(j) + 1) + " "
    OutMesh.write(line + "\n")

OutMesh.close()

```

I got the error like this:

```auto
---> 21 line += str(point_ids.GetId(j) + 1) + " "
     22 OutMesh.write(line + "\n")
     25 OutMesh.close()

AttributeError: 'SwigPyObject' object has no attribute 'GetId'

```

I follow **CellIterator** from this example[https://itk.org/Doxygen/html/Examples\_2DataRepresentation\_2Mesh\_2MeshCellsIteration\_8cxx-example.html](https://itk.org/Doxygen/html/Examples_2DataRepresentation_2Mesh_2MeshCellsIteration_8cxx-example.html).  
In C++, it is like this:

```auto
    while(PointIterator != points->End() )
    {
       PointType p=(PointIterator++).Value();
       OutMesh<<p[0]<<" "<<p[1]<<" "<<p[2]<<std::endl;
    }

    while(cellIterator != end)  
    {
       CellType *cell=cellIterator.Value();

       TriangleType *triangle= dynamic_cast<TriangleType *>(cell);
       TriangleType::PointIdIterator pit=triangle->PointIdsBegin();

       while(pit !=triangle->PointIdsEnd())
	  OutMesh<<(*pit++)+1<<" ";
	  OutMesh<<std::endl;
      ++cellIterator;
    }
    OutMesh.close();

```

Do you have any idea?

Thank you,

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [February 8, 2023, 2:08pm UTC](https://discourse.itk.org/t/write-mesh-data-cell-data-with-python-it-is-not-working/5682/2 "2023-02-08T14:08:38Z")

</div>

@Pranjal_Sahu is there some example or unit test in Python which does this, or something similar?

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [February 9, 2023, 2:07am UTC](https://discourse.itk.org/t/write-mesh-data-cell-data-with-python-it-is-not-working/5682/3 "2023-02-09T02:07:53Z")

</div>

Hi there,

All you need is:

```plaintext
output_mesh = itk.binary_mask3_d_mesh_source(input_image)
itk.meshwrite(output_mesh, args.output_mesh)

```

Here is a full example:

> <https://github.com/InsightSoftwareConsortium/ITKSphinxExamples/pull/416>

---

<div class="post-metadata">

### Author: ![tutti](https://discourse.itk.org/letter_avatar_proxy/v4/letter/t/f4b2a3/32.png) [@tutti](https://discourse.itk.org/u/tutti)
#### Post date: [February 9, 2023, 8:45am UTC](https://discourse.itk.org/t/write-mesh-data-cell-data-with-python-it-is-not-working/5682/4 "2023-02-09T08:45:19Z")

</div>

Thanks @matt.mccormick ,  
I did the same as you mentioned,

```auto
output_mesh = itk.binary_mask3_d_mesh_source(inputImage)
itk.meshwrite(output_mesh,'a.vtk') #cannot write .dat file

```

It wrote the “a.vtk” mesh file with the information:

```auto
# vtk DataFile Version 2.0
File written by itkPolyDataMeshIO
ASCII
DATASET POLYDATA

```

And it does not include any points and cells.  
Could you guide me more?

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [February 9, 2023, 3:01pm UTC](https://discourse.itk.org/t/write-mesh-data-cell-data-with-python-it-is-not-working/5682/5 "2023-02-09T15:01:39Z")

</div>

It may be helpful to verify that there is content in the mesh before writing, e.g.:

```auto
print(output_mesh.GetNumberOfCells())
print(output_mesh.GetNumberOfPoints())

```

---

<div class="post-metadata">

### Author: ![tutti](https://discourse.itk.org/letter_avatar_proxy/v4/letter/t/f4b2a3/32.png) [@tutti](https://discourse.itk.org/u/tutti)
#### Post date: [February 10, 2023, 12:28am UTC](https://discourse.itk.org/t/write-mesh-data-cell-data-with-python-it-is-not-working/5682/6 "2023-02-10T00:28:42Z")

</div>

hi @matt.mccormick ,  
As you can see from my first post:

```auto
MeshSourceType = itk.BinaryMask3DMeshSource[ImageType, MeshType]
meshSource = MeshSourceType.New()
meshSource.SetObjectValue(objectValue)
meshSource.SetInput(inputImage)

NodeNum = meshSource.GetNumberOfNodes()
CellNum = meshSource.GetNumberOfCells()
print("\tNodes =", NodeNum)
print("\tCells =", CellNum)

```

The output is:

```auto
	Nodes = 119740
	Cells = 239488

```

Thanks,

---

<div class="post-metadata">

### Author: ![tutti](https://discourse.itk.org/letter_avatar_proxy/v4/letter/t/f4b2a3/32.png) [@tutti](https://discourse.itk.org/u/tutti)
#### Post date: [February 10, 2023, 1:19am UTC](https://discourse.itk.org/t/write-mesh-data-cell-data-with-python-it-is-not-working/5682/7 "2023-02-10T01:19:45Z")

</div>

Hi,  
I fixed it by following this example:[https://github.com/InsightSoftwareConsortium/ITKSphinxExamples/blob/master/src/Core/Mesh/AddPointsAndEdges/Code.py](https://github.com/InsightSoftwareConsortium/ITKSphinxExamples/blob/master/src/Core/Mesh/AddPointsAndEdges/Code.py)

```auto
for i in range(numCells):
    cell = cells.GetElement(i)
    numPts = cell.GetNumberOfPoints()
    point_ids = cell.GetPointIdsContainer()
    for j in range(num_pts):
        line += str(point_ids[j] + 1) + " "
    OutMesh.write(line + "\n")

```

Thank you =))
