Export vtkDataSetMapper to glTF

Hello everyone! This is my first post here and I hope that I have picked the right category! I’m trying to export an UnstructuredGrid to a glTF file using vtkGLTFExporter and it only generates empty files. I doubted that the problem is with my input grid so I modified one of this sample files.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# by Panos Mavrogiorgos, email : pmav99 >a< gmail

import vtk
import pathlib


def get_program_parameters():
    import argparse
    description = 'Scalar bar widget.'
    epilogue = '''
    '''
    parser = argparse.ArgumentParser(description=description, epilog=epilogue,
                                     formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument('filename', help='uGridEx.vtk.')
    args = parser.parse_args()
    return args.filename


def main():
    colors = vtk.vtkNamedColors()

    colors.SetColor('bkg', [0.1, 0.2, 0.4, 1.0])

    # The source file
    file_name = get_program_parameters()

    # Create a custom lut. The lut is used both at the mapper and at the
    # scalar_bar
    lut = vtk.vtkLookupTable()
    lut.Build()

    # Read the source file.
    reader = vtk.vtkUnstructuredGridReader()
    reader.SetFileName(file_name)
    reader.Update()  # Needed because of GetScalarRange
    output = reader.GetOutput()
    scalar_range = output.GetScalarRange()

    mapper = vtk.vtkDataSetMapper()
    mapper.SetInputData(output)
    mapper.SetScalarRange(scalar_range)
    mapper.SetLookupTable(lut)

    actor = vtk.vtkActor()
    actor.SetMapper(mapper)

    renderer = vtk.vtkRenderer()
    renderer.AddActor(actor)
    renderer.SetBackground(colors.GetColor3d('bkg'))

    render_window = vtk.vtkRenderWindow()
    render_window.AddRenderer(renderer)
    render_window.SetSize(300, 300)

    interactor = vtk.vtkRenderWindowInteractor()
    interactor.SetRenderWindow(render_window)

    # create the scalar_bar
    scalar_bar = vtk.vtkScalarBarActor()
    scalar_bar.SetOrientationToHorizontal()
    scalar_bar.SetLookupTable(lut)

    # create the scalar_bar_widget
    scalar_bar_widget = vtk.vtkScalarBarWidget()
    scalar_bar_widget.SetInteractor(interactor)
    scalar_bar_widget.SetScalarBarActor(scalar_bar)
    scalar_bar_widget.On()

    render_window.Render()
    renderer.GetActiveCamera().SetPosition(-6.4, 10.3, 1.4)
    renderer.GetActiveCamera().SetFocalPoint(1.0, 0.5, 3.0)
    renderer.GetActiveCamera().SetViewUp(0.6, 0.4, -0.7)
    render_window.Render()

    # these lines are added by me to the sample file
    exporter = vtk.vtkGLTFExporter()
    exporter.InlineDataOn()
    exporter.SaveNormalOn()
    exporter.SetFileName('./test.gltf')
    exporter.SetRenderWindow(render_window)
    exporter.Modified()
    exporter.Update()
    exporter.Write()


if __name__ == '__main__':
    main()

And here is the input file:

# vtk DataFile Version 2.0
Unstructured Grid Example
ASCII
DATASET UNSTRUCTURED_GRID

POINTS 27 float
0 0 0  1 0 0  2 0 0  0 1 0  1 1 0  2 1 0
0 0 1  1 0 1  2 0 1  0 1 1  1 1 1  2 1 1
0 1 2  1 1 2  2 1 2  0 1 3  1 1 3  2 1 3
0 1 4  1 1 4  2 1 4  0 1 5  1 1 5  2 1 5
0 1 6  1 1 6  2 1 6

CELLS 11 60
8 0 1 4 3 6 7 10 9
8 1 2 4 5 7 8 10 11
4 6 10 9 12
4 11 14 10 13
6 15 16 17 14 13 12
6 18 15 19 16 20 17
4 22 23 20 19
3 21 22 18
3 22 19 18
2 26 25
1 24

CELL_TYPES 11
12
11
10
8
7
6
9
5
4
3
1

POINT_DATA 27
SCALARS scalars float 1
LOOKUP_TABLE default
0.0 1.0 2.0 3.0 4.0 5.0
6.0 7.0 8.0 9.0 10.0 11.0
12.0 13.0 14.0 15.0 16.0 17.0
18.0 19.0 20.0 21.0 22.0 23.0
24.0 25.0 26.0

VECTORS vectors float
1 0 0  1 1 0  0 2 0  1 0 0  1 1 0  0 2 0
1 0 0  1 1 0  0 2 0  1 0 0  1 1 0  0 2 0
0 0 1  0 0 1  0 0 1  0 0 1  0 0 1  0 0 1
0 0 1  0 0 1  0 0 1  0 0 1  0 0 1  0 0 1
0 0 1  0 0 1  0 0 1

CELL_DATA 11
SCALARS cell_info float 1
LOOKUP_TABLE CellColors
0.0 1.0 2.0 3.0 4.0 5.0
6.0 7.0 8.0 9.0 10.0

LOOKUP_TABLE CellColors 11
1 0 0 1
.4 1 .4 1
.4 1 1 1
1 .4 .4 1
1 .4 1 1
1 1 .4 1
1 1 1 1
1 .5 .5 1
.5 1 .5 1
.5 .5 .5 1
1 .5 .4 1

The generated glTF output looks like this:

{
   "accessors" : null,
   "asset" : 
   {
      "generator" : "VTK",
      "version" : "2.0"
   },
   "bufferViews" : null,
   "buffers" : null,
   "cameras" : null,
   "materials" : null,
   "meshes" : null,
   "nodes" : null,
   "scene" : 0,
   "scenes" : 
   [
      {
         "name" : "Layer 0",
         "nodes" : null
      }
   ]
}

If I try the same code with a PolyDataMapper example it exports as expected. I couldn’t figure out how to cast the vtkDataSetMapper to a PolyDataMapper or pass the vtkUnstructuredGrid to a PolyDataMapper.

Thanks in advance for your help!

You can use vtkDataSetSurfaceFilter or vtkGeometryFilter to extract the outer surface of your unstructured grid.
This surface is a polydata which you can send to a polydata mapper.

1 Like

You can “test” the vtkGLTFExporter by using ParaView and the ExportScene feature.

But you should not need a polydatamapper. Everything rendered should be exported to gltf.
Does this help?
https://kitware.github.io/vtk-examples/site/Cxx/IO/ImportToExport/

@danlipsa – For now I ended up re-writing the code to use vtkPolyData instead of vtkUnstructuredGrid and it exports the data as expected. Still have some issue with how the data are rendered but that’s a different topic. Thank for your help. :slight_smile: