DumpingNodeTree         Dumping a node tree to the log

From Kojack (original thread): Thought some of you might find this handy.
I'd wiki it, but never tried doing that before so I posted it here first. :-)

This a function which recursively traverses a node hierarchy and builds a string containing the names of every node and the names/types of every attached object, with indenting by recursive level.

void DumpNodes(std::stringstream &ss, Ogre::Node *n, int level)
   {
      for(int i = 0; i < level; i++)
      {
         ss << " ";
      }
      ss << "SceneNode: " << n->getName() << std::endl;

      Ogre::SceneNode::ObjectIterator object_it = ((Ogre::SceneNode *)n)->getAttachedObjectIterator();
      Ogre::Node::ChildNodeIterator node_it = n->getChildIterator();

      Ogre::MovableObject *m;
      while(object_it.hasMoreElements())
      {
         for(int i = 0; i < level + 2; i++)
         {
            ss << " ";
         }
         m = object_it.getNext();
         ss << m->getMovableType() << ": " << m->getName() << std::endl;
      }
      while(node_it.hasMoreElements())
      {
         DumpNodes(ss, node_it.getNext(), level + 2);
      }
   }

   std::string DumpNodes(Ogre::Node *n)
   {
      std::stringstream ss;
      ss << std::endl << "Node Hierarchy:" << std::endl;
      DumpNodes(ss, n, 0);
      return ss.str();
   }


The second DumpNodes is the one you call, the first one is just used by the second.
You can call it like this in your code:

LogManager::getSingleton().logMessage(Ogre::LML_NORMAL, QGF4::DumpNodes(m_scene_manager->getRootSceneNode()).c_str());


The result in the log will look like:

Node Hierarchy:
SceneNode: SceneRoot
  SceneNode: Physics Debug NX
    SimpleRenderable: SimpleRenderable2
    SimpleRenderable: SimpleRenderable3
  SceneNode: Physics Debug ODE
    SimpleRenderable: SimpleRenderable0
  SceneNode: Unnamed_10
    SimpleRenderable: SimpleRenderable4
  SceneNode: Unnamed_7
    Entity: ground
  SceneNode: Unnamed_9
    Entity: gir
  SceneNode: Unnamed_8
    Entity: box3


One of the reasons why I wrote this (mid last year or so) was so I could call it from a game console window, to see what the current scene was. But I never got around to making the console work (I had text input which was passed directly to Lua as source code, but no output). :-)

But I did end up using it with some of my students to debug stuff, like doing a node dump every time a certain key is pressed, then reading the log to see what changed each time.

From Falagard: Just thought I'd mention that in addition to this, the DotSceneInterface has a serializer that can serialize a scene to an xml file as well, which includes meshes, lights, etc.

Obviously, it's more complicated so might be overkill, and this one looks like a nice clean function for quick debugging.