[solved by VTK] How to read connectivity, offsets, types of a VTU file?

Given a VTU file, how to read the connectivity, offsets and types under the section?

I’ve already get the reader based on the VTU file, but what’s next? Any suggestions may save my life…

reader = XMLUnstructuredGridReader(registrationName=‘MainFile’, FileName=[inputVTU])

BTW, another option I am trying is to directly use VTK’s python API, but it would be better to stick on Paraview. I don’t want to introduce another python package if unnecessary…


update: I found a way to read the VTU using VTK python module directly after experimenting 2 hours. Feel free to grab it to save your life. Also, if you have a solution by pvpython, welcome to share:

import vtk                                                                                                      
from vtk.util.numpy_support import vtk_to_numpy

def generateEdgesGLTF(inputVtu, outputGltf):                                     
    reader = vtk.vtkXMLUnstructuredGridReader()                                  
    reader.SetFileName(inputVtu)                                                 
    reader.Update()                                                                                                                                                            
    mesh = reader.GetOutput()                                                                                                     
    cells = mesh.GetCells()                                                      
    nCells = cells.GetNumberOfCells()                                                                                                       
    cellConns = vtk_to_numpy(cells.GetConnectivityArray())                       
    cellOffsets = vtk_to_numpy(cells.GetOffsetsArray())                          
    cellTypes = vtk_to_numpy(mesh.GetCellTypesArray())                           
    pointCoords = vtk_to_numpy(mesh.GetPoints().GetData())

pvpython is for controling paraview, not for reading VTK datasets directly.

Hi Mathieu, thanks for this clarifying. Things are clear to me now. :wink: