Replace the Studio parser module with a pure Python implementation

PiperOrigin-RevId: 953181335
Change-Id: I1d2ce6f27f50aa4c30cce6d43caed80186ddef42
This commit is contained in:
Matija Kecman
2026-07-23 23:52:22 -07:00
committed by Copybara-Service
parent a9f56dabc2
commit 91d78346e1
3 changed files with 24 additions and 58 deletions
@@ -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
@@ -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 <memory>
#include <string_view>
#include <mujoco/mujoco.h>
#include <mujoco/experimental/platform/sim/model_holder.h>
#include "structs.h"
#include <pybind11/pybind11.h>
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<platform::ModelHolder> 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<MjModelWrapper*>(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);
}
@@ -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)