Skip to content

Commit

Permalink
Add pydrake bindings for model directives.
Browse files Browse the repository at this point in the history
* Weld error support omitted for simplicity.
* Follow-up to RobotLocomotion#14038
* Completes RobotLocomotion#13282
  • Loading branch information
ggould-tri committed Sep 29, 2020
1 parent ae06238 commit 9c10b90
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions bindings/pydrake/multibody/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ drake_py_unittest(
data = [
"//examples/atlas:models",
"//multibody/benchmarks/acrobot:models",
"//multibody/parsing:process_model_directives_test_models",
],
deps = [
":parsing_py",
Expand Down
33 changes: 33 additions & 0 deletions bindings/pydrake/multibody/parsing_py.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "drake/bindings/pydrake/pydrake_pybind.h"
#include "drake/multibody/parsing/package_map.h"
#include "drake/multibody/parsing/parser.h"
#include "drake/multibody/parsing/process_model_directives.h"

using drake::geometry::SceneGraph;
using drake::multibody::MultibodyPlant;
Expand Down Expand Up @@ -57,6 +58,38 @@ PYBIND11_MODULE(parsing, m) {
py::arg("file_contents"), py::arg("file_type"),
py::arg("model_name") = "", cls_doc.AddModelFromString.doc);
}

// Model Directives
{
constexpr auto& parsing_doc = doc.parsing;
py::class_<parsing::ModelDirectives>(
m, "ModelDirectives", parsing_doc.ModelDirectives.doc);
m.def("LoadModelDirectives", &parsing::LoadModelDirectives,
py::arg("filename"));
py::class_<parsing::ModelInstanceInfo>(m, "ModelInstanceInfo")
.def_readonly("model_name", &parsing::ModelInstanceInfo::model_name)
.def_readonly("model_path", &parsing::ModelInstanceInfo::model_path)
.def_readonly(
"parent_frame_name", &parsing::ModelInstanceInfo::parent_frame_name)
.def_readonly(
"child_frame_name", &parsing::ModelInstanceInfo::child_frame_name)
.def_readonly("X_PC", &parsing::ModelInstanceInfo::X_PC)
.def_readonly(
"model_instance", &parsing::ModelInstanceInfo::model_instance);
m.def(
"ProcessModelDirectives",
[](const parsing::ModelDirectives& directives,
MultibodyPlant<double>* plant, Parser* parser = nullptr) {
std::vector<parsing::ModelInstanceInfo> added_models;
parsing::ProcessModelDirectives(
directives, plant, &added_models, parser);
return added_models;
},
py::arg("directives"), py::arg("plant"), py::arg("parser"),
(std::string(parsing_doc.ProcessModelDirectives.doc) +
"\nNOTE: pydrake does not support `ModelWeldErrorFunction`.")
.c_str());
}
}

} // namespace pydrake
Expand Down
22 changes: 22 additions & 0 deletions bindings/pydrake/multibody/test/parsing_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from pydrake.multibody.parsing import (
Parser,
PackageMap,
LoadModelDirectives,
ProcessModelDirectives,
)

import os
Expand Down Expand Up @@ -82,3 +84,23 @@ def test_parser_string(self):
result = parser.AddModelFromString(
file_contents=sdf_contents, file_type="sdf")
self.assertIsInstance(result, ModelInstanceIndex)

def test_model_directives(self):
model_dir = os.path.dirname(FindResourceOrThrow(
"drake/multibody/parsing/test/"
"process_model_directives_test/package.xml"))
plant = MultibodyPlant(time_step=0.01)
parser = Parser(plant=plant)
parser.package_map().PopulateFromFolder(model_dir)
directives_file = model_dir + "/add_scoped_top.yaml"
directives = LoadModelDirectives(directives_file)
added_models = ProcessModelDirectives(
directives=directives, plant=plant, parser=parser)
# Check for an instance.
model_names = [model.model_name for model in added_models]
self.assertIn("extra_model", model_names)
plant.GetModelInstanceByName("extra_model")

def test_model_directives_doc(self):
"""Check that the warning note in the docstring was added."""
self.assertIn("NOTE:", ProcessModelDirectives.__doc__)
3 changes: 3 additions & 0 deletions multibody/parsing/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ filegroup(
name = "process_model_directives_test_models",
testonly = True,
data = glob(["test/process_model_directives_test/**"]),
visibility = [
"//visibility:public",
],
)

drake_cc_googletest(
Expand Down

0 comments on commit 9c10b90

Please sign in to comment.