diff --git a/python/mujoco/experimental/studio/CMakeLists.txt b/python/mujoco/experimental/studio/CMakeLists.txt index 6067abee..eb732520 100644 --- a/python/mujoco/experimental/studio/CMakeLists.txt +++ b/python/mujoco/experimental/studio/CMakeLists.txt @@ -23,9 +23,6 @@ set(STUDIO_COMMON_LIBS structs_wrappers ) -mujoco_pybind11_module(parser parser.cc) -target_link_libraries(parser PRIVATE ${STUDIO_COMMON_LIBS}) - mujoco_pybind11_module(native_viewer_cc native_viewer.cc) target_link_libraries(native_viewer_cc PRIVATE ${STUDIO_COMMON_LIBS}) @@ -39,7 +36,6 @@ mujoco_pybind11_module(sim sim.cc) target_link_libraries(sim PRIVATE ${STUDIO_COMMON_LIBS}) set(MUJOCO_STUDIO_TARGETS - parser native_viewer_cc renderer ux @@ -73,7 +69,6 @@ set_target_properties(implot PROPERTIES # when linking both GLFW (via Filament) and SDL2 statically on Linux. if(UNIX AND NOT APPLE) set(STUDIO_PY_MODULES - parser native_viewer_cc renderer ux @@ -86,7 +81,6 @@ endif() if(APPLE) set(STUDIO_PY_MODULES - parser native_viewer_cc renderer ux diff --git a/python/mujoco/experimental/studio/parser.cc b/python/mujoco/experimental/studio/parser.cc deleted file mode 100644 index 36f7c2ea..00000000 --- a/python/mujoco/experimental/studio/parser.cc +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright 2026 DeepMind Technologies Limited -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include -#include - -#include -#include -#include "structs.h" -#include - -namespace mujoco::python { - -// Loads, parses, and compiles a MuJoCo model from the given file. Returns the -// python mjData object (which also contains the compiled mjModel). -py::object Parse(std::string_view filepath) { - std::unique_ptr holder; - { - py::gil_scoped_release no_gil; - holder = platform::ModelHolder::FromFile(filepath); - } - if (!holder->ok()) { - throw py::value_error( - std::string("Failed to load model from '") + - std::string(filepath) + "': " + std::string(holder->error())); - } - mjModel* model = holder->ReleaseModel(); - mjData* data = holder->ReleaseData(); - py::object py_model = py::cast(MjModelWrapper(model)); - py::object py_data = - py::cast(MjDataWrapper(py::cast(py_model), data)); - return py_data; -} - -} // namespace mujoco::python - -PYBIND11_MODULE(parser, m, pybind11::mod_gil_not_used()) { - pybind11::module_::import("mujoco._structs"); - m.def("parse", &mujoco::python::Parse, - pybind11::return_value_policy::take_ownership); -} diff --git a/python/mujoco/experimental/studio/parser.py b/python/mujoco/experimental/studio/parser.py new file mode 100644 index 00000000..5b7463bd --- /dev/null +++ b/python/mujoco/experimental/studio/parser.py @@ -0,0 +1,24 @@ +# Copyright 2026 DeepMind Technologies Limited +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""Loads and compiles MuJoCo models for the studio viewer.""" + +import mujoco + + +def parse(filepath: str) -> mujoco.MjData: + if filepath.endswith('.mjb'): + model = mujoco.MjModel.from_binary_path(filepath) + else: + model = mujoco.MjSpec.from_file(filepath).compile() + return mujoco.MjData(model)