[Bf-blender-cvs] [c3f8e9550c1] soc-2020-io-performance: Export triangulated mesh, not modifying the scene

Ankit Meel noreply at git.blender.org
Thu Jun 18 11:04:49 CEST 2020


Commit: c3f8e9550c1ea54a9e16e56fd2945e0ca26fd415
Author: Ankit Meel
Date:   Thu Jun 18 13:45:16 2020 +0530
Branches: soc-2020-io-performance
https://developer.blender.org/rBc3f8e9550c1ea54a9e16e56fd2945e0ca26fd415

Export triangulated mesh, not modifying the scene

Add UI checkbox for triangulating before writing the OBJ file.
The scene is not modified at all. Actual modifier should be used if one
needs the mesh to be modified too.

The settings for triangulation are the same as default ones of the
modifier:
ngon-method: "Beauty", quad-method: "Shortest Diagonal", min vertices:4

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

M	source/blender/editors/io/io_obj.c
M	source/blender/io/wavefront_obj/IO_wavefront_obj.h
M	source/blender/io/wavefront_obj/intern/wavefront_obj_exporter.cc

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

diff --git a/source/blender/editors/io/io_obj.c b/source/blender/editors/io/io_obj.c
index 931f591bb40..bf915f64ce1 100644
--- a/source/blender/editors/io/io_obj.c
+++ b/source/blender/editors/io/io_obj.c
@@ -106,8 +106,10 @@ static int wm_obj_export_exec(bContext *C, wmOperator *op)
   export_params.forward_axis = RNA_enum_get(op->ptr, "forward_axis");
   export_params.up_axis = RNA_enum_get(op->ptr, "up_axis");
   export_params.scaling_factor = RNA_float_get(op->ptr, "scaling_factor");
+
   export_params.export_uv = RNA_boolean_get(op->ptr, "export_uv");
   export_params.export_normals = RNA_boolean_get(op->ptr, "export_normals");
+  export_params.export_triangulated = RNA_boolean_get(op->ptr, "export_triangulated_mesh");
 
   OBJ_export(C, &export_params);
 
@@ -136,7 +138,7 @@ static void ui_obj_export_settings(uiLayout *layout, PointerRNA *imfptr)
   uiItemR(row, imfptr, "end_frame", 0, NULL, ICON_NONE);
   uiLayoutSetEnabled(row, export_animation);
 
-  /* Transform options. */
+  /* Geometry Transform options. */
   box = uiLayoutBox(layout);
   row = uiLayoutRow(box, false);
   uiItemL(row, IFACE_("Geometry Transform"), ICON_NONE);
@@ -153,13 +155,16 @@ static void ui_obj_export_settings(uiLayout *layout, PointerRNA *imfptr)
   /* File write options. */
   box = uiLayoutBox(layout);
   row = uiLayoutRow(box, false);
-  uiItemL(row, IFACE_("File Writer Options"), ICON_NONE);
+  uiItemL(row, IFACE_("File Write Options"), ICON_NONE);
 
   row = uiLayoutRow(box, false);
   uiItemR(row, imfptr, "export_uv", 0, NULL, ICON_NONE);
 
   row = uiLayoutRow(box, false);
   uiItemR(row, imfptr, "export_normals", 0, NULL, ICON_NONE);
+
+  row = uiLayoutRow(box, false);
+  uiItemR(row, imfptr, "export_triangulated_mesh", 0, NULL, ICON_NONE);
 }
 
 static void wm_obj_export_draw(bContext *UNUSED(C), wmOperator *op)
@@ -267,6 +272,13 @@ void WM_OT_obj_export(struct wmOperatorType *ot)
   RNA_def_boolean(ot->srna, "export_uv", true, "Export UVs", "Export UV coordinates");
   RNA_def_boolean(
       ot->srna, "export_normals", true, "Export normals", "Export per face per vertex normals");
