pyvista_js.PolyData#
- class pyvista_js.PolyData(points: ArrayLike, faces: ArrayLike | None = None, *, t_coords: ArrayLike | None = None, scalars: ArrayLike | None = None, scalar_name: str = 'scalars', _scene_data: dict[str, Any] | None = None)#
Bases:
objectBase polygonal mesh class.
- Parameters:
points (array-like) – Vertex coordinates as an (n, 3) array.
faces (array-like, optional) – Cell connectivity information.
- __init__(points: ArrayLike, faces: ArrayLike | None = None, *, t_coords: ArrayLike | None = None, scalars: ArrayLike | None = None, scalar_name: str = 'scalars', _scene_data: dict[str, Any] | None = None) None#
Initialize a PolyData mesh.
Methods
__init__(points[, faces, t_coords, scalars, ...])Initialize a PolyData mesh.
clip([normal, origin, invert])Clip the mesh with a plane.
contour([isosurfaces, scalars, scalar_name])Generate contour lines at constant scalar values.
fill_holes([hole_size])Fill holes in a polygonal mesh.
plot([color, opacity, pbr, metallic, roughness])Plot this mesh.
save(filename)Write this mesh to disk using meshio.
shrink([shrink_factor])Shrink the cells of a mesh towards their centroid.
Generate texture coordinates by projecting points onto the XY plane.
Return a JSON-serializable dict describing this mesh source.
tube(*[, radius, n_sides, capping])Generate a tube around a line polydata.
Attributes
Compute the radius and center of a bounding sphere.
Return the number of faces.
Return the number of points.
Access point data arrays.
- property bounding_sphere: tuple[float, tuple[float, float, float]]#
Compute the radius and center of a bounding sphere.
Uses Ritter’s algorithm to approximate the minimum bounding sphere. Returns NaN values if there are no points.
- Returns:
Sphere radius as a float and center as a tuple of floats
(x, y, z).- Return type:
float, tuple
Examples
>>> import pyvista_js as pv >>> mesh = pv.Sphere(radius=1.5, center=(1, 2, 3)) >>> radius, center = mesh.bounding_sphere >>> round(radius, 5) 1.5 >>> [round(c, 5) for c in center] [1.0, 2.0, 3.0]
- clip(normal: str | tuple[float, float, float] = 'x', origin: tuple[float, float, float] | None = None, *, invert: bool = False) PolyData#
Clip the mesh with a plane.
This filter clips the mesh with a plane defined by a normal vector and an origin point. Points on one side of the plane are removed. It mirrors the PyVista
clipfilter API.Note
The clipping is computed in JavaScript at render time by evaluating the signed distance of each vertex from the clip plane. Cells with all vertices on the clipped side are removed.
vtk.jsdoes not include a built-in clipping filter with the exact PyVista API, so this filter is implemented as a custom JavaScript pass.- Parameters:
normal (str or tuple of float, optional) – The normal vector of the clipping plane. Can be a string specifying a cardinal direction (‘x’, ‘y’, ‘z’, ‘-x’, ‘-y’, ‘-z’) or a 3-tuple of floats (nx, ny, nz). Default is ‘x’.
origin (tuple of float, optional) – The origin point of the clipping plane as (x, y, z). If not provided, defaults to the center of the mesh’s bounding box.
invert (bool, optional) – If True, flip the clipping direction to keep the part that would normally be removed. Default is False.
- Returns:
A new mesh with clipped cells removed.
- Return type:
Examples
>>> import pyvista_js as pv >>> sphere = pv.Sphere() >>> clipped = sphere.clip(normal='x', origin=(0, 0, 0)) >>> isinstance(clipped, pv.PolyData) True
Clip along the negative Y axis:
>>> clipped = sphere.clip(normal='-y') >>> isinstance(clipped, pv.PolyData) True
Clip with a custom normal vector:
>>> clipped = sphere.clip(normal=(1, 1, 0), origin=(0, 0, 0)) >>> isinstance(clipped, pv.PolyData) True
Render the clipped mesh:
>>> clipped.plot()
- contour(isosurfaces: int | list[float] = 10, scalars: ArrayLike | None = None, scalar_name: str | None = None) PolyData#
Generate contour lines at constant scalar values.
This filter extracts isolines from the mesh at specified scalar values using a marching triangles algorithm implemented in JavaScript. It mirrors the PyVista
contourfilter API.Note
The contour is computed in JavaScript at render time by applying the marching triangles algorithm to each triangle of the mesh, interpolating edge crossings at the specified iso-values.
vtk.jsdoes not supportvtkPolyDatainput forvtkContourFilter, so this filter is implemented as a custom JavaScript pass.- Parameters:
isosurfaces (int or list of float, optional) – Number of evenly spaced contours to generate, or a list of explicit scalar values at which to generate contours. Default is 10.
scalars (array-like, optional) – Scalar values per point to use for contouring. If not provided, uses the mesh’s existing
scalarsattribute. Must have length equal ton_points.scalar_name (str, optional) – Name for the scalar array in vtk.js. If not provided, uses the mesh’s existing
scalar_nameattribute or defaults to “scalars”.
- Returns:
A new mesh containing the contour lines.
- Return type:
- Raises:
ValueError – If no scalars are provided and the mesh has no scalar data, or if isosurfaces parameter is invalid.
Examples
>>> import pyvista_js as pv >>> sphere = pv.Sphere() >>> sphere_scalars = sphere.points[:, 2] >>> contours = sphere.contour(isosurfaces=5, scalars=sphere_scalars) >>> isinstance(contours, pv.PolyData) True
Generate contours at specific values:
>>> contours = sphere.contour(isosurfaces=[-0.5, 0.0, 0.5], scalars=sphere_scalars)
Render the contours:
>>> contours.plot()
- fill_holes(hole_size: float = 1000.0) PolyData#
Fill holes in a polygonal mesh.
This filter identifies and fills holes in the mesh by locating boundary edges, linking them together into loops, and then triangulating the resulting loops. A hole size threshold controls which holes are filled.
It mirrors the PyVista
fill_holesfilter API and replicates the behavior of the vtk.js FillHolesFilter example.Note
The hole filling is computed in JavaScript at render time by finding boundary edges (edges belonging to only one polygon), linking them into loops, and fan-triangulating each loop whose perimeter is smaller than
hole_size.- Parameters:
hole_size (float, optional) – The maximum hole size to fill, specified as the length of the perimeter of the hole. Holes with a perimeter larger than this value will not be filled. Default is 1000.0.
- Returns:
A new mesh with holes filled.
- Return type:
- Raises:
ValueError – If
hole_sizeis not positive.
Examples
>>> import pyvista_js as pv >>> sphere = pv.Sphere() >>> filled = sphere.fill_holes(hole_size=100.0) >>> isinstance(filled, pv.PolyData) True
Create a sphere with a hole and fill it:
>>> import pyvista_js as pv >>> import numpy as np >>> # Create a sphere with a hole by clipping >>> sphere = pv.Sphere(radius=1.0) >>> clipped_sphere = sphere.clip(normal='z', origin=(0, 0, 0.5)) >>> # Fill the hole >>> filled_sphere = clipped_sphere.fill_holes(hole_size=10.0) >>> filled_sphere.plot()
Render the filled mesh:
>>> filled.plot()
- property n_faces: int#
Return the number of faces.
- property n_points: int#
Return the number of points.
- plot(color: str | tuple[float, float, float] | None = None, opacity: float = 1.0, pbr: bool = False, metallic: float = 0.0, roughness: float = 0.5) None#
Plot this mesh.
This is a convenience method that creates a
Plotter, adds this mesh, and callsshow().- Parameters:
color (str or tuple, optional) – Color of the mesh. Can be a color name or RGB tuple.
opacity (float, optional) – Opacity of the mesh, between 0 (transparent) and 1 (opaque).
pbr (bool, optional) – Enable physically based rendering (PBR). Default is False.
metallic (float, optional) – Metallic factor for PBR, between 0 and 1. Default is 0.0.
roughness (float, optional) – Roughness factor for PBR, between 0 and 1. Default is 0.5.
Examples
>>> import pyvista_js as pv >>> sphere = pv.Sphere() >>> sphere.plot(color='red')
- property point_data: PointData#
Access point data arrays.
- Returns:
Dict-like container for point data arrays.
- Return type:
Examples
>>> import pyvista_js as pv >>> import numpy as np >>> mesh = pv.Sphere() >>> mesh.point_data['elevation'] = mesh.points[:, 2] >>> 'elevation' in mesh.point_data True
Render with scalar coloring:
>>> plotter = pv.Plotter() >>> _ = plotter.add_mesh(mesh, scalars='elevation', cmap='viridis') >>> plotter.show()
- save(filename: str | Path) None#
Write this mesh to disk using meshio.
The file format is inferred from the extension of
filename. Any format supported by meshio can be used (e.g.'.obj','.vtk','.ply','.stl').Note
Requires
meshioto be installed:pip install "pyvista-js[io]"
In Pyodide / JupyterLite, install it with micropip before calling this method:
import micropip await micropip.install("meshio")
- Parameters:
filename (str or Path) – Output path. The extension determines the file format.
- Return type:
None
- Raises:
ImportError – If
meshiois not installed.
Examples
>>> from pyvista_js import examples >>> mesh = examples.download_trumpet() >>> mesh.save('trumpet.obj')
- shrink(shrink_factor: float = 0.8) PolyData#
Shrink the cells of a mesh towards their centroid.
This filter shrinks the individual cells of a mesh towards their centroids, producing visual separation between adjacent cells. It mirrors the PyVista
shrinkfilter API.Note
The shrink is computed in JavaScript at render time by iterating over the cell array from the vtk.js source, moving each vertex toward its cell’s centroid.
vtk.jsdoes not includevtkShrinkFilter, so this filter is implemented as a custom JavaScript pass.- Parameters:
shrink_factor (float, optional) – The factor to shrink each cell by, between 0 and 1. A value of 1.0 produces no change; lower values produce more shrinkage. Default is 0.8.
- Returns:
A new mesh with shrunk cells.
- Return type:
Examples
>>> import pyvista_js as pv >>> sphere = pv.Sphere() >>> shrunk = sphere.shrink(shrink_factor=0.8) >>> isinstance(shrunk, pv.PolyData) True
Render the shrunk mesh:
>>> shrunk.plot()
- texture_map_to_plane() PolyData#
Generate texture coordinates by projecting points onto the XY plane.
Maps the mesh’s X and Y extents to UV coordinates in the [0, 1] range. This mirrors the PyVista
pyvista.DataSet.texture_map_to_plane()API.- Returns:
A new mesh with texture coordinates (
t_coords) set.- Return type:
Examples
>>> import pyvista_js as pv >>> mesh = pv.Sphere() >>> mapped = mesh.texture_map_to_plane() >>> mapped.t_coords is not None True >>> mapped.t_coords.shape == (mesh.n_points, 2) True
- to_scene_data() dict[str, object]#
Return a JSON-serializable dict describing this mesh source.
- Returns:
Source configuration with
"type"key and type-specific parameters.- Return type:
dict
- tube(*, radius: float = 0.5, n_sides: int = 20, capping: bool = True) PolyData#
Generate a tube around a line polydata.
This filter creates a tube representation around lines in the mesh by sweeping a polygonal cross-section along each line. It mirrors the PyVista
tubefilter API and is backed by vtk.js’svtkTubeFilter.Note
This filter is intended for use with line-based polydata (such as the output of
Line). It uses vtk.js’svtkTubeFilter, which generates a tube by sweeping a circle withn_sidessides along the line segments.- Parameters:
radius (float, optional) – The radius of the tube. Default is 0.5.
n_sides (int, optional) – The number of sides for the tube cross-section. Higher values produce smoother tubes. Default is 20.
capping (bool, optional) – Whether to cap the ends of the tube. Default is True.
- Returns:
A new mesh representing the tube.
- Return type:
Examples
>>> import pyvista_js as pv >>> line = pv.Line() >>> tube = line.tube(radius=0.05, n_sides=20) >>> isinstance(tube, pv.PolyData) True
Render the tube:
>>> tube.plot()