[Bf-blender-cvs] [fa1b28499a4] usd-importer-T81257: Initial commit of USD importer work in progress, which includes basic support for reading meshes with UVs.

makowalski noreply at git.blender.org
Thu Oct 1 22:29:39 CEST 2020


Commit: fa1b28499a42eb40ce203730402b6e9d86780730
Author: makowalski
Date:   Wed Sep 30 00:01:35 2020 -0400
Branches: usd-importer-T81257
https://developer.blender.org/rBfa1b28499a42eb40ce203730402b6e9d86780730

Initial commit of USD importer work in progress, which includes basic support for reading meshes with UVs.

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

M	release/scripts/startup/bl_ui/space_topbar.py
M	source/blender/editors/io/io_ops.c
M	source/blender/editors/io/io_usd.c
M	source/blender/editors/io/io_usd.h
M	source/blender/io/usd/CMakeLists.txt
M	source/blender/io/usd/intern/usd_capi.cc
A	source/blender/io/usd/intern/usd_importer_context.h
A	source/blender/io/usd/intern/usd_reader_mesh.cc
A	source/blender/io/usd/intern/usd_reader_mesh.h
A	source/blender/io/usd/intern/usd_reader_object.cc
A	source/blender/io/usd/intern/usd_reader_object.h
A	source/blender/io/usd/intern/usd_util.cc
A	source/blender/io/usd/intern/usd_util.h
M	source/blender/io/usd/usd.h

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

diff --git a/release/scripts/startup/bl_ui/space_topbar.py b/release/scripts/startup/bl_ui/space_topbar.py
index 6fc29119cdc..795d601f657 100644
--- a/release/scripts/startup/bl_ui/space_topbar.py
+++ b/release/scripts/startup/bl_ui/space_topbar.py
@@ -441,6 +441,8 @@ class TOPBAR_MT_file_import(Menu):
                                  text="Collada (Default) (.dae)")
         if bpy.app.build_options.alembic:
             self.layout.operator("wm.alembic_import", text="Alembic (.abc)")
+        if bpy.app.build_options.usd:
+            self.layout.operator("wm.usd_import", text="Universal Scene Description (.usd, .usdc, .usda)")
 
 
 class TOPBAR_MT_file_export(Menu):
diff --git a/source/blender/editors/io/io_ops.c b/source/blender/editors/io/io_ops.c
index acb511a414d..0a6930dbab0 100644
--- a/source/blender/editors/io/io_ops.c
+++ b/source/blender/editors/io/io_ops.c
@@ -52,6 +52,7 @@ void ED_operatortypes_io(void)
 #endif
 #ifdef WITH_USD
   WM_operatortype_append(WM_OT_usd_export);
+  WM_operatortype_append(WM_OT_usd_import);
 #endif
 
   WM_operatortype_append(CACHEFILE_OT_open);
diff --git a/source/blender/editors/io/io_usd.c b/source/blender/editors/io/io_usd.c
index 45ea52bdebc..6d6b8fe9850 100644
--- a/source/blender/editors/io/io_usd.c
+++ b/source/blender/editors/io/io_usd.c
@@ -24,6 +24,8 @@
 #ifdef WITH_USD
 #  include "DNA_space_types.h"
 
+#  include "ED_object.h"
+
 #  include "BKE_context.h"
 #  include "BKE_main.h"
 #  include "BKE_report.h"
@@ -242,4 +244,115 @@ void WM_OT_usd_export(struct wmOperatorType *ot)
                "are different settings for viewport and rendering");
 }
 
