Exporting a vtk file to vtkjs

I’m able to load a .vtk file using the Python vtk library and view it on a little window (i.e., I can confirm the import works correctly), but how exactly may I export it to a .vtkjs file? Couldn’t find much in the docs.

In ParaView I can do this by simply opening the file and then going to FileExport.vtkjs extension.

Here’s the file: pattern.vtk (437.9 KB)

import vtk

# Create a reader for your input file
reader = vtk.vtkStructuredGridReader()
reader.SetFileName("pattern.vtk")
reader.Update()

# Create a mapper
mapper = vtk.vtkDataSetMapper()
mapper.SetInputConnection(reader.GetOutputPort())

# Create an actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)

# Create a rendering window and renderer
renderer = vtk.vtkRenderer()
renderer.SetBackground(1, 1, 1) # Set background color to white
renderWindow = vtk.vtkRenderWindow()
renderWindow.SetSize(500, 500)  # Set window size
renderWindow.AddRenderer(renderer)

# Assign actor to the renderer
renderer.AddActor(actor)

# Reset camera and zoom in a little
renderer.ResetCamera()
renderer.GetActiveCamera().Zoom(1.5)

# Create a renderwindowinteractor
interactor = vtk.vtkRenderWindowInteractor()
interactor.SetRenderWindow(renderWindow)

# Enable user interface interactor
interactor.Initialize()
renderWindow.Render()
interactor.Start()

(Happy to use PyVista in case that makes things easier)

Thanks a lot!

Are you sure you want a .vtkjs? Looking at your data, it seems to be a surface mesh so extracting the surface to generate a polydata and saving it as a vtp would allow vtk.js to read it back and render it.

The .vtkjs format is capturing the full scene, not just the data.

Otherwise, ParaView is using the vtkJSONSceneExporter under the cover to generate that vtkjs export.

1 Like

Exporting to a VTP works great, but for some reason it seems to create a small artifact when running on a headless server:

from vtk import (vtkAppendFilter, vtkDataSetSurfaceFilter,
                 vtkStructuredGridReader, vtkXMLPolyDataWriter)

# Read VTK
reader = vtkStructuredGridReader()
reader.SetFileName('pattern.vtk') # replace with your filename
reader.Update()

# Convert structured grid to unstructured grid
append_filter = vtkAppendFilter()
append_filter.AddInputData(reader.GetOutput())
append_filter.Update()

# Extract surface
surface_filter = vtkDataSetSurfaceFilter()
surface_filter.SetInputConnection(append_filter.GetOutputPort())
surface_filter.Update()

# Save output to a VTP file
writer = vtkXMLPolyDataWriter()
writer.SetFileName("pattern.vtp")
writer.SetInputData(surface_filter.GetOutput())
writer.Write()

On my laptop, it works perfectly fine, but for some reason when the code runs on a headless server it creates this odd line, seemingly around 360 deg (this is a spherical plot):

Not sure why, but you should not need the vtkAppendFilter.
Actually the issue might come from duplicate points. Try adding vtkCleanPolyData

# Read VTK
reader = vtkStructuredGridReader()
reader.SetFileName('pattern.vtk') # replace with your filename

# Extract surface
surface_filter = vtkDataSetSurfaceFilter()
surface_filter.SetInputConnection(reader.GetOutputPort())

clean_filter = vtkCleanPolyData()
clean_filter.SetInputConnection(surface_filter.GetOutputPort())

# Save output to a VTP file
writer = vtkXMLPolyDataWriter()
writer.SetFileName("pattern.vtp")
writer.SetInputConnection(clean_filter.GetOutputPort())
writer.Write()

Looks like the artifact remains. :confused:

In case it helps debug the problem: bad_pattern.vtp (373.4 KB)

Are you sure about your data to actually match at the seam? Unless there is overlap?

Yeah, because the artifact only occurs when running the script on the headless server (works normally on my laptop).

Here’s a concrete example…

VTK input: test.vtk (439.8 KB)


Output by headless server: headless.vtp (343.5 KB)


Output by my laptop: laptop.vtp (343.5 KB)


The artifact clearly only shows up when ran from the headless server for some reason.

This is really strange as this is just data processing. Are you sure, you are using the same version (not the rendering part but the version of the code 9.2) of VTK between your two machines?

1 Like

Good call! I thought I had the same version, but apparently the server one (running on arm64) used osmesa. After installing this one though (Release vtk-9.2.6.dev0-cp310-cp310-linux_aarch64 · finsberg/vtk-aarch64 · GitHub), it works perfectly!

Edit: actually, that wasn’t it… After re-installing, I got the same issue, but I noticed I added an extra indentation causing the VTP generation to occur while the VTK file was being written (or something like that). That seems to be it rather than the different build. In any case, works now! :pray: