[Bf-blender-cvs] [69251411bc7] universal-scene-description: USD Export: Added triangulate options

Michael Kowalski noreply at git.blender.org
Wed Sep 28 20:38:17 CEST 2022


Commit: 69251411bc7e897e571a944d6ed016f3b7d76081
Author: Michael Kowalski
Date:   Wed Sep 28 14:36:41 2022 -0400
Branches: universal-scene-description
https://developer.blender.org/rB69251411bc7e897e571a944d6ed016f3b7d76081

USD Export: Added triangulate options

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

M	source/blender/editors/io/io_usd.c
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 044d5dfad44..abc94836d9c 100644
--- a/source/blender/editors/io/io_usd.c
+++ b/source/blender/editors/io/io_usd.c
@@ -320,6 +320,11 @@ static int wm_usd_export_exec(bContext *C, wmOperator *op)
 
   const bool usdz_is_arkit = RNA_boolean_get(op->ptr, "usdz_is_arkit");
 
+  const bool triangulate_meshes = RNA_boolean_get(op->ptr, "triangulate_meshes");
+
+  const int quad_method = RNA_enum_get(op->ptr, "quad_method");
+  const int ngon_method = RNA_enum_get(op->ptr, "ngon_method");
+
   const bool export_blender_metadata = RNA_boolean_get(op->ptr, "export_blender_metadata");
 
   struct USDExportParams params = {RNA_int_get(op->ptr, "start"),
@@ -382,7 +387,10 @@ static int wm_usd_export_exec(bContext *C, wmOperator *op)
                                    usdz_downscale_size,
                                    usdz_downscale_custom_size,
                                    usdz_is_arkit,
-                                   export_blender_metadata};
+                                   export_blender_metadata,
+                                   triangulate_meshes,
+                                   quad_method,
+                                   ngon_method};
 
   /* Take some defaults from the scene, if not specified explicitly. */
   Scene *scene = CTX_data_scene(C);
@@ -471,6 +479,13 @@ static void wm_usd_export_draw(bContext *C, wmOperator *op)
     uiItemR(box, ptr, "export_face_maps", 0, NULL, ICON_NONE);
     uiItemR(box, ptr, "export_uvmaps", 0, NULL, ICON_NONE);
     uiItemR(box, ptr, "export_normals", 0, NULL, ICON_NONE);
+
+    uiItemR(box, ptr, "triangulate_meshes", 0, NULL, ICON_NONE);
+
+    uiLayout *sub = uiLayoutColumn(box, false);
+    uiLayoutSetActive(sub, RNA_boolean_get(ptr, "triangulate_meshes"));
+    uiItemR(sub, ptr, "quad_method", 0, IFACE_("Method Quads"), ICON_NONE);
+    uiItemR(sub, ptr, "ngon_method", 0, IFACE_("Polygons"), ICON_NONE);
   }
 
   box = uiLayoutBox(layout);
@@ -533,7 +548,6 @@ static void wm_usd_export_draw(bContext *C, wmOperator *op)
       uiItemR(box, ptr, "overwrite_textures", 0, NULL, ICON_NONE);
     }
 
-    //!TODO: add custom size int
     uiItemR(box, ptr, "usdz_downscale_size", 0, NULL, ICON_NONE);
     if (RNA_enum_get(ptr, "usdz_downscale_size") == USD_TEXTURE_SIZE_CUSTOM) {
       uiItemR(box, ptr, "usdz_downscale_custom_size", 0, NULL, ICON_NONE);
@@ -548,7 +562,6 @@ static void wm_usd_export_draw(bContext *C, wmOperator *op)
   uiItemL(box, IFACE_("Experimental:"), ICON_NONE);
   uiItemR(box, ptr, "use_instancing", 0, NULL, ICON_NONE);
   uiItemR(box, ptr, "fix_skel_root", 0, NULL, ICON_NONE);
-
 }
 
 static bool wm_usd_export_check(bContext *UNUSED(C), wmOperator *op)
