[Bf-blender-cvs] [3c8131fde31] temp-usd-preview-surf-export: USD Export: USD Preview Surface conversion.

Michael Kowalski noreply at git.blender.org
Tue Dec 21 14:36:21 CET 2021


Commit: 3c8131fde31542149b46dcc675e4ef18db4913c4
Author: Michael Kowalski
Date:   Tue Dec 21 08:24:45 2021 -0500
Branches: temp-usd-preview-surf-export
https://developer.blender.org/rB3c8131fde31542149b46dcc675e4ef18db4913c4

USD Export: USD Preview Surface conversion.

Added "USD Preview Surface From Nodes" export option,
to convert a Principled BSDF material node tree to an
approximate USD Preview Surface shader representation.

Also added the following options for texture export.

Export Textures: If converting Preview Surface, export
textures referenced by shader nodes to a 'textures'
directory next to the USD file.

Overwrite Textures: Allow overwriting existing texture
files when exporting textures (this option is off by
default).

Relative Texture Paths: Save material texture asset
paths as relative paths in the USD.

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

M	source/blender/editors/io/io_usd.c
M	source/blender/io/usd/CMakeLists.txt
M	source/blender/io/usd/intern/usd_writer_abstract.cc
M	source/blender/io/usd/intern/usd_writer_abstract.h
A	source/blender/io/usd/intern/usd_writer_material.cc
A	source/blender/io/usd/intern/usd_writer_material.h
M	source/blender/io/usd/intern/usd_writer_mesh.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 4e2ccea36ab..386b752887c 100644
--- a/source/blender/editors/io/io_usd.c
+++ b/source/blender/editors/io/io_usd.c
@@ -131,6 +131,11 @@ static int wm_usd_export_exec(bContext *C, wmOperator *op)
   const bool use_instancing = RNA_boolean_get(op->ptr, "use_instancing");
   const bool evaluation_mode = RNA_enum_get(op->ptr, "evaluation_mode");
 
+  const bool generate_preview_surface = RNA_boolean_get(op->ptr, "generate_preview_surface");
+  const bool export_textures = RNA_boolean_get(op->ptr, "export_textures");
+  const bool overwrite_textures = RNA_boolean_get(op->ptr, "overwrite_textures");
+  const bool relative_texture_paths = RNA_boolean_get(op->ptr, "relative_texture_paths");
+
   struct USDExportParams params = {
       export_animation,
       export_hair,
@@ -141,6 +146,10 @@ static int wm_usd_export_exec(bContext *C, wmOperator *op)
       visible_objects_only,
       use_instancing,
       evaluation_mode,
+      generate_preview_surface,
+      export_textures,
+      overwrite_textures,
+      relative_texture_paths,
   };
 
   bool ok = USD_export(C, filename, &params, as_background_job);
@@ -172,6 +181,27 @@ static void wm_usd_export_draw(bContext *UNUSED(C), wmOperator *op)
   col = uiLayoutColumn(box, true);
   uiItemR(col, ptr, "evaluation_mode", 0, NULL, ICON_NONE);
 
+  box = uiLayoutBox(layout);
+  col = uiLayoutColumnWithHeading(box, true, IFACE_("Materials"));
+  uiItemR(col, ptr, "generate_preview_surface", 0, NULL, ICON_NONE);
+  bool export_mtl = RNA_boolean_get(ptr, "export_materials");
+
+  uiLayoutSetEnabled(col, export_mtl);
+
+  uiLayout *row = uiLayoutRow(col, true);
+  uiItemR(row, ptr, "export_textures", 0, NULL, ICON_NONE);
+  bool preview = RNA_boolean_get(ptr, "generate_preview_surface");
+  uiLayoutSetEnabled(row, export_mtl && preview);
+
+  row = uiLayoutRow(col, true);
+  uiItemR(row, ptr, "overwrite_textures", 0, NULL, ICON_NONE);
+  bool export_tex = RNA_boolean_get(ptr, "export_textures");
+  uiLayoutSetEnabled(row, export_mtl && preview && export_tex);
+
+  row = uiLayoutRow(col, true);
+  uiItemR(row, ptr, "relative_texture_paths", 0, NULL, ICON_NONE);
+  uiLayoutSetEnabled(row, export_mtl && preview);
+
   box = uiLayoutBox(layout);
   uiItemL(box, IFACE_("Experimental"), ICON_NONE);
   uiItemR(box, ptr, "use_instancing", 0, NULL, ICON_NONE);
