diff --git a/pandatool/src/xfile/xFileDataNodeTemplate.cxx b/pandatool/src/xfile/xFileDataNodeTemplate.cxx index 1fbf6cccc7..175d9530f6 100644 --- a/pandatool/src/xfile/xFileDataNodeTemplate.cxx +++ b/pandatool/src/xfile/xFileDataNodeTemplate.cxx @@ -247,5 +247,9 @@ get_element(int n) const { //////////////////////////////////////////////////////////////////// const XFileDataObject *XFileDataNodeTemplate:: get_element(const string &name) const { + int child_index = _template->find_child_index(name); + if (child_index >= 0) { + return get_element(child_index); + } return NULL; } diff --git a/pandatool/src/xfile/xFileNode.cxx b/pandatool/src/xfile/xFileNode.cxx index 7ee3c300dd..51920ef413 100644 --- a/pandatool/src/xfile/xFileNode.cxx +++ b/pandatool/src/xfile/xFileNode.cxx @@ -57,12 +57,29 @@ find_child(const string &name) const { ChildrenByName::const_iterator ni; ni = _children_by_name.find(name); if (ni != _children_by_name.end()) { - return (*ni).second; + return get_child((*ni).second); } return NULL; } +//////////////////////////////////////////////////////////////////// +// Function: XFileNode::find_child_index +// Access: Public +// Description: Returns the index number of the child with the +// indicated name, if any, or -1 if none. +//////////////////////////////////////////////////////////////////// +int XFileNode:: +find_child_index(const string &name) const { + ChildrenByName::const_iterator ni; + ni = _children_by_name.find(name); + if (ni != _children_by_name.end()) { + return (*ni).second; + } + + return -1; +} + //////////////////////////////////////////////////////////////////// // Function: XFileNode::find_descendent // Access: Public @@ -117,13 +134,13 @@ get_guid() const { //////////////////////////////////////////////////////////////////// void XFileNode:: add_child(XFileNode *node) { - _children.push_back(node); if (node->has_name()) { - _children_by_name[node->get_name()] = node; + _children_by_name[node->get_name()] = (int)_children.size(); } if (node->has_guid()) { _x_file->_nodes_by_guid[node->get_guid()] = node; } + _children.push_back(node); } //////////////////////////////////////////////////////////////////// diff --git a/pandatool/src/xfile/xFileNode.h b/pandatool/src/xfile/xFileNode.h index 1639daa1bb..6ec97a8de7 100644 --- a/pandatool/src/xfile/xFileNode.h +++ b/pandatool/src/xfile/xFileNode.h @@ -50,6 +50,7 @@ public: INLINE int get_num_children() const; INLINE XFileNode *get_child(int n) const; XFileNode *find_child(const string &name) const; + int find_child_index(const string &name) const; XFileNode *find_descendent(const string &name) const; virtual bool has_guid() const; @@ -73,7 +74,7 @@ protected: typedef pvector< PT(XFileNode) > Children; Children _children; - typedef pmap ChildrenByName; + typedef pmap ChildrenByName; ChildrenByName _children_by_name; public: