Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bindings: Bind pydrake.multibody.Parser.AddModelFromString #13826

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)