+
+static int wm_usd_import_invoke(bContext *C, wmOperator *op, const wmEvent *event)
+{
+  eUSDOperatorOptions *options = MEM_callocN(sizeof(eUSDOperatorOptions), "eUSDOperatorOptions");
+  options->as_background_job = true;
+  op->customdata = options;
+
+  return WM_operator_filesel(C, op, event);
+}
+
+static int wm_usd_import_exec(bContext *C, wmOperator *op)
+{
+  if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
+    BKE_report(op->reports, RPT_ERROR, "No filename given");
+    return OPERATOR_CANCELLED;
+  }
+
+  char filename[FILE_MAX];
+  RNA_string_get(op->ptr, "filepath", filename);
+
+  eUSDOperatorOptions *options = (eUSDOperatorOptions *)op->customdata;
+  const bool as_background_job = (options != NULL && options->as_background_job);
+  MEM_SAFE_FREE(op->customdata);
+
+  const bool import_uvs = RNA_boolean_get(op->ptr, "import_uvs");
+
+  const float scale = RNA_float_get(op->ptr, "scale");
+
+  const bool debug = RNA_boolean_get(op->ptr, "debug");
+
+  /* Switch out of edit mode to avoid being stuck in it (T54326). */
+  Object *obedit = CTX_data_edit_object(C);
+  if (obedit) {
+    ED_object_mode_set(C, OB_MODE_OBJECT);
+  }
+
+  struct USDImportParams params = {
+    import_uvs,
+    scale,
+    debug
+  };
+
+  bool ok = USD_import(C, filename, &params, as_background_job);
+
+  return as_background_job || ok ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
+}
+
+
+static void wm_usd_import_draw(bContext *UNUSED(C), wmOperator *op)
+{
+  uiLayout *layout = op->layout;
+  uiLayout *col;
+  struct PointerRNA *ptr = op->ptr;
+
+  uiLayoutSetPropSep(layout, true);
+
+  uiLayout *box = uiLayoutBox(layout);
+
+  uiItemR(box, ptr, "scale", 0, NULL, ICON_NONE);
+
+  col = uiLayoutColumn(box, true);
+  uiItemR(col, ptr, "import_uvs", 0, NULL, ICON_NONE);
+  uiItemR(col, ptr, "debug", 0, NULL, ICON_NONE);
+
+}
+
+void WM_OT_usd_import(wmOperatorType *ot)
+{
+  printf("WM_OT_usd_import\n");
+  ot->name = "Import USD";
+  ot->description = "Load a USD file";
+  ot->idname = "WM_OT_usd_import";
+  ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
+
+  ot->invoke = wm_usd_export_invoke;
+  ot->exec = wm_usd_import_exec;
+  ot->poll = WM_operator_winactive;
+  ot->ui = wm_usd_import_draw;
+
+  WM_operator_properties_filesel(ot,
+                                 FILE_TYPE_FOLDER | FILE_TYPE_USD,
+                                 FILE_BLENDER,
+                                 FILE_OPENFILE,
+                                 WM_FILESEL_FILEPATH | WM_FILESEL_SHOW_PROPS,
+                                 FILE_DEFAULTDISPLAY,
+                                 FILE_SORT_ALPHA);
+
+  RNA_def_boolean(ot->srna,
+    "import_uvs",
+    true,
+    "uvs",
+    "When checked, import mesh uvs.");
+
+  RNA_def_float(
+    ot->srna,
+    "scale",
+    1.0f,
+    0.0001f,
+    1000.0f,
+    "Scale",
+    "Value by which to enlarge or shrink the objects with respect to the world's origin",
+    0.0001f,
+    1000.0f);
+
+  RNA_def_boolean(ot->srna,
+    "debug",
+    false,
+    "debug",
+    "When checked, output debug information to the shell.");
+}
+
 #endif /* WITH_USD */
diff --git a/source/blender/editors/io/io_usd.h b/source/blender/editors/io/io_usd.h
index 671984b6f34..d65358f4ebc 100644
--- a/source/blender/editors/io/io_usd.h
+++ b/source/blender/editors/io/io_usd.h
@@ -26,3 +26,4 @@
 struct wmOperatorType;
 
 void WM_OT_usd_export(struct wmOperatorType *ot);
