# How to convert Gmsh into Explicit Structured (or VTI) with VTK?

**URL:** https://discourse.vtk.org/t/how-to-convert-gmsh-into-explicit-structured-or-vti-with-vtk/9992
**Category:** Support
**Tags:** python, file-formats
**Created:** [November 28, 2022, 7:45pm UTC](https://discourse.vtk.org/t/how-to-convert-gmsh-into-explicit-structured-or-vti-with-vtk/9992 "2022-11-28T19:45:00Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![edgar](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/e/b487fb/32.png) [@edgar](https://discourse.vtk.org/u/edgar)
#### Post date: [November 28, 2022, 7:45pm UTC](https://discourse.vtk.org/t/how-to-convert-gmsh-into-explicit-structured-or-vti-with-vtk/9992/1 "2022-11-28T19:45:00Z")

</div>

Hello,

I am trying to convert a Gmsh (unstructured) mesh into a structured grid (VTI).

Below is a part of a question that I [asked on the PyVista slack forum](https://pyvista.slack.com/archives/CEZKR0Y94/p1669633787465889) (before I was granted access to VTK’s discourse forum). The code is based on [https://kitware.github.io/vtk-examples/site/Python/ExplicitStructuredGrid/LoadESGrid/](https://kitware.github.io/vtk-examples/site/Python/ExplicitStructuredGrid/LoadESGrid/), [https://kitware.github.io/vtk-examples/site/Python/ExplicitStructuredGrid/CreateESGrid/](https://kitware.github.io/vtk-examples/site/Python/ExplicitStructuredGrid/CreateESGrid/) and [https://kitware.github.io/vtk-examples/site/Python/IO/ReadUnstructuredGrid/](https://kitware.github.io/vtk-examples/site/Python/IO/ReadUnstructuredGrid/). I could not upload the [`.msh` file](https://anonbin.io/?9ff0979ea2460724#7sDpsb2VrUgUt529WZjSvprJGMwzjM5Q2ma1CURXS41j)

```python
import meshio
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkFiltersCore import vtkUnstructuredGridToExplicitStructuredGrid
from vtkmodules.vtkIOXML import vtkXMLUnstructuredGridReader
from vtkmodules.vtkInteractionStyle import vtkInteractorStyleRubberBandPick
from vtkmodules.vtkRenderingCore import (
    vtkActor,
    vtkDataSetMapper,
    vtkRenderWindow,
    vtkRenderWindowInteractor,
    vtkRenderer
)
# exported with Gmsh as VTK, then:
meshio.read("mat_gmsh_simple_mesh.vtk").write("mat_gmsh_simple_mesh.vtu")
reader = vtkXMLUnstructuredGridReader()
reader.SetFileName("mat_gmsh_simple_mesh.vtu")
reader.Update()
#
converter = vtkUnstructuredGridToExplicitStructuredGrid()
# converter.GlobalWarningDisplayOff() # hide VTK errors
converter.GlobalWarningDisplayOn()
converter.SetInputConnection(reader.GetOutputPort())
converter.SetInputArrayToProcess(0, 0, 0, 1, 'BLOCK_I')
converter.SetInputArrayToProcess(1, 0, 0, 1, 'BLOCK_J')
converter.SetInputArrayToProcess(2, 0, 0, 1, 'BLOCK_K')
converter.Update()
#
grid = converter.GetOutput()
grid.ComputeFacesConnectivityFlagsArray()

```

The error message is

```nohighlight
( 6.617s) [57842B80]vtkUnstructuredGridToEx:81 ERR| vtkUnstructuredGridToExplicitStructuredGrid (0x561812d759a0): An ijk array has not be set using SetInputArrayToProcess, aborting.
( 6.617s) [57842B80] vtkExecutive.cxx:752 ERR| vtkCompositeDataPipeline (0x5618130e26e0): Algorithm vtkUnstructuredGridToExplicitStructuredGrid(0x561812d759a0) returned failure for request: vtkInformation (0x5618130ec1c0)
  Debug: Off
  Modified Time: 556
  Reference Count: 1
  Registered Events: (none)
  Request: REQUEST_DATA
  FROM_OUTPUT_PORT: 0
  ALGORITHM_AFTER_FORWARD: 1
  FORWARD_DIRECTION: 0

Process Python segmentation fault (core dumped)

```

Running software:

```nohighlight
VTK 9.1.0, Python 3.10.8, Meshio 5.3.4, Arch Linux

```

Your help and time are much appreciated. Thanks!

---

<div class="post-metadata">

### Author: ![edgar](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/e/b487fb/32.png) [@edgar](https://discourse.vtk.org/u/edgar)
#### Post date: [November 29, 2022, 4:02pm UTC](https://discourse.vtk.org/t/how-to-convert-gmsh-into-explicit-structured-or-vti-with-vtk/9992/2 "2022-11-29T16:02:04Z")

</div>

I got a solution with PyVista. Thanks!

For those who are interested, [this](https://stackoverflow.com/questions/61711074/voxelize-vtu-file-in-python) may also help, but may involve converting the cell data into point data first.
