@@ -1990,6 +1990,32 @@ def read_numpy_mesh_object(
19901990# ---------------------------------------------------------------------------
19911991
19921992
1993+ def _import_pyvista () -> Any :
1994+ """Import PyVista and apply forward-compatibility fixes.
1995+
1996+ PyVista 0.43 deprecated ``PolyData.n_faces`` (which used to return the
1997+ total cell count, equivalent to ``n_cells``); PyVista 0.46 converted that
1998+ deprecation into a hard ``AttributeError``. Calling
1999+ ``use_strict_n_faces(True)`` opts into the new, permanent semantics where
2000+ ``n_faces`` returns only the polygon (face) count — identical to
2001+ ``n_faces_strict`` — rather than raising an error.
2002+
2003+ This is safe to call multiple times; the flag is a class-level boolean on
2004+ ``pyvista.PolyData`` and the call is idempotent.
2005+ """
2006+ try :
2007+ import pyvista as pv # type: ignore[import]
2008+ except ImportError as exc :
2009+ raise ImportError (
2010+ "pyvista is not installed. Install it with: pip install pyvista"
2011+ ) from exc
2012+ # Enable strict n_faces mode: makes n_faces return n_faces_strict (polygon
2013+ # count) instead of raising AttributeError in PyVista >= 0.46.
2014+ if hasattr (pv .PolyData , "use_strict_n_faces" ):
2015+ pv .PolyData .use_strict_n_faces (True )
2016+ return pv
2017+
2018+
19932019def numpy_mesh_to_pyvista (mesh : NumpyMesh ) -> Any :
19942020 """Convert a :class:`NumpyMesh` to the appropriate PyVista dataset.
19952021
@@ -2006,10 +2032,7 @@ def numpy_mesh_to_pyvista(mesh: NumpyMesh) -> Any:
20062032 * :class:`NumpySurfaceMesh` → ``pyvista.PolyData(points, faces=faces)``
20072033 * :class:`NumpyVolumeMesh` → ``pyvista.UnstructuredGrid(cells, cell_types, points)``
20082034 """
2009- try :
2010- import pyvista as pv # type: ignore[import]
2011- except ImportError as exc :
2012- raise ImportError ("pyvista is not installed. " "Install it with: pip install pyvista" ) from exc
2035+ pv = _import_pyvista ()
20132036
20142037 pts = mesh .points # (N, 3) float64 — no copy
20152038
@@ -2040,10 +2063,7 @@ def numpy_multi_mesh_to_pyvista(multi: "NumpyMultiMesh") -> Any:
20402063
20412064 Requires ``pyvista`` to be installed (``pip install pyvista``).
20422065 """
2043- try :
2044- import pyvista as pv # type: ignore[import]
2045- except ImportError as exc :
2046- raise ImportError ("pyvista is not installed. Install it with: pip install pyvista" ) from exc
2066+ pv = _import_pyvista ()
20472067
20482068 block : pv .MultiBlock = pv .MultiBlock ()
20492069 for child in multi .children :
0 commit comments