[Bf-blender-cvs] [e27cd0e8927] temp-usd-prev-export2: USD material export code cleanup.

Michael Kowalski noreply at git.blender.org
Sat Jan 15 02:22:43 CET 2022


Commit: e27cd0e8927a879fe7584f8654060d9840e0c72c
Author: Michael Kowalski
Date:   Fri Jan 14 19:48:48 2022 -0500
Branches: temp-usd-prev-export2
https://developer.blender.org/rBe27cd0e8927a879fe7584f8654060d9840e0c72c

USD material export code cleanup.

Moved repeated code for setting the shader
input value to a common function, as suggested
by Sybren in his review.

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

M	source/blender/io/usd/intern/usd_writer_material.cc

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

diff --git a/source/blender/io/usd/intern/usd_writer_material.cc b/source/blender/io/usd/intern/usd_writer_material.cc
index 4d3cad08842..0d28d322c47 100644
--- a/source/blender/io/usd/intern/usd_writer_material.cc
+++ b/source/blender/io/usd/intern/usd_writer_material.cc
@@ -122,6 +122,10 @@ static std::string get_tex_image_asset_path(bNode *node,
 static InputSpecMap &preview_surface_input_map();
 static bNode *traverse_channel(bNodeSocket *input, short target_type);
 
+template<typename T1, typename T2> void create_input(pxr::UsdShadeShader &shader,
+                                                     const InputSpec &spec,
+                                                     const void *value);
+
 void create_usd_preview_surface_material(const USDExporterContext &usd_export_context,
                                          Material *material,
                                          pxr::UsdShadeMaterial &usd_material,
@@ -179,24 +183,16 @@ void create_usd_preview_surface_material(const USDExporterContext &usd_export_co
       /* Set hardcoded value. */
       switch (sock->type) {
         case SOCK_FLOAT: {
-          bNodeSocketValueFloat *float_value = static_cast<bNodeSocketValueFloat *>(
-              sock->default_value);
-          preview_surface.CreateInput(input_spec.input_name, input_spec.input_type)
-              .Set(pxr::VtValue(float_value->value));
+          create_input<bNodeSocketValueFloat, float>(
+              preview_surface, input_spec, sock->default_value);
         } break;
         case SOCK_VECTOR: {
-          bNodeSocketValueVector *vec_data = static_cast<bNodeSocketValueVector *>(
-              sock->default_value);
-          preview_surface.CreateInput(input_spec.input_name, input_spec.input_type)
-              .Set(pxr::VtValue(
-                  pxr::GfVec3f(vec_data->value[0], vec_data->value[1], vec_data->value[2])));
+          create_input<bNodeSocketValueVector, pxr::GfVec3f>(
+              preview_surface, input_spec, sock->default_value);
         } break;
         case SOCK_RGBA: {
-          bNodeSocketValueRGBA *rgba_data = static_cast<bNodeSocketValueRGBA *>(
-              sock->default_value);
-          preview_surface.CreateInput(input_spec.input_name, input_spec.input_type)
-              .Set(pxr::VtValue(
-                  pxr::GfVec3f(rgba_data->value[0], rgba_data->value[1], rgba_data->value[2])));
+          create_input<bNodeSocketValueRGBA, pxr::GfVec3f>(
+              preview_surface, input_spec, sock->default_value);
         } break;
         default:
           break;
@@ -260,6 +256,18 @@ static InputSpecMap &preview_surface_input_map()
   return input_map;
 }
 
+/* Create an input on the given shader with name and type
+ * provided by the InputSpec and assign the given value to the
+ * input.  Parameters T1 and T2 indicate the Blender and USD
+ * value types, respectively. */
+template<typename T1, typename T2> void create_input(pxr::UsdShadeShader &shader,
+                                                     const InputSpec &spec,
+                                                     const void *value)
+{
+  const T1 *cast_value = static_cast<const T1 *>(value);
+  shader.CreateInput(spec.input_name, spec.input_type).Set(T2(cast_value->value));
+}
+
 /* Find the UVMAP node input to the given texture image node and convert it
  * to a USD primvar reader shader. If no UVMAP node is found, create a primvar
  * reader for the given default uv set.  The primvar reader will be attached to



More information about the Bf-blender-cvs mailing list