[Bf-blender-cvs] [c2eb16f6624] soc-2020-io-performance: Cleanup: style guide, reorder classes, comments

Ankit Meel noreply at git.blender.org
Fri Jul 3 17:21:54 CEST 2020


Commit: c2eb16f66249f73a69f2a87aa0bd33e5fb6f526f
Author: Ankit Meel
Date:   Fri Jul 3 20:51:18 2020 +0530
Branches: soc-2020-io-performance
https://developer.blender.org/rBc2eb16f66249f73a69f2a87aa0bd33e5fb6f526f

Cleanup: style guide, reorder classes, comments

https://wiki.blender.org/wiki/Style_Guide

Changes here:
Move non trivial and non-inlined implementations to cc files instead
of header files.

Reorder classes according to style guide.

Remove documentation comments from header files and keep it near
implementation

Break up some functions into smaller ones: `insert_frame_in_path` etc.

Use reference as much as possible, instead of pointers.

Move `_` prefix to suffix of private class members.

Clean up warnings of implicit float conversions, uint conversions.

Use `ATTR_FALLTHROUGH` in switch cases. Move break inside the case
braces.

Remove `blender::io::obj::OBJ_export` function. It was a mistake.

Use `const` in getters.

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

M	source/blender/io/wavefront_obj/intern/wavefront_obj.cc
M	source/blender/io/wavefront_obj/intern/wavefront_obj.hh
M	source/blender/io/wavefront_obj/intern/wavefront_obj_exporter.cc
M	source/blender/io/wavefront_obj/intern/wavefront_obj_exporter.hh
M	source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_mesh.cc
M	source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_mesh.hh
M	source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_mtl.cc
M	source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_mtl.hh
M	source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_nurbs.cc
M	source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_nurbs.hh
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/io/wavefront_obj/intern/wavefront_obj.cc b/source/blender/io/wavefront_obj/intern/wavefront_obj.cc
index 75c7a5164ee..e1fc5a38fb0 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj.cc
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj.cc
@@ -31,18 +31,19 @@
 #include "wavefront_obj_exporter.hh"
 
 /**
- * Called from io_obj.c. Calls internal functions in IO:OBJ.
- * When more preferences are there, will be used to set appropriate flags.
+ * Time the full export process.
+ * TODO ankitm Collect errors in the export process and notify the user here.
  */
 void OBJ_export(bContext *C, const OBJExportParams *export_params)
 {
   double start_time = PIL_check_seconds_timer();
-  blender::io::obj::exporter_main(C, export_params);
+  blender::io::obj::exporter_main(C, *export_params);
   double end_time = PIL_check_seconds_timer();
   std::cout << "\nOBJ export time: " << (end_time - start_time) * 1000 << " milliseconds\n";
 }
