'How can I get the faceIdlist from a vtkPolyhedron object in python?

I'm a learner of vtk, I want to use vtkpolyhedron object to describe an irregular polyhedron, then write it into .vtu file. It has an error when I write it into unstructured grid. I use a cube as example, here is my code

# create a cube polyhedron
polyhedron = vtkPolyhedron()
for i in range(8):
    polyhedron.GetPointIds().InsertNextId(i)
polyhedron.Initialize()
polyhedron.GetPoints().InsertNextPoint(0, 0, 0)
polyhedron.GetPoints().InsertNextPoint(1, 0, 0)
polyhedron.GetPoints().InsertNextPoint(1, 1, 0)
polyhedron.GetPoints().InsertNextPoint(0, 1, 0)
polyhedron.GetPoints().InsertNextPoint(0, 0, 1)
polyhedron.GetPoints().InsertNextPoint(1, 0, 1)
polyhedron.GetPoints().InsertNextPoint(1, 1, 1)
polyhedron.GetPoints().InsertNextPoint(0, 1, 1)
face_1 = [6,
          4, 0, 3, 2, 1,
          4, 0, 4, 7, 3,
          4, 4, 5, 6, 7,
          4, 5, 1, 2, 6,
          4, 0, 1, 5, 4,
          4, 2, 3, 7, 6]
polyhedron.SetFaces(face_1)

# write into unstructured grid
polyGrid = vtkUnstructuredGrid()
polyGrid.InsertNextCell(polyhedron.GetCellType(), polyhedron.GetFaces())

The error is

Traceback (most recent call last):
  File "D:/science/NMM/python-NMM/script/3D_script/vtk_study/011_convert_unstructured_grid_to_polydata.py", line 145, in <module>
    polyGrid.InsertNextCell(polyhedron.GetCellType(), polyhedron.GetFaces())
TypeError: InsertNextCell argument %Id: %V

Then I try to print the polyhedron.getfaces

print(polyhedron.GetFaces())
print(type(polyhedron.GetFaces()))

I get

_0000013b5aca1e00_p_void
<class 'str'>

The document of vtkpolyhedron show it should return a vtkIdList https://vtk.org/doc/nightly/html/classvtkPolyhedron.html#a070ddbec07089276d5f4286b975c830d

Is there any error in the use process? Thank you very much!



Solution 1:[1]

You should call Initialize after SetFaces (at least before further access to the cell)

See this similar example

In found from this doc

Edit

As GetFaces does not seem to work as expected in python I advise to create your data following the pattern of this other example:

  • create a vtkPoints, add it to the vtkUnstructuredGrid
  • create a vtkIdList to list the point ids for each face
  • Add the polyhedron cell to the grid: ugrid.InsertNextCell(VTK_POLYHEDRON, cellIdList)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1