plot3d¶
3D plotting for WhipperSnapPy using pythreejs (Three.js) for Jupyter.
This module provides interactive 3D brain visualization for Jupyter notebooks using Three.js/WebGL. It works in all Jupyter environments (browser, JupyterLab, Colab, VS Code).
Unlike the desktop GUI (whippersnap command), this renders entirely in the
browser via WebGL and is designed for notebook environments.
Usage:
from whippersnappy import plot3d
viewer = plot3d(mesh='path/to/lh.white', bg_map='path/to/lh.curv')
display(viewer)
- Dependencies:
pythreejs, ipywidgets, numpy
- whippersnappy.plot3d.plot3d(mesh, overlay=None, annot=None, bg_map=None, roi=None, minval=None, maxval=None, invert=False, scale=1.85, color_mode=None, width=800, height=800, ambient=0.1)[source]¶
Create an interactive 3D notebook viewer using pythreejs (Three.js).
This function prepares geometry and color information (via
whippersnappy.geometry.prepare_geometry()) and constructs a pythreejs renderer and controls wrapped in anipywidgets.VBoxfor display inside a Jupyter notebook.The mesh can be any triangular surface — not just brain surfaces. Supported file formats: FreeSurfer binary surface (e.g.
lh.white), ASCII OFF (.off), legacy ASCII VTK PolyData (.vtk), ASCII PLY (.ply), or a(vertices, faces)numpy array tuple.- Parameters:
- mesh
strortupleof(array_like, array_like) Path to a mesh file (FreeSurfer binary,
.off,.vtk, or.ply) or a(vertices, faces)tuple.- overlay
str, array_like,orNone,optional Path to a per-vertex scalar file, or a (N,) array of per-vertex scalar values.
- annot
str,tuple,orNone,optional Path to a FreeSurfer .annot file, or a
(labels, ctab)/(labels, ctab, names)tuple for categorical labeling.- bg_map
str, array_like,orNone,optional Path to a per-vertex scalar file or a (N,) array used as grayscale background shading for non-overlay regions.
- roi
str, array_like,orNone,optional Path to a FreeSurfer label file or a (N,) boolean array to restrict overlay coloring to a subset of vertices.
- minval, maxval
floatorNone,optional Threshold and saturation values used for color mapping. If
None, sensible defaults are chosen automatically.- invert
bool,optional If True, invert the overlay color map. Default is
False.- scale
float,optional Global geometry scale applied during preparation. Default is
1.85.- color_mode
ColorSelectionorNone,optional Which sign of overlay values to color (BOTH/POSITIVE/NEGATIVE). If None, defaults to
ColorSelection.BOTH.- width, height
int,optional Canvas dimensions for the generated renderer. Default is
800.- ambient
float,optional Ambient lighting strength passed to the Three.js shader. Default is
0.1.
- mesh
- Returns:
ipywidgets.VBoxA widget containing the pythreejs Renderer and a small info panel.
- Raises:
ValueError,FileNotFoundErrorErrors from
prepare_geometry()are propagated (for example shape mismatches between overlay and mesh vertex count).
Examples
In a Jupyter notebook:
from whippersnappy import plot3d from IPython.display import display # FreeSurfer surface viewer = plot3d('lh.white', overlay='lh.thickness', bg_map='lh.curv') display(viewer) # Any triangular mesh via OFF / VTK / PLY viewer = plot3d('mesh.off', overlay='values.mgh') display(viewer) # Array inputs import numpy as np v = np.random.randn(500, 3).astype(np.float32) f = np.zeros((1, 3), dtype=np.uint32) viewer = plot3d((v, f)) display(viewer)
- whippersnappy.plot3d.create_threejs_mesh_with_custom_shaders(vertices, faces, colors, normals, ambient=0.1)[source]¶
Create a pythreejs.Mesh using custom shader material and buffers.
The function builds a BufferGeometry with position, color and normal attributes, attaches an index buffer, and creates a ShaderMaterial using the WebGL shader snippets returned by
get_webgl_shaders().- Parameters:
- vertices
numpy.ndarray Array of shape (N, 3) with vertex positions (float32).
- faces
numpy.ndarray Integer face index array shape (M, 3) or flattened (3*M,) dtype uint32.
- colors
numpy.ndarray Array of shape (N, 3) with per-vertex RGB colors (float32).
- normals
numpy.ndarray Array of shape (N, 3) with per-vertex normals (float32).
- ambient
float,optional,default0.1 Ambient lighting strength for the shader (passed to Three.js uniform).
- vertices
- Returns:
pythreejs.MeshMesh object ready to be inserted into a pythreejs.Scene.