Skip to content

Commit

Permalink
Introduce datalibrary methods for Element and it's derivatives
Browse files Browse the repository at this point in the history
Introduce getChildofTypeFromLibrary and getChildrenOfTypeFromLibrary to avoid potential conflicts with existing getChildrenOfType and getChildofType. Once the datalibrary concept is more acceptable, we will revisit the API names and create Python & Javascript bindings.
  • Loading branch information
ashwinbhat committed Oct 11, 2024
1 parent 6782a81 commit abb8f64
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 30 deletions.
12 changes: 6 additions & 6 deletions source/JsMaterialX/JsMaterialXCore/JsElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ namespace mx = MaterialX;
#define BIND_VALUE_ELEMENT_FUNC_INSTANCE(NAME, T) \
BIND_MEMBER_FUNC("setValue" #NAME, mx::ValueElement, setValue<T>, 1, 2, const T&, stRef)

#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) \
#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>))

#define BIND_ELEMENT_FUNC_INSTANCE(NAME, 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 @@ -80,13 +80,13 @@ class MX_CORE_API Document : public GraphElement
/// Return the NodeGraph, if any, with the given name.
NodeGraphPtr getNodeGraph(const string& name) const
{
return hasDataLibrary() ? getChildOfType<NodeGraph>(getRegisteredDataLibrary(), name) : getChildOfType<NodeGraph>(name);
return hasDataLibrary() ? getChildofTypeFromLibrary<NodeGraph>(getRegisteredDataLibrary(), name) : getChildOfType<NodeGraph>(name);
}

/// Return a vector of all NodeGraph elements in the document.
vector<NodeGraphPtr> getNodeGraphs() const
{
return hasDataLibrary() ? getChildrenOfType<NodeGraph>(getRegisteredDataLibrary()) : getChildrenOfType<NodeGraph>();
return hasDataLibrary() ? getChildrenOfTypeFromLibrary<NodeGraph>(getRegisteredDataLibrary()) : getChildrenOfType<NodeGraph>();
}

/// Remove the NodeGraph, if any, with the given name.
Expand Down Expand Up @@ -347,13 +347,13 @@ class MX_CORE_API Document : public GraphElement
/// Return the NodeDef, if any, with the given name.
NodeDefPtr getNodeDef(const string& name) const
{
return hasDataLibrary() ? getChildOfType<NodeDef>(getRegisteredDataLibrary(), name) : getChildOfType<NodeDef>(name);
return hasDataLibrary() ? getChildofTypeFromLibrary<NodeDef>(getRegisteredDataLibrary(), name) : getChildOfType<NodeDef>(name);
}

/// Return a vector of all NodeDef elements in the document.
vector<NodeDefPtr> getNodeDefs() const
{
return hasDataLibrary() ? getChildrenOfType<NodeDef>(getRegisteredDataLibrary()) : getChildrenOfType<NodeDef>();
return hasDataLibrary() ? getChildrenOfTypeFromLibrary<NodeDef>(getRegisteredDataLibrary()) : getChildrenOfType<NodeDef>();
}

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

/// Return a vector of all AttributeDef elements in the document.
vector<AttributeDefPtr> getAttributeDefs() const
{
return hasDataLibrary() ? getChildrenOfType<AttributeDef>(getRegisteredDataLibrary()) : getChildrenOfType<AttributeDef>();
return hasDataLibrary() ? getChildrenOfTypeFromLibrary<AttributeDef>(getRegisteredDataLibrary()) : getChildrenOfType<AttributeDef>();
}

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

/// Return a vector of all TargetDef elements in the document.
vector<TargetDefPtr> getTargetDefs() const
{
return hasDataLibrary() ? getChildrenOfType<TargetDef>(getRegisteredDataLibrary()) : getChildrenOfType<TargetDef>();
return hasDataLibrary() ? getChildrenOfTypeFromLibrary<TargetDef>(getRegisteredDataLibrary()) : getChildrenOfType<TargetDef>();
}

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

/// Return a vector of all Implementation elements in the document.
vector<ImplementationPtr> getImplementations() const
{
return hasDataLibrary() ? getChildrenOfType<Implementation>(getRegisteredDataLibrary()) : getChildrenOfType<Implementation>();
return hasDataLibrary() ? getChildrenOfTypeFromLibrary<Implementation>(getRegisteredDataLibrary()) : getChildrenOfType<Implementation>();
}

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

/// Return a vector of all Member elements in the TypeDef.
vector<UnitDefPtr> getUnitDefs() const
{
return hasDataLibrary() ? getChildrenOfType<UnitDef>(getRegisteredDataLibrary()) : getChildrenOfType<UnitDef>();
return hasDataLibrary() ? getChildrenOfTypeFromLibrary<UnitDef>(getRegisteredDataLibrary()) : getChildrenOfType<UnitDef>();
}

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

/// Return a vector of all UnitTypeDef elements in the document.
vector<UnitTypeDefPtr> getUnitTypeDefs() const
{
return hasDataLibrary() ? getChildrenOfType<UnitTypeDef>(getRegisteredDataLibrary()) : getChildrenOfType<UnitTypeDef>();
return hasDataLibrary() ? getChildrenOfTypeFromLibrary<UnitTypeDef>(getRegisteredDataLibrary()) : getChildrenOfType<UnitTypeDef>();
}

/// Remove the UnitTypeDef, if any, with the given name.
Expand Down
4 changes: 2 additions & 2 deletions source/MaterialXCore/Element.h
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ class MX_CORE_API Element : public std::enable_shared_from_this<Element>
/// 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> getChildOfType(ConstElementPtr datalibrary, const string& name) const
template <class T> shared_ptr<T> getChildofTypeFromLibrary(ConstElementPtr datalibrary, const string& name) const
{
ElementPtr child = datalibrary ? datalibrary->getChild(name) : nullptr;
if (!child)
Expand Down Expand Up @@ -487,7 +487,7 @@ class MX_CORE_API Element : public std::enable_shared_from_this<Element>
/// 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>> getChildrenOfType(ConstElementPtr datalibrary, const string& category = EMPTY_STRING) const
template <class T> vector<shared_ptr<T>> getChildrenOfTypeFromLibrary(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);
Expand Down
2 changes: 1 addition & 1 deletion source/MaterialXCore/Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ GeomPropDefPtr Input::getDefaultGeomProp() const
{
ConstDocumentPtr doc = getDocument();
if (doc->hasDataLibrary())
return doc->getChildOfType<GeomPropDef>(doc->getRegisteredDataLibrary(),defaultGeomProp);
return doc->getChildofTypeFromLibrary<GeomPropDef>(doc->getRegisteredDataLibrary(),defaultGeomProp);
else
return doc->getChildOfType<GeomPropDef>(defaultGeomProp);
}
Expand Down
10 changes: 5 additions & 5 deletions source/MaterialXGenShader/ShaderGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,11 @@ void ShaderGraph::addDefaultGeomNode(ShaderInput* input, const GeomPropDef& geom
// input here and ignore the type of the geomprop. They are required to have the same type.
string geomNodeDefName = "ND_" + geomprop.getGeomProp() + "_" + input->getType().getName();
NodeDefPtr geomNodeDef = _document->getNodeDef(geomNodeDefName);
if (!geomNodeDef)
{
throw ExceptionShaderGenError("Could not find a nodedef named '" + geomNodeDefName +
"' for defaultgeomprop on input '" + input->getFullName() + "'");
}
if (!geomNodeDef)
{
throw ExceptionShaderGenError("Could not find a nodedef named '" + geomNodeDefName +
"' for defaultgeomprop on input '" + input->getFullName() + "'");
}

ShaderNodePtr geomNode = ShaderNode::create(this, geomNodeName, *geomNodeDef, context);
addNode(geomNode);
Expand Down
4 changes: 2 additions & 2 deletions source/PyMaterialX/PyMaterialXCore/PyElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

#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("_getChildOfType" #T, &mx::Element::getChildOfType<mx::T>) \
.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 abb8f64

Please sign in to comment.