Preview an SG Mesh with PyVista#
Problem Description#
Once a Structure Gene (SG) is read into sgio, it is useful to see the mesh —
the geometry, the cell blocks, and per-element data such as property_id —
before running an analysis. This example reads a VABS cross-section and renders
its mesh with PyVista.
Solution#
sgio.SGMesh is a backend-neutral mesh container, and sgio provides a
bridge to PyVista so any SG mesh can be handed to VTK’s renderer:
Read the cross-section into a
sgio.StructureGenewithsgio.read().Call
sg.mesh.to_pyvista()to get apyvista.UnstructuredGrid. Geometry,point_data, andcell_data(e.g.property_id) cross the bridge.Render it — here a headless screenshot; drop
off_screen/screenshotfor an interactive window.SGMesh.from_pyvista(grid)shows the reverse bridge (grid back toSGMesh).
"""Preview a Structure Gene mesh with pyvista.
Reads a VABS cross-section, bridges its ``SGMesh`` to a ``pyvista`` grid
(:meth:`SGMesh.to_pyvista`), renders a preview image, and demonstrates the
reverse bridge (:meth:`SGMesh.from_pyvista`).
pyvista is an optional dependency::
pip install sgio[pyvista] # or: uv pip install pyvista
"""
import logging
from pathlib import Path
import sgio
from sgio.core.mesh import SGMesh
logging.basicConfig(level=logging.INFO)
cwd = Path(__file__).resolve().parent
# 1. Read a Structure Gene (2D triangular cross-section mesh).
sg = sgio.read(
str(cwd / "sg21t_tri3.sg"),
file_format="vabs",
format_version="4",
sgdim=2,
model_type="BM2",
)
# 2. Bridge the SG mesh to a pyvista UnstructuredGrid.
grid = sg.mesh.to_pyvista()
print(grid)
# 3. Preview. ``off_screen`` + ``screenshot`` make the example run headless;
# drop both arguments for an interactive window (grid.plot(...)).
# The 2D cross-section lives in the Y-Z plane, so look down the X axis.
grid.plot(
scalars="property_id",
show_edges=True,
cpos="yz",
off_screen=True,
screenshot=str(cwd / "preview.png"),
)
print(f"Saved preview to {cwd / 'preview.png'}")
# 4. Reverse bridge: pyvista grid back to an SGMesh.
mesh_back = SGMesh.from_pyvista(grid)
print("Round-trip cell blocks:", [(cb.type, len(cb)) for cb in mesh_back.cells])
Note
PyVista is an optional dependency (it pulls in vtk). The core sgio IR does
not require it; to_pyvista imports PyVista lazily and only fails if you call it
without PyVista installed. Install it with pip install sgio[pyvista].
Result#
The script prints the grid summary and the reverse-bridge cell blocks, and
writes preview.png — the airfoil cross-section colored by property_id with
mesh edges shown. The 2D section lies in the Y-Z plane, so the camera looks down
the X axis (cpos="yz").
File List#
run.py: Main Python script
sg21t_tri3.sg: Input VABS cross-section (triangular mesh)
preview.png: Generated preview image