Skip to content

Commit

Permalink
Merge pull request #1 from stepjam/feature/model_customization_callback
Browse files Browse the repository at this point in the history
Add on_loaded callback
  • Loading branch information
chernyadev authored Mar 25, 2024
2 parents 2c250af + e1ab2fd commit 724a5fb
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion mojo/mojo.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Callable, Optional

import mujoco.viewer
import numpy as np
from dm_control import mjcf
Expand Down Expand Up @@ -85,8 +87,25 @@ def get_material(self, path: str) -> mjcf.Element:
def store_material(self, path: str, material_mjcf: mjcf.Element) -> mjcf.Element:
self._texture_store[path] = material_mjcf

def load_model(self, path: str, parent: MujocoElement = None):
def load_model(
self,
path: str,
parent: MujocoElement = None,
on_loaded: Optional[Callable[[mjcf.RootElement], None]] = None,
):
"""Load a Mujoco model from the xml file and attach it to the specified parent element.
:param path: The file path to the Mujoco model XML file.
:param parent: The parent MujocoElement to which the loaded model will be attached.
If None, it attaches to the root element.
:param on_loaded: An optional callback function to be executed after the model is loaded.
Use it to customize the Mujoco model before attaching it to the parent.
:return: A Body element representing the attached model.
"""

model_mjcf = mjcf.from_path(path)
if on_loaded is not None:
on_loaded(model_mjcf)
attach_site = self.root_element.mjcf if parent is None else parent
attached_model_mjcf = attach_site.attach(model_mjcf)
self.mark_dirty()
Expand Down

0 comments on commit 724a5fb

Please sign in to comment.