@@ -249,6 +279,32 @@ void WM_OT_usd_export(struct wmOperatorType *ot)
                "Use Settings for",
                "Determines visibility of objects, modifier settings, and other areas where there "
                "are different settings for viewport and rendering");
+
+  RNA_def_boolean(ot->srna,
+                  "generate_preview_surface",
+                  true,
+                  "USD Preview Surface From Nodes",
+                  "Generate an approximate USD Preview Surface shader "
+                  "representation of a Principled BSDF node network ");
+
+  RNA_def_boolean(ot->srna,
+                  "export_textures",
+                  true,
+                  "Export Textures",
+                  "If exporting materials, export textures referenced by material nodes "
+                  "to a 'textures' directory in the same directory as the USD");
+
+  RNA_def_boolean(ot->srna,
+                 "overwrite_textures",
+                 false,
+                 "Overwrite Textures",
+                 "Allow overwriting existing texture files when exporting textures");
+
+  RNA_def_boolean(ot->srna,
+                  "relative_texture_paths",
+                  true,
+                  "Relative Texture Paths",
+                  "When checked, material texture asset paths will be saved as relative paths in the USD");
 }
 
 /* ====== USD Import ====== */
diff --git a/source/blender/io/usd/CMakeLists.txt b/source/blender/io/usd/CMakeLists.txt
index 12015bf1698..980f33fffa1 100644
--- a/source/blender/io/usd/CMakeLists.txt
+++ b/source/blender/io/usd/CMakeLists.txt
@@ -64,6 +64,7 @@ set(SRC
   intern/usd_writer_camera.cc
   intern/usd_writer_hair.cc
   intern/usd_writer_light.cc
+  intern/usd_writer_material.cc
   intern/usd_writer_mesh.cc
   intern/usd_writer_metaball.cc
   intern/usd_writer_transform.cc
@@ -89,6 +90,7 @@ set(SRC
   intern/usd_writer_camera.h
   intern/usd_writer_hair.h
   intern/usd_writer_light.h
+  intern/usd_writer_material.h
   intern/usd_writer_mesh.h
   intern/usd_writer_metaball.h
   intern/usd_writer_transform.h
diff --git a/source/blender/io/usd/intern/usd_writer_abstract.cc b/source/blender/io/usd/intern/usd_writer_abstract.cc
index 2b5326eb4c1..99856c80ca4 100644
--- a/source/blender/io/usd/intern/usd_writer_abstract.cc
+++ b/source/blender/io/usd/intern/usd_writer_abstract.cc
@@ -18,11 +18,15 @@
  */
 #include "usd_writer_abstract.h"
 #include "usd_hierarchy_iterator.h"
+#include "usd_writer_material.h"
 
 #include <pxr/base/tf/stringUtils.h>
 
+#include "BKE_customdata.h"
 #include "BLI_assert.h"
 
+#include "DNA_mesh_types.h"
+
 /* TfToken objects are not cheap to construct, so we do it once. */
 namespace usdtokens {
 /* Materials */
@@ -36,6 +40,47 @@ static const pxr::TfToken surface("surface", pxr::TfToken::Immortal);
 
 namespace blender::io::usd {
 
+/* The following three utility function for getting active layers
+ * were coppied from the collada utils. */
+
+/* Returns name of the active layer of the given type or null
+ * if no such active layer is defined. */
+static const char *customData_get_active_layer_name(const CustomData *data, int type)
+{
+  /* get the layer index of the active layer of type */
+  int layer_index = CustomData_get_active_layer_index(data, type);
+  if (layer_index < 0) {
+    return nullptr;
+  }
+
+  return data->layers[layer_index].name;
+}
+
+/* Returns name of the active UV layer or the empty string if no active
+ * UV Layer defined. */
+static std::string get_active_uvlayer_name(Mesh *me)
+{
+  int num_layers = CustomData_number_of_layers(&me->ldata, CD_MLOOPUV);
+  if (num_layers) {
+    const char *layer_name = customData_get_active_layer_name(&me->ldata, CD_MLOOPUV);
+    if (layer_name) {
+      return std::string(layer_name);
+    }
+  }
+  return "";
+}
+
+/* Returns name of the acive Layer or empty String if no active UV Layer defined,
+ * assuming the Object is of type MESH. */
+static std::string get_active_uvlayer_name(Object *ob)
+{
+  if (!ob || !ob->type == OB_MESH) {
+    return "";
+  }
+  Mesh *me = static_cast<Mesh *>(ob->data);
+  return get_active_uvlayer_name(me);
+}
+
 USDAbstractWriter::USDAbstractWriter(const USDExporterContext &usd_export_context)
     : usd_export_context_(usd_export_context), frame_has_been_written_(false), is_animated_(false)
 {
@@ -78,7 +123,8 @@ const pxr::SdfPath &USDAbstractWriter::usd_path() const
   return usd_export_context_.usd_path;
 }
 
-pxr::UsdShadeMaterial USDAbstractWriter::ensure_usd_material(Material *material)
+pxr::UsdShadeMaterial USDAbstractWriter::ensure_usd_material(Material *material,
+                                                             const HierarchyContext &context)
 {
   static pxr::SdfPath material_library_path("/_materials");
   pxr::UsdStageRefPtr stage = usd_export_context_.stage;
@@ -92,17 +138,14 @@ pxr::UsdShadeMaterial USDAbstractWriter::ensure_usd_material(Material *material)
   }
   usd_material = pxr::UsdShadeMaterial::Define(stage, usd_path);
 
-  /* Construct the shader. */
-  pxr::SdfPath shader_path = usd_path.AppendChild(usdtokens::preview_shader);
-  pxr::UsdShadeShader shader = pxr::UsdShadeShader::Define(stage, shader_path);
-  shader.CreateIdAttr(pxr::VtValue(usdtokens::preview_surface));
-  shader.CreateInput(usdtokens::diffuse_color, pxr::SdfValueTypeNames->Color3f)
-      .Set(pxr::GfVec3f(material->r, material->g, material->b));
-  shader.CreateInput(usdtokens::roughness, pxr::SdfValueTypeNames->Float).Set(material->roughness);
-  shader.CreateInput(usdtokens::metallic, pxr::SdfValueTypeNames->Float).Set(material->metallic);
-
-  /* Connect the shader and the material together. */
-  usd_material.CreateSurfaceOutput().ConnectToSource(shader, usdtokens::surface);
+  if (material->use_nodes && this->usd_export_context_.export_params.generate_preview_surface) {
+    std::string active_uv_name = get_active_uvlayer_name(context.object);
+    create_usd_preview_surface_material(
+      this->usd_export_context_, material, usd_material, active_uv_name);
+  }
+  else {
+    create_usd_viewport_material(this->usd_export_context_, material, usd_material);
+  }
 
   return usd_material;
 }
diff --git a/source/blender/io/usd/intern/usd_writer_abstract.h b/source/blender/io/usd/intern/usd_writer_abstract.h
index dd81dd47c83..b87a04926f2 100644
--- a/source/blender/io/usd/intern/usd_writer_abstract.h
+++ b/source/blender/io/usd/intern/usd_writer_abstract.h
@@ -69,7 +69,8 @@ class USDAbstractWriter : public AbstractHierarchyWriter {
   virtual void do_write(HierarchyContext &context) = 0;
   pxr::UsdTimeCode get_export_time_code() const;
 
-  pxr::UsdShadeMaterial ensure_usd_material(Material *material);
+  pxr::UsdShadeMaterial ensure_usd_material(Material *material,
+                                            const HierarchyContext &context);
 
   void write_visibility(const HierarchyContext &context,
                         const pxr::UsdTimeCode timecode,
diff --git a/source/blender/io/usd/intern/usd_writer_material.cc b/source/blender/io/usd/intern/usd_writer_material.cc
new file mode 100644
index 00000000000..18ecdac09c9
--- /dev/null
+++ b/source/blender/io/usd/intern/usd_writer_material.cc
@@ -0,0 +1,934 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public Licens

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list