+
 /**
- * Called from io_obj.c. Currently not implemented.
+ * Currently not implemented.
  */
 void OBJ_import(bContext *UNUSED(C), const OBJImportParams *UNUSED(import_params))
 {
diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj.hh b/source/blender/io/wavefront_obj/intern/wavefront_obj.hh
index f8a11fb5e5b..406f457a378 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj.hh
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj.hh
@@ -24,24 +24,4 @@
 #ifndef __WAVEFRONT_OBJ_HH__
 #define __WAVEFRONT_OBJ_HH__
 
-#include "IO_wavefront_obj.h"
-
-namespace blender {
-namespace io {
-namespace obj {
-
-/**
- * Called from io_obj.c. Calls internal functions in IO:OBJ.
- * When more preferences are there, will be used to set appropriate flags.
- */
-void OBJ_export(bContext *C, const OBJExportParams *export_params);
-/**
- * Called from io_obj.c. Currently not implemented.
- */
-void OBJ_import(bContext *C, const OBJImportParams *import_params);
-
-}  // namespace obj
-}  // namespace io
-}  // namespace blender
-
 #endif /* __WAVEFRONT_OBJ_HH__ */
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 4da0b5c4bad..3de99d3f25a 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter.cc
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter.cc
@@ -40,54 +40,65 @@
 #include "wavefront_obj_exporter_nurbs.hh"
 #include "wavefront_obj_file_handler.hh"
 
-namespace blender {
-namespace io {
-namespace obj {
-
+namespace blender::io::obj {
 /**
- * Scan objects in a scene to find exportable objects, as per export_params and object types, and
+ * Scan objects in a scene to find exportable objects, as per export settings and object types, and
  * add them to the given Vectors.
+ * \note Curves are also stored as OBJMesh if export settings specify so.
  */
-static void filter_exportable_objects(bContext *C,
-                                      const OBJExportParams *export_params,
-                                      Vector<std::unique_ptr<OBJMesh>> &exportable_meshes,
-                                      Vector<std::unique_ptr<OBJNurbs>> &exportable_nurbs)
+static void find_exportable_objects(ViewLayer *view_layer,
+                                    Depsgraph *depsgraph,
+                                    const OBJExportParams &export_params,
+                                    Vector<std::unique_ptr<OBJMesh>> &r_exportable_meshes,
+                                    Vector<std::unique_ptr<OBJNurbs>> &r_exportable_nurbs)
 {
-  ViewLayer *view_layer = CTX_data_view_layer(C);
   LISTBASE_FOREACH (Base *, base, &view_layer->object_bases) {
     Object *object_in_layer = base->object;
-    if (export_params->export_selected_objects && !(object_in_layer->base_flag & BASE_SELECTED)) {
+    if (export_params.export_selected_objects && !(object_in_layer->base_flag & BASE_SELECTED)) {
       continue;
     }
     switch (object_in_layer->type) {
       case OB_SURF:
+        /* Export in mesh form: vertices and polygons. */
+        ATTR_FALLTHROUGH;
       case OB_MESH: {
-        OBJMesh *objmesh = new OBJMesh(C, export_params, object_in_layer);
-        exportable_meshes.append(std::unique_ptr<OBJMesh>(objmesh));
+        OBJMesh *objmesh = new OBJMesh(depsgraph, export_params, object_in_layer);
+        r_exportable_meshes.append(std::unique_ptr<OBJMesh>(objmesh));
         break;
       }
       case OB_CURVE: {
         Curve *curve = (Curve *)object_in_layer->data;
         Nurb *nurb = (Nurb *)curve->nurb.first;
-        if (nurb->type == CU_NURBS) {
-          if (export_params->export_curves_as_nurbs) {
-            OBJNurbs *objnurb = new OBJNurbs(C, object_in_layer);
-            exportable_nurbs.append(std::unique_ptr<OBJNurbs>(objnurb));
+        switch (nurb->type) {
+          case CU_NURBS: {
+            if (export_params.export_curves_as_nurbs) {
+              /* Export in parameter form: control points. */
+              OBJNurbs *objnurb = new OBJNurbs(depsgraph, object_in_layer);
+              r_exportable_nurbs.append(std::unique_ptr<OBJNurbs>(objnurb));
+            }
+            else {
+              /* Export in mesh form: edges and vertices. */
+              OBJMesh *objmesh = new OBJMesh(depsgraph, export_params, object_in_layer);
+              r_exportable_meshes.append(std::unique_ptr<OBJMesh>(objmesh));
+            }
+            break;
           }
-          else {
-            OBJMesh *objmesh = new OBJMesh(C, export_params, object_in_layer);
-            exportable_meshes.append(std::unique_ptr<OBJMesh>(objmesh));
+          case CU_BEZIER: {
+            /* Always export in mesh form: edges and vertices. */
+            OBJMesh *objmesh = new OBJMesh(depsgraph, export_params, object_in_layer);
+            r_exportable_meshes.append(std::unique_ptr<OBJMesh>(objmesh));
+            break;
+          }
+          default: {
+            /* Other curve types are not supported. */
+            break;
           }
         }
-        if (nurb->type == CU_BEZIER) {
-          OBJMesh *objmesh = new OBJMesh(C, export_params, object_in_layer);
-          exportable_meshes.append(std::unique_ptr<OBJMesh>(objmesh));
-        }
-        /* Other types of curves are not supported.  */
-        break;
       }
-      default:
+      default: {
+        /* Other object types are not supported. */
         break;
+      }
     }
   }
 }
@@ -95,45 +106,49 @@ static void filter_exportable_objects(bContext *C,
 /**
  * Traverses over and exports a single frame to a single OBJ file.
  */
-static void export_frame(bContext *C, const OBJExportParams *export_params, const char *filepath)
+static void export_frame(ViewLayer *view_layer,
+                         Depsgraph *depsgraph,
+                         const OBJExportParams &export_params,
+                         const char *filepath)
 {
   OBJWriter frame_writer(export_params);
-  if (!frame_writer.init_writer()) {
-    fprintf(stderr, "Error in creating the file: %s\n", export_params->filepath);
+  if (!frame_writer.init_writer(filepath)) {
+    fprintf(stderr, "Error in creating the file: %s\n", filepath);
     return;
   }
 
+  /* Meshes, and curves to be exported in mesh form. */
   Vector<std::unique_ptr<OBJMesh>> exportable_meshes;
+  /* NURBS to be exported in parameter form. */
   Vector<std::unique_ptr<OBJNurbs>> exportable_nurbs;
-  filter_exportable_objects(C, export_params, exportable_meshes, exportable_nurbs);
+  find_exportable_objects(
+      view_layer, depsgraph, export_params, exportable_meshes, exportable_nurbs);
 
-  if (export_params->export_materials) {
-    /* Write MTL filename to the OBJ file. Also create an empty MTL file of the same name as the
-     * OBJ. */
+  if (export_params.export_materials) {
+    /* Create an empty MTL file in the beginning, to be appended later. */
     frame_writer.write_mtllib(filepath);
   }
   for (std::unique_ptr<OBJMesh> &mesh_to_export : exportable_meshes) {
-
     frame_writer.write_object_name(*mesh_to_export);
     frame_writer.write_vertex_coords(*mesh_to_export);
 
-    /* For curves converted to mesh and primitive circle. */
-    if (mesh_to_export->tot_poly_normals() == 0) {
+    /* Write edges of curves converted to mesh and primitive circle. */
+    if (mesh_to_export->tot_polygons() == 0) {
       frame_writer.write_curve_edges(*mesh_to_export);
     }
     else {
       Vector<Vector<uint>> uv_indices;
-      if (export_params->export_normals) {
+      if (export_params.export_normals) {
         frame_writer.write_poly_normals(*mesh_to_export);
       }
-      if (export_params->export_uv) {
+      if (export_params.export_uv) {
         frame_writer.write_uv_coords(*mesh_to_export, uv_indices);
       }
-      if (export_params->export_materials) {
+      if (export_params.export_materials) {
         MTLWriter mtl_writer(filepath);
         mtl_writer.append_materials(*mesh_to_export);
       }
-      frame_writer.write_poly_indices(*mesh_to_export, uv_indices);
+      frame_writer.write_poly_elements(*mesh_to_export, uv_indices);
     }
     frame_writer.update_index_offsets(*mesh_to_export);
   }
@@ -143,50 +158,58 @@ static void export_frame(bContext *C, const OBJExportParams *export_params, cons
   }
 }
 
+/**
+ * Insert frame number in OBJ filepath for animation export.
+ */
+static bool insert_frame_in_path(const char *filepath, char *r_filepath_with_frames, int frame)
+{
+  BLI_strncpy(r_filepath_with_frames, filepath, FILE_MAX);
+  BLI_path_extension_replace(r_filepath_with_frames, FILE_MAX, "");
+  int digits = frame == 0 ? 1 : integer_digits_i(abs(frame));
+  BLI_path_frame(r_filepath_with_frames, frame, digits);
+  bool filepath_ok = BLI_path_extension_replace(r_filepath_with_frames, FILE_MAX, ".obj");
+  return filepath_ok;
+}
+
 /**
  * Central internal function to call scene update & writer functions.
  */
-void exporter_main(bContext *C, const OBJExportParams *export_params)
+void exporter_main(bContext *C, const OBJExportParams &export_params)
 {
   /* TODO ankitm: find a better way to exit edit mode that doesn't hit assert
    * https://hastebin.com/mitihetagi 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list