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

Adds drake package for model URI resolution #14363

Closed
Closed
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
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ exports_files([
".bazelproject",
".clang-format",
".drake-find_resource-sentinel",
"package.xml",
])

# Drake's top-level module; all drake_py_stuff rules add this to deps.
Expand Down
2 changes: 2 additions & 0 deletions bindings/pydrake/multibody/parsing_py.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ PYBIND11_MODULE(parsing, m) {
py::arg("package_path"), cls_doc.Add.doc)
.def("Contains", &Class::Contains, py::arg("package_name"),
cls_doc.Contains.doc)
.def("Remove", &Class::Remove, py::arg("package_name"),
cls_doc.Remove.doc)
.def("size", &Class::size, cls_doc.size.doc)
.def("GetPath", &Class::GetPath, py::arg("package_name"),
cls_doc.GetPath.doc)
Expand Down
5 changes: 4 additions & 1 deletion bindings/pydrake/multibody/test/parsing_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ def test_package_map(self):
model = FindResourceOrThrow(
"drake/examples/atlas/urdf/atlas_minimal_contact.urdf")

# Simple coverage test for Add, Contains, size, GetPath, AddPackageXml.
# Simple coverage test for Remove, Add, Contains, size, GetPath,
# AddPackageXml.
dut.Remove('drake')
self.assertEqual(dut.size(), 0)
dut.Add(package_name="root", package_path=tmpdir)
self.assertEqual(dut.size(), 1)
self.assertTrue(dut.Contains(package_name="root"))
Expand Down
18 changes: 18 additions & 0 deletions multibody/parsing/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ drake_cc_library(
hdrs = [
"package_map.h",
],
data = [
"//:package.xml",
],
visibility = [
"//visibility:public",
],
Expand Down Expand Up @@ -379,6 +382,21 @@ drake_cc_googletest(
],
)

drake_cc_googletest(
name = "drake_pkg_uri_test",
data = [
":test_models",
],
deps = [
"//common:find_resource",
"//geometry:drake_visualizer",
"//geometry:scene_graph",
"//multibody/parsing",
"//systems/analysis:simulator",
"//systems/framework:diagram_builder",
],
)

