Binding between a mujoco.Physics and an mjcf.Element or a list of Elements. This object should normally be created by calling `physics.bind(element)` where `physics` is an instance of `mjcf.Physics`. See docstring for that function for details.
| 240 | |
| 241 | |
| 242 | class Binding: |
| 243 | """Binding between a mujoco.Physics and an mjcf.Element or a list of Elements. |
| 244 | |
| 245 | This object should normally be created by calling `physics.bind(element)` |
| 246 | where `physics` is an instance of `mjcf.Physics`. See docstring for that |
| 247 | function for details. |
| 248 | """ |
| 249 | __slots__ = ( |
| 250 | '_attributes', |
| 251 | '_physics', |
| 252 | '_namespace', |
| 253 | '_named_index', |
| 254 | '_named_indexers', |
| 255 | '_getattr_cache', |
| 256 | '_array_index_cache', |
| 257 | ) |
| 258 | |
| 259 | def __init__(self, physics, namespace, named_index): |
| 260 | try: |
| 261 | self._attributes = _ATTRIBUTES[namespace] |
| 262 | except KeyError: |
| 263 | raise ValueError('elements of type {!r} cannot be bound to physics' |
| 264 | .format(namespace)) from None |
| 265 | self._physics = physics |
| 266 | self._namespace = namespace |
| 267 | self._named_index = named_index |
| 268 | self._named_indexers = {} |
| 269 | self._getattr_cache = {} |
| 270 | self._array_index_cache = {} |
| 271 | |
| 272 | def __dir__(self): |
| 273 | return sorted(set(dir(type(self))).union(self._attributes)) |
| 274 | |
| 275 | def _get_cached_named_indexer(self, name): |
| 276 | named_indexer = self._named_indexers.get(name) |
| 277 | if named_indexer is None: |
| 278 | try: |
| 279 | named_indexer = self._attributes[name].get_named_indexer(self._physics) |
| 280 | self._named_indexers[name] = named_indexer |
| 281 | except KeyError as e: |
| 282 | raise AttributeError('bound element <{}> does not have attribute {!r}' |
| 283 | .format(self._namespace, name)) from e |
| 284 | return named_indexer |
| 285 | |
| 286 | def _get_cached_array_and_index(self, name): |
| 287 | """Returns `(array, index)` for a given field name.""" |
| 288 | named_indexer = self._get_cached_named_indexer(name) |
| 289 | array = named_indexer._field # pylint: disable=protected-access |
| 290 | try: |
| 291 | index = self._array_index_cache[name] |
| 292 | except KeyError: |
| 293 | index = named_indexer._convert_key(self._named_index) # pylint: disable=protected-access |
| 294 | self._array_index_cache[name] = index |
| 295 | return array, index |
| 296 | |
| 297 | @property |
| 298 | def element_id(self): |
| 299 | """The ID number of this element within MuJoCo's data structures.""" |
no outgoing calls
no test coverage detected
searching dependent graphs…