gazebo-classic
gazebo-classic copied to clipboard
Toggle the GUI visualization of light frustrums / green lines
Original report (archived issue) by Andrew Symington (Bitbucket: asymingt).
I would like to be able to turn off the visualization of lights in the GUI, as I have a green laser plugin for my model that looks confusingly like the lines used to visualize all directional/spot lights! The method would be similar to the <visualize>
child of <sensor>
in SDF.
Perhaps something along these lines:
<light type="...">
<visualize>false</visualize>
...
</light>
In addition, since one cannot attach a VisualPlugin
to a light visual, it would be extremely useful to set visibility flags in the msgs::Light
, so that one can programatically create / update light visibility.
Original comment by Steve Peters (Bitbucket: Steven Peters, GitHub: scpeters).
We already have ways to toggle visualization of the Grid, Origin, Contacts, Link Frames, Joints, etc. I think we could add this for lights as well.
Original comment by Steve Peters (Bitbucket: Steven Peters, GitHub: scpeters).
- changed component from "rendering" to "gui"
Original comment by Steve Peters (Bitbucket: Steven Peters, GitHub: scpeters).
We added Link Frame visualizations to the GUI in pull request #1762, which may provide an example for some of the changes to be made, for example adding a new bool
to ScenePrivate.hh for showLights
. Since the visual already exists, it should be easier to solve this problem, just adding the enable/disable visualization logic.
Original comment by Ian Chen (Bitbucket: Ian Chen, GitHub: iche033).
Issue #2508 was marked as a duplicate of this issue.
With #3011, is there anything exposed to the end user to toggle light visualization (e.g. from the CLI, GUI or within URDF)? Thanks!
not that I know of. In the end, we wrote a plugin that loops through the lights in the scene and call ShowVisual(false)
to turn them off.
@iche033 Would you be able to point me to this plugin you wrote that disables the light visual? I'm attempting to do something similar. Thanks!
looks like that particular branch with the plugin was lost when migrating a repo from bitbucket to github. So I'll just post the code here. I took out some include headers related to other projects so there maybe additional gazebo headers you'll need to add to the code to build it.
LightVisualPlugin.cc
/*
* Copyright (C) 2018 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <map>
#include <gazebo/common/Assert.hh>
#include <gazebo/common/Events.hh>
#include "LightVisualPlugin.hh"
namespace gazebo
{
/// \internal
/// \brief Private data for the Light class
class LightVisualPluginPrivate
{
/// \brief Map of light names to visualize flag
public: std::map<std::string, bool> lights;
/// \brief PreRender connection
public: event::ConnectionPtr preRenderCon;
/// \brief PreRender connection
public: rendering::ScenePtr scene;
};
}
using namespace gazebo;
GZ_REGISTER_VISUAL_PLUGIN(LightVisualPlugin)
/////////////////////////////////////////////////
LightVisualPlugin::LightVisualPlugin()
: dataPtr(new LightVisualPluginPrivate)
{
}
/////////////////////////////////////////////////
void LightVisualPlugin::Load(rendering::VisualPtr _parent, sdf::ElementPtr _sdf)
{
GZ_ASSERT(_parent, "LightVisualPlugin parent pointer is NULL");
this->dataPtr->scene = _parent->GetScene();
rendering::VisualPtr linkVis = _parent->GetParent();
if (_sdf->HasElement("light"))
{
auto sdfLight = _sdf->GetElement("light");
while (sdfLight)
{
if (sdfLight->HasElement("id") && sdfLight->HasElement("visualize"))
{
std::string lightId = sdfLight->Get<std::string>("id");
size_t pos = 0;
while ((pos = lightId.find("/", pos)) != std::string::npos)
{
lightId = lightId.replace(pos, 1, "::");
pos += 2;
}
bool visualize = sdfLight->Get<bool>("visualize");
std::string lightName = linkVis->Name() + "::" + lightId;
this->dataPtr->lights[lightName] = visualize;
}
sdfLight = sdfLight->GetNextElement("light");
}
}
if (!this->dataPtr->lights.empty())
{
this->dataPtr->preRenderCon =
event::Events::ConnectPreRender(
std::bind(&LightVisualPlugin::PreRender, this));
}
}
/////////////////////////////////////////////////
void LightVisualPlugin::PreRender()
{
for (auto lightIt = this->dataPtr->lights.begin();
lightIt != this->dataPtr->lights.end();)
{
rendering::LightPtr light =
this->dataPtr->scene->LightByName(lightIt->first);
if (light)
{
light->ShowVisual(lightIt->second);
this->dataPtr->lights.erase(lightIt++);
}
else
{
++lightIt;
}
}
if (this->dataPtr->lights.empty())
{
this->dataPtr->preRenderCon.reset();
return;
}
}
LightVisualPlugin.hh
/*
* Copyright (C) 2018 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef GAZEBO_LIGHTVISUALPLUGIN_HH_
#define GAZEBO_LIGHTVISUALPLUGIN_HH_
#include <memory>
#include <gazebo/rendering/rendering.hh>
#include <gazebo/common/Plugin.hh>
#include <sdf/sdf.hh>
namespace gazebo
{
// forward declaration
class LightVisualPluginPrivate;
/// \brief A plugin that toggles light visuals
class LightVisualPlugin : public VisualPlugin
{
/// \brief Constructor
public: LightVisualPlugin();
// Documentation inherited
public: virtual void Load(rendering::VisualPtr _parent,
sdf::ElementPtr _sdf);
/// \brief PreRender event callback
public: virtual void PreRender();
/// \internal
/// \brief Pointer to private data
private: std::unique_ptr<LightVisualPluginPrivate> dataPtr;
};
}
#endif
If I remember correctly, the usage is:
<plugin ....>
<light>
<id>some_light_name</id>
<visualize>false</visualize>
</light>
<light>
...
</light>
</plugin>