+  RNA_def_boolean(
+      ot->srna,
+      "export_triangulated_mesh",
+      false,
+      "Export Triangulated Mesh",
+      "The mesh in viewport will not be affected. Behaves the same as Triangulate Modifier with "
+      "ngon-method: \"Beauty\", quad-method: \"Shortest Diagonal\", min vertices: 4");
 }
 
 static int wm_obj_import_invoke(bContext *C, wmOperator *op, const wmEvent *event)
diff --git a/source/blender/io/wavefront_obj/IO_wavefront_obj.h b/source/blender/io/wavefront_obj/IO_wavefront_obj.h
index 3c5dcd8b079..1e068d69204 100644
--- a/source/blender/io/wavefront_obj/IO_wavefront_obj.h
+++ b/source/blender/io/wavefront_obj/IO_wavefront_obj.h
@@ -65,6 +65,7 @@ struct OBJExportParams {
   /** File Write Options */
   bool export_uv;
   bool export_normals;
+  bool export_triangulated;
 };
 
 struct OBJImportParams {
diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter.cc b/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter.cc
index 201db7681d8..6f095a782ec 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter.cc
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter.cc
@@ -26,6 +26,7 @@
 #include <stdio.h>
 #include <vector>
 
+#include "BKE_lib_id.h"
 #include "BKE_mesh.h"
 #include "BKE_mesh_mapping.h"
 #include "BKE_mesh_runtime.h"
@@ -35,13 +36,18 @@
 #include "BLI_path_util.h"
 #include "BLI_vector.hh"
 
+#include "bmesh.h"
+#include "bmesh_tools.h"
+
 #include "DEG_depsgraph.h"
 #include "DEG_depsgraph_query.h"
 
 #include "DNA_layer_types.h"
+#include "DNA_modifier_types.h"
 #include "DNA_scene_types.h"
 
 #include "ED_object.h"
+
 #include "IO_wavefront_obj.h"
 
 #include "wavefront_obj.hh"
@@ -186,6 +192,35 @@ static void get_geometry_per_object(const OBJExportParams *export_params,
   Object *ob_eval = DEG_get_evaluated_object(depsgraph, ob);
   Mesh *me_eval = BKE_object_get_evaluated_mesh(ob_eval);
 
+  /* Defined here to free it outside the following if-block. */
+  Mesh *triangulated;
+  /* Obtain triangulated mesh to later evaluate its face vertices and indices. */
+  if (export_params->export_triangulated) {
+    struct BMeshCreateParams bm_create_params {
+      .use_toolflags = false
+    };
+    struct BMeshFromMeshParams bm_convert_params {
+      /* If false, it triggers BLI_assert(BM_face_is_normal_valid(f))
+       * We don't need the face normals it calculates.
+       */
+      .calc_face_normal = true, 0, 0, 0
+    };
+    int triangulate_min_verts = 4;
+    BMesh *bmesh = BKE_mesh_to_bmesh_ex(me_eval, &bm_create_params, &bm_convert_params);
+
+    BM_mesh_triangulate(bmesh,
+                        MOD_TRIANGULATE_NGON_BEAUTY,
+                        MOD_TRIANGULATE_QUAD_SHORTEDGE,
+                        triangulate_min_verts,
+                        false,
+                        NULL,
+                        NULL,
+                        NULL);
+    triangulated = BKE_mesh_from_bmesh_for_eval_nomain(bmesh, NULL, me_eval);
+    me_eval = triangulated;
+    BM_mesh_free(bmesh);
+  }
+
   get_transformed_mesh_vertices(me_eval, ob_eval, object_to_export);
   get_polygon_vert_indices(me_eval, object_to_export);
   if (export_params->export_normals) {
@@ -194,6 +229,9 @@ static void get_geometry_per_object(const OBJExportParams *export_params,
   if (export_params->export_uv) {
     get_uv_coordinates(me_eval, object_to_export);
   }
+  if (export_params->export_triangulated) {
+    BKE_id_free(NULL, triangulated);
+  }
 }
 
 /**



More information about the Bf-blender-cvs mailing list