diff --git a/include/osg/Node b/include/osg/Node index bda039fbd..f216483bd 100644 --- a/include/osg/Node +++ b/include/osg/Node @@ -177,10 +177,15 @@ class SG_EXPORT Node : public Object /** set the node's StateSet.*/ inline void setStateSet(osg::StateSet* dstate) { _dstate = dstate; } - /** return the node's StateSet.*/ + /** return the node's StateSet, if one does not already exist create it + * set the node and return the newly created StateSet. This ensures + * that a valid StateSet is always returned and can be used directly.*/ + osg::StateSet* getValidStateSet(); + + /** return the node's StateSet. returns NULL if a stateset is not attached.*/ inline osg::StateSet* getStateSet() { return _dstate.get(); } - /** return the node's const StateSet.*/ + /** return the node's const StateSet. returns NULL if a stateset is not attached.*/ inline const osg::StateSet* getStateSet() const { return _dstate.get(); } /** get the bounding sphere of node. diff --git a/src/osg/Node.cpp b/src/osg/Node.cpp index fa40fc6a8..b12e11982 100644 --- a/src/osg/Node.cpp +++ b/src/osg/Node.cpp @@ -68,6 +68,13 @@ void Node::ascend(NodeVisitor& nv) std::for_each(_parents.begin(),_parents.end(),NodeAcceptOp(nv)); } +osg::StateSet* Node::getValidStateSet() +{ + if (!_dstate) _dstate = osgNew StateSet; + return _dstate.get(); +} + + void Node::setAppCallback(NodeCallback* nc) { // if no changes just return.