USD Preview Surface import as a fallback.

Added logic to fall back on importing existing
USD Preview Surface shaders if importing MDL
is selected as an option but the material has
no MDL shaders.
This commit is contained in:
Michael Kowalski 2021-11-09 13:02:22 -05:00
parent 85172cb5e1
commit f71ad78dc1
4 changed files with 37 additions and 14 deletions

View File

@ -85,7 +85,9 @@ const EnumPropertyItem rna_enum_usd_import_shaders_mode_items[] = {
"USD MDL",
0,
"MDL",
"Convert MDL shaders to Blender materials"},
"Convert MDL shaders to Blender materials; if no MDL shaders "
"exist on the material, log a warning and import existing USD "
"Preview Surface shaders instead"},
{0, NULL, 0, NULL, NULL},
};

View File

@ -33,6 +33,8 @@
#include "DNA_material_types.h"
#include "WM_api.h"
#include <pxr/base/gf/vec3f.h>
#include <pxr/usd/ar/resolver.h>
#include <pxr/usd/usdShade/material.h>
@ -347,18 +349,29 @@ Material *USDMaterialReader::add_material(const pxr::UsdShadeMaterial &usd_mater
* if there is one. */
pxr::UsdShadeShader usd_preview;
if (get_usd_preview_surface(usd_material, usd_preview)) {
/* Always set the viewport material properties from the USD
* Preview Surface settings. */
set_viewport_material_props(mtl, usd_preview);
/* Optionally, create shader nodes to represent a UsdPreviewSurface. */
if (params_.import_shaders_mode == USD_IMPORT_USD_PREVIEW_SURFACE) {
import_usd_preview(mtl, usd_preview);
}
}
if (params_.import_shaders_mode == USD_IMPORT_MDL) {
if (params_.import_shaders_mode == USD_IMPORT_USD_PREVIEW_SURFACE
&& usd_preview) {
/* Create shader nodes to represent a UsdPreviewSurface. */
import_usd_preview(mtl, usd_preview);
}
else if (params_.import_shaders_mode == USD_IMPORT_MDL) {
bool imported_mdl = false;
#ifdef WITH_PYTHON
umm_import_material(mtl, usd_material);
/* Invoke UMM to convert to MDL. */
imported_mdl = umm_import_material(mtl, usd_material, true /* Verbose */);
#endif
if (!imported_mdl && usd_preview) {
/* We failed to import an MDL, so fall back on importing UsdPreviewSuface. */
std::string message = "Couldn't import MDL shaders for material "
+ mtl_name + ", importing USD Preview Surface shaders instead";
WM_reportf(RPT_INFO, message.c_str());
import_usd_preview(mtl, usd_preview);
}
}
return mtl;

View File

@ -577,7 +577,11 @@ static bool import_material(Material *mtl,
if (ret) {
std::cout << "result:\n";
print_obj(ret);
report_notification(ret);
if (report_notification(ret)) {
/* The function returned a notification object,
* indicating a failure. */
success = false;
}
Py_DECREF(ret);
}
@ -712,7 +716,7 @@ bool umm_module_loaded()
return loaded;
}
bool umm_import_material(Material *mtl, const pxr::UsdShadeMaterial &usd_material)
bool umm_import_material(Material *mtl, const pxr::UsdShadeMaterial &usd_material, bool verbose)
{
if (!(mtl && usd_material)) {
return false;
@ -725,13 +729,17 @@ bool umm_import_material(Material *mtl, const pxr::UsdShadeMaterial &usd_materia
/* Check if we have an mdl source asset. */
pxr::SdfAssetPath source_asset;
if (!surf_shader.GetSourceAsset(&source_asset, usdtokens::mdl)) {
std::cout << "No mdl source asset for shader " << surf_shader.GetPath() << std::endl;
if (verbose) {
std::cout << "No mdl source asset for shader " << surf_shader.GetPath() << std::endl;
}
return false;
}
pxr::TfToken source_asset_sub_identifier;
if (!surf_shader.GetSourceAssetSubIdentifier(&source_asset_sub_identifier, usdtokens::mdl)) {
std::cout << "No mdl source asset sub identifier for shader " << surf_shader.GetPath()
<< std::endl;
if (verbose) {
std::cout << "No mdl source asset sub identifier for shader " << surf_shader.GetPath()
<< std::endl;
}
return false;
}

View File

@ -31,7 +31,7 @@ struct USDExporterContext;
bool umm_module_loaded();
bool umm_import_material(Material *mtl, const pxr::UsdShadeMaterial &usd_material);
bool umm_import_material(Material *mtl, const pxr::UsdShadeMaterial &usd_material, bool verbose = false);
bool umm_export_material(const USDExporterContext &usd_export_context,
const Material *mtl,