[Bf-blender-cvs] [40736795ec4] soc-2020-io-performance: Export mutiple objects; remove unused fstream.

Ankit Meel noreply at git.blender.org
Mon Jun 15 10:13:34 CEST 2020


Commit: 40736795ec4efe3e0f6652d91b8d5faeb196ba03
Author: Ankit Meel
Date:   Sat Jun 13 16:22:00 2020 +0530
Branches: soc-2020-io-performance
https://developer.blender.org/rB40736795ec4efe3e0f6652d91b8d5faeb196ba03

Export mutiple objects; remove unused fstream.

Adds support for multiple objects to be exported from a frame.
Also checks for them to be OB_MESH types. More object types need to be
supported yet.

Renamed data_to_export to more fitting, object_to_export of which we
use vector to contain all exportable objects.

Remove unused fstream based file writer since fprintf is consistently
faster.

Also reduce function calls of fprintf since lengthy arguments were
already removed.

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

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_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.hh b/source/blender/io/wavefront_obj/intern/wavefront_obj.hh
index 1d7b3cb12be..0d98c1e741a 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj.hh
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj.hh
@@ -31,6 +31,7 @@
 
 #include "DNA_mesh_types.h"
 #include "DNA_meshdata_types.h"
+#include "DNA_object_types.h"
 
 namespace io {
 namespace obj {
@@ -55,12 +56,11 @@ struct Polygon {
 
 /**
  * Stores geometry of one object to be exported.
- * TODO (ankitm): Extend it to contain multiple objects' geometry.
  */
-struct OBJ_data_to_export {
+typedef struct OBJ_object_to_export {
   bContext *C;
   Depsgraph *depsgraph;
-  Object *ob_eval;
+  Object *object;
 
   /** Vertices in a mesh to export. */
   MVert *mvert;
@@ -77,7 +77,7 @@ struct OBJ_data_to_export {
   std::vector<std::array<float, 2>> uv_coords;
   /** Number of UV vertices of a mesh in texture map. */
   uint tot_uv_vertices;
-};
+} OBJ_object_to_export;
 }  // namespace obj
 }  // namespace io
 
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 d249ed1f92b..e861687e0d6 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter.cc
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter.cc
@@ -37,6 +37,8 @@
 #include "DEG_depsgraph.h"
 #include "DEG_depsgraph_query.h"
 
+#include "DNA_layer_types.h"
+
 #include "IO_wavefront_obj.h"
 
 #include "wavefront_obj.hh"
@@ -46,39 +48,53 @@
 namespace io {
 namespace obj {
 
+static void object_is_exportable(Object *object,
+                                 std::vector<OBJ_object_to_export> &object_to_export)
+{
+  switch (object->type) {
+    case OB_MESH:
+      object_to_export.push_back(OBJ_object_to_export());
+      object_to_export.back().object = object;
+      break;
+      /* Do nothing for all other cases for now. */
+    default:
+      break;
+  }
+}
+
 /**
- * Store the mesh vertex coordinates in data_to_export, in world coordinates.
+ * Store the mesh vertex coordinates in object_to_export, in world coordinates.
  */
 static void get_transformed_mesh_vertices(Mesh *me_eval,
                                           Object *ob_eval,
-                                          OBJ_data_to_export *data_to_export)
+                                          OBJ_object_to_export &object_to_export)
 {
-  uint num_verts = data_to_export->tot_vertices = me_eval->totvert;
+  uint num_verts = object_to_export.tot_vertices = me_eval->totvert;
   float transformed_co[3];
-  data_to_export->mvert = (MVert *)MEM_callocN(num_verts * sizeof(MVert),
-                                               "OBJ vertex coordinates & normals");
+  object_to_export.mvert = (MVert *)MEM_callocN(num_verts * sizeof(MVert),
+                                                "OBJ object vertex coordinates & normals");
 
   for (uint i = 0; i < num_verts; i++) {
     copy_v3_v3(transformed_co, me_eval->mvert[i].co);
     mul_m4_v3(ob_eval->obmat, transformed_co);
-    copy_v3_v3(data_to_export->mvert[i].co, transformed_co);
+    copy_v3_v3(object_to_export.mvert[i].co, transformed_co);
   }
 }
 
 /**
- * Store the mesh vertex normals in data_to_export, in world coordinates.
- * Memory for data_to_export->mvert pre-allocated in get_transformed_mesh_vertices.
+ * Store the mesh vertex normals in object_to_export, in world coordinates.
+ * Memory for object_to_export.mvert pre-allocated in get_transformed_mesh_vertices.
  */
 static void get_transformed_vertex_normals(Mesh *me_eval,
                                            Object *ob_eval,
-                                           OBJ_data_to_export *data_to_export)
+                                           OBJ_object_to_export &object_to_export)
 {
   BKE_mesh_ensure_normals(me_eval);
   float transformed_normal[3];
   for (uint i = 0; i < me_eval->totvert; i++) {
     normal_short_to_float_v3(transformed_normal, me_eval->mvert[i].no);
     mul_mat3_m4_v3(ob_eval->obmat, transformed_normal);
-    normal_float_to_short_v3(data_to_export->mvert[i].no, transformed_normal);
+    normal_float_to_short_v3(object_to_export.mvert[i].no, transformed_normal);
   }
 }
 
@@ -86,31 +102,31 @@ static void get_transformed_vertex_normals(Mesh *me_eval,
  * Store a polygon's vertex indices, indexing into the pre-defined
  * vertex coordinates list.
  */
-static void get_polygon_vert_indices(Mesh *me_eval, OBJ_data_to_export *data_to_export)
+static void get_polygon_vert_indices(Mesh *me_eval, OBJ_object_to_export &object_to_export)
 {
   const MLoop *mloop;
   const MPoly *mpoly = me_eval->mpoly;
 
-  data_to_export->tot_poly = me_eval->totpoly;
-  data_to_export->polygon_list.resize(me_eval->totpoly);
+  object_to_export.tot_poly = me_eval->totpoly;
+  object_to_export.polygon_list.resize(me_eval->totpoly);
 
   for (uint i = 0; i < me_eval->totpoly; i++, mpoly++) {
     mloop = &me_eval->mloop[mpoly->loopstart];
-    data_to_export->polygon_list[i].total_vertices_per_poly = mpoly->totloop;
+    object_to_export.polygon_list[i].total_vertices_per_poly = mpoly->totloop;
 
-    data_to_export->polygon_list[i].vertex_index.resize(mpoly->totloop);
+    object_to_export.polygon_list[i].vertex_index.resize(mpoly->totloop);
 
     for (int j = 0; j < mpoly->totloop; j++) {
       /* mloop->v is 0-based index. Indices in OBJ start from 1. */
-      data_to_export->polygon_list[i].vertex_index[j] = (mloop + j)->v + 1;
+      object_to_export.polygon_list[i].vertex_index[j] = (mloop + j)->v + 1;
     }
   }
 }
 /**
- * Store UV vertex coordinates in data_to_export->uv_coords as well as their indices, in
+ * Store UV vertex coordinates in object_to_export.uv_coords as well as their indices, in
  * a polygon[i].uv_vertex_index.
  */
-static void get_uv_coordinates(Mesh *me_eval, OBJ_data_to_export *data_to_export)
+static void get_uv_coordinates(Mesh *me_eval, OBJ_object_to_export &object_to_export)
 {
   const CustomData *ldata = &me_eval->ldata;
 
@@ -128,34 +144,35 @@ static void get_uv_coordinates(Mesh *me_eval, OBJ_data_to_export *data_to_export
     UvVertMap *uv_vert_map = BKE_mesh_uv_vert_map_create(
         mpoly, mloop, mloopuv, me_eval->totpoly, me_eval->totvert, limit, false, false);
 
-    data_to_export->tot_uv_vertices = -1;
+    object_to_export.tot_uv_vertices = -1;
     for (int vertex_index = 0; vertex_index < me_eval->totvert; vertex_index++) {
       const UvMapVert *uv_vert = BKE_mesh_uv_vert_map_get_vert(uv_vert_map, vertex_index);
       while (uv_vert != NULL) {
         if (uv_vert->separate) {
-          data_to_export->tot_uv_vertices++;
+          object_to_export.tot_uv_vertices++;
         }
-        Polygon &polygon_of_uv_vert = data_to_export->polygon_list[uv_vert->poly_index];
+        Polygon &polygon_of_uv_vert = object_to_export.polygon_list[uv_vert->poly_index];
         const uint vertices_in_poly = polygon_of_uv_vert.total_vertices_per_poly;
         /* Resize UV vertices index list. */
         polygon_of_uv_vert.uv_vertex_index.resize(vertices_in_poly);
 
         /* Fill up UV vertex index for current polygon's one vertex. */
         polygon_of_uv_vert.uv_vertex_index[uv_vert->loop_of_poly_index] =
-            data_to_export->tot_uv_vertices;
+            object_to_export.tot_uv_vertices;
 
         /* Fill up UV vertices' coordinates. We don't know how many unique vertices are there, so
          * need to push back everytime. */
-        data_to_export->uv_coords.push_back(std::array<float, 2>());
-        data_to_export->uv_coords[data_to_export->tot_uv_vertices][0] =
+        object_to_export.uv_coords.push_back(std::array<float, 2>());
+        object_to_export.uv_coords[object_to_export.tot_uv_vertices][0] =
             mloopuv[mpoly[uv_vert->poly_index].loopstart + uv_vert->loop_of_poly_index].uv[0];
-        data_to_export->uv_coords[data_to_export->tot_uv_vertices][1] =
+        object_to_export.uv_coords[object_to_export.tot_uv_vertices][1] =
             mloopuv[mpoly[uv_vert->poly_index].loopstart + uv_vert->loop_of_poly_index].uv[1];
 
         uv_vert = uv_vert->next;
       }
     }
-    data_to_export->tot_uv_vertices++;
+    /* Actual number of total UV vertices is 1-based, as opposed to the index: 0-based. */
+    object_to_export.tot_uv_vertices += 1;
     BKE_mesh_uv_vert_map_free(uv_vert_map);
     /* No need to go over other layers. */
     break;
@@ -163,17 +180,17 @@ static void get_uv_coordinates(Mesh *me_eval, OBJ_data_to_export *data_to_export
 }
 
 static void get_geometry_per_object(const OBJExportParams *export_params,
-                                    OBJ_data_to_export *data_to_export)
+                                    OBJ_object_to_export &object_to_export)
 {
-  Depsgraph *depsgraph = data_to_export->depsgraph;
-  Object *ob = CTX_data_active_object(data_to_export->C);
-  Object *ob_eval = data_to_export->ob_eval = DEG_get_evaluated_object(depsgraph, ob);
+  Depsgraph *depsgraph = object_to_export.depsgraph;
+  Object *ob = object_to_export.object;
+  Object *ob_eval = DEG_get_evaluated_object(depsgraph, ob);
   Mesh *me_eval = BKE_object_get_evaluated_mesh(ob_eval);
 
-  get_transformed_mesh_vertices(me_eval, ob_eval, data_to_export);
-  get_transformed_vertex_normals(me_eval, ob_eval, data_to_export);
-  get_polygon_vert_indices(me_eval, data_to_export);
-  get_uv_coordinates(me_eval, data_to_export);
+  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);
 }
 
 /**
@@ -182,16 +199,29 @@ static void get_geometry_per_object(const OBJExportParams *export_params,
 void exporter_main(bContext *C, const OBJExportParams *export_params)
 {
   const char *filepath = export_params->filepath;
-  struct OBJ_data_to_export data_to_export;
-  data_to_export.C = C;
-  data_to_export.depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
+  std::vector<OBJ_object_to_export> exportable_objects;
+
+  ViewLayer *view_layer = CTX_data_view_layer(C);
 
-  get_geometry_per_ob

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list