forked from blender/blender
BLEN-326: Pass MaterialX to Hydra via hdMaterialNetwork #5
@ -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
|
||||
renderContexts,
|
||||
out,
|
||||
UsdTimeCode::Default());
|
||||
}
|
||||
}
|
||||
BogdanNagirniak marked this conversation as resolved
Bogdan Nagirniak
commented
Change construction Change construction `if (...) if (...) .... ` to `if (!...) return; if (!...) return` as more understandable and easier to maintain.
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user
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.