How to merge vtk files?

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.

Please see https://kitware.github.io/vtk-examples/site/Cxx/Filtering/CombinePolyData/ for an example of how to do this.

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?

  1. The output.vtk created has a version of 5.1 while the file1 and file2 has a version of 2.0.
  2. 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

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

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

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?

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.