Skip to content

Commit

Permalink
Update getMatching* logic and build fixes
Browse files Browse the repository at this point in the history
Update getMatchingNodeDefs and getMatchingImplementations such that node from datalibrary and local document are searched.

Build fixes and code cleanup
  • Loading branch information
ashwinbhat committed Oct 9, 2024
1 parent 0bc65b0 commit 604499a
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 43 deletions.
40 changes: 14 additions & 26 deletions source/MaterialXCore/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,51 +357,39 @@ vector<OutputPtr> Document::getMaterialOutputs() const

vector<NodeDefPtr> Document::getMatchingNodeDefs(const string& nodeName) const
{
// Return all nodedefs from datalibrary if available
if (_dataLibrary)
{
auto datalibrarynodes = _dataLibrary->getMatchingNodeDefs(nodeName);
if (!datalibrarynodes.empty())
return datalibrarynodes;
}
// Gather all nodedefs from datalibrary if available
vector<NodeDefPtr> matchingNodeDefs = hasDataLibrary() ?
getRegisteredDataLibrary()->getMatchingNodeDefs(nodeName) :
vector<NodeDefPtr>();

// Refresh the cache.
_cache->refresh();

// Return all nodedefs matching the given node name.
if (_cache->nodeDefMap.count(nodeName))
{
return _cache->nodeDefMap.at(nodeName);
}
else
{
return vector<NodeDefPtr>();
matchingNodeDefs.insert(matchingNodeDefs.end(), _cache->nodeDefMap.at(nodeName).begin(), _cache->nodeDefMap.at(nodeName).end());
}

return matchingNodeDefs;
}

vector<InterfaceElementPtr> Document::getMatchingImplementations(const string& nodeDef) const
{

// Return all implementations from datalibrary if available
if (_dataLibrary)
{
auto datalibrarynodes = _dataLibrary->getMatchingImplementations(nodeDef);
if (!datalibrarynodes.empty())
return datalibrarynodes;
}

// Gather all implementations from datalibrary if available
vector<InterfaceElementPtr> matchingImplementations = hasDataLibrary() ?
getRegisteredDataLibrary()->getMatchingImplementations(nodeDef) :
vector<InterfaceElementPtr>();
// Refresh the cache.
_cache->refresh();

// Return all implementations matching the given nodedef string.
if (_cache->implementationMap.count(nodeDef))
{
return _cache->implementationMap.at(nodeDef);
}
else
{
return vector<InterfaceElementPtr>();
matchingImplementations.insert(matchingImplementations.end(), _cache->implementationMap.at(nodeDef).begin(), _cache->implementationMap.at(nodeDef).end());
}

return matchingImplementations;
}

bool Document::validate(string* message) const
Expand Down
4 changes: 2 additions & 2 deletions source/MaterialXCore/Document.h
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ 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);
return hasDataLibrary() ? getChildOfType<UnitDef>(getRegisteredDataLibrary(), name) : getChildOfType<UnitDef>(name);
}

/// Return a vector of all Member elements in the TypeDef.
Expand Down Expand Up @@ -577,7 +577,7 @@ 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);
return hasDataLibrary() ? getChildOfType<UnitTypeDef>(getRegisteredDataLibrary(), name) : getChildOfType<UnitTypeDef>(name);
}

/// Return a vector of all UnitTypeDef elements in the document.
Expand Down
5 changes: 1 addition & 4 deletions source/MaterialXCore/Element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -564,10 +564,7 @@ bool ValueElement::validate(string* message) const
const string& unittype = getUnitType();
if (!unittype.empty())
{

unitTypeDef = getDocument()->hasDataLibrary() ?
getDocument()->getRegisteredDataLibrary()->getUnitTypeDef(unittype) :
getDocument()->getUnitTypeDef(unittype);
unitTypeDef = getDocument()->getUnitTypeDef(unittype);
validateRequire(unitTypeDef != nullptr, res, message, "Unit type definition does not exist in document");
}
}
Expand Down
4 changes: 2 additions & 2 deletions source/MaterialXCore/Element.h
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,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(ConstDocumentPtr datalibrary, const string& name) const
template <class T> shared_ptr<T> getChildOfType(ConstElementPtr datalibrary, const string& name) const
{
ElementPtr child = datalibrary->getChild(name);
if (!child)
Expand Down Expand Up @@ -483,7 +483,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(ConstDocumentPtr datalibrary, const string& category = EMPTY_STRING) const
template <class T> vector<shared_ptr<T>> getChildrenOfType(ConstElementPtr datalibrary, const string& category = EMPTY_STRING) const
{
vector<shared_ptr<T>> libraryChildren = datalibrary->getChildrenOfType<T>(category);
vector<shared_ptr<T>> children = getChildrenOfType<T>(category);
Expand Down
9 changes: 5 additions & 4 deletions source/MaterialXCore/Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,11 @@ GeomPropDefPtr Input::getDefaultGeomProp() const
const string& defaultGeomProp = getAttribute(DEFAULT_GEOM_PROP_ATTRIBUTE);
if (!defaultGeomProp.empty())
{
ConstDocumentPtr doc = getDocument()->hasDataLibrary() ?
getDocument()->getRegisteredDataLibrary() :
getDocument();
return doc->getChildOfType<GeomPropDef>(defaultGeomProp);
ConstDocumentPtr doc = getDocument();
if (doc->hasDataLibrary())
return doc->getChildOfType<GeomPropDef>(doc->getRegisteredDataLibrary(),defaultGeomProp);
else
return doc->getChildOfType<GeomPropDef>(defaultGeomProp);
}
return nullptr;
}
Expand Down
1 change: 1 addition & 0 deletions source/MaterialXCore/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ string Node::getConnectedNodeName(const string& inputName) const

NodeDefPtr Node::getNodeDef(const string& target, bool allowRoughMatch) const
{
// Collect document nodes
vector<NodeDefPtr> nodeDefs = getDocument()->getMatchingNodeDefs(getQualifiedName(getCategory()));
vector<NodeDefPtr> secondary = getDocument()->getMatchingNodeDefs(getCategory());
nodeDefs.insert(nodeDefs.end(), secondary.begin(), secondary.end());
Expand Down
5 changes: 0 additions & 5 deletions source/MaterialXGenShader/ShaderGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +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 && _document->hasDataLibrary())
{
geomNodeDef = _document->getRegisteredDataLibrary()->getNodeDef(geomNodeDefName);
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

0 comments on commit 604499a

Please sign in to comment.