BLEN-326: Pass MaterialX to Hydra via hdMaterialNetwork #5

Merged
Bogdan Nagirniak merged 4 commits from George-Shakula/blender:mtlx-translation into hydra-render 2023-03-02 12:35:51 +01:00
Showing only changes of commit e51b97d4cc - Show all commits

View File

@ -24,26 +24,6 @@ namespace mx = MaterialX;
PXR_NAMESPACE_OPEN_SCOPE
template<typename R> static bool _Read(UsdStagePtr stage, R &&reader)
{
try {
auto doc = reader();
if (doc) {
UsdMtlxRead(doc, stage);
return true;
}
}
catch (mx::ExceptionFoundCycle &x) {
TF_RUNTIME_ERROR("MaterialX cycle found: %s\n", x.what());
return false;
}
catch (mx::Exception &x) {
TF_RUNTIME_ERROR("MaterialX error: %s\n", x.what());
return false;
}
return false;
}
void HdMtlxConvertToMaterialNetworkMap(std::string const &mtlxPath,
TfTokenVector const &shaderSourceTypes,
TfTokenVector const &renderContexts,
@ -63,20 +43,31 @@ void HdMtlxConvertToMaterialNetworkMap(std::string const &mtlxPath,
std::string mtlxName = TfGetBaseName(mtlxPath);
std::string stageId = TfStringPrintf(
"%s%s%s.usda", basePath.c_str(), ARCH_PATH_SEP, mtlxName.c_str());
auto stage = UsdStage::CreateInMemory(stageId, context);
if (_Read(stage, [&mtlxPath]() { return UsdMtlxReadDocument(mtlxPath); })) {
if (UsdPrim materials = stage->GetPrimAtPath(SdfPath("/MaterialX/Materials"))) {
if (UsdPrimSiblingRange children = materials.GetChildren()) {
if (auto material = UsdShadeMaterial(
*children.begin())) { // TODO: specify which material to use from the mtlx
if (UsdShadeShader mtlxSurface = material.ComputeSurfaceSource(renderContexts)) {
UsdImagingBuildHdMaterialNetworkFromTerminal(mtlxSurface.GetPrim(),
HdMaterialTerminalTokens->surface,
shaderSourceTypes,
renderContexts,
out,
UsdTimeCode::Default());
}
UsdStageRefPtr stage = UsdStage::CreateInMemory(stageId, context);
try {
mx::DocumentPtr doc = UsdMtlxReadDocument(mtlxPath);
UsdMtlxRead(doc, stage);
}
catch (mx::ExceptionFoundCycle &x) {
TF_RUNTIME_ERROR("MaterialX cycle found: %s\n", x.what());
return;
}
catch (mx::Exception &x) {
TF_RUNTIME_ERROR("MaterialX error: %s\n", x.what());
return;
}
if (UsdPrim materials = stage->GetPrimAtPath(SdfPath("/MaterialX/Materials"))) {
if (UsdPrimSiblingRange children = materials.GetChildren()) {
if (auto material = UsdShadeMaterial(*children.begin())) {
if (UsdShadeShader mtlxSurface = material.ComputeSurfaceSource(renderContexts)) {
UsdImagingBuildHdMaterialNetworkFromTerminal(mtlxSurface.GetPrim(),
HdMaterialTerminalTokens->surface,
shaderSourceTypes,
George-Shakula marked this conversation as resolved
Review

This part with _Read() and lambda function is hard to maintain in future + used in one place, can you move it content of _Read here.

This part with `_Read()` and lambda function is hard to maintain in future + used in one place, can you move it content of `_Read` here.
renderContexts,
out,
UsdTimeCode::Default());
}
}
BogdanNagirniak marked this conversation as resolved
Review

Change construction if (...) if (...) .... to if (!...) return; if (!...) return as more understandable and easier to maintain.

Change construction `if (...) if (...) .... ` to `if (!...) return; if (!...) return` as more understandable and easier to maintain.
}