register_plotter_component#
- register_plotter_component( ) Callable[[type], type][source]#
Register a custom namespaced component on a PyVista plotter class.
Once registered, the component becomes available as
plotter.<name>.<method>(...)on every instance oftarget_clsand its subclasses. The component class is instantiated lazily on first access and cached on the plotter instance, so subsequent accesses return the same component object.Components participate in the plotter lifecycle: if the class defines
__plotter_close__it is invoked when the plotter closes; if it defines__plotter_deep_clean__it is invoked fromBasePlotter.deep_clean. Both hooks fire only on components that were actually constructed (touched at least once).Mirrors
register_dataset_accessor()on the registration surface so plugin authors learn one decorator pattern.Added in version 0.48.0.
- Parameters:
- name
str The namespace the component will be attached under. Must be a valid Python identifier that does not start with an underscore.
- target_cls
type,optional The plotter class (or base class) to attach the component to. Defaults to
pyvista.BasePlotter, which exposes the component on every plotter subclass (includingpyvistaqt.QtInteractor).- overridebool, default:
False If
True, allow registering a name that shadows an existing built-in attribute ontarget_cls(a method, property, or any other non-component attribute). The prior attribute is restored onunregister_plotter_component().
- name
- Returns:
callable()Decorator that registers the component 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 component ontarget_clsor any ancestor. Passoverride=Trueto silence the warning when replacement is intentional.
See also
Examples
Register a minimal component on the plotter.
>>> import pyvista as pv >>> @pv.register_plotter_component('demo') ... class DemoComponent: ... def __init__(self, plotter): ... self._plotter = plotter ... ... def title(self): ... return self._plotter.title >>> pl = pv.Plotter() >>> pl.demo.title() == pl.title True
Components are cached per plotter. Repeated access returns the same object.
>>> pl.demo is pl.demo True
Clean up so the namespace is not visible to later doctest examples.
>>> pv.unregister_plotter_component('demo') >>> pl.close()