@@ -976,6 +989,26 @@ void WM_OT_usd_export(struct wmOperatorType *ot)
                   true,
                   "Export Blender Metadata",
                   "Write Blender-specific information to the Stage's customLayerData");
+
+  RNA_def_boolean(ot->srna,
+                  "triangulate_meshes",
+                  false,
+                  "Triangulate Meshes",
+                  "Triangulate meshes during export");
+
+  RNA_def_enum(ot->srna,
+               "quad_method",
+               rna_enum_modifier_triangulate_quad_method_items,
+               MOD_TRIANGULATE_QUAD_SHORTEDGE,
+               "Quad Method",
+               "Method for splitting the quads into triangles");
+
+  RNA_def_enum(ot->srna,
+               "ngon_method",
+               rna_enum_modifier_triangulate_ngon_method_items,
+               MOD_TRIANGULATE_NGON_BEAUTY,
+               "N-gon Method",
+               "Method for splitting the n-gons into triangles");
 }
 
 /* ====== USD Import ====== */
diff --git a/source/blender/io/usd/intern/usd_writer_mesh.cc b/source/blender/io/usd/intern/usd_writer_mesh.cc
index 8b2d76d92f5..cdf38d65697 100644
--- a/source/blender/io/usd/intern/usd_writer_mesh.cc
+++ b/source/blender/io/usd/intern/usd_writer_mesh.cc
@@ -21,6 +21,9 @@
 #include "BKE_modifier.h"
 #include "BKE_object.h"
 
+#include "bmesh.h"
+#include "bmesh_tools.h"
+
 #include "DEG_depsgraph.h"
 
 #include "DNA_layer_types.h"
@@ -130,6 +133,29 @@ void USDGenericMeshWriter::do_write(HierarchyContext &context)
     return;
   }
 
+  if (usd_export_context_.export_params.triangulate_meshes) {
+    const bool tag_only = false;
+    const int quad_method = usd_export_context_.export_params.quad_method;
+    const int ngon_method = usd_export_context_.export_params.ngon_method;
+
+    BMeshCreateParams bmesh_create_params{};
+    BMeshFromMeshParams bmesh_from_mesh_params{};
+    bmesh_from_mesh_params.calc_face_normal = true;
+    bmesh_from_mesh_params.calc_vert_normal = true;
+    BMesh *bm = BKE_mesh_to_bmesh_ex(mesh, &bmesh_create_params, &bmesh_from_mesh_params);
+
+    BM_mesh_triangulate(bm, quad_method, ngon_method, 4, tag_only, nullptr, nullptr, nullptr);
+
+    Mesh *triangulated_mesh = BKE_mesh_from_bmesh_for_eval_nomain(bm, nullptr, mesh);
+    BM_mesh_free(bm);
+
+    if (needsfree) {
+      free_export_mesh(mesh);
+    }
+    mesh = triangulated_mesh;
+    needsfree = true;
+  }
+
   try {
     write_mesh(context, mesh);
 
diff --git a/source/blender/io/usd/usd.h b/source/blender/io/usd/usd.h
index 2baf6bb7e9f..3523547b58c 100644
--- a/source/blender/io/usd/usd.h
+++ b/source/blender/io/usd/usd.h
@@ -135,6 +135,9 @@ struct USDExportParams {
   int usdz_downscale_custom_size;
   bool usdz_is_arkit;
   bool export_blender_metadata;
+  bool triangulate_meshes;
+  int quad_method;
+  int ngon_method;
 };
 
 struct USDImportParams {
@@ -171,6 +174,7 @@ struct USDImportParams {
   bool create_background_shader;
   eUSDMtlNameCollisionMode mtl_name_collision_mode;
   eUSDAttrImportMode attr_import_mode;
+  bool triangulate_meshes;
 };
 
 /* The USD_export takes a as_background_job parameter, and returns a boolean.



More information about the Bf-blender-cvs mailing list