set_new_attribute#
- set_new_attribute(obj: object, name: str, value: Any) None[source]#
Set a new attribute for this object.
Python allows arbitrarily setting new attributes on objects at any time, but PyVista’s classes do not allow this. If an attribute is not part of PyVista’s API, an
AttributeErroris normally raised when attempting to set it.Use
set_new_attribute()to override this and set a new attribute anyway.See also
pyvista.allow_new_attributesContext manager for controlling if setting new attributes is allowed.
Examples#
Download Python source code | Download Jupyter notebook
Set a new custom attribute on a mesh.
>>> import pyvista as pv
>>> mesh = pv.PolyData()
>>> pv.set_new_attribute(mesh, 'foo', 42)
>>> mesh.foo
42
This is equivalent to using allow_new_attributes with True.
>>> with pv.allow_new_attributes(True):
... mesh.foo = 42
Added in version 0.46.