Jul-27-2021, 01:02 PM
import vtk from vtk.util.numpy_support import vtk_to_numpy import numpy as np import scipy.io as sio files = 10 for index in range(files): file_in = f"RP_DATA_{index:04d}.vtp" # RP_DATA_0000.vtp, RP_DATA_0001.vtp, ..., RP_DATA_0009.vtp | END file_out = f"out_{index}.mat" # out_0.mat, out_1.mat, ..., out_9.mat | END points = vtk.vtkXMLPolyDataReader() points.SetFileName(file_in) points.Update() data = points.GetOutput() point_data = data.GetPointData() vel = vtk_to_numpy(point_data.GetAbstractArray('Velocity')) points = vtk_to_numpy(data.GetPoints().GetData()) sio.savemat(file_out, {'vel':vel, 'points':points})I can't test the code.
Use string formatting or f-strings to substitute text and not the old % formatting. It's still supported, but should not used.
If you want to convert all existing files of a directory:
import time from pathlib import Path def convert_vtk(source, target): """ your code here to: 1) Load the source Here is maybe a conversion of source required. Not all libraries can handle Path objects, so they must converted to a str() Example: points.SetFileName(str(source)) 2) Get Data out 3) Save data to target scipy.io.savemat should handle the Path object right so there is no conversion to a str required """ time.sleep(0.5) print(f"Converting {source.name} to {target.name}") # simulating the creation of target file target.touch() def convert_all(root): for source in Path(root).glob("*.vtk"): if not source.is_file(): # skipping it it's not a regular file e.g. a directory continue # create a Path instance with the same name, but different suffix target = source.with_suffix(".mat") # Condition to skip a source, if target already exists if target.exists(): continue convert_vtk(source, target) convert_all(Path.home() / "test_vtk")Docs of Pathlib: https://docs.python.org/3/library/pathlib.html
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!