[Bf-blender-cvs] [82a6f152abb] usd-importer-T81257: USD import Set Material Blend option.

makowalski noreply at git.blender.org
Fri Feb 19 05:14:33 CET 2021


Commit: 82a6f152abb269a6a7b49e4084dceec60462da6d
Author: makowalski
Date:   Thu Feb 18 23:08:16 2021 -0500
Branches: usd-importer-T81257
https://developer.blender.org/rB82a6f152abb269a6a7b49e4084dceec60462da6d

USD import Set Material Blend option.

New Set Material Blend option which, if enabled together
with the UsdPreviewSurface option, will automatically set
the material blend method based on the shader's opacity
and opcityThreshold inputs.

===================================================================

M	source/blender/editors/io/io_usd.c
M	source/blender/io/usd/import/usd_material_importer.cc
M	source/blender/io/usd/usd.h

===================================================================

diff --git a/source/blender/editors/io/io_usd.c b/source/blender/editors/io/io_usd.c
index 9bb3c764dc0..fc39cf9b488 100644
--- a/source/blender/editors/io/io_usd.c
+++ b/source/blender/editors/io/io_usd.c
@@ -290,6 +290,8 @@ static int wm_usd_import_exec(bContext *C, wmOperator *op)
   const bool import_proxy = RNA_boolean_get(op->ptr, "import_proxy");
   const bool import_render = RNA_boolean_get(op->ptr, "import_render");
 
+  const bool set_material_blend = RNA_boolean_get(op->ptr, "set_material_blend");
+
   /* Switch out of edit mode to avoid being stuck in it (T54326). */
   Object *obedit = CTX_data_edit_object(C);
   if (obedit) {
@@ -308,7 +310,8 @@ static int wm_usd_import_exec(bContext *C, wmOperator *op)
                                    transform_constraint,
                                    import_guide,
                                    import_proxy,
-                                   import_render};
+                                   import_render,
+                                   set_material_blend};
 
   bool ok = USD_import(C, filename, &params, as_background_job);
 
@@ -349,6 +352,7 @@ static void wm_usd_import_draw(bContext *UNUSED(C), wmOperator *op)
   uiItemL(box, IFACE_("Experimental"), ICON_NONE);
   uiItemR(box, ptr, "use_instancing", 0, NULL, ICON_NONE);
   uiItemR(box, ptr, "import_usdpreview", 0, NULL, ICON_NONE);
+  uiItemR(box, ptr, "set_material_blend", 0, NULL, ICON_NONE);
 }
 
 void WM_OT_usd_import(wmOperatorType *ot)
@@ -419,6 +423,14 @@ void WM_OT_usd_import(wmOperatorType *ot)
       "Import UsdPreviewSurface",
       "When checked, convert UsdPreviewSurface shaders to Principled BSD shader networks");
 
+  RNA_def_boolean(ot->srna,
+                  "set_material_blend",
+                  false,
+                  "Set Material Blend",
+                  "When checked and if the Import UsdPreviewSurface option is enabled, "
+                  "the material blend method will automatically be set based on the "
+                  "shader's opacity and opacityThreshold inputs");
+
   RNA_def_boolean(ot->srna,
                   "is_sequence",
                   false,
diff --git a/source/blender/io/usd/import/usd_material_importer.cc b/source/blender/io/usd/import/usd_material_importer.cc
index d9623a7ec09..6acba783aaa 100644
--- a/source/blender/io/usd/import/usd_material_importer.cc
+++ b/source/blender/io/usd/import/usd_material_importer.cc
@@ -57,6 +57,7 @@ static const pxr::TfToken metallic("metallic", pxr::TfToken::Immortal);
 static const pxr::TfToken normal("normal", pxr::TfToken::Immortal);
 static const pxr::TfToken occlusion("occlusion", pxr::TfToken::Immortal);
 static const pxr::TfToken opacity("opacity", pxr::TfToken::Immortal);
+static const pxr::TfToken opacityThreshold("opacityThreshold", pxr::TfToken::Immortal);
 static const pxr::TfToken r("r", pxr::TfToken::Immortal);
 static const pxr::TfToken result("result", pxr::TfToken::Immortal);
 static const pxr::TfToken rgb("rgb", pxr::TfToken::Immortal);
@@ -123,6 +124,44 @@ static pxr::UsdShadeShader get_source_shader(const pxr::UsdShadeConnectableAPI &
   return pxr::UsdShadeShader();
 }
 
+// Returns true if the given shader may have opacity < 1.0, based
+// on heuristics.  Also returns the shader's opacityThreshold input
+// in r_opacity_threshold, if this input has an authored value.
+static bool needs_blend(const pxr::UsdShadeShader &usd_shader, float &r_opacity_threshold)
+{
+  if (!usd_shader) {
+    return false;
+  }
+
+  bool needs_blend;
+
+  if (pxr::UsdShadeInput opacity_input = usd_shader.GetInput(usdtokens::opacity)) {
+
+    if (opacity_input.HasConnectedSource()) {
+      needs_blend = true;
+    }
+    else {
+      pxr::VtValue val;
+      if (opacity_input.GetAttr().HasAuthoredValue() && opacity_input.GetAttr().Get(&val)) {
+        float opacity = val.Get<float>();
+        needs_blend = opacity < 1.0f;
+      }
+    }
+  }
+
+  if (pxr::UsdShadeInput opacity_threshold_input = usd_shader.GetInput(
+          usdtokens::opacityThreshold)) {
+
+    pxr::VtValue val;
+    if (opacity_threshold_input.GetAttr().HasAuthoredValue() &&
+        opacity_threshold_input.GetAttr().Get(&val)) {
+      r_opacity_threshold = val.Get<float>();
+    }
+  }
+
+  return needs_blend;
+}
+
 namespace blender::io::usd {
 
 namespace {
@@ -281,6 +320,21 @@ void USDMaterialImporter::import_usd_preview(Material *mtl,
   }
 
   nodeSetActive(ntree, output);
+
+  // Optionally, set the material blend mode.
+
+  if (context_.import_params.set_material_blend) {
+    float opacity_threshold = 0.0f;
+    if (needs_blend(usd_shader, opacity_threshold)) {
+      if (opacity_threshold > 0.0f) {
+        mtl->blend_method = MA_BM_CLIP;
+        mtl->alpha_threshold = opacity_threshold;
+      }
+      else {
+        mtl->blend_method = MA_BM_BLEND;
+      }
+    }
+  }
 }
 
 /* Convert the given USD shader input to an input on the given node. */
diff --git a/source/blender/io/usd/usd.h b/source/blender/io/usd/usd.h
index c8387e32c9d..d9bf6381d22 100644
--- a/source/blender/io/usd/usd.h
+++ b/source/blender/io/usd/usd.h
@@ -72,6 +72,7 @@ struct USDImportParams {
   bool import_guide;
   bool import_proxy;
   bool import_render;
+  bool set_material_blend;
 };
 
 /* The USD_import function takes a as_background_job parameter, and returns a boolean.



More information about the Bf-blender-cvs mailing list