+void WM_OT_usd_import(struct wmOperatorType *ot);
diff --git a/source/blender/io/usd/CMakeLists.txt b/source/blender/io/usd/CMakeLists.txt
index 79b15c60b94..70cd8b58b75 100644
--- a/source/blender/io/usd/CMakeLists.txt
+++ b/source/blender/io/usd/CMakeLists.txt
@@ -55,6 +55,9 @@ set(INC_SYS
 set(SRC
   intern/usd_capi.cc
   intern/usd_hierarchy_iterator.cc
+  intern/usd_reader_mesh.cc
+  intern/usd_reader_object.cc
+  intern/usd_util.cc
   intern/usd_writer_abstract.cc
   intern/usd_writer_camera.cc
   intern/usd_writer_hair.cc
@@ -66,6 +69,10 @@ set(SRC
   usd.h
   intern/usd_exporter_context.h
   intern/usd_hierarchy_iterator.h
+  intern/usd_importer_context.h
+  intern/usd_reader_mesh.h
+  intern/usd_reader_object.h
+  intern/usd_util.h
   intern/usd_writer_abstract.h
   intern/usd_writer_camera.h
   intern/usd_writer_hair.h
diff --git a/source/blender/io/usd/intern/usd_capi.cc b/source/blender/io/usd/intern/usd_capi.cc
index 4717f27dbd9..d18bf511985 100644
--- a/source/blender/io/usd/intern/usd_capi.cc
+++ b/source/blender/io/usd/intern/usd_capi.cc
@@ -19,10 +19,16 @@
 
 #include "usd.h"
 #include "usd_hierarchy_iterator.h"
+#include "usd_importer_context.h"
+#include "usd_reader_object.h"
+#include "usd_util.h"
 
 #include <pxr/base/plug/registry.h>
 #include <pxr/pxr.h>
 #include <pxr/usd/usd/stage.h>
+#include <pxr/usd/usd/prim.h>
+#include <pxr/usd/usd/primRange.h>
+#include <pxr/usd/usdGeom/metrics.h>
 #include <pxr/usd/usdGeom/tokens.h>
 
 #include "MEM_guardedalloc.h"
@@ -39,13 +45,37 @@
 #include "BKE_global.h"
 #include "BKE_scene.h"
 
+// XXX check the following
+#include "BKE_main.h"
+#include "BKE_material.h"
+#include "BKE_mesh.h"
+#include "BKE_modifier.h"
+#include "BKE_object.h"
+// XXX check the following
+#include "BKE_cachefile.h"
+#include "BKE_context.h"
+#include "BKE_curve.h"
+#include "BKE_global.h"
+#include "BKE_layer.h"
+#include "BKE_lib_id.h"
+#include "BKE_object.h"
+#include "BKE_scene.h"
+#include "BKE_screen.h"
+#include "DNA_mesh_types.h"
+#include "DNA_meshdata_types.h"
+
 #include "BLI_fileops.h"
 #include "BLI_path_util.h"
 #include "BLI_string.h"
 
+#include "ED_undo.h"
+
 #include "WM_api.h"
 #include "WM_types.h"
 
+#include <iostream>
+
+
 namespace blender::io::usd {
 
 struct ExportJobData {
@@ -183,6 +213,205 @@ static void export_endjob(void *customdata)
   WM_set_locked_interface(data->wm, false);
 }
 
+
+struct ImportJobData {
+  bContext *C;
+  Main *bmain;
+  Scene *scene;
+  ViewLayer *view_layer;
+  wmWindowManager *wm;
+
+  char filename[1024];
+
+  USDImportParams params;
+
+  short *stop;
+  short *do_update;
+  float *progress;
+
+  bool was_cancelled;
+  bool import_ok;
+  bool is_background_job;
+
+  pxr::UsdStageRefPtr stage;
+  UsdObjectReader::ptr_vector readers;
+};
+
+static void import_startjob(void *user_data, short *stop, short *do_update, float *progress)
+{
+  ImportJobData *data = static_cast<ImportJobData *>(user_data);
+
+  data->import_ok = false;
+  data->stop = stop;
+  data->do_update = do_update;
+  data->progress = progress;
+
+  Scene *scene = data->scene;
+
+  WM_set_locked_interface(data->wm, true);
+
+  *data->do_update = true;
+  *data->progress = 0.05f;
+
+  data->stage = pxr::UsdStage::Open(data->filename);
+  if (!data->stage) {
+    WM_reportf(
+      RPT_ERROR, "USD Export: couldn't open USD stage for file %s", data->filename);
+    return;
+  }
+
+  pxr::TfToken up_axis = pxr::UsdGeomGetStageUpAxis(data->stage);
+  USDImporterContext import_ctx{ up_axis, data->params };
+
+  // Optionally print the stage contents for debugging.
+  if (data->params.debug)
+  {
+    debug_traverse_stage(data->stage);
+  }
+
+  if (G.is_break) {
+    data->was_cancelled = true;
+    return;
+  }
+
+  *data->do_update = true;
+  *data->progress = 0.1f;
+
+  create_readers(data->stage, data->readers, import_ctx);
+
+  // Create objects
+
+  const float size = static_cast<float>(data->readers.size());
+  size_t i = 0;
+
+  double min_time = std::numeric_limits<double>::max();
+  double max_time = std::numeric_limits<double>::min();
+
+  double time = CFRA;
+
+  std::vector<UsdObjectReader *>::iterator iter;
+  for (iter = data->readers.begin(); iter != data->readers.end(); ++iter) {
+    UsdObjectReader *reader = *iter;
+
+    if (reader->valid()) {
+      reader->readObjectData(data->bmain, time);
+
+      min_time = std::min(min_time, reader->minTime());
+      max_time = std::max(max_time, reader->maxTime());
+    }
+    else {
+      std::cerr << "Object " << reader->name() << " in USD file " << data->filename
+                << " is invalid.\n";
+    }
+
+    *data->progress = 0.1f + 0.3f * (++i / size);
+    *data->do_update = true;
+
+    if (G.is_break) {
+      data->was_cancelled = true;
+      return;
+    }
+  }
+
+  // Setup transformations.
+  i = 0;
+  for (iter = data->readers.begin(); iter != data->readers.end(); ++iter) {
+    UsdObjectReader *reader = *iter;
+    reader->setupObjectTransform(time);
+
+    *data->progress = 0.7f + 0.3f * (++i / size);
+    *data->do_update = true;
+
+    if (G.is_break) {
+      data->was_cancelled = true;
+      return;
+    }
+  }
+}
+
+static void import_endjob(void *user_dat

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list