[osg-users] Update node color on demand

Eran Cohen ceranco at gmail.com
Sat Nov 24 22:51:03 PST 2018

Hi Diego,

You can pass user events to the viewer (and thus to its Event Handlers):


// This struct will be passed to the event handler with the relevant parameters (for example, the node you want to affect and the color to change it to)
struct ChangeColorEvent : public osg::Referenced
  ChangeColorEvent(float r, float g, float b, osg::Node* node)
    this->r = r;
    this->g = g;
    this->b = b;
    this->node = node;

  float r;
  float g;
  float b;
  osg::Node* node;

// When you want to call the event (on a button click in QT for example)
viewer->getEventQueue()->userEvent(new ChangeColorEvent{ 1.0, 0.3, 0.4, node }, 0);

To handle said event in your EventHandler:


class ColorHandler : public osgGA::GUIEventHandler 
   virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) override 
      if (ea.getEventType() == ea.USER) 
         auto changeColorEvent = dynamic_cast<const ChangeColorEvent*>(ea.getUserData());
         if (changeColorEvent != nullptr)
           // do whatever you want here, for example run the visitor on the node
           return true;
      return false;


Read this topic online here:

More information about the osg-users mailing list