# convert file into vtk and stl format

**URL:** https://discourse.vtk.org/t/convert-file-into-vtk-and-stl-format/10599
**Category:** Support
**Tags:** python, file-formats
**Created:** [February 2, 2023, 5:44pm UTC](https://discourse.vtk.org/t/convert-file-into-vtk-and-stl-format/10599 "2023-02-02T17:44:03Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Abdelkareem\_Elkhateb](https://discourse.vtk.org/user_avatar/discourse.vtk.org/abdelkareem_elkhateb/32/6483_2.png) [@Abdelkareem\_Elkhateb](https://discourse.vtk.org/u/Abdelkareem_Elkhateb)
#### Post date: [February 2, 2023, 5:44pm UTC](https://discourse.vtk.org/t/convert-file-into-vtk-and-stl-format/10599/1 "2023-02-02T17:44:03Z")

</div>

I am trying to save a cube into a vtk and stl files! this is the task my question is I read the docs and save it into .ply file this is the stl extension, so how can I save it as a vtk file with python ! i know the question is not clear but this is who I told can someone explain me anything ? I get confused,thanks in advance

---

<div class="post-metadata">

### Author: ![rexthor](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rexthor/32/6481_2.png) [@rexthor](https://discourse.vtk.org/u/rexthor)
#### Post date: [February 2, 2023, 6:20pm UTC](https://discourse.vtk.org/t/convert-file-into-vtk-and-stl-format/10599/2 "2023-02-02T18:20:21Z")

</div>

I’m assuming that you have point locations and then the point locations that make up each triangle. I am assuming you are using triangles. You might be able to do it sort of like this (in Python) - I’m not an expert, so here goes:

```python
import vtk

pts = vtk.vtkPoints()
pts.InsertNextPoint(0, 0, 0)
pts.InsertNextPoint(0, 0, 1)
pts.InsertNextPoint(0, 1, 0)
pts.InsertNextPoint(0, 1, 1)
pts.InsertNextPoint(1, 0, 0)
pts.InsertNextPoint(1, 0, 1)
pts.InsertNextPoint(1, 1, 0)
pts.InsertNextPoint(1, 1, 1)
u = vtk.vtkUnstructuredGrid()
u.SetPoints(pts)
u.InsertNextCell(vtk.VTK_HEXAHEDRON, 8, [0, 1, 2, 3, 4, 5, 6, 7])

w = vtk.vtkUnstructuredGridWriter()
w.SetFileName('cube.vtu')
w.SetInputData(u)
w.Write()

c = vtk.vtkDataSetSurfaceFilter()
c.SetInputData(u)
c.Update()

w = vtk.vtkSTLWriter()
w.SetFileName('cube.stl')
w.SetInputConnection(c.GetOutputPort())
w.Write()

# Or, just make a vtkPolyData instead of a vtkUnstructuredGrid.

```

---

<div class="post-metadata">

### Author: ![Abdelkareem\_Elkhateb](https://discourse.vtk.org/user_avatar/discourse.vtk.org/abdelkareem_elkhateb/32/6483_2.png) [@Abdelkareem\_Elkhateb](https://discourse.vtk.org/u/Abdelkareem_Elkhateb)
#### Post date: [February 4, 2023, 4:35am UTC](https://discourse.vtk.org/t/convert-file-into-vtk-and-stl-format/10599/4 "2023-02-04T04:35:45Z")

</div>

thanks, I understand the part about converting to stl but the vtk part didn’t get it yet! I just started learning vtk yesterday so a lot of things are still not clear! how I can now save the files to .vtu extensions? is vtu the vtk extensions they are the same?

```auto
#!~ How to write it into vtk then?

colors = vtkNamedColors()

# filename = get_program_parameters()

cube = vtk.vtkCubeSource()

cube.Update()

# Write the stl file to disk

stlWriter = vtkSTLWriter()

stlWriter.SetFileName('cube.stl')

stlWriter.SetInputConnection(cube.GetOutputPort())

stlWriter.Write()

```

---

<div class="post-metadata">

### Author: ![rexthor](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rexthor/32/6481_2.png) [@rexthor](https://discourse.vtk.org/u/rexthor)
#### Post date: [February 6, 2023, 1:10pm UTC](https://discourse.vtk.org/t/convert-file-into-vtk-and-stl-format/10599/5 "2023-02-06T13:10:20Z")

</div>

You are likely looking for [vtkXMLUnstructuredGridWriter](https://vtk.org/doc/nightly/html/classvtkXMLUnstructuredGridWriter.html), just change the writer object constructor and the filename.

The [newer format is the XML format](https://kitware.github.io/vtk-examples/site/VTKFileFormats/). The text format that I had in the script is called “legacy” format.
