Python example for GLTF export/import

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

https://lorensen.github.io/VTKExamples/site/Cxx/IO/GLTFExporter/

for GLTF export/import?
Thanks!

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

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

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)

Thanks for sharing!