Skip to content

Commit

Permalink
Consolidate mx::Element api
Browse files Browse the repository at this point in the history
Consolidate
  getChildOfTypeWithLibrary into getChildOfType
  getChildrenOfTypeWithLibrary into getChildrenOfType
by using an optional datalibrary argument
  • Loading branch information
ashwinbhat committed Oct 11, 2024
1 parent ca832a2 commit e9c74ff
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 62 deletions.
2 changes: 1 addition & 1 deletion source/JsMaterialX/JsMaterialXCore/JsElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace mx = MaterialX;
#define BIND_ELEMENT_CHILD_FUNC_INSTANCE(NAME, T) \
BIND_MEMBER_FUNC("addChild" #NAME, mx::Element, addChild<T>, 0, 1, stRef) \
.function("getChildOfType" #NAME, &mx::Element::getChildOfType<T>) \
BIND_MEMBER_FUNC("getChildrenOfType" #NAME, mx::Element, getChildrenOfType<T>, 0, 1, stRef) \
BIND_MEMBER_FUNC("getChildrenOfType" #NAME, mx::Element, getChildrenOfType<T>, 0, 2, stRef) \
.function("removeChildOfType" #NAME, &mx::Element::removeChildOfType<T>) \
BIND_MEMBER_FUNC("isA" #NAME, mx::Element, isA<T>, 0, 1, stRef) \
.function("asA" #NAME, ems::select_overload<std::shared_ptr<T>()>(&mx::Element::asA<T>))
Expand Down
28 changes: 14 additions & 14 deletions source/MaterialXCore/Document.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<NodeGraph>(getDataLibrary(), name);
return getChildOfType<NodeGraph>(name, getDataLibrary());
}

/// Return a vector of all NodeGraph elements in the document.
vector<NodeGraphPtr> getNodeGraphs() const
{
return getChildrenOfTypeWithLibrary<NodeGraph>(getDataLibrary());
return getChildrenOfType<NodeGraph>(EMPTY_STRING, getDataLibrary());
}

/// Remove the NodeGraph, if any, with the given name.
Expand Down Expand Up @@ -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<NodeDef>(getDataLibrary(), name);
return getChildOfType<NodeDef>(name, getDataLibrary());
}

/// Return a vector of all NodeDef elements in the document.
vector<NodeDefPtr> getNodeDefs() const
{
return getChildrenOfTypeWithLibrary<NodeDef>(getDataLibrary());
return getChildrenOfType<NodeDef>(EMPTY_STRING, getDataLibrary());
}

/// Remove the NodeDef, if any, with the given name.
Expand Down Expand Up @@ -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<AttributeDef>(getDataLibrary(), name);
return getChildOfType<AttributeDef>(name, getDataLibrary());
}

/// Return a vector of all AttributeDef elements in the document.
vector<AttributeDefPtr> getAttributeDefs() const
{
return getChildrenOfTypeWithLibrary<AttributeDef>(getDataLibrary());
return getChildrenOfType<AttributeDef>(EMPTY_STRING, getDataLibrary());
}

/// Remove the AttributeDef, if any, with the given name.
Expand All @@ -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<TargetDef>(getDataLibrary(), name);
return getChildOfType<TargetDef>(name, getDataLibrary());
}

/// Return a vector of all TargetDef elements in the document.
vector<TargetDefPtr> getTargetDefs() const
{
return getChildrenOfTypeWithLibrary<TargetDef>(getDataLibrary());
return getChildrenOfType<TargetDef>(EMPTY_STRING, getDataLibrary());
}

/// Remove the TargetDef, if any, with the given name.
Expand Down Expand Up @@ -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<Implementation>(getDataLibrary(), name);
return getChildOfType<Implementation>(name, getDataLibrary());
}

/// Return a vector of all Implementation elements in the document.
vector<ImplementationPtr> getImplementations() const
{
return getChildrenOfTypeWithLibrary<Implementation>(getDataLibrary());
return getChildrenOfType<Implementation>(EMPTY_STRING, getDataLibrary());
}

/// Remove the Implementation, if any, with the given name.
Expand Down Expand Up @@ -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<UnitDef>(getDataLibrary(), name);
return getChildOfType<UnitDef>(name, getDataLibrary());
}

/// Return a vector of all Member elements in the TypeDef.
vector<UnitDefPtr> getUnitDefs() const
{
return getChildrenOfTypeWithLibrary<UnitDef>(getDataLibrary());
return getChildrenOfType<UnitDef>(EMPTY_STRING, getDataLibrary());
}

/// Remove the UnitDef, if any, with the given name.
Expand All @@ -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<UnitTypeDef>(getDataLibrary(), name);
return getChildOfType<UnitTypeDef>(name, getDataLibrary());
}

/// Return a vector of all UnitTypeDef elements in the document.
vector<UnitTypeDefPtr> getUnitTypeDefs() const
{
return getChildrenOfTypeWithLibrary<UnitTypeDef>(getDataLibrary());
return getChildrenOfType<UnitTypeDef>(EMPTY_STRING, getDataLibrary());
}

