From 38ba3f83cbe6ee6c904f34b753a2ec9404681515 Mon Sep 17 00:00:00 2001 From: Jeremy Nimmer Date: Wed, 5 Aug 2020 17:47:08 -0400 Subject: [PATCH] bindings: Bind pydrake.multibody.Parser.AddModelFromString --- bindings/pydrake/multibody/parsing_py.cc | 5 ++++- .../pydrake/multibody/test/parsing_test.py | 19 ++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/bindings/pydrake/multibody/parsing_py.cc b/bindings/pydrake/multibody/parsing_py.cc index 1326c1ea6d8d..6b149ecf9233 100644 --- a/bindings/pydrake/multibody/parsing_py.cc +++ b/bindings/pydrake/multibody/parsing_py.cc @@ -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); } } diff --git a/bindings/pydrake/multibody/test/parsing_test.py b/bindings/pydrake/multibody/test/parsing_test.py index 6eaf356a70e6..204c62a75a3f 100644 --- a/bindings/pydrake/multibody/test/parsing_test.py +++ b/bindings/pydrake/multibody/test/parsing_test.py @@ -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( @@ -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)