diff --git a/include/sdf/Root.hh b/include/sdf/Root.hh index 1d88defcc..7a286e044 100644 --- a/include/sdf/Root.hh +++ b/include/sdf/Root.hh @@ -235,6 +235,11 @@ namespace sdf public: sdf::ElementPtr ToElement( const OutputConfig &_config = OutputConfig::GlobalConfig()) const; + /// \brief Remove the actor, light, or model if one of them exists. + /// The SDF Root object can only hold one, or none, from the set + /// [Actor, Light, Model]. + public: void ClearActorLightModel(); + /// \brief Private data pointer GZ_UTILS_IMPL_PTR(dataPtr) }; diff --git a/python/src/sdf/pyRoot.cc b/python/src/sdf/pyRoot.cc index 42c21907a..a5eef468c 100644 --- a/python/src/sdf/pyRoot.cc +++ b/python/src/sdf/pyRoot.cc @@ -94,6 +94,10 @@ void defineRoot(pybind11::object module) .def("set_light", &sdf::Root::SetLight, "Set the light object. This will override any existing model, " "actor, and light object.") + .def("clear_actor_light_model", &sdf::Root::ClearActorLightModel, + "Remove the actor, light, or model if one of them exists." + "The SDF Root object can only hold one, or none, from the set" + "[Actor, Light, Model].") .def("add_world", [](Root &self, const World &_world) { ThrowIfErrors(self.AddWorld(_world)); diff --git a/python/test/pyRoot_TEST.py b/python/test/pyRoot_TEST.py index 0df1d3023..b4cbe08e1 100644 --- a/python/test/pyRoot_TEST.py +++ b/python/test/pyRoot_TEST.py @@ -324,6 +324,33 @@ def test_element_to_light(self): # ASSERT_EQ(None, root2.Actor()) self.assertEqual(0, root2.world_count()) + def test_clear_actor_light_model(self): + root = Root() + + # \TODO(anyone) Wrap the Actor class. + # self.assertEqual(None, root.actor()) + # actor1 = Actor() + # actor1.set_name("actor1") + # root.set_actor(actor1) + # self.assertNotEqual(None, root.actor()) + # root.clear_actor_light_model() + # self.assertEqual(None, root.actor()) + + self.assertEqual(None, root.light()) + light1 = Light() + light1.set_name("light1") + root.set_light(light1) + self.assertNotEqual(None, root.light()) + root.clear_actor_light_model() + self.assertEqual(None, root.light()) + + self.assertEqual(None, root.model()) + model1 = Model() + model1.set_name("model1") + root.set_model(model1) + self.assertNotEqual(None, root.model()) + root.clear_actor_light_model() + self.assertEqual(None, root.model()) if __name__ == '__main__': unittest.main() diff --git a/src/Root.cc b/src/Root.cc index 337323459..ad858e410 100644 --- a/src/Root.cc +++ b/src/Root.cc @@ -616,3 +616,9 @@ sdf::ElementPtr Root::ToElement(const OutputConfig &_config) const return elem; } + +///////////////////////////////////////////////// +void Root::ClearActorLightModel() +{ + this->dataPtr->modelLightOrActor = std::monostate{}; +} diff --git a/src/Root_TEST.cc b/src/Root_TEST.cc index fd169a7c3..e413d2eb2 100644 --- a/src/Root_TEST.cc +++ b/src/Root_TEST.cc @@ -630,3 +630,33 @@ TEST(DOMRoot, WorldByName) ASSERT_TRUE(root.WorldNameExists("world2")); EXPECT_EQ("world2", root.WorldByName("world2")->Name()); } + +///////////////////////////////////////////////// +TEST(DOMRoot, ClearActorLightModel) +{ + sdf::Root root; + EXPECT_EQ(nullptr, root.Actor()); + EXPECT_EQ(nullptr, root.Light()); + EXPECT_EQ(nullptr, root.Model()); + + sdf::Actor actor1; + actor1.SetName("actor1"); + root.SetActor(actor1); + EXPECT_NE(nullptr, root.Actor()); + root.ClearActorLightModel(); + EXPECT_EQ(nullptr, root.Actor()); + + sdf::Light light1; + light1.SetName("light1"); + root.SetLight(light1); + EXPECT_NE(nullptr, root.Light()); + root.ClearActorLightModel(); + EXPECT_EQ(nullptr, root.Light()); + + sdf::Model model1; + model1.SetName("model1"); + root.SetModel(model1); + EXPECT_NE(nullptr, root.Model()); + root.ClearActorLightModel(); + EXPECT_EQ(nullptr, root.Model()); +}