[Bf-blender-cvs] [7c511f1b47d] master: STL: Add new C++ based STL importer

Iyad Ahmed noreply at git.blender.org
Mon Jun 6 19:59:36 CEST 2022


Commit: 7c511f1b47d857f37aa36ee6ed8107cb88eb5c39
Author: Iyad Ahmed
Date:   Mon Jun 6 20:57:38 2022 +0300
Branches: master
https://developer.blender.org/rB7c511f1b47d857f37aa36ee6ed8107cb88eb5c39

STL: Add new C++ based STL importer

A new experimentatl STL importer, written in C++. Roughly 7-9x faster than the
Python based one.

Reviewed By: Aras Pranckevicius, Hans Goudey.
Differential Revision: https://developer.blender.org/D14941

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

M	release/scripts/startup/bl_ui/space_topbar.py
M	source/blender/editors/io/CMakeLists.txt
M	source/blender/editors/io/io_obj.c
M	source/blender/editors/io/io_ops.c
A	source/blender/editors/io/io_stl_ops.c
A	source/blender/editors/io/io_stl_ops.h
M	source/blender/editors/space_file/filelist.c
M	source/blender/io/CMakeLists.txt
M	source/blender/io/common/CMakeLists.txt
A	source/blender/io/common/IO_orientation.h
A	source/blender/io/common/intern/orientation.c
A	source/blender/io/stl/CMakeLists.txt
A	source/blender/io/stl/IO_stl.cc
A	source/blender/io/stl/IO_stl.h
A	source/blender/io/stl/importer/stl_import.cc
A	source/blender/io/stl/importer/stl_import.hh
A	source/blender/io/stl/importer/stl_import_ascii_reader.cc
A	source/blender/io/stl/importer/stl_import_ascii_reader.hh
A	source/blender/io/stl/importer/stl_import_binary_reader.cc
A	source/blender/io/stl/importer/stl_import_binary_reader.hh
A	source/blender/io/stl/importer/stl_import_mesh.cc
A	source/blender/io/stl/importer/stl_import_mesh.hh
M	source/blender/io/wavefront_obj/IO_wavefront_obj.h
M	source/blender/io/wavefront_obj/exporter/obj_export_mesh.cc
M	source/blender/io/wavefront_obj/exporter/obj_export_mesh.hh
M	source/blender/io/wavefront_obj/exporter/obj_export_nurbs.cc
M	source/blender/io/wavefront_obj/exporter/obj_export_nurbs.hh
M	source/blender/io/wavefront_obj/importer/importer_mesh_utils.cc
M	source/blender/io/wavefront_obj/tests/obj_exporter_tests.cc
M	source/blender/io/wavefront_obj/tests/obj_exporter_tests.hh
M	source/blender/io/wavefront_obj/tests/obj_importer_tests.cc

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

diff --git a/release/scripts/startup/bl_ui/space_topbar.py b/release/scripts/startup/bl_ui/space_topbar.py
index ba1e9aa019e..2980ec9ace7 100644
--- a/release/scripts/startup/bl_ui/space_topbar.py
+++ b/release/scripts/startup/bl_ui/space_topbar.py
@@ -455,6 +455,7 @@ class TOPBAR_MT_file_import(Menu):
 
         self.layout.operator("wm.gpencil_import_svg", text="SVG as Grease Pencil")
         self.layout.operator("wm.obj_import", text="Wavefront (.obj) (experimental)")
+        self.layout.operator("wm.stl_import", text="STL (.stl) (experimental)")
 
 
 class TOPBAR_MT_file_export(Menu):
