[Bf-blender-cvs] [41f47cd8d6a] soc-2020-io-performance: Make UV coords and face normals optional to write

Ankit Meel noreply at git.blender.org
Wed Jun 17 15:30:04 CEST 2020


Commit: 41f47cd8d6a4699bafa4f6ae973c775f00185810
Author: Ankit Meel
Date:   Wed Jun 17 18:51:29 2020 +0530
Branches: soc-2020-io-performance
https://developer.blender.org/rB41f47cd8d6a4699bafa4f6ae973c775f00185810

Make UV coords and face normals optional to write

Adds options in the UI for writing normals and UV coordinates to
the OBJ file.
{F8627897}

As expected, calculating both of them is also skipped conditionally.
However, the memory for normals is still allocated; just not used.

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

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
M	source/blender/io/wavefront_obj/intern/wavefront_obj_file_handler.cc
M	source/blender/io/wavefront_obj/intern/wavefront_obj_file_handler.hh

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

diff --git a/source/blender/editors/io/io_obj.c b/source/blender/editors/io/io_obj.c
index adf69f33de7..931f591bb40 100644
--- a/source/blender/editors/io/io_obj.c
+++ b/source/blender/editors/io/io_obj.c
@@ -106,6 +106,8 @@ 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");
 
   OBJ_export(C, &export_params);
 
@@ -147,6 +149,17 @@ static void ui_obj_export_settings(uiLayout *layout, PointerRNA *imfptr)
 
   row = uiLayoutRow(box, false);
   uiItemR(row, imfptr, "scaling_factor", 0, NULL, ICON_NONE);
+
+  /* File write options. */
+  box = uiLayoutBox(layout);
+  row = uiLayoutRow(box, false);
+  uiItemL(row, IFACE_("File Writer 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);
 }
 
 static void wm_obj_export_draw(bContext *UNUSED(C), wmOperator *op)
@@ -186,6 +199,7 @@ static bool wm_obj_export_check(bContext *C, wmOperator *op)
   if ((RNA_enum_get(op->ptr, "forward_axis")) % 3 == (RNA_enum_get(op->ptr, "up_axis")) % 3) {
     /* TODO (ankitm) Show a warning here. */
     RNA_enum_set(op->ptr, "up_axis", RNA_enum_get(op->ptr, "up_axis") % 3 + 1);
+    ret = true;
   }
   return ret;
 }
@@ -250,6 +264,9 @@ void WM_OT_obj_export(struct wmOperatorType *ot)
                 "Scaling Factor: both position and object size are affected",
                 0.01,
                 1000.000f);
+  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");
 }
 
 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 9095fa88a8a..3c5dcd8b079 100644
--- a/source/blender/io/wavefront_obj/IO_wavefront_obj.h
+++ b/source/blender/io/wavefront_obj/IO_wavefront_obj.h
@@ -61,6 +61,10 @@ struct OBJExportParams {
   int forward_axis;
   int up_axis;
   float scaling_factor;
+
+  /** File Write Options */
+  bool export_uv;
+  bool export_normals;
 };
 
 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 32e9081b4fa..201db7681d8 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter.cc
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter.cc
@@ -187,9 +187,13 @@ static void get_geometry_per_object(const OBJExportParams *export_params,
   Mesh *me_eval = BKE_object_get_evaluated_mesh(ob_eval);
 
   get_transformed_mesh_vertices(me_eval, ob_eval, object_to_export);
-  get_transformed_vertex_normals(me_eval, ob_eval, object_to_export);
   get_polygon_vert_indices(me_eval, object_to_export);
-  get_uv_coordinates(me_eval, object_to_export);
+  if (export_params->export_normals) {
+    get_transformed_vertex_normals(me_eval, ob_eval, object_to_export);
+  }
+  if (export_params->export_uv) {
+    get_uv_coordinates(me_eval, object_to_export);
+  }
 }
 
 /**
@@ -235,7 +239,7 @@ static void export_frame(bContext *C, const OBJExportParams *export_params, cons
     get_geometry_per_object(export_params, object_to_export);
   }
 
-  write_object_fprintf(filepath, exportable_objects);
+  write_object_fprintf(filepath, exportable_objects, export_params);
 
   for (uint i = 0; i < exportable_objects.size(); i++) {
     MEM_freeN(exportable_objects[i].mvert);
diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_file_handler.cc b/source/blender/io/wavefront_obj/intern/wavefront_obj_file_handler.cc
index 1982a103bfc..a6e2bff6d0f 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_file_handler.cc
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_file_handler.cc
@@ -48,7 +48,8 @@ MALWAYS_INLINE short face_normal_axis_component(const Polygon &poly_to_write,
 
 static void write_geomtery_per_object(FILE *outfile,
                                       OBJ_object_to_export &object_to_export,
-                                      uint offset[3])
+                                      uint offset[3],
+                                      const OBJExportParams *export_params)
 {
   /** Write object name, as seen in outliner. First two characters are ID code, so skipped. */
   fprintf(outfile, "o %s\n", object_to_export.object->id.name + 2);
