Create parsing system that converts supported nodes and ignores unsupported #2

Merged
Bogdan Nagirniak merged 14 commits from Vasyl-Pidhirskyi/blender:BLEN-500 into matx-export-material 2023-08-28 12:29:46 +02:00
4 changed files with 24 additions and 27 deletions
Showing only changes of commit d559b3b710 - Show all commits

View File

@ -52,7 +52,9 @@ class HydraSceneDelegate : public pxr::HdSceneDelegate {
const View3D *view3d = nullptr;
Main *bmain = nullptr;
Scene *scene = nullptr;
ShadingSettings shading_settings;
bool use_materialx = true;
private:
ObjectDataMap objects_;

View File

@ -70,9 +70,21 @@ void MaterialData::init()
time,
export_params,
image_cache_file_path()};
/* Create USD material. */
//pxr::UsdShadeMaterial usd_material = usd::create_usd_material(export_context, material_path, (Material *)id, "st");
pxr::UsdShadeMaterial usd_material;
if (scene_delegate_->use_materialx) {
MaterialX::DocumentPtr doc = blender::nodes::materialx::export_to_materialx(scene_delegate_->depsgraph, (Material *)id);
pxr::UsdMtlxRead(doc, stage);
if (pxr::UsdPrim materials = stage->GetPrimAtPath(pxr::SdfPath("/MaterialX/Materials"))) {
pxr::UsdPrimSiblingRange children = materials.GetChildren();
if (!children.empty()) {
usd_material = pxr::UsdShadeMaterial(*children.begin());
}
}
}
else {
usd_material = usd::create_usd_material(export_context, material_path, (Material *)id, "st");
}
/* Convert USD material to Hydra material network map, adapted for render delegate. */
const pxr::HdRenderDelegate *render_delegate = scene_delegate_->GetRenderIndex().GetRenderDelegate();
@ -81,24 +93,14 @@ void MaterialData::init()
pxr::HdMaterialNetworkMap network_map;
MaterialX::DocumentPtr doc = blender::nodes::materialx::export_to_materialx(scene_delegate_->depsgraph, (Material *)id);
pxr::UsdMtlxRead(doc, stage);
if (pxr::UsdPrim materials = stage->GetPrimAtPath(pxr::SdfPath("/MaterialX/Materials"))) {
if (pxr::UsdPrimSiblingRange children = materials.GetChildren()) {
if (auto usd_material = pxr::UsdShadeMaterial(*children.begin())) {
if (pxr::UsdShadeShader mtlx_surface = usd_material.ComputeSurfaceSource(contextVector))
{
UsdImagingBuildHdMaterialNetworkFromTerminal(mtlx_surface.GetPrim(),
if (pxr::UsdShadeShader mtlx_surface = usd_material.ComputeSurfaceSource(contextVector)) {
pxr::UsdImagingBuildHdMaterialNetworkFromTerminal(mtlx_surface.GetPrim(),
pxr::HdMaterialTerminalTokens->surface,
shaderSourceTypes,
contextVector,
&network_map,
time);
}
}
}
}
material_network_map_ = pxr::VtValue(network_map);
}

View File

@ -50,14 +50,6 @@ MaterialX::DocumentPtr export_to_materialx(Depsgraph *depsgraph, Material *mater
else {
create_standard_surface(doc, material);
}
char dir_path[FILE_MAX], file_path[FILE_MAX], file_name[32];
BLI_path_join(dir_path, sizeof(dir_path), BKE_tempdir_session(), "hydra", "materialx");
BLI_dir_create_recursive(dir_path);
SNPRINTF(file_name, "mat_%p.mtlx", material);
BLI_path_join(file_path, sizeof(file_path), dir_path, file_name);
MaterialX::writeToXmlFile(doc, MaterialX::FilePath(file_path));
return doc;
}

View File

@ -89,6 +89,7 @@ void Engine::sync(Depsgraph *depsgraph, bContext *context)
pxr::SdfPath scene_path = pxr::SdfPath::AbsoluteRootPath().AppendElementString("scene");
hydra_scene_delegate_ = std::make_unique<io::hydra::HydraSceneDelegate>(render_index_.get(),
scene_path);
hydra_scene_delegate_->use_materialx = bl_engine_->type->flag & RE_USE_MATERIALX;
}
hydra_scene_delegate_->populate(depsgraph, context ? CTX_wm_view3d(context) : nullptr);
}