drake_cc_googletest(
name = "detail_path_utils_test",
data = [
Expand Down
6 changes: 5 additions & 1 deletion multibody/parsing/package_map.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "drake/common/drake_path.h"
#include "drake/common/drake_throw.h"
#include "drake/common/filesystem.h"
#include "drake/common/find_resource.h"
#include "drake/common/text_logging.h"

namespace drake {
Expand All @@ -22,7 +23,10 @@ using std::string;
using tinyxml2::XMLDocument;
using tinyxml2::XMLElement;

PackageMap::PackageMap() {}
PackageMap::PackageMap() {
std::string drake_pkg = FindResourceOrThrow("drake/package.xml");
AddPackageXml(drake_pkg);
}

void PackageMap::Add(const string& package_name, const string& package_path) {
DRAKE_THROW_UNLESS(map_.count(package_name) == 0);
Expand Down
92 changes: 92 additions & 0 deletions multibody/parsing/test/drake_pkg_uri_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
@file
Parses and visualizes sdf/urdf.

If you wish to visualize this mesh, in its own terminal run:

$ bazel-bin/tools/drake_visualizer

In a new terminal, run example to visualize model:

$ bazel run \
//multibody/parsing/test:drake_pkg_uri_test -- \
--visualize=true --visualize_sec=1

*/

#include <chrono>
#include <string>
#include <thread>

#include <fmt/format.h>
#include <gflags/gflags.h>
#include <gtest/gtest.h>

#include "drake/common/find_resource.h"
#include "drake/geometry/drake_visualizer.h"
#include "drake/geometry/scene_graph.h"
#include "drake/multibody/parsing/parser.h"
#include "drake/systems/analysis/simulator.h"
#include "drake/systems/framework/diagram_builder.h"

DEFINE_bool(
visualize, false, "Publish model to Drake Visualizer.");
DEFINE_double(
visualize_sec, 0., "Amount of time to pause between each model to "
"visualize in Drake Visualizer.");

namespace drake {
namespace multibody {
namespace {

using geometry::DrakeVisualizer;
using geometry::SceneGraph;
using multibody::AddMultibodyPlantSceneGraph;
using multibody::Parser;
using systems::DiagramBuilder;
using systems::Simulator;

class ParseTest : public testing::TestWithParam<std::string> {};

TEST_P(ParseTest, ParsesSdfAndVisualizes) {
const std::string object_name = GetParam();
const std::string filename = FindResourceOrThrow(fmt::format(
"drake/multibody/parsing/test/drake_pkg_uri_test/{}",
object_name));

DiagramBuilder<double> builder;
auto [plant, scene_graph] = AddMultibodyPlantSceneGraph(&builder, 0.0);
// Record the default number of models before a new model is added.
const int default_num_models = plant.num_model_instances();

// Check to ensure model is parsable.
EXPECT_NO_THROW(Parser(&plant).AddModelFromFile(filename));

// Ensure there was exactly one model instance added for the new model.
EXPECT_EQ(plant.num_model_instances() - default_num_models, 1);

// Visualize via publishing, if requested.
if (FLAGS_visualize || FLAGS_visualize_sec > 0.0) {
DrakeVisualizer::AddToBuilder(&builder, scene_graph);
plant.Finalize();
auto diagram = builder.Build();
drake::log()->info("Visualize: {}", object_name);
Simulator<double> simulator(*diagram);
simulator.Initialize();
diagram->Publish(simulator.get_context());
if (FLAGS_visualize_sec > 0.0) {
drake::log()->info("Pausing for {} sec...", FLAGS_visualize_sec);
std::this_thread::sleep_for(
std::chrono::milliseconds(
static_cast<int>(1000 * FLAGS_visualize_sec)));
}
}
}


INSTANTIATE_TEST_SUITE_P(DRAKE_URI_MODEL, ParseTest,
testing::Values("cube_visual.sdf", "cube_visual.urdf"));

} // namespace
} // namespace multibody
} // namespace drake
18 changes: 18 additions & 0 deletions multibody/parsing/test/drake_pkg_uri_test/cube_visual.sdf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<sdf version="1.9">
<model name="cube_robot">
<link name="link0">
<visual name="cube">
<geometry>
<mesh>
<uri>package://drake/multibody/parsing/test/tri_cube.obj</uri>
</mesh>
</geometry>
</visual>
</link>
<joint name="weld_link0" type="fixed">
<parent>world</parent>
<child>link0</child>
</joint>
</model>
</sdf>
13 changes: 13 additions & 0 deletions multibody/parsing/test/drake_pkg_uri_test/cube_visual.urdf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0"?>
<robot name="cube_robot">
<link name="link0">
<inertial>
<mass value="1"/>
</inertial>
<visual name="cube">
<geometry>
<mesh filename="package://drake/multibody/parsing/test/tri_cube.obj"/>
</geometry>
</visual>
</link>
</robot>
8 changes: 8 additions & 0 deletions multibody/parsing/test/package_map_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ GTEST_TEST(PackageMapTest, TestManualPopulation) {
};

PackageMap package_map;
package_map.Remove("drake");

for (const auto& it : expected_packages) {
package_map.Add(it.first, it.second);
}
Expand All @@ -95,6 +97,7 @@ GTEST_TEST(PackageMapTest, TestPopulateFromXml) {
const string xml_dirname =
filesystem::path(xml_filename).parent_path().string();
PackageMap package_map;
package_map.Remove("drake");
package_map.AddPackageXml(xml_filename);

map<string, string> expected_packages = {
Expand All @@ -107,6 +110,7 @@ GTEST_TEST(PackageMapTest, TestPopulateFromXml) {
GTEST_TEST(PackageMapTest, TestPopulateMapFromFolder) {
const string root_path = GetTestDataRoot();
PackageMap package_map;
package_map.Remove("drake");
package_map.PopulateFromFolder(root_path);
VerifyMatchWithTestDataRoot(package_map);
}
Expand All @@ -116,6 +120,7 @@ GTEST_TEST(PackageMapTest, TestPopulateMapFromFolder) {
GTEST_TEST(PackageMapTest, TestPopulateMapFromFolderExtraTrailingSlashes) {
const string root_path = GetTestDataRoot();
PackageMap package_map;
package_map.Remove("drake");
package_map.PopulateFromFolder(root_path + "///////");
VerifyMatchWithTestDataRoot(package_map);
}
Expand All @@ -129,6 +134,7 @@ GTEST_TEST(PackageMapTest, TestPopulateUpstreamToDrake) {
"sdf/test_model.sdf");

PackageMap package_map;
package_map.Remove("drake");
package_map.PopulateUpstreamToDrake(sdf_file_name);

map<string, string> expected_packages = {
Expand All @@ -146,6 +152,7 @@ GTEST_TEST(PackageMapTest, TestPopulateUpstreamToDrake) {
// Tests that PackageMap can be populated from an env var.
GTEST_TEST(PackageMapTest, TestPopulateFromEnvironment) {
PackageMap package_map;
package_map.Remove("drake");

// Test a null environment.
package_map.PopulateFromEnvironment("FOOBAR");
Expand Down Expand Up @@ -176,6 +183,7 @@ GTEST_TEST(PackageMapTest, TestStreamingToString) {
};

PackageMap package_map;
package_map.Remove("drake");
for (const auto& it : expected_packages) {
package_map.Add(it.first, it.second);
}
Expand Down
4 changes: 2 additions & 2 deletions multibody/parsing/test/parser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,14 @@ GTEST_TEST(FileParserTest, FindDrakePackageWhenAdding) {
MultibodyPlant<double> plant(0.0);
geometry::SceneGraph<double> scene_graph;
Parser parser(&plant, &scene_graph);
EXPECT_EQ(parser.package_map().size(), 0);
const int orig_package_size = parser.package_map().size();

// Because the box.sdf references an obj via a package: URI, this would
// throw if the package were not found.
EXPECT_NO_THROW(add_func(FindResourceOrThrow(file_name), &parser));

// Now we explicitly confirm the package map has been modified.
EXPECT_EQ(parser.package_map().size(), 1);
EXPECT_EQ(parser.package_map().size(), orig_package_size+1);
EXPECT_TRUE(parser.package_map().Contains("box_model"));
}
}
Expand Down
8 changes: 8 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<package format="2">
<name>drake</name>
<description>
Model-Based Design and Verification for Robotics.
</description>
<maintainer>Drake Developers</maintainer>
<license>BSD-3-Clause</license>
</package>