Hello. I am currently trying to create a Rectilinear Grid vtr file using the Python vtk library and display it in Paraview. However, the Paraview fails to load the file and outputs the error as follows:
ERROR: In vtkXMLParser.cxx, line 375
vtkXMLDataParser (0x5595720a54b0): Error parsing XML in stream at line 20, column 0, byte index 1379: junk after document element
ERROR: In vtkXMLReader.cxx, line 520
vtkXMLRectilinearGridReader (0x559573606d40): Error parsing input file. ReadXMLInformation aborting.
The Python script to generate the vtr file is as follows:
import vtk
x = vtk.vtkDoubleArray()
y = vtk.vtkDoubleArray()
z = vtk.vtkDoubleArray()
n = 100
for i in range(n+1):
x.InsertNextValue(i)
y.InsertNextValue(i)
z.InsertNextValue(i)
grid = vtk.vtkRectilinearGrid()
grid.SetDimensions(n+1,n+1,n+1)
grid.SetXCoordinates(x)
grid.SetYCoordinates(y)
grid.SetZCoordinates(z)
s = vtk.vtkDoubleArray()
s.SetName("random")
s.SetNumberOfComponents(1)
rng = np.random.default_rng()
v = vtk.vtkDoubleArray()
v.SetName("vector")
v.SetNumberOfComponents(3)
for r in rng.random(n**3):
s.InsertNextValue(r)
for r1, r2, r3 in zip(rng.random(n**3), rng.random(n**3), rng.random(n**3)):
v.InsertNextTuple3(r1,r2,r3)
grid.GetCellData().SetScalars(s)
grid.GetCellData().SetVectors(v)
writer = vtk.vtkXMLRectilinearGridWriter()
writer.SetInputData(grid)
writer.SetFileName("./data/random.vtr")
writer.Write()
The vtr file generated by this Python script cannot be read by Paraview, but can be read by Python vtk.vtkXMLRectilinearGridReader
or ‘Visualize your data with Kitware Glance’. However, when I try to read in C++ as shown below, I get the same error as with Paraview.
#include <vtkSmartPointer.h>
#include <vtkXMLRectilinearGridReader.h>
int main(int argc, char const *argv[]) {
vtkSmartPointer<vtkXMLRectilinearGridReader> reader =
vtkSmartPointer<vtkXMLRectilinearGridReader>::New();
reader->SetFileName("../data/random.vtr");
reader->Update();
return 0;
}
2024-02-28 15:40:06.326 ( 0.031s) [ 44A62B00] vtkXMLParser.cxx:375 ERR| vtkXMLDataParser (0xaa5350): Error parsing XML in stream at line 20, column 0, byte index 1379: junk after document element
2024-02-28 15:40:06.327 ( 0.031s) [ 44A62B00] vtkXMLReader.cxx:521 ERR| vtkXMLRectilinearGridReader (0xa9e5c0): Error parsing input file. ReadXMLInformation aborting.
2024-02-28 15:40:06.327 ( 0.031s) [ 44A62B00] vtkExecutive.cxx:740 ERR| vtkCompositeDataPipeline (0xa70200): Algorithm vtkXMLRectilinearGridReader (0xa9e5c0) returned failure for request: vtkInformation (0xaa3080)
Debug: Off
Modified Time: 153
Reference Count: 1
Registered Events: (none)
Request: REQUEST_INFORMATION
ALGORITHM_AFTER_FORWARD: 1
FORWARD_DIRECTION: 0
I assume that the reason it cannot be loaded in Paraview is because it uses the C++ VTK ToolKit, but why does it behave this way? Also, how can I load a vtr file generated by Python into Paraview?