Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions python/rcs/_core/sim.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class Sim:
def is_converged(self) -> bool: ...
def reset(self) -> None: ...
def set_config(self, cfg: SimConfig) -> bool: ...
def set_model_data(self, mjmdl: int, mjdata: int) -> None: ...
def step(self, k: int) -> None: ...
def step_until_convergence(self) -> None: ...
def sync_gui(self) -> None: ...
Expand Down
3 changes: 2 additions & 1 deletion python/rcs/envs/scenes.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ def add_task_mujoco(cfg: TaskConfig, composer: ModelComposer, env_cfg: "SimEnvCr
"""Add task-specific elements to the Mujoco scene."""

@staticmethod
def add_task_env(_cfg: TaskConfig, env: gym.Env, _simulation: Sim, _env_cfg: "SimEnvCreatorConfig") -> gym.Env:
def add_task_env(cfg: TaskConfig, env: gym.Env, simulation: Sim, env_cfg: "SimEnvCreatorConfig") -> gym.Env:
"""Add task-specific wrappers to the environment."""
_ = (cfg, simulation, env_cfg)
return env


Expand Down
36 changes: 23 additions & 13 deletions python/rcs/sim/sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,7 @@ class Sim(_Sim):
STATE_SPEC = mj.mjtState.mjSTATE_INTEGRATION

def __init__(self, mjmdl: str | PathLike | ModelComposer, cfg: SimConfig | None = None):
if isinstance(mjmdl, ModelComposer):
self.model = mjmdl.get_model()
else:
mjmdl = Path(mjmdl)
if mjmdl.suffix == ".xml":
self.model = mj.MjModel.from_xml_path(str(mjmdl))
elif mjmdl.suffix == ".mjb":
self.model = mj.MjModel.from_binary_path(str(mjmdl))
else:
msg = f"Filetype {mjmdl.suffix} is unknown"
logger.error(msg)

self.data = mj.MjData(self.model)
self.model, self.data = self.get_model_data(mjmdl)
super().__init__(self.model._address, self.data._address)
self._mp_context = mp.get_context("spawn")
self._gui_uuid: Optional[str] = None
Expand All @@ -71,6 +59,28 @@ def __init__(self, mjmdl: str | PathLike | ModelComposer, cfg: SimConfig | None
if cfg is not None:
self.set_config(cfg)

def get_model_data(self, mjmdl: str | PathLike | ModelComposer) -> tuple[mj.MjModel, mj.MjData]:
if isinstance(mjmdl, ModelComposer):
model = mjmdl.get_model()
else:
mjmdl = Path(mjmdl)
if mjmdl.suffix == ".xml":
model = mj.MjModel.from_xml_path(str(mjmdl))
elif mjmdl.suffix == ".mjb":
model = mj.MjModel.from_binary_path(str(mjmdl))
else:
msg = f"Filetype {mjmdl.suffix} is unknown"
logger.error(msg)
data = mj.MjData(model)
return model, data

def set_model_data(self, model: mj.MjModel, data: mj.MjData):
super().set_model_data(model._address, data._address)

def instantiate_new_sim(self, mjmdl: str | PathLike | ModelComposer):
self.model, self.data = self.get_model_data(mjmdl)
self.set_model_data(self.model, self.data)

def get_state_spec(self) -> int:
return int(self.STATE_SPEC)

Expand Down
6 changes: 6 additions & 0 deletions src/pybind/rcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,12 @@ PYBIND11_MODULE(_core, m) {
return std::make_shared<rcs::sim::Sim>((mjModel*)m, (mjData*)d);
}),
py::arg("mjmdl"), py::arg("mjdata"))
.def(
"set_model_data",
[](rcs::sim::Sim& self, long model, long data) {
self.set_model_data((mjModel*)model, (mjData*)data);
},
py::arg("mjmdl"), py::arg("mjdata"))
.def("step_until_convergence", &rcs::sim::Sim::step_until_convergence,
py::call_guard<py::gil_scoped_release>())
.def("is_converged", &rcs::sim::Sim::is_converged)
Expand Down
7 changes: 6 additions & 1 deletion src/sim/sim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ bool get_last_return_value(ConditionCallback cb) {
return cb.last_return_value;
}

Sim::Sim(mjModel* m, mjData* d) : m(m), d(d), renderer(m) {};
Sim::Sim(mjModel* m, mjData* d) : m(m), d(d), renderer(m) {}

void Sim::set_model_data(mjModel* model, mjData* data) {
this->m = model;
this->d = data;
}

bool Sim::set_config(const SimConfig& cfg) {
this->cfg = cfg;
Expand Down
1 change: 1 addition & 0 deletions src/sim/sim.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class Sim {
mjModel* m;
mjData* d;
Sim(mjModel* m, mjData* d);
void set_model_data(mjModel* model, mjData* data);
bool set_config(const SimConfig& cfg);
SimConfig get_config();
bool is_converged();
Expand Down
Loading