register_dataset_accessor#
- register_dataset_accessor( ) Callable[[type], type][source]#
Register a custom namespaced accessor on a PyVista dataset class.
Mirrors the accessor pattern used by pandas and xarray. Once registered, the accessor becomes available as
dataset.<name>.<method>(...)on every instance oftarget_clsand its subclasses. The accessor class is instantiated lazily on first access and cached on the dataset instance, so subsequent accesses return the same accessor object.Added in version 0.48.0.
- Parameters:
- name
str The namespace the accessor will be attached under. Must be a valid Python identifier that does not start with an underscore.
- target_cls
type The PyVista dataset class (or base class) to attach the accessor to. Registering against
DataSetexposes the accessor on every subclass (PolyData,UnstructuredGrid,ImageData, and so on). Registering againstDataObjectadditionally coversMultiBlock.- overridebool, default:
False If
True, allow registering a name that shadows an existing built-in attribute ontarget_cls(a filter method, property, or any other non-accessor attribute). The prior attribute is restored onunregister_dataset_accessor().
- name
- Returns:
callable()Decorator that registers the accessor class passed to it and returns that class unchanged.
- Raises:
ValueErrorIf
nameis empty, not a valid identifier, starts with_, or shadows a built-in attribute ontarget_clswithoutoverride=True.TypeErrorIf
target_clsis not a class, ornameis not a string.
- Warns:
UserWarningIf
namealready refers to a registered accessor ontarget_clsor any ancestor. Emitting a warning rather than raising matches pandas’ behavior and avoids hard-failing when two plugins collide.
Examples
Register a minimal accessor on
PolyData.>>> import pyvista as pv >>> @pv.register_dataset_accessor('demo', pv.PolyData) ... class DemoAccessor: ... def __init__(self, mesh): ... self._mesh = mesh ... ... def n_points_doubled(self): ... return self._mesh.n_points * 2 >>> sphere = pv.Sphere() >>> sphere.demo.n_points_doubled() == sphere.n_points * 2 True
The accessor is cached per instance. Repeated access returns the same object.
>>> sphere.demo is sphere.demo True
Clean up so the namespace is not visible to later doctest examples.
>>> pv.unregister_dataset_accessor('demo', pv.PolyData)