I want to read VTK files to do some processing. Since I have to do this processing on both Linux and Windows, it is easier from me to do it with Python3. Therefore, both Linux and Windows have Python3 (3.6.0) and its module VTK (version 8.1.2).
I create the MWE to highlight the problem:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from vtk import *
import sys
import os
if __name__ == "__main__":
pathFile1 = os.getcwd()+'/Output_253.vtk'
print(pathFile1)
if os.path.exists(pathFile1):
# Creation of variables with the right type to read STRUCTURES_POINTS VTK files
readerVTK1 = vtk.vtkStructuredPointsReader()
# We put the content of our files in our variables
readerVTK1.SetFileName(pathFile1)
readerVTK1.Update()
# We read our variables datas, hence we have our VTK files datas in these variables
dataVTK1 = readerVTK1.GetOutput()
# We check if the dimensions are not zeros
if dataVTK1.GetDimensions()!=(0,0,0):
(dimX,dimY,dimZ) = dataVTK1.GetDimensions()
print((dimX,dimY,dimZ))
else :
print('dimensions are null... Problem !')
else:
print(' [WARN] ','the file you are looking for do not exist')
print(' pathFile1: ', pathFile1 )
The file Output_253.vtk in reference in the script can be download through the link:here
Then when I run this script on Linux I get '(1000,1,1)' which coincides with the file header and with the rest of my processing. While on Windows I get 'dimensions are null... Problem !'.
I tried to reinstall the VTK module on Windows, but I get the same problem.
Is that a bug ? Or is there any way to workaround ? Or Ideas ?
Take a look at the vtkStructuredPointsWriter class, in the doc it says:
Warning Binary files written on one system may not be readable on other systems.
That's probably the cause of your problem (edit your file in a text editor, it's binary):
https://vtk.org/doc/nightly/html/classvtkStructuredPointsWriter.html
So to fix this:
read the file in Linux (as it seems to work)
use a vtkStructuredPointsWriter to re-write a new version of the file but remember to set the writer to ASCII mode (by calling SetFileTypeToASCII())
For example, you can convert it to ASCII with this python script:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from vtk import *
import sys
import os
if __name__ == "__main__":
pathFile1 = os.getcwd()+'/Output_253.vtk'
print(pathFile1)
if os.path.exists(pathFile1):
# Creation of variables with the right type to read STRUCTURES_POINTS VTK files
readerVTK1 = vtk.vtkStructuredPointsReader()
# We put the content of our files in our variables
readerVTK1.SetFileName(pathFile1)
readerVTK1.Update()
# We read our variables datas, hence we have our VTK files datas in these variables
dataVTK1 = readerVTK1.GetOutput()
pathFile2 = os.getcwd()+'/Output_253_ASCII.vtk'
writer = vtk.vtkStructuredPointsWriter()
writer.SetFileName(pathFile2)
writer.SetFileTypeToASCII()
writer.SetInputData(dataVTK1)
writer.Write()
else:
print(' [WARN] ','the file you are looking for do not exist')
print(' pathFile1: ', pathFile1 )
You can check the Python and VTK versions you are using while running the your script with this code:
import platform
import vtk
print(platform.python_version())
print(platform.python_version_tuple())
print(vtk.vtkVersion.GetVTKSourceVersion())
As it works on my setup, I suggest you double check the path (put everything f.ex. in c:\temp and test if it works!).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With