Note
Go to the end to download the full example code.
Export a USD file to HTML#
This example shows how to convert a USD file (or an in-memory
pxr.Usd.Stage) into a self-contained HTML viewer page. The page
embeds all geometry as a base64-encoded GLB file and renders it with
Three.js — only a CDN connection is required to open
it in any modern browser.
Note
This feature requires the [usd] optional dependencies:
pip install ansys-tools-visualization-interface[usd]
Create a sample USD file#
Build a minimal USD stage with one triangular mesh and save it to disk so the file-path examples below have something to read.
from pxr import Gf, Usd, UsdGeom
stage = Usd.Stage.CreateNew("my_model.usd")
mesh = UsdGeom.Mesh.Define(stage, "/Triangle")
stage.SetDefaultPrim(mesh.GetPrim())
mesh.GetPointsAttr().Set(
[Gf.Vec3f(0, 0, 0), Gf.Vec3f(1, 0, 0), Gf.Vec3f(0, 1, 0)]
)
mesh.GetFaceVertexCountsAttr().Set([3])
mesh.GetFaceVertexIndicesAttr().Set([0, 1, 2])
stage.Save()
Export from a USD file on disk#
Pass a file path string or pathlib.Path to
export_usd_to_html().
The function returns the pathlib.Path to the generated HTML file.
Viewer written to: /home/runner/work/ansys-tools-visualization-interface/ansys-tools-visualization-interface/examples/02-basic-usd-examples/my_model_viewer.html
Export from an in-memory stage#
You can pass a pxr.Usd.Stage directly — no .usd file on disk
is required. A temporary file is created automatically, used to generate
the GLB, and removed when the function returns.
Viewer written to: /home/runner/work/ansys-tools-visualization-interface/ansys-tools-visualization-interface/examples/02-basic-usd-examples/triangle_viewer.html
Add a wireframe overlay#
Set show_mesh_lines=True to inject a Three.js LineSegments overlay
that traces every polygon edge. Use line_color (CSS hex) and
line_opacity (0–1) to style it.
Viewer with wireframe written to: /home/runner/work/ansys-tools-visualization-interface/ansys-tools-visualization-interface/examples/02-basic-usd-examples/my_model_viewer.html
Total running time of the script: (0 minutes 0.306 seconds)