[Bf-blender-cvs] [235ad7bc50f] usd-importer-T81257-merge: USD Import: new usd_capi_import.cc file.

makowalski noreply at git.blender.org
Wed Apr 14 01:31:19 CEST 2021


Commit: 235ad7bc50f4966422a0f4f2104404407a95a1c8
Author: makowalski
Date:   Tue Apr 13 16:29:46 2021 -0400
Branches: usd-importer-T81257-merge
https://developer.blender.org/rB235ad7bc50f4966422a0f4f2104404407a95a1c8

USD Import: new usd_capi_import.cc file.

Moved import related code from usd_capi.cc to usd_capi_import.cc.
Per review for differential D10700.

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

M	source/blender/io/usd/CMakeLists.txt
M	source/blender/io/usd/intern/usd_capi.cc
A	source/blender/io/usd/intern/usd_capi_import.cc
A	source/blender/io/usd/intern/usd_common.cc
A	source/blender/io/usd/intern/usd_common.h

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

diff --git a/source/blender/io/usd/CMakeLists.txt b/source/blender/io/usd/CMakeLists.txt
index 07e7afc8de9..56fa06b8594 100644
--- a/source/blender/io/usd/CMakeLists.txt
+++ b/source/blender/io/usd/CMakeLists.txt
@@ -57,6 +57,8 @@ set(INC_SYS
 
 set(SRC
   intern/usd_capi.cc
+  intern/usd_capi_import.cc
+  intern/usd_common.cc
   intern/usd_hierarchy_iterator.cc
   intern/usd_writer_abstract.cc
   intern/usd_writer_camera.cc
@@ -80,6 +82,8 @@ set(SRC
   intern/usd_reader_volume.cc
 
   usd.h
+
+  intern/usd_common.h
   intern/usd_exporter_context.h
   intern/usd_hierarchy_iterator.h
   intern/usd_writer_abstract.h
diff --git a/source/blender/io/usd/intern/usd_capi.cc b/source/blender/io/usd/intern/usd_capi.cc
index fee22ad81ad..25f12e683cf 100644
--- a/source/blender/io/usd/intern/usd_capi.cc
+++ b/source/blender/io/usd/intern/usd_capi.cc
@@ -17,23 +17,14 @@
  * All rights reserved.
  */
 
-#include "IO_types.h"
 #include "usd.h"
+#include "usd_common.h"
 #include "usd_hierarchy_iterator.h"
-#include "usd_reader_instance.h"
-#include "usd_reader_mesh.h"
-#include "usd_reader_prim.h"
-#include "usd_reader_stage.h"
 
 #include <pxr/base/plug/registry.h>
 #include <pxr/pxr.h>
 #include <pxr/usd/usd/stage.h>
-#include <pxr/usd/usdGeom/metrics.h>
-#include <pxr/usd/usdGeom/scope.h>
 #include <pxr/usd/usdGeom/tokens.h>
-#include <pxr/usd/usdGeom/xformCommonAPI.h>
-#include <pxr/usd/usdLux/domeLight.h>
-#include <pxr/usd/usdShade/materialBindingAPI.h>
 
 #include "MEM_guardedalloc.h"
 
@@ -41,217 +32,23 @@
 #include "DEG_depsgraph_build.h"
 #include "DEG_depsgraph_query.h"
 
-#include "DNA_cachefile_types.h"
-#include "DNA_collection_types.h"
-#include "DNA_node_types.h"
 #include "DNA_scene_types.h"
-#include "DNA_world_types.h"
 
 #include "BKE_appdir.h"
 #include "BKE_blender_version.h"
-#include "BKE_cachefile.h"
-#include "BKE_cdderivedmesh.h"
 #include "BKE_context.h"
-#include "BKE_curve.h"
 #include "BKE_global.h"
-#include "BKE_layer.h"
-#include "BKE_lib_id.h"
-#include "BKE_library.h"
-#include "BKE_main.h"
-#include "BKE_node.h"
-#include "BKE_object.h"
 #include "BKE_scene.h"
-#include "BKE_world.h"
 
 #include "BLI_fileops.h"
-#include "BLI_listbase.h"
-#include "BLI_math_matrix.h"
-#include "BLI_math_rotation.h"
 #include "BLI_path_util.h"
 #include "BLI_string.h"
 
 #include "WM_api.h"
 #include "WM_types.h"
 
-#include "usd_reader_geom.h"
-#include "usd_reader_prim.h"
-
-#include <iostream>
-
 namespace blender::io::usd {
 
-CacheArchiveHandle *handle_from_stage_reader(USDStageReader *reader)
-{
-  return reinterpret_cast<CacheArchiveHandle *>(reader);
-}
-
-USDStageReader *stage_reader_from_handle(CacheArchiveHandle *handle)
-{
-  return reinterpret_cast<USDStageReader *>(handle);
-}
-
-static bool gather_objects_paths(const pxr::UsdPrim &object, ListBase *object_paths)
-{
-  if (!object.IsValid()) {
-    return false;
-  }
-
-  for (const pxr::UsdPrim &childPrim : object.GetChildren()) {
-    gather_objects_paths(childPrim, object_paths);
-  }
-
-  void *usd_path_void = MEM_callocN(sizeof(CacheObjectPath), "CacheObjectPath");
-  CacheObjectPath *usd_path = static_cast<CacheObjectPath *>(usd_path_void);
-
-  BLI_strncpy(usd_path->path, object.GetPrimPath().GetString().c_str(), sizeof(usd_path->path));
-  BLI_addtail(object_paths, usd_path);
-
-  return true;
-}
-
-/* Create a collection with the given parent and name. */
-static Collection *create_collection(Main *bmain, Collection *parent, const char *name)
-{
-  if (!bmain) {
-    return nullptr;
-  }
-
-  Collection *coll = BKE_collection_add(bmain, parent, name);
-
-  if (coll) {
-    id_fake_user_set(&coll->id);
-    DEG_id_tag_update(&coll->id, ID_RECALC_COPY_ON_WRITE);
-  }
-
-  return coll;
-}
-
-/* Set the instance collection on the given instance reader.
- *  The collection is assigned from the given map based on
- *  the prototype (maser) prim path. */
-static void set_instance_collection(
-    USDInstanceReader *instance_reader,
-    const std::map<pxr::SdfPath, Collection *> &proto_collection_map)
-{
-  if (!instance_reader) {
-    return;
-  }
-
-  pxr::SdfPath proto_path = instance_reader->proto_path();
-
-  std::map<pxr::SdfPath, Collection *>::const_iterator it = proto_collection_map.find(proto_path);
-
-  if (it != proto_collection_map.end()) {
-    instance_reader->set_instance_collection(it->second);
-  }
-  else {
-    std::cerr << "WARNING: Couldn't find prototype collection for " << instance_reader->prim_path()
-              << std::endl;
-  }
-}
-
-/* Create instance collections for the USD instance readers. */
-static void create_proto_collections(Main *bmain,
-                                     ViewLayer *view_layer,
-                                     Collection *parent_collection,
-                                     const ProtoReaderMap &proto_readers,
-                                     const std::vector<USDPrimReader *> &readers)
-{
-  Collection *all_protos_collection = create_collection(bmain, parent_collection, "prototypes");
-
-  std::map<pxr::SdfPath, Collection *> proto_collection_map;
-
-  for (const auto &pair : proto_readers) {
-
-    std::string proto_collection_name = pair.first.GetString();
-
-    // TODO(makowalski): Is it acceptable to have slashes in the collection names? Or should we
-    // replace them with another character, like an underscore, as in the following?
-    // std::replace(proto_collection_name.begin(), proto_collection_name.end(), '/', '_');
-
-    Collection *proto_collection = create_collection(
-        bmain, all_protos_collection, proto_collection_name.c_str());
-
-    LayerCollection *proto_lc = BKE_layer_collection_first_from_scene_collection(view_layer,
-                                                                                 proto_collection);
-    if (proto_lc) {
-      proto_lc->flag |= LAYER_COLLECTION_HIDE;
-    }
-
-    proto_collection_map.insert(std::make_pair(pair.first, proto_collection));
-  }
-
-  // Set the instance collections on the readers, including the prototype
-  // readers, as instancing may be recursive.
-
-  for (const auto &pair : proto_readers) {
-    for (USDPrimReader *reader : pair.second) {
-      if (USDInstanceReader *instance_reader = dynamic_cast<USDInstanceReader *>(reader)) {
-        set_instance_collection(instance_reader, proto_collection_map);
-      }
-    }
-  }
-
-  for (USDPrimReader *reader : readers) {
-    if (USDInstanceReader *instance_reader = dynamic_cast<USDInstanceReader *>(reader)) {
-      set_instance_collection(instance_reader, proto_collection_map);
-    }
-  }
-
-  // Add the prototype objects to the collections.
-  for (const auto &pair : proto_readers) {
-
-    std::map<pxr::SdfPath, Collection *>::const_iterator it = proto_collection_map.find(
-        pair.first);
-
-    if (it == proto_collection_map.end()) {
-      std::cerr << "WARNING: Couldn't find collection when adding objects for prototype "
-                << pair.first << std::endl;
-      continue;
-    }
-
-    for (USDPrimReader *reader : pair.second) {
-      Object *ob = reader->object();
-
-      if (!ob) {
-        continue;
-      }
-
-      Collection *coll = it->second;
-
-      BKE_collection_object_add(bmain, coll, ob);
-
-      DEG_id_tag_update(&coll->id, ID_RECALC_COPY_ON_WRITE);
-      DEG_id_tag_update_ex(bmain,
-                           &ob->id,
-                           ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION |
-                               ID_RECALC_BASE_FLAGS);
-    }
-  }
-}
-
-// Update the given import settings with the global rotation matrix to orient
-// imported objects with Z-up, if necessary
-static void set_global_rotation(pxr::UsdStageRefPtr stage, ImportSettings &r_settings)
-{
-  if (!stage || pxr::UsdGeomGetStageUpAxis(stage) == pxr::UsdGeomTokens->z) {
-    // Nothing to do.
-    return;
-  }
-
-  r_settings.do_convert_mat = true;
-
-  // Rotate 90 degrees about the X-axis.
-  float rmat[3][3];
-  float axis[3] = {1.0f, 0.0f, 0.0f};
-  axis_angle_normalized_to_mat3(rmat, axis, M_PI / 2.0f);
-
-  unit_m4(r_settings.conversion_mat);
-  copy_m4_m3(r_settings.conversion_mat, rmat);
-}
-
-/* ********************** Export file ********************** */
-
 struct ExportJobData {
   Main *bmain;
   Depsgraph *depsgraph;
@@ -263,21 +60,6 @@ struct ExportJobData {
   bool export_ok;
 };
 
-static void ensure_usd_plugin_path_registered()
-{
-  static bool plugin_path_registered = false;
-  if (plugin_path_registered) {
-    return;
-  }
-  plugin_path_registered = true;
-
-  /* Tell USD which directory to search for its JSON files. If 'datafiles/usd'
-   * does not exist, the USD library will not be able to read or write any files. */
-  const std::string blender_usd_datafiles = BKE_appdir_folder_id(BLENDER_DATAFILES, "usd");
-  /* The trailing slash indicates to the USD library that the path is a directory. */
-  pxr::PlugRegistry::GetInstance().RegisterPlugins(blender_usd_datafiles + "/");
-}
-
 static void export_startjob(void *customdata,
                             /* Cannot be const, this function implements wm_jobs_start_callback.
                              * NOLINTNEXTLINE: readability-non-const-parameter. */
@@ -414,7 +196,6 @@ bool USD_export(bContext *C,
   if (as_background_job) {
     wmJob *wm_job = WM_jobs_get(
         job->wm, CTX_wm_window(C), scene, "USD Export", WM_JOB_PROGRESS, WM_JOB_TYPE_ALEMBIC);
-    // TODO: Should create WM_JOB_TYPE_USD...
 
     /* setup job */
     WM_jobs_customdata_set(wm_job, job, MEM_freeN);
@@ -455,505 +236,3 @@ int USD_get_version(void)
    */
   return PXR_VERSION;
 }
-
-/* ********************** Import file ********************** */
-
-namespace blender::io::usd {
-enum {
-  USD_NO_ERROR = 0,
-  USD_ARCHIVE_FAIL,
-};
-
-struct ImportJobData {
-  Main *bmain;
-  Scene *scene;
-  ViewLayer *view_layer;
-  wmWindowManager *wm;
-
-  char filename[1024];
-  USDImportParams params;
-  ImportSettings settings;
-
-  USDStageReader *archive;
-
-  short *stop;
-  short *do_update;
-  float *progress;
-
-  char error_code;
-  bool was_canceled;
-  bool import_ok;
-};
-
-static void import_startjob(void *customdata, short *stop, short *do_update, float *progress)
-{
-  ImportJob

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list