/// Remove the UnitTypeDef, if any, with the given name.
Expand Down
50 changes: 10 additions & 40 deletions source/MaterialXCore/Element.h
Original file line number Diff line number Diff line change
Expand Up @@ -436,29 +436,19 @@ class MX_CORE_API Element : public std::enable_shared_from_this<Element>
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 <class T> shared_ptr<T> getChildOfType(const string& name) const
template <class T> shared_ptr<T> getChildOfType(const string& name, ConstElementPtr dataLibrary = nullptr) const
{
ElementPtr child = getChild(name);
return child ? child->asA<T>() : shared_ptr<T>();
}

/// 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 <class T> shared_ptr<T> 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);
}
return child ? child->asA<T>() : shared_ptr<T>();
}


/// Return a constant vector of all child elements.
/// The returned vector maintains the order in which children were added.
const vector<ElementPtr>& getChildren() const
Expand All @@ -469,9 +459,9 @@ class MX_CORE_API Element : public std::enable_shared_from_this<Element>
/// 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 <class T> vector<shared_ptr<T>> getChildrenOfType(const string& category = EMPTY_STRING) const
template <class T> vector<shared_ptr<T>> getChildrenOfType(const string& category = EMPTY_STRING, ConstElementPtr dataLibrary = nullptr) const
{
vector<shared_ptr<T>> children;
vector<shared_ptr<T>> children = dataLibrary ? dataLibrary->getChildrenOfType<T>(category) : vector<shared_ptr<T>>();
for (ElementPtr child : _childOrder)
{
shared_ptr<T> instance = child->asA<T>();
Expand All @@ -484,17 +474,6 @@ class MX_CORE_API Element : public std::enable_shared_from_this<Element>
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 <class T> vector<shared_ptr<T>> getChildrenOfTypeWithLibrary(ConstElementPtr datalibrary, const string& category = EMPTY_STRING) const
{
vector<shared_ptr<T>> libraryChildren = datalibrary ? datalibrary->getChildrenOfType<T>(category) : vector<shared_ptr<T>>();
vector<shared_ptr<T>> children = getChildrenOfType<T>(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);
Expand Down Expand Up @@ -839,27 +818,18 @@ class MX_CORE_API Element : public std::enable_shared_from_this<Element>
/// @}

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 <class T> shared_ptr<T> resolveNameReference(const string& name, ConstElementPtr parent = nullptr) const
{
ConstElementPtr scope = parent ? parent : getRoot();
shared_ptr<T> child = scope->getChildOfType<T>(getQualifiedName(name));
return child ? child : scope->getChildOfType<T>(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 <class T> shared_ptr<T> resolveNameReference(ConstElementPtr datalibrary, const string& name, ConstElementPtr parent = nullptr) const
template <class T> shared_ptr<T> resolveNameReference(const string& name, ConstElementPtr parent = nullptr, ConstElementPtr datalibrary = nullptr) const
{
shared_ptr<T> child = datalibrary ? datalibrary->getChildOfType<T>(getQualifiedName(name)) : nullptr;

if (child)
return child;

return resolveNameReference<T>(name, parent);
ConstElementPtr scope = parent ? parent : getRoot();
child = scope->getChildOfType<T>(getQualifiedName(name));
return child ? child : scope->getChildOfType<T>(name);
}

// Enforce a requirement within a validate method, updating the validation
Expand Down
2 changes: 1 addition & 1 deletion source/MaterialXCore/Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ GeomPropDefPtr Input::getDefaultGeomProp() const
if (!defaultGeomProp.empty())
{
ConstDocumentPtr doc = getDocument();
return doc->getChildOfTypeWithLibrary<GeomPropDef>(doc->getDataLibrary(),defaultGeomProp);
return doc->getChildOfType<GeomPropDef>(defaultGeomProp, doc->getDataLibrary());
}
return nullptr;
}
Expand Down
8 changes: 3 additions & 5 deletions source/MaterialXCore/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,10 @@ NodeDefPtr Node::getNodeDef(const string& target, bool allowRoughMatch) const
NodeDefPtr resolvedNode = resolveNameReference<NodeDef>(getNodeDefString());
if (!resolvedNode && getDocument()->hasDataLibrary())
{
return resolveNameReference<NodeDef>(getDocument()->getDataLibrary(), getNodeDefString());
}
else
{
return resolvedNode;
return resolveNameReference<NodeDef>(getNodeDefString(), nullptr, getDocument()->getDataLibrary());
}

return resolvedNode;
}

// Collect document nodes
Expand Down
2 changes: 1 addition & 1 deletion source/PyMaterialX/PyMaterialXCore/PyElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#define BIND_ELEMENT_FUNC_INSTANCE(T) \
.def("_addChild" #T, &mx::Element::addChild<mx::T>) \
.def("_getChildOfType" #T, &mx::Element::getChildOfType<mx::T>) \
.def("_getChildrenOfType" #T, &mx::Element::getChildrenOfType<mx::T>, py::arg("category") = mx::EMPTY_STRING) \
.def("_getChildrenOfType" #T, &mx::Element::getChildrenOfType<mx::T>, py::arg("category") = mx::EMPTY_STRING, py::arg("datalibrary") = nullptr) \
.def("_removeChildOfType" #T, &mx::Element::removeChildOfType<mx::T>)

#define BIND_VALUE_ELEMENT_FUNC_INSTANCE(NAME, T) \
Expand Down

0 comments on commit e9c74ff

Please sign in to comment.