| 1 |
"""Wrapper for :vtk:`vtkPropCollection`.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from collections.abc import Iterable |
| 6 |
from collections.abc import MutableSequence |
| 7 |
from typing import TYPE_CHECKING |
| 8 |
|
| 9 |
import numpy as np |
| 10 |
|
| 11 |
from pyvista import _validation |
| 12 |
from pyvista import _vtk |
| 13 |
|
| 14 |
if TYPE_CHECKING: |
| 15 |
from typing import Any |
| 16 |
|
| 17 |
|
| 18 |
class _PropCollection(MutableSequence[_vtk.vtkProp]): |
| 19 |
"""Sequence wrapper for a :vtk:`vtkPropCollection` with a dict-like interface. |
| 20 |
|
| 21 |
.. versionadded:: 0.45 |
| 22 |
|
| 23 |
""" |
| 24 |
|
| 25 |
def __init__(self, prop_collection: _vtk.vtkPropCollection): |
| 26 |
super().__init__() |
| 27 |
self._prop_collection = prop_collection |
| 28 |
|
| 29 |
def __getitem__(self, key): |
| 30 |
if isinstance(key, (int, np.integer)): |
| 31 |
# lookup from index number |
| 32 |
key = self._validate_index(key) |
| 33 |
return self._prop_collection.GetItemAsObject(int(key)) |
| 34 |
elif isinstance(key, str): |
| 35 |
# lookup from actor name |
| 36 |
names = self.keys() |
| 37 |
try: |
| 38 |
index = names.index(key) |
| 39 |
return self._prop_collection.GetItemAsObject(index) |
| 40 |
except ValueError: |
| 41 |
msg = f"No item found with name '{key}'." |
| 42 |
raise KeyError(msg) |
| 43 |
msg = f'Key must be an index or a string, got {type(key).__name__}.' |
| 44 |
raise TypeError(msg) |
| 45 |
|
| 46 |
def __len__(self): |
| 47 |
return self._prop_collection.GetNumberOfItems() |
| 48 |
|
| 49 |
def __delitem__(self, key): |
| 50 |
if isinstance(key, (int, np.integer)): |
| 51 |
# remove by index |
| 52 |
key = self._validate_index(key) |
| 53 |
self._prop_collection.RemoveItem(key) |
| 54 |
elif isinstance(key, str): |
| 55 |
# remove by name |
| 56 |
names = self.keys() |
| 57 |
try: |
| 58 |
index = names.index(key) |
| 59 |
except ValueError: |
| 60 |
msg = f"No item found with name '{key}'." |
| 61 |
raise KeyError(msg) |
| 62 |
del self[index] |
| 63 |
else: |
| 64 |
msg = f'Key must be an index or a string, got {type(key).__name__}.' |
| 65 |
raise TypeError(msg) |
| 66 |
|
| 67 |
def __setitem__(self, key, value): |
| 68 |
_validation.check_instance(value, _vtk.vtkProp) |
| 69 |
if isinstance(key, (int, np.integer)): |
| 70 |
# set by index |
| 71 |
key = self._validate_index(key) |
| 72 |
self._prop_collection.ReplaceItem(key, value) |
| 73 |
elif isinstance(key, str): |
| 74 |
if hasattr(value, 'name') and value.name != key: |
| 75 |
msg = f"Name of the new actor '{value.name}' must match the key name '{key}'." |
| 76 |
raise ValueError(msg) |
| 77 |
# set by name |
| 78 |
index = self.keys().index(key) |
| 79 |
self[index] = value |
| 80 |
else: |
| 81 |
msg = f'Key must be an index or a string, got {type(key).__name__}.' |
| 82 |
raise TypeError(msg) |
| 83 |
|
| 84 |
def insert(self, index, value) -> None: |
| 85 |
_validation.check_instance(value, _vtk.vtkProp) |
| 86 |
if len(self) == 0: |
| 87 |
self.append(value) |
| 88 |
return |
| 89 |
if index < 0: |
| 90 |
index = len(self) + index + 1 |
| 91 |
index = min(index, len(self)) |
| 92 |
self._prop_collection.InsertItem(index - 1, value) |
| 93 |
|
| 94 |
def append(self, value: _vtk.vtkProp): |
| 95 |
_validation.check_instance(value, _vtk.vtkProp) |
| 96 |
self._prop_collection.AddItem(value) |
| 97 |
|
| 98 |
def keys(self) -> list[str]: |
| 99 |
return [ |
| 100 |
prop.name |
| 101 |
if hasattr(prop, 'name') |
| 102 |
else f'{type(prop).__name__}({prop.GetAddressAsString("")})' |
| 103 |
for prop in self |
| 104 |
] |
| 105 |
|
| 106 |
def items(self) -> Iterable[tuple[str, _vtk.vtkProp]]: |
| 107 |
yield from zip(self.keys(), self, strict=True) |
| 108 |
|
| 109 |
def __del__(self): |
| 110 |
self._prop_collection = None # type: ignore[assignment] |
| 111 |
|
| 112 |
def _validate_index(self, index: int | np.integer[Any]) -> int: |
| 113 |
if index < 0: |
| 114 |
index = len(self) + index |
| 115 |
if index < 0 or index >= len(self): |
| 116 |
msg = 'Index out of range.' |
| 117 |
raise IndexError(msg) |
| 118 |
return int(index) |