rasunag27
(Sunag R A)
September 23, 2021, 11:38am
1
Dear all,
I have two vtk files obtained after the simulation that needs to be merged for visualization.
I saw a post related to this where vtkAppendPolyData() function is used. I tried in this way, but gave me an error.
import vtk
from vtk import vtkAppendPolyData, vtkPolyDataReader, vtkPolyDataWriter
a = vtkAppendPolyData()
a.AddInput('file1.vtk')
a.AddInput('file2.vtk')
AttributeError: ‘vtkmodules.vtkFiltersCore.vtkAppendPolyData’ object has no attribute 'AddInput’
What attributes do I need to give so as to merge the vtk files?
Regards,
Sunag R A.
cory.quammen
(Cory Quammen (Kitware))
September 23, 2021, 2:45pm
2
rasunag27
(Sunag R A)
September 24, 2021, 4:18am
3
Dear Cory,
Thank you very much for the info.
I managed to merged the vtk files. But I am unable to visualize the output vtk file although it contains all the data.
The code structure goes like this.
import vtk
from vtk import vtkAppendPolyData, vtkPolyDataReader, vtkPolyDataWriter, vtkPolyData, vtkUnstructuredGridReader
reader = vtkUnstructuredGridReader()#vtkPolyDataReader()
append = vtkAppendPolyData()
filenames = ['file1.vtk', 'file2.vtk']
for file in filenames:
reader.SetFileName(file)
reader.Update()
polydata = vtkPolyData()
polydata.ShallowCopy(reader.GetOutput())
append.AddInputData(polydata)
append.Update()
writer = vtkPolyDataWriter()
writer.SetFileName('output.vtk')
writer.SetInputData(append.GetOutput())
writer.Write()
I am unable to visualize the “output.vtk” file. I think below might be the possible issue?
The output.vtk created has a version of 5.1 while the file1 and file2 has a version of 2.0.
The file1 and file2 vtk has a name as “DATASET UNSTRUCTURED_GRID” while output.vtk has a name of “DATASET POLYDATA” in its file.
If so, how to make it work?
File1 vtk:
# vtk DataFile Version 2.0
simulation
ASCII
DATASET UNSTRUCTURED_GRID
POINTS 283933 float
output vtk
# vtk DataFile Version 5.1
vtk output
ASCII
DATASET POLYDATA
POINTS 284154 float
For reference, I have attached the link to File1 and File2 vtk files.
file1 and file2
cory.quammen
(Cory Quammen (Kitware))
September 24, 2021, 1:51pm
4
Replace vtkAppendPolyData
with vtkAppendFilter
. vtkAppendPolyData
works only with vtkPolyData
inputs, but you have vtkUnstructuredGrid
datasets. Hence you need to use the more general vtkAppendFilter
.
1 Like
rasunag27
(Sunag R A)
September 25, 2021, 4:00am
5
Dear Cory,
Thanks for the reply. I changed vtkAppendPolyData
with vtkAppendFilter
. But I am still unable to see visualization even after saving with Unstructured Grid . Below is the latest code changes.
from vtk import vtkAppendFilter, vtkUnstructuredGridWriter, vtkPolyData, vtkUnstructuredGridReader
reader = vtkUnstructuredGridReader()
append = vtkAppendFilter()
filenames = ['file1.vtk', 'file2.vtk']
for file in filenames:
reader.SetFileName(file)
reader.Update()
polydata = vtkPolyData()
polydata.ShallowCopy(reader.GetOutput())
append.AddInputData(polydata)
append.Update()
writer = vtkUnstructuredGridWriter()
#writer = vtkPolyDataWriter()
writer.SetFileName('output.vtk')
writer.SetInputData(append.GetOutput())
writer.Write()
Regards,
Sunag R A.
Also discussed in this cross post
1 Like
cory.quammen
(Cory Quammen (Kitware))
September 27, 2021, 10:27pm
7
Could you clarify what you mean and what you are expecting? Do you expect to be able to see a visualization after calling writer.Write()
? If so, you current code will not create a visualization.
Or are you trying to view output.vtk
in another program, and the output is empty?
rasunag27
(Sunag R A)
September 28, 2021, 3:30am
8
I was trying to view the output.vtk
in paraview. The output was not empty, but visually it was not rendering. There was a data loading problem. As suggested by Nicolas, I changed the data loading code and now its rendering well. Below are the changes.
- polydata = vtkPolyData()
- polydata.ShallowCopy(reader.GetOutput())
+ unstructured = vtkUnstructuredGrid()
+ unstructured.ShallowCopy(reader.GetOutput())
Regards,
Sunag R A.