diff --git a/VirtualRobot/SceneObject.cpp b/VirtualRobot/SceneObject.cpp index 985139f05564856dd814cd438a161eebfda1e50c..1c633f1c3ba7716f1f39a8af9932071968cbdd7e 100644 --- a/VirtualRobot/SceneObject.cpp +++ b/VirtualRobot/SceneObject.cpp @@ -579,6 +579,18 @@ bool SceneObject::hasChild( SceneObjectPtr child, bool recursive ) const return false; } +bool SceneObject::hasChild(const std::string &childName, bool recursive ) const +{ + for (size_t i=0;i<children.size();i++) + { + if (children[i]->getName() == childName) + return true; + if (recursive && children[i]->hasChild(childName,true)) + return true; + } + return false; +} + void SceneObject::detachChild( SceneObjectPtr child ) { VR_ASSERT(child); @@ -604,6 +616,11 @@ bool SceneObject::attachChild( SceneObjectPtr child ) VR_WARNING << " Trying to attach object that has already a parent: " << getName() << "->" << child->getName() << ", child's parent:" << child->getParent()->getName() << endl; return false; } + if (hasChild(child->getName())) + { + VR_ERROR << " Trying to attach object with name: " << child->getName() << " to " << getName() << ", but a child with same name is already present!" << endl; + return false; + } children.push_back(child); child->attached(shared_from_this()); return true; diff --git a/VirtualRobot/SceneObject.h b/VirtualRobot/SceneObject.h index 248138d7e2dbe710d1fd69b71f01ca4c22ad7b65..38af66e6bafe98aede3732566518523195ae8608 100644 --- a/VirtualRobot/SceneObject.h +++ b/VirtualRobot/SceneObject.h @@ -332,6 +332,11 @@ public: */ virtual bool hasChild(SceneObjectPtr child, bool recursive = false) const; + /*! + \return true, if child is attached + */ + virtual bool hasChild(const std::string &childName, bool recursive = false) const; + /*! \return true, if this object is attached to another object. */