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 File → Export → .vtkjs extension.
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)
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.
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):
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?
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!