From e90941a8b3bccc0d2c93f4a63ee940624c94123b Mon Sep 17 00:00:00 2001
From: scrawl <scrawl@baseoftrash.de>
Date: Mon, 30 Jan 2017 18:59:04 +0100
Subject: [PATCH] Wrap the scene template reference in another object for const
 correctness and to avoid it from being serialized with the osgDB serializer

---
 components/resource/scenemanager.cpp | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/components/resource/scenemanager.cpp b/components/resource/scenemanager.cpp
index e4fc459ac..cc719e231 100644
--- a/components/resource/scenemanager.cpp
+++ b/components/resource/scenemanager.cpp
@@ -413,13 +413,27 @@ namespace Resource
         return node;
     }
 
+    class TemplateRef : public osg::Object
+    {
+    public:
+        TemplateRef(const Object* object)
+            : mObject(object) {}
+        TemplateRef() {}
+        TemplateRef(const TemplateRef& copy, const osg::CopyOp&) : mObject(copy.mObject) {}
+
+        META_Object(Resource, TemplateRef)
+
+    private:
+        osg::ref_ptr<const Object> mObject;
+    };
+
     osg::ref_ptr<osg::Node> SceneManager::createInstance(const std::string& name)
     {
         osg::ref_ptr<const osg::Node> scene = getTemplate(name);
         osg::ref_ptr<osg::Node> cloned = osg::clone(scene.get(), SceneUtil::CopyOp());
 
         // add a ref to the original template, to hint to the cache that it's still being used and should be kept in cache
-        cloned->getOrCreateUserDataContainer()->addUserObject(const_cast<osg::Node*>(scene.get()));
+        cloned->getOrCreateUserDataContainer()->addUserObject(new TemplateRef(scene));
 
         return cloned;
     }