Skip to content

Commit

Permalink
Change scope of getChildOfType and getChildrenOfType
Browse files Browse the repository at this point in the history
- Restore Element::getChildOfType and Element::getChildrenOfType and move the datalibrary changes to mx::Document
- Restore Py and Js binding for Element class.
  • Loading branch information
ashwinbhat committed Oct 14, 2024
1 parent fa27416 commit 7194c14
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 26 deletions.
3 changes: 3 additions & 0 deletions source/JsMaterialX/JsMaterialXCore/JsElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ 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) \
.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
2 changes: 1 addition & 1 deletion source/MaterialXCore/Definition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ StringVec TargetDef::getMatchingTargets() const
vector<UnitDefPtr> UnitTypeDef::getUnitDefs() const
{
vector<UnitDefPtr> unitDefs;
for (UnitDefPtr unitDef : getDocument()->getChildrenOfType<UnitDef>(EMPTY_STRING, getDocument()->getDataLibrary()))
for (UnitDefPtr unitDef : getDocument()->getChildrenOfType<UnitDef>())
{
if (unitDef->getUnitType() == _name)
{
Expand Down
55 changes: 41 additions & 14 deletions source/MaterialXCore/Document.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ class MX_CORE_API Document : public GraphElement
/// Return the NodeGraph, if any, with the given name.
NodeGraphPtr getNodeGraph(const string& name) const
{
return getChildOfType<NodeGraph>(name, getDataLibrary());
return getChildOfType<NodeGraph>(name);
}

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

/// Remove the NodeGraph, if any, with the given name.
Expand All @@ -121,6 +121,33 @@ class MX_CORE_API Document : public GraphElement
/// nodes, and include both Input and Output elements.
vector<PortElementPtr> getMatchingPorts(const string& nodeName) const;

/// Return the child element, if any, with the given document and data library
template <class T> shared_ptr<T> getChildOfType(const string& name) const
{
ElementPtr child = hasDataLibrary() ? getDataLibrary()->getChild(name) : nullptr;
if (!child)
{
child = getChild(name);
}
return child ? child->asA<T>() : shared_ptr<T>();
}

/// Return a vector of all child elements within the document and data library
template <class T> vector<shared_ptr<T>> getChildrenOfType(const string& category = EMPTY_STRING) const
{
vector<shared_ptr<T>> children = hasDataLibrary() ? getDataLibrary()->getChildrenOfType<T>(category) : vector<shared_ptr<T>>();
for (ElementPtr child : _childOrder)
{
shared_ptr<T> instance = child->asA<T>();
if (!instance)
continue;
if (!category.empty() && child->getCategory() != category)
continue;
children.push_back(instance);
}
return children;
}

/// @}
/// @name GeomInfo Elements
/// @{
Expand Down Expand Up @@ -368,13 +395,13 @@ class MX_CORE_API Document : public GraphElement
/// Return the NodeDef, if any, with the given name.
NodeDefPtr getNodeDef(const string& name) const
{
return getChildOfType<NodeDef>(name, getDataLibrary());
return getChildOfType<NodeDef>(name);
}

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

/// Remove the NodeDef, if any, with the given name.
Expand Down Expand Up @@ -403,13 +430,13 @@ class MX_CORE_API Document : public GraphElement
/// Return the AttributeDef, if any, with the given name.
AttributeDefPtr getAttributeDef(const string& name) const
{
return getChildOfType<AttributeDef>(name, getDataLibrary());
return getChildOfType<AttributeDef>(name);
}

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

/// Remove the AttributeDef, if any, with the given name.
Expand All @@ -435,13 +462,13 @@ class MX_CORE_API Document : public GraphElement
/// Return the AttributeDef, if any, with the given name.
TargetDefPtr getTargetDef(const string& name) const
{
return getChildOfType<TargetDef>(name, getDataLibrary());
return getChildOfType<TargetDef>(name);
}

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

/// Remove the TargetDef, if any, with the given name.
Expand Down Expand Up @@ -531,13 +558,13 @@ class MX_CORE_API Document : public GraphElement
/// Return the Implementation, if any, with the given name.
ImplementationPtr getImplementation(const string& name) const
{
return getChildOfType<Implementation>(name, getDataLibrary());
return getChildOfType<Implementation>(name);
}

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

/// Remove the Implementation, if any, with the given name.
Expand Down Expand Up @@ -567,13 +594,13 @@ class MX_CORE_API Document : public GraphElement
/// Return the UnitDef, if any, with the given name.
UnitDefPtr getUnitDef(const string& name) const
{
return getChildOfType<UnitDef>(name, getDataLibrary());
return getChildOfType<UnitDef>(name);
}

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

/// Remove the UnitDef, if any, with the given name.
Expand All @@ -598,13 +625,13 @@ class MX_CORE_API Document : public GraphElement
/// Return the UnitTypeDef, if any, with the given name.
UnitTypeDefPtr getUnitTypeDef(const string& name) const
{
return getChildOfType<UnitTypeDef>(name, getDataLibrary());
return getChildOfType<UnitTypeDef>(name);
}

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

/// Remove the UnitTypeDef, if any, with the given name.
Expand Down
14 changes: 5 additions & 9 deletions source/MaterialXCore/Element.h
Original file line number Diff line number Diff line change
Expand Up @@ -436,16 +436,12 @@ class MX_CORE_API Element : public std::enable_shared_from_this<Element>
return (it != _childMap.end()) ? it->second : ElementPtr();
}

/// Return the child element from data library or content, if any, with the given name and subclass.
/// Return the child element, 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, ConstElementPtr dataLibrary = nullptr) const
template <class T> shared_ptr<T> getChildOfType(const string& name) const
{
ElementPtr child = dataLibrary ? dataLibrary->getChild(name) : nullptr;
if (!child)
{
child = getChild(name);
}
ElementPtr child = getChild(name);
return child ? child->asA<T>() : shared_ptr<T>();
}

Expand All @@ -459,9 +455,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, ConstElementPtr dataLibrary = nullptr) const
template <class T> vector<shared_ptr<T>> getChildrenOfType(const string& category = EMPTY_STRING) const
{
vector<shared_ptr<T>> children = dataLibrary ? dataLibrary->getChildrenOfType<T>(category) : vector<shared_ptr<T>>();
vector<shared_ptr<T>> children;
for (ElementPtr child : _childOrder)
{
shared_ptr<T> instance = child->asA<T>();
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->getChildOfType<GeomPropDef>(defaultGeomProp, doc->getDataLibrary());
return doc->getChildOfType<GeomPropDef>(defaultGeomProp);
}
return nullptr;
}
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, py::arg("datalibrary") = nullptr) \
.def("_getChildrenOfType" #T, &mx::Element::getChildrenOfType<mx::T>, py::arg("category") = mx::EMPTY_STRING) \
.def("_removeChildOfType" #T, &mx::Element::removeChildOfType<mx::T>)

#define BIND_VALUE_ELEMENT_FUNC_INSTANCE(NAME, T) \
Expand Down

0 comments on commit 7194c14

Please sign in to comment.