diff --git a/source/JsMaterialX/JsMaterialXCore/JsElement.cpp b/source/JsMaterialX/JsMaterialXCore/JsElement.cpp index c912d036c1..48a1f37248 100644 --- a/source/JsMaterialX/JsMaterialXCore/JsElement.cpp +++ b/source/JsMaterialX/JsMaterialXCore/JsElement.cpp @@ -24,7 +24,7 @@ namespace mx = MaterialX; #define BIND_ELEMENT_CHILD_FUNC_INSTANCE(NAME, T) \ BIND_MEMBER_FUNC("addChild" #NAME, mx::Element, addChild, 0, 1, stRef) \ .function("getChildOfType" #NAME, &mx::Element::getChildOfType) \ - BIND_MEMBER_FUNC("getChildrenOfType" #NAME, mx::Element, getChildrenOfType, 0, 1, stRef) \ + BIND_MEMBER_FUNC("getChildrenOfType" #NAME, mx::Element, getChildrenOfType, 0, 2, stRef) \ .function("removeChildOfType" #NAME, &mx::Element::removeChildOfType) \ BIND_MEMBER_FUNC("isA" #NAME, mx::Element, isA, 0, 1, stRef) \ .function("asA" #NAME, ems::select_overload()>(&mx::Element::asA)) diff --git a/source/MaterialXCore/Document.h b/source/MaterialXCore/Document.h index 90927c9440..9779c06c2e 100644 --- a/source/MaterialXCore/Document.h +++ b/source/MaterialXCore/Document.h @@ -79,13 +79,13 @@ class MX_CORE_API Document : public GraphElement /// Return the NodeGraph, if any, with the given name. NodeGraphPtr getNodeGraph(const string& name) const { - return getChildOfTypeWithLibrary(getDataLibrary(), name); + return getChildOfType(name, getDataLibrary()); } /// Return a vector of all NodeGraph elements in the document. vector getNodeGraphs() const { - return getChildrenOfTypeWithLibrary(getDataLibrary()); + return getChildrenOfType(EMPTY_STRING, getDataLibrary()); } /// Remove the NodeGraph, if any, with the given name. @@ -346,13 +346,13 @@ class MX_CORE_API Document : public GraphElement /// Return the NodeDef, if any, with the given name. NodeDefPtr getNodeDef(const string& name) const { - return getChildOfTypeWithLibrary(getDataLibrary(), name); + return getChildOfType(name, getDataLibrary()); } /// Return a vector of all NodeDef elements in the document. vector getNodeDefs() const { - return getChildrenOfTypeWithLibrary(getDataLibrary()); + return getChildrenOfType(EMPTY_STRING, getDataLibrary()); } /// Remove the NodeDef, if any, with the given name. @@ -381,13 +381,13 @@ class MX_CORE_API Document : public GraphElement /// Return the AttributeDef, if any, with the given name. AttributeDefPtr getAttributeDef(const string& name) const { - return getChildOfTypeWithLibrary(getDataLibrary(), name); + return getChildOfType(name, getDataLibrary()); } /// Return a vector of all AttributeDef elements in the document. vector getAttributeDefs() const { - return getChildrenOfTypeWithLibrary(getDataLibrary()); + return getChildrenOfType(EMPTY_STRING, getDataLibrary()); } /// Remove the AttributeDef, if any, with the given name. @@ -413,13 +413,13 @@ class MX_CORE_API Document : public GraphElement /// Return the AttributeDef, if any, with the given name. TargetDefPtr getTargetDef(const string& name) const { - return getChildOfTypeWithLibrary(getDataLibrary(), name); + return getChildOfType(name, getDataLibrary()); } /// Return a vector of all TargetDef elements in the document. vector getTargetDefs() const { - return getChildrenOfTypeWithLibrary(getDataLibrary()); + return getChildrenOfType(EMPTY_STRING, getDataLibrary()); } /// Remove the TargetDef, if any, with the given name. @@ -509,13 +509,13 @@ class MX_CORE_API Document : public GraphElement /// Return the Implementation, if any, with the given name. ImplementationPtr getImplementation(const string& name) const { - return getChildOfTypeWithLibrary(getDataLibrary(), name); + return getChildOfType(name, getDataLibrary()); } /// Return a vector of all Implementation elements in the document. vector getImplementations() const { - return getChildrenOfTypeWithLibrary(getDataLibrary()); + return getChildrenOfType(EMPTY_STRING, getDataLibrary()); } /// Remove the Implementation, if any, with the given name. @@ -545,13 +545,13 @@ class MX_CORE_API Document : public GraphElement /// Return the UnitDef, if any, with the given name. UnitDefPtr getUnitDef(const string& name) const { - return getChildOfTypeWithLibrary(getDataLibrary(), name); + return getChildOfType(name, getDataLibrary()); } /// Return a vector of all Member elements in the TypeDef. vector getUnitDefs() const { - return getChildrenOfTypeWithLibrary(getDataLibrary()); + return getChildrenOfType(EMPTY_STRING, getDataLibrary()); } /// Remove the UnitDef, if any, with the given name. @@ -576,13 +576,13 @@ class MX_CORE_API Document : public GraphElement /// Return the UnitTypeDef, if any, with the given name. UnitTypeDefPtr getUnitTypeDef(const string& name) const { - return getChildOfTypeWithLibrary(getDataLibrary(), name); + return getChildOfType(name, getDataLibrary()); } /// Return a vector of all UnitTypeDef elements in the document. vector getUnitTypeDefs() const { - return getChildrenOfTypeWithLibrary(getDataLibrary()); + return getChildrenOfType(EMPTY_STRING, getDataLibrary()); } /// Remove the UnitTypeDef, if any, with the given name. diff --git a/source/MaterialXCore/Element.h b/source/MaterialXCore/Element.h index efef97011a..b8460b12a2 100644 --- a/source/MaterialXCore/Element.h +++ b/source/MaterialXCore/Element.h @@ -436,21 +436,12 @@ class MX_CORE_API Element : public std::enable_shared_from_this return (it != _childMap.end()) ? it->second : ElementPtr(); } - /// Return the child element, if any, with the given name and subclass. + /// Return the child element from data library or content, if any, with the given name and subclass. /// If a child with the given name exists, but belongs to a different /// subclass, then an empty shared pointer is returned. - template shared_ptr getChildOfType(const string& name) const + template shared_ptr getChildOfType(const string& name, ConstElementPtr dataLibrary = nullptr) const { - ElementPtr child = getChild(name); - return child ? child->asA() : shared_ptr(); - } - - /// Return the child element from data library , if any, with the given name and subclass. - /// If a child with the given name exists, but belongs to a different - /// subclass, then an empty shared pointer is returned. - template shared_ptr getChildOfTypeWithLibrary(ConstElementPtr datalibrary, const string& name) const - { - ElementPtr child = datalibrary ? datalibrary->getChild(name) : nullptr; + ElementPtr child = dataLibrary ? dataLibrary->getChild(name) : nullptr; if (!child) { child = getChild(name); @@ -458,7 +449,6 @@ class MX_CORE_API Element : public std::enable_shared_from_this return child ? child->asA() : shared_ptr(); } - /// Return a constant vector of all child elements. /// The returned vector maintains the order in which children were added. const vector& getChildren() const @@ -469,9 +459,9 @@ class MX_CORE_API Element : public std::enable_shared_from_this /// Return a vector of all child elements that are instances of the given /// subclass, optionally filtered by the given category string. The returned /// vector maintains the order in which children were added. - template vector> getChildrenOfType(const string& category = EMPTY_STRING) const + template vector> getChildrenOfType(const string& category = EMPTY_STRING, ConstElementPtr dataLibrary = nullptr) const { - vector> children; + vector> children = dataLibrary ? dataLibrary->getChildrenOfType(category) : vector>(); for (ElementPtr child : _childOrder) { shared_ptr instance = child->asA(); @@ -484,17 +474,6 @@ class MX_CORE_API Element : public std::enable_shared_from_this return children; } - /// Return a combined vector of all child elements including the Data Library that are instances of the given - /// subclass, optionally filtered by the given category string. The returned - /// vector maintains the order in which children were added. - template vector> getChildrenOfTypeWithLibrary(ConstElementPtr datalibrary, const string& category = EMPTY_STRING) const - { - vector> libraryChildren = datalibrary ? datalibrary->getChildrenOfType(category) : vector>(); - vector> children = getChildrenOfType(category); - libraryChildren.insert(libraryChildren.end(), children.begin(), children.end()); - return libraryChildren; - } - /// Set the index of the child, if any, with the given name. /// If the given index is out of bounds, then an exception is thrown. void setChildIndex(const string& name, int index); @@ -839,27 +818,18 @@ class MX_CORE_API Element : public std::enable_shared_from_this /// @} protected: - // Resolve a reference to a named element at the scope of the given parent, + // Resolve a reference to a named element at the scope of the given datalibrary and parent, // taking the namespace at the scope of this element into account. If no parent // is provided, then the root scope of the document is used. - template shared_ptr resolveNameReference(const string& name, ConstElementPtr parent = nullptr) const - { - ConstElementPtr scope = parent ? parent : getRoot(); - shared_ptr child = scope->getChildOfType(getQualifiedName(name)); - return child ? child : scope->getChildOfType(name); - } - - // Resolve a reference to a named element at the scope of the given datalibrary and parent - // taking the namespace at the scope of this element into account. If no datalibrary or parent - // is provided, then the root scope of the document is used. - template shared_ptr resolveNameReference(ConstElementPtr datalibrary, const string& name, ConstElementPtr parent = nullptr) const + template shared_ptr resolveNameReference(const string& name, ConstElementPtr parent = nullptr, ConstElementPtr datalibrary = nullptr) const { shared_ptr child = datalibrary ? datalibrary->getChildOfType(getQualifiedName(name)) : nullptr; - if (child) return child; - return resolveNameReference(name, parent); + ConstElementPtr scope = parent ? parent : getRoot(); + child = scope->getChildOfType(getQualifiedName(name)); + return child ? child : scope->getChildOfType(name); } // Enforce a requirement within a validate method, updating the validation diff --git a/source/MaterialXCore/Interface.cpp b/source/MaterialXCore/Interface.cpp index dc3138921a..1ccaadf08f 100644 --- a/source/MaterialXCore/Interface.cpp +++ b/source/MaterialXCore/Interface.cpp @@ -283,7 +283,7 @@ GeomPropDefPtr Input::getDefaultGeomProp() const if (!defaultGeomProp.empty()) { ConstDocumentPtr doc = getDocument(); - return doc->getChildOfTypeWithLibrary(doc->getDataLibrary(),defaultGeomProp); + return doc->getChildOfType(defaultGeomProp, doc->getDataLibrary()); } return nullptr; } diff --git a/source/MaterialXCore/Node.cpp b/source/MaterialXCore/Node.cpp index 598aedadc1..90a2f46889 100644 --- a/source/MaterialXCore/Node.cpp +++ b/source/MaterialXCore/Node.cpp @@ -75,12 +75,10 @@ NodeDefPtr Node::getNodeDef(const string& target, bool allowRoughMatch) const NodeDefPtr resolvedNode = resolveNameReference(getNodeDefString()); if (!resolvedNode && getDocument()->hasDataLibrary()) { - return resolveNameReference(getDocument()->getDataLibrary(), getNodeDefString()); - } - else - { - return resolvedNode; + return resolveNameReference(getNodeDefString(), nullptr, getDocument()->getDataLibrary()); } + + return resolvedNode; } // Collect document nodes diff --git a/source/PyMaterialX/PyMaterialXCore/PyElement.cpp b/source/PyMaterialX/PyMaterialXCore/PyElement.cpp index cb70d04704..3ab3f92691 100644 --- a/source/PyMaterialX/PyMaterialXCore/PyElement.cpp +++ b/source/PyMaterialX/PyMaterialXCore/PyElement.cpp @@ -15,7 +15,7 @@ #define BIND_ELEMENT_FUNC_INSTANCE(T) \ .def("_addChild" #T, &mx::Element::addChild) \ .def("_getChildOfType" #T, &mx::Element::getChildOfType) \ -.def("_getChildrenOfType" #T, &mx::Element::getChildrenOfType, py::arg("category") = mx::EMPTY_STRING) \ +.def("_getChildrenOfType" #T, &mx::Element::getChildrenOfType, py::arg("category") = mx::EMPTY_STRING, py::arg("datalibrary") = nullptr) \ .def("_removeChildOfType" #T, &mx::Element::removeChildOfType) #define BIND_VALUE_ELEMENT_FUNC_INSTANCE(NAME, T) \