Rendering a gltf/glb file in jupyter notebook using vtk and k3d

Viewed 837

I have explored the available methods of how to render a gltf/glb file inline in a jupyter notebook to keep the viewer callback interactivity intact. I have eventually ended up using vtk and k3d to achieve this. The two hurdles I have are:

  1. How to use the vtkGLTFReader() to get vtkPolyData objects from the glb and render these in k3d? SOLUTION: See method posted in the comments.

  2. How to display colors/textures embedded in the gltf/glb to show them in k3d?

1 Answers

Here is the code to get vtkPolyData and pass it to k3d.

import k3d
import vtk
import ipywidgets as widgets

reader = vtk.vtkGLTFReader() 
reader.SetFileName('sample_glb/GroundVehicle.glb')
reader.Update() 

plot = k3d.plot()
mb = reader.GetOutput()

iterator = mb.NewIterator()

vtk_polyobjects = []
while not iterator.IsDoneWithTraversal():
    item = iterator.GetCurrentDataObject()
    vtk_polyobjects.append(item)
    iterator.GoToNextItem()

    
for obj in vtk_polyobjects:
    plot += k3d.vtk_poly_data(obj, color=0x222222)
plot.display()

debug_info = widgets.HTML()
Related