From 8c160459113c5761a07b3e479657a11936749ce0 Mon Sep 17 00:00:00 2001 From: "Rohan P. Singh" Date: Mon, 18 Mar 2024 19:30:11 +0900 Subject: [PATCH] model merging: Introduce handling of include elements --- src/mj_utils_merge_mujoco_models.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/mj_utils_merge_mujoco_models.cpp b/src/mj_utils_merge_mujoco_models.cpp index 19fb524..61f6ebd 100644 --- a/src/mj_utils_merge_mujoco_models.cpp +++ b/src/mj_utils_merge_mujoco_models.cpp @@ -340,6 +340,34 @@ static void merge_mujoco_model(const std::string & robot, const std::string & xm { mc_rtc::log::error_and_throw("No mujoco root node in {}", xmlFile); } + auto handle_mujoco_includes = [&](pugi::xml_node & out, const char * attr) + { + for(const auto & inc : root.child(attr).children("include")) + { + bfs::path include_file_path(inc.attribute("file").value()); + if(!include_file_path.is_absolute()) + { + bfs::path xmlPath = bfs::path(xmlFile).parent_path(); + include_file_path = xmlPath / include_file_path; + } + pugi::xml_document includeDoc; + if (!includeDoc.load_file(include_file_path.c_str())) + { + mc_rtc::log::error_and_throw("Failed to load {}", include_file_path.c_str()); + } + for(pugi::xml_node include_node : includeDoc.child("mujoco").children()) + { + out.append_copy(include_node); + } + } + }; + /** Handle includes here (non-exhaustive list for now) */ + static const char * elements[] = {"asset", "contact", "actuator", "sensor"}; + for(const auto & element : elements) + { + auto out = root.child(element); + handle_mujoco_includes(out, element); + } /** Merge compiler flags */ { auto compiler_out = get_child_or_create(out, "compiler");