Skip to content

Commit

Permalink
bindings: Bind pydrake.multibody.Parser.AddModelFromString
Browse files Browse the repository at this point in the history
  • Loading branch information
jwnimmer-tri committed Aug 5, 2020
1 parent f5a8d4b commit 38ba3f8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
5 changes: 4 additions & 1 deletion bindings/pydrake/multibody/parsing_py.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ PYBIND11_MODULE(parsing, m) {
.def("AddAllModelsFromFile", &Class::AddAllModelsFromFile,
py::arg("file_name"), cls_doc.AddAllModelsFromFile.doc)
.def("AddModelFromFile", &Class::AddModelFromFile, py::arg("file_name"),
py::arg("model_name") = "", cls_doc.AddModelFromFile.doc);
py::arg("model_name") = "", cls_doc.AddModelFromFile.doc)
.def("AddModelFromString", &Class::AddModelFromString,
py::arg("file_contents"), py::arg("file_type"),
py::arg("model_name") = "", cls_doc.AddModelFromString.doc);
}
}

Expand Down
19 changes: 16 additions & 3 deletions bindings/pydrake/multibody/test/parsing_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ def test_package_map(self):
dut.PopulateFromEnvironment('TEST_TMPDIR')
dut.PopulateFromFolder(tmpdir)

def test_parser(self):
# Calls every combination of arguments for the Parser methods and
# inspects their return type.
def test_parser_file(self):
"""Calls every combination of arguments for the Parser methods which
use a file_name (not contents) and inspects their return type.
"""
sdf_file = FindResourceOrThrow(
"drake/multibody/benchmarks/acrobot/acrobot.sdf")
urdf_file = FindResourceOrThrow(
Expand Down Expand Up @@ -69,3 +70,15 @@ def test_parser(self):
assert result_dim is list
self.assertIsInstance(result, list)
self.assertIsInstance(result[0], ModelInstanceIndex)

def test_parser_string(self):
"""Checks parsing from a string (not file_name)."""
sdf_file = FindResourceOrThrow(
"drake/multibody/benchmarks/acrobot/acrobot.sdf")
with open(sdf_file, "r") as f:
sdf_contents = f.read()
plant = MultibodyPlant(time_step=0.01)
parser = Parser(plant=plant)
result = parser.AddModelFromString(
file_contents=sdf_contents, file_type="sdf")
self.assertIsInstance(result, ModelInstanceIndex)

0 comments on commit 38ba3f8

Please sign in to comment.