Converting VTK to USD and back#

This example shows how to convert a VTK file to USD and then back to VTK.

# Import necessary modules
from ansys.tools.usdviewer.vtk_converter import VTKConverter
from ansys.tools.usdviewer.viewer import USDViewer
from pxr import Usd
import pyvista

# Read a simple VTK file
sphere = pyvista.read("sphere.vtk")

# Create a new USD stage
stage = Usd.Stage.CreateNew("sphere.usda")

# Convert VTK to USD and add to stage
stage = VTKConverter.convert_vtk_file_to_usd("sphere.vtk", stage)

# View the USD file in the USD Viewer
viewer = USDViewer(title="USD Viewer", size=(800, 800))
viewer.load_usd("sphere.usda")
viewer.show()

# Convert USD back to VTK
vtk_pd = VTKConverter.convert_usd_to_vtk(stage)

# Visualize the converted VTK data using PyVista
plotter = pyvista.Plotter()
plotter.add_mesh(vtk_pd, color="lightblue", show_edges=True)
plotter.show()