@@ -62,20 +63,24 @@ static void write_geomtery_per_object(FILE *outfile,
   /**
    * Write texture coordinates, vt u v for all vertices in a object's texture space.
    */
-  for (uint i = 0; i < object_to_export.tot_uv_vertices; i++) {
-    std::array<float, 2> &uv_vertex = object_to_export.uv_coords[i];
-    fprintf(outfile, "vt %f %f\n", uv_vertex[0], uv_vertex[1]);
+  if (export_params->export_uv) {
+    for (uint i = 0; i < object_to_export.tot_uv_vertices; i++) {
+      std::array<float, 2> &uv_vertex = object_to_export.uv_coords[i];
+      fprintf(outfile, "vt %f %f\n", uv_vertex[0], uv_vertex[1]);
+    }
   }
 
   /** Write vn nx ny nz for all face normals. */
-  for (uint i = 0; i < object_to_export.tot_poly; i++) {
-    MVert *vertex_list = object_to_export.mvert;
-    const Polygon &polygon = object_to_export.polygon_list[i];
-    fprintf(outfile,
-            "vn %hd %hd %d\n",
-            face_normal_axis_component(polygon, AXIS_X, vertex_list),
-            face_normal_axis_component(polygon, AXIS_Y, vertex_list),
-            face_normal_axis_component(polygon, AXIS_Z, vertex_list));
+  if (export_params->export_normals) {
+    for (uint i = 0; i < object_to_export.tot_poly; i++) {
+      MVert *vertex_list = object_to_export.mvert;
+      const Polygon &polygon = object_to_export.polygon_list[i];
+      fprintf(outfile,
+              "vn %hd %hd %d\n",
+              face_normal_axis_component(polygon, AXIS_X, vertex_list),
+              face_normal_axis_component(polygon, AXIS_Y, vertex_list),
+              face_normal_axis_component(polygon, AXIS_Z, vertex_list));
+    }
   }
 
   /**
@@ -83,18 +88,60 @@ static void write_geomtery_per_object(FILE *outfile,
    * i-th vn is always i + 1, guaranteed by face normal loop above.
    * Both loop over the same polygon list.
    */
-  for (uint i = 0; i < object_to_export.tot_poly; i++) {
-    const Polygon &polygon = object_to_export.polygon_list[i];
-    fprintf(outfile, "f ");
-    for (int j = 0; j < polygon.total_vertices_per_poly; j++) {
-      /* i + 1: This loop index is 0-based. Indices in OBJ start from 1. */
-      fprintf(outfile,
-              "%d/%d/%d ",
-              polygon.vertex_index[j] + offset[0],
-              polygon.uv_vertex_index[j] + 1 + offset[1],
-              i + 1 + offset[2]);
+  if (export_params->export_normals) {
+    if (export_params->export_uv) {
+      /* Write both normals and UV. f v1/vt1/vn1 */
+      for (uint i = 0; i < object_to_export.tot_poly; i++) {
+        const Polygon &polygon = object_to_export.polygon_list[i];
+        fprintf(outfile, "f ");
+        for (int j = 0; j < polygon.total_vertices_per_poly; j++) {
+          fprintf(outfile,
+                  "%d/%d/%d ",
+                  polygon.vertex_index[j] + offset[0],
+                  polygon.uv_vertex_index[j] + 1 + offset[1],
+                  i + 1 + offset[2]);
+        }
+        fprintf(outfile, "\n");
+      }
+    }
+    else {
+      /* Write normals but not UV. f v1//vn1 */
+      for (uint i = 0; i < object_to_export.tot_poly; i++) {
+        const Polygon &polygon = object_to_export.polygon_list[i];
+        fprintf(outfile, "f ");
+        for (int j = 0; j < polygon.total_vertices_per_poly; j++) {
+          fprintf(outfile, "%d//%d ", polygon.vertex_index[j] + offset[0], i + 1 + offset[2]);
+        }
+        fprintf(outfile, "\n");
+      }
+    }
+  }
+  else {
+    if (export_params->export_uv) {
+      /* Write UV but not normals. f v1/vt1 */
+      for (uint i = 0; i < object_to_export.tot_poly; i++) {
+        const Polygon &polygon = object_to_export.polygon_list[i];
+        fprintf(outfile, "f ");
+        for (int j = 0; j < polygon.total_vertices_per_poly; j++) {
+          fprintf(outfile,
+                  "%d/%d ",
+                  polygon.vertex_index[j] + offset[0],
+                  polygon.uv_vertex_index[j] + 1 + offset[1]);
+        }
+        fprintf(outfile, "\n");
+      }
+    }
+    else {
+      /* Write neither normals nor UV. f v1 */
+      for (uint i = 0; i < object_to_export.tot_poly; i++) {
+        const Polygon &polygon = object_to_export.polygon_list[i];
+        fprintf(outfile, "f ");
+        for (int j = 0; j < polygon.total_vertices_per_poly; j++) {
+          fprintf(outfile, "%d ", polygon.vertex_index[j] + offset[0]);
+        }
+        fprintf(outfile, "\n");
+      }
     }
-    fprintf(outfile, "\n");
   }
 }
 
@@ -102,7 +149,8 @@ static void write_geomtery_per_object(FILE *outfile,
  * Low level writer to the OBJ file at filepath.
  */
 void write_object_fprintf(const char *filepath,
-                          std::vector<OBJ_object_to_export> &objects_to_export)
+                          std::vector<OBJ_object_to_export> &objects_to_export,
+                          const OBJExportParams *export_params)
 {
   FILE *outfile = fopen(filepath, "w");
   if (outfile == NULL) {
@@ -118,7 +166,7 @@ void write_object_fprintf(const char *filepath,
 
   fprintf(outfile, "# Blender 2.90\n");
   for (uint i = 0; i < objects_to_export.size(); i++) {
-    write_geomtery_per_object(outfile, objects_to_export[i], index_offset);
+    write_geomtery_per_object(outfile, objects_to_export[i], index_offset, export_params);
     index_offset[0] += objects_to_export[i].tot_vertices;
     index_offset[1] += objects_to_export[i

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list