Initializes a new or existing `Physics` instance from a `wrapper.MjData`. Assigns all attributes, sets up named indexing, and creates rendering contexts if rendering is enabled. The default constructor as well as the other `reload_from` methods should delegate to this method.
(self, data)
| 390 | self._reload_from_data(data) |
| 391 | |
| 392 | def _reload_from_data(self, data): |
| 393 | """Initializes a new or existing `Physics` instance from a `wrapper.MjData`. |
| 394 | |
| 395 | Assigns all attributes, sets up named indexing, and creates rendering |
| 396 | contexts if rendering is enabled. |
| 397 | |
| 398 | The default constructor as well as the other `reload_from` methods should |
| 399 | delegate to this method. |
| 400 | |
| 401 | Args: |
| 402 | data: Instance of `wrapper.MjData`. |
| 403 | """ |
| 404 | if not isinstance(data, wrapper.MjData): |
| 405 | raise TypeError(f'Expected wrapper.MjData. Got: {type(data)}.') |
| 406 | self._data = data |
| 407 | |
| 408 | # Performance optimization: pre-allocate numpy arrays used when checking for |
| 409 | # MuJoCo warnings on each step. |
| 410 | self._warnings = self.data.warning.number |
| 411 | self._warnings_before = np.empty_like(self._warnings) |
| 412 | self._new_warnings = np.empty(dtype=bool, shape=(len(self._warnings),)) |
| 413 | |
| 414 | # Forcibly free any previous GL context in order to avoid problems with GL |
| 415 | # implementations that do not support multiple contexts on a given device. |
| 416 | with self._contexts_lock: |
| 417 | if self._contexts: |
| 418 | self._free_rendering_contexts() |
| 419 | |
| 420 | # Call kinematics update to enable rendering. |
| 421 | try: |
| 422 | self.after_reset() |
| 423 | except _control.PhysicsError as e: |
| 424 | logging.warning(e) |
| 425 | |
| 426 | # Set up named indexing. |
| 427 | axis_indexers = index.make_axis_indexers(self.model) |
| 428 | self._named = NamedIndexStructs( |
| 429 | model=index.struct_indexer(self.model, 'mjmodel', axis_indexers), |
| 430 | data=index.struct_indexer(self.data, 'mjdata', axis_indexers),) |
| 431 | |
| 432 | def free(self): |
| 433 | """Frees the native MuJoCo data structures held by this `Physics` instance. |
no test coverage detected