Skip to content

Commit

Permalink
MeshcatVisualizer sets visibility in constructor
Browse files Browse the repository at this point in the history
Previously, the visibility parameter was not respected (a) before the
initialization event or (b) if delete_on_initialization_event was
false.
  • Loading branch information
RussTedrake committed Jun 12, 2023
1 parent 37d6c59 commit 686ff02
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
2 changes: 2 additions & 0 deletions geometry/meshcat_visualizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ MeshcatVisualizer<T>::MeshcatVisualizer(std::shared_ptr<Meshcat> meshcat,
this->DeclareAbstractInputPort("query_object", Value<QueryObject<T>>())
.get_index();

meshcat_->SetProperty(params_.prefix, "visible", params_.visible_by_default);

if (params_.enable_alpha_slider) {
meshcat_->AddSlider(
alpha_slider_name_, 0.02, 1.0, 0.02, alpha_value_);
Expand Down
32 changes: 19 additions & 13 deletions geometry/test/meshcat_visualizer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ class MeshcatVisualizerWithIiwaTest : public ::testing::Test {
context_ = diagram_->CreateDefaultContext();
}

void CheckVisible(const std::string& path, bool visibility) {
ASSERT_TRUE(meshcat_->HasPath(path));
const std::string property =
meshcat_->GetPackedProperty(path, "visible");
msgpack::object_handle oh =
msgpack::unpack(property.data(), property.size());
auto data = oh.get().as<internal::SetPropertyData<bool>>();
EXPECT_EQ(data.property, "visible");
EXPECT_EQ(data.value, visibility);
}

std::shared_ptr<Meshcat> meshcat_;
multibody::MultibodyPlant<double>* plant_{};
SceneGraph<double>* scene_graph_{};
Expand Down Expand Up @@ -89,6 +100,8 @@ TEST_F(MeshcatVisualizerWithIiwaTest, BasicTest) {
simulator.AdvanceTo(0.1);
EXPECT_NE(meshcat_->GetPackedTransform("visualizer/iiwa14/iiwa_link_7"),
packed_X_W7);

CheckVisible("/drake/visualizer", true);
}

TEST_F(MeshcatVisualizerWithIiwaTest, PublishPeriod) {
Expand Down Expand Up @@ -168,26 +181,15 @@ TEST_F(MeshcatVisualizerWithIiwaTest, NotVisibleByDefault) {

// Create the diagram and publish both the initialization and periodic event.
SetUpDiagram(params);
{
auto events = diagram_->AllocateCompositeEventCollection();
diagram_->GetInitializationEvents(*context_, events.get());
diagram_->Publish(*context_, events->get_publish_events());
diagram_->ForcedPublish(*context_);
}

// Confirm that the path was added but was set to be invisible.
ASSERT_TRUE(meshcat_->HasPath("/drake/visualizer"));
const std::string property =
meshcat_->GetPackedProperty("/drake/visualizer", "visible");
msgpack::object_handle oh = msgpack::unpack(property.data(), property.size());
auto data = oh.get().as<internal::SetPropertyData<bool>>();
EXPECT_EQ(data.property, "visible");
EXPECT_EQ(data.value, false);
CheckVisible("/drake/visualizer", false);
}

TEST_F(MeshcatVisualizerWithIiwaTest, DeletePrefixOnInitialization) {
MeshcatVisualizerParams params;
params.delete_on_initialization_event = true;
params.visible_by_default = false;
SetUpDiagram(params);
// Scribble a transform onto the scene tree beneath the visualizer prefix.
meshcat_->SetTransform("/drake/visualizer/my_random_path",
Expand All @@ -201,10 +203,13 @@ TEST_F(MeshcatVisualizerWithIiwaTest, DeletePrefixOnInitialization) {
}
// Confirm that my scribble was deleted.
EXPECT_FALSE(meshcat_->HasPath("/drake/visualizer/my_random_path"));
// Confirm that the path that was re-added is again set to be invisible.
CheckVisible("/drake/visualizer", false);

// Repeat, but this time with delete prefix disabled.
params.delete_on_initialization_event = false;
SetUpDiagram(params);
CheckVisible("/drake/visualizer", false);
meshcat_->SetTransform("/drake/visualizer/my_random_path",
math::RigidTransformd());
{ // Send an initialization event.
Expand All @@ -214,6 +219,7 @@ TEST_F(MeshcatVisualizerWithIiwaTest, DeletePrefixOnInitialization) {
}
// Confirm that my scribble remains.
EXPECT_TRUE(meshcat_->HasPath("/drake/visualizer/my_random_path"));
CheckVisible("/drake/visualizer", false);
}

TEST_F(MeshcatVisualizerWithIiwaTest, Delete) {
Expand Down

0 comments on commit 686ff02

Please sign in to comment.