Export an SG Mesh to VTU#
Problem description#
An SG mesh often needs to be inspected in ParaView or exchanged with another VTK-based tool. The output must retain mesh geometry, connectivity, and the node and element arrays needed to color or filter the mesh.
Explaination of the solution#
run.py reads an existing VABS Structure Gene and writes its mesh through
sgio.write() with file_format="vtu". It also writes a VTM scene whose
mesh, local_y1, local_y2, and local_y3 blocks contain the full mesh and
explicit local-axis glyph geometry for ParaView.
"""Export an existing Structure Gene mesh as a VTU file for ParaView.
The example reads the VABS cross-section shared with ``preview_sg_mesh`` and
writes only its geometry, topology, point data, and cell data to VTU. Open the
resulting file in ParaView or PyVista; sgio does not read VTK/VTU files here.
"""
from __future__ import annotations
from pathlib import Path
import sgio
EXAMPLE_DIR = Path(__file__).resolve().parent
INPUT_FILE = EXAMPLE_DIR.parent / "preview_sg_mesh" / "sg21t_tri3.sg"
OUTPUT_FILE = EXAMPLE_DIR / "sg21t_tri3.vtu"
LOCAL_AXES_FILE = EXAMPLE_DIR / "sg21t_tri3_local_axes.vtm"
def main() -> None:
"""Write the mesh VTU and a VTM scene with explicit local-axis geometry."""
sg = sgio.read(
str(INPUT_FILE),
file_format="vabs",
format_version="4",
sgdim=2,
model_type="BM2",
)
sgio.write(sg, str(OUTPUT_FILE), file_format="vtu")
local_axes = sgio.create_pyvista_local_axis_multiblock(sg.mesh)
local_axes.save(LOCAL_AXES_FILE)
print(f"Wrote VTU mesh for ParaView/PyVista: {OUTPUT_FILE.resolve()}")
print(f"Wrote VTM local-axis scene for ParaView: {LOCAL_AXES_FILE.resolve()}")
if __name__ == "__main__":
main()
Run the example:
uv run python examples/export_sg_mesh_to_vtu/run.py
The VTK/VTU adapter is a mesh-only writer: it preserves points, cell
topology, point_data, and cell_data, including multicomponent local-axis
arrays. It deliberately rejects element-nodal fields and point/cell sets
because those containers have no lossless VTK/VTU mapping in this interface.
It does not implement sgio.read(..., file_format="vtu").
Use the separate plot.py example to read the Structure Gene through the
high-level sgio.plot_sg_pyvista() interface and produce a browser scene.
The VTM written by run.py remains complete for ParaView; the browser scene
samples local-axis glyphs for responsive rendering.
uv run python examples/export_sg_mesh_to_vtu/plot.py
For a desktop inspection window, run plot_desktop.py. Its checkboxes toggle
the local axes, faces, edges, and nodes independently. Desktop widgets require
a live PyVista window, so they are deliberately separate from the HTML export.
uv run python examples/export_sg_mesh_to_vtu/plot_desktop.py
Result#
The script creates sg21t_tri3.vtu and sg21t_tri3_local_axes.vtm. Open the
VTM in ParaView to inspect the complete local-axis arrows, or open
pyvista.html after running plot.py for sampled browser rendering. Run
plot_desktop.py for the desktop visibility controls.
List of all files#
run.py: VTU/VTM export script.
plot.py: High-level PyVista HTML plot.
plot_desktop.py: High-level PyVista desktop plot with visibility controls.
sg21t_tri3.sg: Existing VABS input.
sg21t_tri3.vtu: Generated VTU mesh.sg21t_tri3_local_axes.vtm: Generated complete local-axis scene.pyvista.html: Generated sampled browser scene.