Creates a binding between this `Physics` instance and `mjcf.Element`s. The binding allows for easier interaction with the `Physics` data structures related to an MJCF element. For example, in order to access the Cartesian position of a geom, we can use: ```python physics.bind(g
(self, mjcf_elements)
| 513 | self._dirty = False |
| 514 | |
| 515 | def bind(self, mjcf_elements) -> Binding: |
| 516 | """Creates a binding between this `Physics` instance and `mjcf.Element`s. |
| 517 | |
| 518 | The binding allows for easier interaction with the `Physics` data structures |
| 519 | related to an MJCF element. For example, in order to access the Cartesian |
| 520 | position of a geom, we can use: |
| 521 | |
| 522 | ```python |
| 523 | physics.bind(geom_element).pos |
| 524 | ``` |
| 525 | |
| 526 | instead of the more cumbersome: |
| 527 | |
| 528 | ```python |
| 529 | physics.named.model.geom_pos[geom_element.full_identifier] |
| 530 | ``` |
| 531 | |
| 532 | Note that the binding takes into account the type of element. This allows us |
| 533 | to remove prefixes from certain common attributes in order to unify access. |
| 534 | For example, we can use: |
| 535 | |
| 536 | ```python |
| 537 | physics.bind(geom_element).pos = [1, 2, 3] |
| 538 | physics.bind(site_element).pos = [4, 5, 6] |
| 539 | ``` |
| 540 | |
| 541 | instead of: |
| 542 | |
| 543 | ```python |
| 544 | physics.named.model.geom_pos[geom_element.full_identifier] = [1, 2, 3] |
| 545 | physics.named.model.site_pos[site_element.full_identifier] = [4, 5, 6] |
| 546 | ``` |
| 547 | |
| 548 | This in turn allows for the creation of common algorithms that can operate |
| 549 | across a wide range of element type. |
| 550 | |
| 551 | When attribute values are modified through the binding, future queries of |
| 552 | derived values are automatically recalculated if necessary. For example, |
| 553 | if a joint's `qpos` is modified and a site's `xpos` is later read, the value |
| 554 | of the `xpos` is updated according to the new joint configuration. This is |
| 555 | done lazily when an updated value is required, so repeated value |
| 556 | modifications do not incur a performance penalty. |
| 557 | |
| 558 | It is also possible to bind a sequence containing one or more elements, |
| 559 | provided they are all of the same type. In this case the binding exposes |
| 560 | `SynchronizingArrayWrapper`s, which are array-like objects that provide |
| 561 | writeable views onto the corresponding memory addresses in MuJoCo. Writing |
| 562 | into a `SynchronizingArrayWrapper` causes the underlying values in MuJoCo |
| 563 | to be updated, and if necessary causes derived values to be recalculated. |
| 564 | Note that in order to trigger recalculation it is necessary to reference a |
| 565 | derived attribute of a binding. |
| 566 | |
| 567 | ```python |
| 568 | bound_joints = physics.bind([joint1, joint2]) |
| 569 | bound_bodies = physics.bind([body1, body2]) |
| 570 | # `qpos_view` and `xpos_view` are `SynchronizingArrayWrapper`s providing |
| 571 | # views onto `physics.data.qpos` and `physics.data.xpos` respectively. |
| 572 | qpos_view = bound_joints.qpos |