Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Material-X: flatten filenames upon read #974 #2660

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
1. Call mx::flattenFilenames() anytime a mx::read* function is called.
  • Loading branch information
Derek-Nvidia committed Oct 18, 2023
commit 55351969e720d5f54b6e298687937761f2ab2f8c
29 changes: 11 additions & 18 deletions pxr/usd/usdMtlx/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "pxr/base/tf/stringUtils.h"
#include "pxr/base/vt/array.h"
#include <MaterialXCore/Util.h>
#include <MaterialXFormat/Util.h>
#include <MaterialXFormat/XmlIo.h>
#include <map>
#include <type_traits>
Expand Down Expand Up @@ -295,6 +296,9 @@ _ReadFromString(mx::DocumentPtr doc, const std::string& s,
};

mx::readFromXmlString(doc, s, searchPath, &readOptions);

// Flatten all filenames in document
mx::flattenFilenames(doc);
}

static void
Expand Down Expand Up @@ -336,6 +340,10 @@ UsdMtlxReadDocument(const std::string& resolvedPath)
// whatever backing store it points to.
if (TfIsFile(resolvedPath)) {
mx::readFromXmlFile(doc, resolvedPath);

// Flatten all filenames in document
mx::flattenFilenames(doc);

return doc;
}
else {
Expand Down Expand Up @@ -553,10 +561,11 @@ UsdMtlxGetUsdType(const std::string& mtlxTypeName)

VtValue
UsdMtlxGetUsdValue(
const mx::ConstTypedElementPtr& mtlx,
const mx::ConstElementPtr& mtlx,
bool getDefaultValue)
{
static const std::string defaultAttr("default");
static const std::string typeAttr = mx::TypedElement::TYPE_ATTRIBUTE;
static const std::string valueAttr = mx::ValueElement::VALUE_ATTRIBUTE;

// Bail if no element.
Expand All @@ -569,24 +578,8 @@ UsdMtlxGetUsdValue(
getDefaultValue ? mtlx->getAttribute(defaultAttr)
: mtlx->getAttribute(valueAttr);

auto&& type = mtlx->getType();

if (!mx::StringResolver::isResolvedType(type)) {
return _GetUsdValue(valueString, type);
}

auto stringResolver = mtlx->createStringResolver();
if (!stringResolver) {
TF_RUNTIME_ERROR("Unable to get MaterialX StringResolver for element '%s'",
mtlx->getNamePath().c_str());
return VtValue();
}

// resolve so that any prefixes are applied.
auto resolvedValue = stringResolver->resolve(valueString, type);

// Get the value.
return _GetUsdValue(resolvedValue, type);
return _GetUsdValue(valueString, mtlx->getAttribute(typeAttr));
}

std::vector<VtValue>
Expand Down
2 changes: 1 addition & 1 deletion pxr/usd/usdMtlx/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ UsdMtlxGetUsdType(const std::string& mtlxTypeName);
/// that silently returns an empty VtValue.
USDMTLX_API
VtValue
UsdMtlxGetUsdValue(const MaterialX::ConstTypedElementPtr& mtlx,
UsdMtlxGetUsdValue(const MaterialX::ConstElementPtr& mtlx,
bool getDefaultValue = false);

/// Return the MaterialX values in \p values assuming it contains an
Expand Down
Loading