diff --git a/source/blender/editors/io/CMakeLists.txt b/source/blender/editors/io/CMakeLists.txt
index ef093a01ff8..98438c3e959 100644
--- a/source/blender/editors/io/CMakeLists.txt
+++ b/source/blender/editors/io/CMakeLists.txt
@@ -13,6 +13,7 @@ set(INC
   ../../io/gpencil
   ../../io/usd
   ../../io/wavefront_obj
+  ../../io/stl
   ../../makesdna
   ../../makesrna
   ../../windowmanager
@@ -33,6 +34,7 @@ set(SRC
   io_obj.c
   io_ops.c
   io_usd.c
+  io_stl_ops.c
 
   io_alembic.h
   io_cache.h
@@ -41,12 +43,14 @@ set(SRC
   io_obj.h
   io_ops.h
   io_usd.h
+  io_stl_ops.h
 )
 
 set(LIB
   bf_blenkernel
   bf_blenlib
   bf_wavefront_obj
+  bf_stl
 )
 
 if(WITH_OPENCOLLADA)
diff --git a/source/blender/editors/io/io_obj.c b/source/blender/editors/io/io_obj.c
index 59e8c20da04..05bccce7948 100644
--- a/source/blender/editors/io/io_obj.c
+++ b/source/blender/editors/io/io_obj.c
@@ -31,28 +31,11 @@
 
 #include "DEG_depsgraph.h"
 
+#include "IO_orientation.h"
 #include "IO_path_util_types.h"
 #include "IO_wavefront_obj.h"
 #include "io_obj.h"
 
-static const EnumPropertyItem io_obj_transform_axis_forward[] = {
-    {OBJ_AXIS_X_FORWARD, "X_FORWARD", 0, "X", "Positive X axis"},
-    {OBJ_AXIS_Y_FORWARD, "Y_FORWARD", 0, "Y", "Positive Y axis"},
-    {OBJ_AXIS_Z_FORWARD, "Z_FORWARD", 0, "Z", "Positive Z axis"},
-    {OBJ_AXIS_NEGATIVE_X_FORWARD, "NEGATIVE_X_FORWARD", 0, "-X", "Negative X axis"},
-    {OBJ_AXIS_NEGATIVE_Y_FORWARD, "NEGATIVE_Y_FORWARD", 0, "-Y", "Negative Y axis"},
-    {OBJ_AXIS_NEGATIVE_Z_FORWARD, "NEGATIVE_Z_FORWARD", 0, "-Z", "Negative Z axis"},
-    {0, NULL, 0, NULL, NULL}};
-
-static const EnumPropertyItem io_obj_transform_axis_up[] = {
-    {OBJ_AXIS_X_UP, "X_UP", 0, "X", "Positive X axis"},
-    {OBJ_AXIS_Y_UP, "Y_UP", 0, "Y", "Positive Y axis"},
-    {OBJ_AXIS_Z_UP, "Z_UP", 0, "Z", "Positive Z axis"},
-    {OBJ_AXIS_NEGATIVE_X_UP, "NEGATIVE_X_UP", 0, "-X", "Negative X axis"},
-    {OBJ_AXIS_NEGATIVE_Y_UP, "NEGATIVE_Y_UP", 0, "-Y", "Negative Y axis"},
-    {OBJ_AXIS_NEGATIVE_Z_UP, "NEGATIVE_Z_UP", 0, "-Z", "Negative Z axis"},
-    {0, NULL, 0, NULL, NULL}};
-
 static const EnumPropertyItem io_obj_export_evaluation_mode[] = {
     {DAG_EVAL_RENDER, "DAG_EVAL_RENDER", 0, "Render", "Export objects as they appear in render"},
     {DAG_EVAL_VIEWPORT,
@@ -296,13 +279,9 @@ void WM_OT_obj_export(struct wmOperatorType *ot)
               INT_MIN,
               INT_MAX);
   /* Object transform options. */
-  RNA_def_enum(ot->srna,
-               "forward_axis",
-               io_obj_transform_axis_forward,
-               OBJ_AXIS_NEGATIVE_Z_FORWARD,
-               "Forward Axis",
-               "");
-  RNA_def_enum(ot->srna, "up_axis", io_obj_transform_axis_up, OBJ_AXIS_Y_UP, "Up Axis", "");
+  RNA_def_enum(
+      ot->srna, "forward_axis", io_transform_axis, IO_AXIS_NEGATIVE_Z, "Forward Axis", "");
+  RNA_def_enum(ot->srna, "up_axis", io_transform_axis, IO_AXIS_Y, "Up Axis", "");
   RNA_def_float(ot->srna,
                 "scaling_factor",
                 1.0f,
@@ -480,13 +459,9 @@ void WM_OT_obj_import(struct wmOperatorType *ot)
       "Resize the objects to keep bounding box under this value. Value 0 disables clamping",
       0.0f,
       1000.0f);
-  RNA_def_enum(ot->srna,
-               "forward_axis",
-               io_obj_transform_axis_forward,
-               OBJ_AXIS_NEGATIVE_Z_FORWARD,
-               "Forward Axis",
-               "");
-  RNA_def_enum(ot->srna, "up_axis", io_obj_transform_axis_up, OBJ_AXIS_Y_UP, "Up Axis", "");
+  RNA_def_enum(
+      ot->srna, "forward_axis", io_transform_axis, IO_AXIS_NEGATIVE_Z, "Forward Axis", "");
+  RNA_def_enum(ot->srna, "up_axis", io_transform_axis, IO_AXIS_Y, "Up Axis", "");
   RNA_def_boolean(ot->srna,
                   "validate_meshes",
                   false,
diff --git a/source/blender/editors/io/io_ops.c b/source/blender/editors/io/io_ops.c
index 094f89d1540..098186a75e3 100644
--- a/source/blender/editors/io/io_ops.c
+++ b/source/blender/editors/io/io_ops.c
@@ -24,6 +24,7 @@
 #include "io_cache.h"
 #include "io_gpencil.h"
 #include "io_obj.h"
+#include "io_stl_ops.h"
 
 void ED_operatortypes_io(void)
 {
@@ -60,4 +61,6 @@ void ED_operatortypes_io(void)
 
   WM_operatortype_append(WM_OT_obj_export);
   WM_operatortype_append(WM_OT_obj_import);
+
+  WM_operatortype_append(WM_OT_stl_import);
 }
diff --git a/source/blender/editors/io/io_stl_ops.c b/source/blender/editors/io/io_stl_ops.c
new file mode 100644
index 00000000000..d5993bc4f37
--- /dev/null
+++ b/source/blender/editors/io/io_stl_ops.c
@@ -0,0 +1,129 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+/** \file
+ * \ingroup editor/io
+ */
+
+#include "BKE_context.h"
+#include "BKE_report.h"
+
+#include "WM_api.h"
+#include "WM_types.h"
+
+#include "DNA_space_types.h"
+
+#include "ED_outliner.h"
+
+#include "RNA_access.h"
+#include "RNA_define.h"
+
+#include "IO_stl.h"
+#include "io_stl_ops.h"
+
+static int wm_stl_import_invoke(bContext *C, wmOperator *op, const wmEvent *event)
+{
+  return WM_operator_filesel(C, op, event);
+}
+
+static int wm_stl_import_execute(bContext *C, wmOperator *op)
+{
+  struct STLImportParams params;
+  params.forward_axis = RNA_enum_get(op->ptr, "forward_axis");
+  params.up_axis = RNA_enum_get(op->ptr, "up_axis");
+  params.use_facet_normal = RNA_boolean_get(op->ptr, "use_facet_normal");
+  params.use_scene_unit = RNA_boolean_get(op->ptr, "use_scene_unit");
+  params.global_scale = RNA_float_get(op->ptr, "global_scale");
+  params.use_mesh_validate = RNA_boolean_get(op->ptr, "use_mesh_validate");
+
+  int files_len = RNA_collection_length(op->ptr, "files");
+
+  if (files_len) {
+    PointerRNA fileptr;
+    PropertyRNA *prop;
+    char dir_only[FILE_MAX], file_only[FILE_MAX];
+
+    RNA_string_get(op->ptr, "directory", dir_only);
+    prop = RNA_struct_find_property(op->ptr, "files");
+    for (int i = 0; i < files_len; i++) {
+      RNA_property_collection_lookup_int(op->ptr, prop, i, &fileptr);
+      RNA_string_get(&fileptr, "name", file_only);
+      BLI_join_dirfile(params.filepath, sizeof(params.filepath), dir_only, file_only);
+      STL_import(C, &params);
+    }
+  }
+  else if (RNA_struct_property_is_set(op->ptr, "filepath")) {
+    RNA_string_get(op->ptr, "filepath", params.filepath);
+    STL_import(C, &params);
+  }
+  else {
+    BKE_report(op->reports, RPT_ERROR, "No filename given");
+    return OPERATOR_CANCELLED;
+  }
+
+  Scene *scene = CTX_data_scene(C);
+  WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
+  WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
+  WM_event_add_notifier(C, NC_SCENE | ND_LAYER_CONTENT, scene);
+  ED_outliner_select_sync_from_object_tag(C);
+
+  return OPERATOR_FINISHED;
+}
+
+static bool wm_stl_import_check(bContext *UNUSED(C), wmOperator *op)
+{
+  const int num_axes = 3;
+  /* Both forward and up axes cannot be the same (or same except opposite sign). */
+  if (RNA_enum_get(op->ptr, "forward_axis") % num_axes ==
+      (RNA_enum_get(op->ptr, "up_axis") % num_axes)) {
+    RNA_enum_set(op->ptr, "up_axis", RNA_enum_get(op->ptr, "up_axis") % num_axes + 1);
+    return true;
+  }
+  return false;
+}
+
+void WM_OT_stl_import(struct wmOperatorType *ot)
+{
+  PropertyRNA *prop;
+
+  ot->name = "Import STL";
+  ot->description = "Import an STL file as an object";
+  ot->idname = "WM_OT_stl_import";
+
+  ot->invoke = wm_stl_import_invoke;
+  ot->exec = wm_stl_import_execute;
+  ot->poll = WM_operator_winactive;
+  ot->check = wm_stl_import_check;
+  ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
+
+  WM_operator_properties_filesel(ot,
+                                 FILE_TYPE_FOLDER,
+                                 FILE_BLENDER,
+                                 FILE_OPENFILE,
+                                 WM_FILESEL_FILEPATH | WM_FILESEL_FILES | WM_FILESEL_DIRECTORY |
+                                     WM_FILESEL_SHOW_PROPS,
+                                 FILE_DEFAULTDISPLAY,
+                                 FILE_SORT_ALPHA);
+
+  RNA_def_float(ot->srna, "global_scale", 1.0f, 1e-6f, 1e6f, "Scale", "", 0.001f, 1000.0f);
+  RNA_def_boolean(ot->srna,
+                  "use_scene_unit",
+                  false,
+                  "Scene Unit",
+                  "Apply current scene's unit (as defined by unit scale) to imported data");
+  RNA_def_boolean(ot->srna,
+                  "use_facet_normal",
+                  false,
+                  "Facet Normals",
+                  "Use (import) facet normals (note that this will still give flat shading)");
+  RNA_def_enum(ot->srna, "forward_axis", io_transform_axis, IO_AXIS_Y, "Forward Axis", "");
+  RNA_def_enum(ot->srna, "up_axis", io_transform_axis, IO_AXIS_Z, "Up Axis", "");
+  RNA_def_boolean(ot->srna,
+                  "use_mesh_validate",
+                  false,
+                  "Validate Mesh",
+                  "Validate and correct imported mesh (slow)");
+
+  /* Only show .stl files by default. */
+  prop = RNA_def_string(ot->srna, "filter_glob", "*.stl", 0, "Extension Filter", "");
+  RNA_def_property_flag(prop, PROP_HIDDEN);
+}
diff --git a/source/blender/editors/io/io_stl_ops.h b/source/blender/editors/io/io_stl_ops.h
new file mode 100644
index 00000000000..8f548f75985
--- /dev/null
+++ b/source/blender/editors/io/io_stl_ops.h
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+/** \file
+ * \ingroup editor/io
+ */
+
+#pragma once
+
+struct wmOperatorType;
+
+void WM_OT_stl_export(struct wmOperatorType *ot);
+void WM_OT_stl_import(struct wmOperatorType *ot);
diff --git a/source/blender/editors/space_file/filelist.c b/source/blender/edi

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list