# How to merge vtk files?

**URL:** https://discourse.vtk.org/t/how-to-merge-vtk-files/6678
**Category:** Support
**Created:** [September 23, 2021, 11:38am UTC](https://discourse.vtk.org/t/how-to-merge-vtk-files/6678 "2021-09-23T11:38:49Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![rasunag27](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rasunag27/32/3160_2.png) [@rasunag27](https://discourse.vtk.org/u/rasunag27)
#### Post date: [September 23, 2021, 11:38am UTC](https://discourse.vtk.org/t/how-to-merge-vtk-files/6678/1 "2021-09-23T11:38:49Z")

</div>

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.

```auto
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.

---

<div class="post-metadata">

### Author: ![cory.quammen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/cory.quammen/32/6751_2.png) [@cory.quammen](https://discourse.vtk.org/u/cory.quammen)
#### Post date: [September 23, 2021, 2:45pm UTC](https://discourse.vtk.org/t/how-to-merge-vtk-files/6678/2 "2021-09-23T14:45:55Z")

</div>

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

---

<div class="post-metadata">

### Author: ![rasunag27](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rasunag27/32/3160_2.png) [@rasunag27](https://discourse.vtk.org/u/rasunag27)
#### Post date: [September 24, 2021, 4:18am UTC](https://discourse.vtk.org/t/how-to-merge-vtk-files/6678/3 "2021-09-24T04:18:56Z")

</div>

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.

```auto
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:

```auto
# vtk DataFile Version 2.0
simulation
ASCII
DATASET UNSTRUCTURED_GRID
POINTS 283933 float

```

output vtk

```auto
# 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](https://drive.google.com/file/d/1AKI-3AusYgOUuNbC-xbuWr6Kmb-wmxGU/view?usp=sharing)

---

<div class="post-metadata">

### Author: ![cory.quammen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/cory.quammen/32/6751_2.png) [@cory.quammen](https://discourse.vtk.org/u/cory.quammen)
#### Post date: [September 24, 2021, 1:51pm UTC](https://discourse.vtk.org/t/how-to-merge-vtk-files/6678/4 "2021-09-24T13:51:31Z")

</div>

Replace `vtkAppendPolyData` with `vtkAppendFilter`. `vtkAppendPolyData` works only with `vtkPolyData` inputs, but you have `vtkUnstructuredGrid` datasets. Hence you need to use the more general `vtkAppendFilter`.

---

<div class="post-metadata">

### Author: ![rasunag27](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rasunag27/32/3160_2.png) [@rasunag27](https://discourse.vtk.org/u/rasunag27)
#### Post date: [September 25, 2021, 4:00am UTC](https://discourse.vtk.org/t/how-to-merge-vtk-files/6678/5 "2021-09-25T04:00:57Z")

</div>

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.

```auto
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.

---

<div class="post-metadata">

### Author: ![nicolas.vuaille](https://discourse.vtk.org/user_avatar/discourse.vtk.org/nicolas.vuaille/32/5363_2.png) [@nicolas.vuaille](https://discourse.vtk.org/u/nicolas.vuaille)
#### Post date: [September 27, 2021, 11:51am UTC](https://discourse.vtk.org/t/how-to-merge-vtk-files/6678/6 "2021-09-27T11:51:53Z")

</div>

Also discussed in [this cross post](https://stackoverflow.com/questions/69344430/merging-unstructured-vtk-file-is-not-showing-any-display-in-paraview)

---

<div class="post-metadata">

### Author: ![cory.quammen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/cory.quammen/32/6751_2.png) [@cory.quammen](https://discourse.vtk.org/u/cory.quammen)
#### Post date: [September 27, 2021, 10:27pm UTC](https://discourse.vtk.org/t/how-to-merge-vtk-files/6678/7 "2021-09-27T22:27:03Z")

</div>

> [@rasunag27](#):
>
> **But I am still unable to see visualization even after saving with Unstructured Grid**. Below is the latest code changes

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?

---

<div class="post-metadata">

### Author: ![rasunag27](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rasunag27/32/3160_2.png) [@rasunag27](https://discourse.vtk.org/u/rasunag27)
#### Post date: [September 28, 2021, 3:30am UTC](https://discourse.vtk.org/t/how-to-merge-vtk-files/6678/8 "2021-09-28T03:30:10Z")

</div>

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.

```auto
- polydata = vtkPolyData()
- polydata.ShallowCopy(reader.GetOutput())
+ unstructured = vtkUnstructuredGrid()
+ unstructured.ShallowCopy(reader.GetOutput())

```

Regards,  
Sunag R A.
