# Python example for GLTF export/import

**URL:** https://discourse.vtk.org/t/python-example-for-gltf-export-import/4596
**Category:** Support
**Created:** [November 6, 2020, 3:16pm UTC](https://discourse.vtk.org/t/python-example-for-gltf-export-import/4596 "2020-11-06T15:16:54Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![desheng](https://discourse.vtk.org/user_avatar/discourse.vtk.org/desheng/32/2764_2.png) [@desheng](https://discourse.vtk.org/u/desheng)
#### Post date: [November 6, 2020, 3:16pm UTC](https://discourse.vtk.org/t/python-example-for-gltf-export-import/4596/1 "2020-11-06T15:16:54Z")

</div>

Is it possible to provide an example in Python like in CXX

[https://lorensen.github.io/VTKExamples/site/Cxx/IO/GLTFExporter/](https://lorensen.github.io/VTKExamples/site/Cxx/IO/GLTFExporter/)

for GLTF export/import?  
Thanks!

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [November 6, 2020, 3:49pm UTC](https://discourse.vtk.org/t/python-example-for-gltf-export-import/4596/2 "2020-11-06T15:49:59Z")

</div>

If you don’t find a Python example for something then it would be great if you could convert it and contribute it to the example repository. Converting VTK C++ examples to Python is fairly straightforward (replace `->` and `::` by `.`, construct objects as `someObject = vtk.vtkSomeClass()`), but if anything is not clear then let us know.

You can also have a look at the syntax used in VTK glTF exporter in Python in 3D Slicer’s OpenAnatomy extension: [https://github.com/PerkLab/SlicerOpenAnatomy/blob/6998a875900f1be8e4a278b56f44582aadc7ce6e/OpenAnatomyExport/OpenAnatomyExport.py#L153-L276](https://github.com/PerkLab/SlicerOpenAnatomy/blob/6998a875900f1be8e4a278b56f44582aadc7ce6e/OpenAnatomyExport/OpenAnatomyExport.py#L153-L276)

---

<div class="post-metadata">

### Author: ![desheng](https://discourse.vtk.org/user_avatar/discourse.vtk.org/desheng/32/2764_2.png) [@desheng](https://discourse.vtk.org/u/desheng)
#### Post date: [November 6, 2020, 4:12pm UTC](https://discourse.vtk.org/t/python-example-for-gltf-export-import/4596/3 "2020-11-06T16:12:55Z")

</div>

Thanks. If I can successfully convert that CXX to python I will share that.  
Thanks for the example.

---

<div class="post-metadata">

### Author: ![fraseyboo](https://discourse.vtk.org/user_avatar/discourse.vtk.org/fraseyboo/32/2411_2.png) [@fraseyboo](https://discourse.vtk.org/u/fraseyboo)
#### Post date: [November 6, 2020, 9:02pm UTC](https://discourse.vtk.org/t/python-example-for-gltf-export-import/4596/4 "2020-11-06T21:02:49Z")

</div>

I took a chunk out of my existing code, you should just be able to pass the render window as a source. As GLTF exporting was only recently added in VTK 9.0 I also have a try/except block.

> ```
> try:
> exporter = vtk.vtkGLTFExporter()
> except AttributeError:
> print('Gltf exporting is not supported in your version of VTK, try updating')
> exporter.SetInput(source)
> exporter.InlineDataOn()
> exporter.SetFileName(savename)
> exporter.Update()
> exporter.Write()
> if verbose:
> print('file written')
> 
> ```

For importing a GLTF file i use the following code, this will return the GLTF or GLB as an actor but it’s easy enough to modify.

```
def load_gltf(filename,
                renderer=None,
                opacity=1.0,
                specular=0.1,
                ambient=0.0,
                scale=(1, 1, 1),
                translate=(0, 0, 0),
                rotate=(0, 0, 0),
                mesh_color="blue",
                use_wireframe=False,
                scale_then_translate=False):

    colors = vtk.vtkNamedColors()

    reader = vtk.vtkGLTFReader()
    reader.SetFileName(filename)

    reader.Update()

    polydata = vtk.vtkCompositeDataGeometryFilter()
    polydata.SetInputConnection(reader.GetOutputPort())

    transform = vtk.vtkTransform()
    transform.RotateX(rotate[0])
    transform.RotateY(rotate[1])
    transform.RotateZ(rotate[2])

    if scale_then_translate:
        transform.Scale(scale)
        transform.Translate(translate)
    else:
        transform.Translate(translate)
        transform.Scale(scale)

    transformFilter = vtk.vtkTransformPolyDataFilter()
    transformFilter.SetInputConnection(polydata.GetOutputPort())
    transformFilter.SetTransform(transform)
    transformFilter.Update()

    mapper = vtk.vtkPolyDataMapper()

    mapper.SetInputConnection(0, transformFilter.GetOutputPort(0))

    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
    if mesh_color is not None:
        actor.GetProperty().SetColor(colors.GetColor3d(mesh_color))
    actor.GetProperty().SetOpacity(opacity)

    actor.GetProperty().SetSpecular(specular)
    actor.GetProperty().SetSpecularPower(80.0)
    actor.GetProperty().SetAmbient(ambient)

    actor.GetProperty().SetInterpolationToGouraud()

    if use_wireframe:
        actor.GetProperty().SetRepresentationToWireframe()

    if renderer is None:
        return actor
    else:
        renderer.AddActor(actor)

```

---

<div class="post-metadata">

### Author: ![desheng](https://discourse.vtk.org/user_avatar/discourse.vtk.org/desheng/32/2764_2.png) [@desheng](https://discourse.vtk.org/u/desheng)
#### Post date: [December 21, 2020, 2:04pm UTC](https://discourse.vtk.org/t/python-example-for-gltf-export-import/4596/5 "2020-12-21T14:04:55Z")

</div>

Thanks for sharing!
