[Bf-blender-cvs] [605c0d1f1bc] usd-importer-T81257: USD importer: new USDPrimIterator class.

Michael A. Kowalski noreply at git.blender.org
Thu Oct 29 01:42:47 CET 2020


Commit: 605c0d1f1bc37ed958038d7839b11d747f6cc8ac
Author: Michael A. Kowalski
Date:   Wed Oct 28 20:40:43 2020 -0400
Branches: usd-importer-T81257
https://developer.blender.org/rB605c0d1f1bc37ed958038d7839b11d747f6cc8ac

USD importer:  new USDPrimIterator class.

Moved USD stage traversal and related functions
from usd_import_util.* to a dedicated
USDPrimIterator class.

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

M	source/blender/io/usd/CMakeLists.txt
M	source/blender/io/usd/import/usd_import_util.cc
M	source/blender/io/usd/import/usd_import_util.h
A	source/blender/io/usd/import/usd_prim_iterator.cc
A	source/blender/io/usd/import/usd_prim_iterator.h
M	source/blender/io/usd/intern/usd_capi.cc

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

diff --git a/source/blender/io/usd/CMakeLists.txt b/source/blender/io/usd/CMakeLists.txt
index 7dae815ddaf..1a0f85fcef8 100644
--- a/source/blender/io/usd/CMakeLists.txt
+++ b/source/blender/io/usd/CMakeLists.txt
@@ -55,6 +55,7 @@ set(INC_SYS
 set(SRC
 
   import/usd_import_util.cc
+  import/usd_prim_iterator.cc
   import/usd_reader_mesh.cc
   import/usd_reader_object.cc
   import/usd_reader_transform.cc
@@ -71,6 +72,7 @@ set(SRC
   usd.h
   import/usd_importer_context.h
   import/usd_import_util.h
+  import/usd_prim_iterator.h
   import/usd_reader_mesh.h
   import/usd_reader_object.h
   import/usd_reader_transform.h
diff --git a/source/blender/io/usd/import/usd_import_util.cc b/source/blender/io/usd/import/usd_import_util.cc
index 182b6b8e7ac..a0d1983e3b7 100644
--- a/source/blender/io/usd/import/usd_import_util.cc
+++ b/source/blender/io/usd/import/usd_import_util.cc
@@ -59,13 +59,6 @@
 #include <iostream>
 #include <map>
 
-/* TfToken objects are not cheap to construct, so we do it once. */
-namespace usdtokens {
-static const pxr::TfToken xform_type("Xform", pxr::TfToken::Immortal);
-static const pxr::TfToken mesh_type("Mesh", pxr::TfToken::Immortal);
-static const pxr::TfToken scope_type("Scope", pxr::TfToken::Immortal);
-}  // namespace usdtokens
-
 namespace {
 /* Copy between Z-up and Y-up. */
 
@@ -89,36 +82,6 @@ inline void copy_zup_from_yup(float zup[3], const float yup[3])
 
 namespace blender::io::usd {
 
-static USDObjectReader *get_reader(const pxr::UsdPrim &prim, const USDImporterContext &context)
-{
-  USDObjectReader *result = nullptr;
-
-  if (prim.IsA<pxr::UsdGeomMesh>()) {
-    result = new USDMeshReader(prim, context);
-  }
-  else if (prim.IsA<pxr::UsdGeomXform>()) {
-    result = new USDTransformReader(prim, context);
-  }
-
-  return result;
-}
-
-void debug_traverse_stage(const pxr::UsdStageRefPtr &usd_stage)
-{
-  if (!usd_stage) {
-    return;
-  }
-
-  pxr::UsdPrimRange prims = usd_stage->Traverse(
-      pxr::UsdTraverseInstanceProxies(pxr::UsdPrimAllPrimsPredicate));
-
-  for (const pxr::UsdPrim &prim : prims) {
-    pxr::SdfPath path = prim.GetPath();
-    printf("%s\n", path.GetString().c_str());
-    printf("  Type: %s\n", prim.GetTypeName().GetString().c_str());
-  }
-}
-
 void create_swapped_rotation_matrix(float rot_x_mat[3][3],
                                     float rot_y_mat[3][3],
                                     float rot_z_mat[3][3],
@@ -230,69 +193,4 @@ void copy_m44_axis_swap(float dst_mat[4][4], float src_mat[4][4], UsdAxisSwapMod
   mul_m4_m4m4(dst_mat, dst_mat, dst_scale_mat);
 }
 
-void create_readers(const pxr::UsdPrim &prim,
-                    const USDImporterContext &context,
-                    std::vector<USDObjectReader *> &r_readers,
-                    std::vector<USDObjectReader *> &r_child_readers)
-{
-  if (!prim) {
-    return;
-  }
-
-  bool is_root = prim.IsPseudoRoot();
-
-  std::vector<USDObjectReader *> child_readers;
-
-  /* Recursively create readers for the child prims. */
-  pxr::UsdPrimSiblingRange child_prims = prim.GetFilteredChildren(
-      pxr::UsdTraverseInstanceProxies(pxr::UsdPrimDefaultPredicate));
-
-  for (const pxr::UsdPrim &child_prim : child_prims) {
-    create_readers(child_prim, context, r_readers, child_readers);
-  }
-
-  if (is_root) {
-    /* We're at the pseudo root, so we're done. */
-    return;
-  }
-
-  /* We prune away empty transform or scope hierarchies (we can add an import flag to make this
-   * behavior optional).  Therefore, we skip this prim if it's an Xform or Scope and if
-   * it has no corresponding child readers. */
-  if ((prim.IsA<pxr::UsdGeomXform>() || prim.IsA<pxr::UsdGeomScope>()) && child_readers.empty()) {
-    return;
-  }
-
-  /* If this is an Xform prim, see if we can merge with the child reader.
-   * We only merge if the child reader hasn't yet been merged
-   * and if it corresponds to a mesh prim.  The list of child types that
-   * can be merged will be expanded as we support more reader types
-   * (e.g., for lights, curves, etc.). */
-
-  if (prim.IsA<pxr::UsdGeomXform>() && child_readers.size() == 1 &&
-      !child_readers.front()->merged_with_parent() &&
-      child_readers.front()->prim().IsA<pxr::UsdGeomMesh>()) {
-    child_readers.front()->set_merged_with_parent(true);
-    /* Don't create a reader for the Xform but, instead, return the grandchild
-     * that we merged. */
-    r_child_readers.push_back(child_readers.front());
-    return;
-  }
-
-  USDObjectReader *reader = get_reader(prim, context);
-
-  if (reader) {
-    for (USDObjectReader *child_reader : child_readers) {
-      child_reader->set_parent(reader);
-    }
-    r_child_readers.push_back(reader);
-    r_readers.push_back(reader);
-  }
-  else {
-    /* No reader was allocated for this prim, so we pass our child readers back to the caller,
-     * for possible handling by a parent reader. */
-    r_child_readers.insert(r_child_readers.end(), child_readers.begin(), child_readers.end());
-  }
-}
-
 } /* namespace blender::io::usd */
diff --git a/source/blender/io/usd/import/usd_import_util.h b/source/blender/io/usd/import/usd_import_util.h
index 40fd6fc2719..5e0ffd1a64b 100644
--- a/source/blender/io/usd/import/usd_import_util.h
+++ b/source/blender/io/usd/import/usd_import_util.h
@@ -26,11 +26,6 @@
 
 namespace blender::io::usd {
 
-struct USDImporterContext;
-class USDObjectReader;
-
-void debug_traverse_stage(const pxr::UsdStageRefPtr &usd_stage);
-
 /* TODO(makowalski):  copy_m44_axis_swap, create_swapped_rotation_matrix
  * and copy_zup_from_yup below are duplicates of the declarations in
  * abc_axis_conversion.h, and should be moved to a shared location. */
@@ -57,9 +52,4 @@ BLI_INLINE void copy_zup_from_yup(float zup[3], const float yup[3])
   zup[2] = old_yup1;
 }
 
-void create_readers(const pxr::UsdPrim &root,
-                    const USDImporterContext &context,
-                    std::vector<USDObjectReader *> &r_readers,
-                    std::vector<USDObjectReader *> &r_child_readers);
-
 } /* namespace blender::io::usd */
diff --git a/source/blender/io/usd/import/usd_prim_iterator.cc b/source/blender/io/usd/import/usd_prim_iterator.cc
new file mode 100644
index 00000000000..6567c411549
--- /dev/null
+++ b/source/blender/io/usd/import/usd_prim_iterator.cc
@@ -0,0 +1,133 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) 2020 Blender Foundation.
+ * All rights reserved.
+ */
+
+#include "usd_prim_iterator.h"
+
+#include "usd.h"
+#include "usd_importer_context.h"
+#include "usd_reader_mesh.h"
+#include "usd_reader_object.h"
+#include "usd_reader_transform.h"
+
+#include <pxr/base/plug/registry.h>
+#include <pxr/pxr.h>
+#include <pxr/usd/usd/prim.h>
+#include <pxr/usd/usd/primRange.h>
+#include <pxr/usd/usd/stage.h>
+#include <pxr/usd/usdGeom/mesh.h>
+#include <pxr/usd/usdGeom/scope.h>
+#include <pxr/usd/usdGeom/tokens.h>
+#include <pxr/usd/usdGeom/xformable.h>
+#include <iostream>
+
+namespace blender::io::usd {
+
+USDObjectReader *USDPrimIterator::get_reader(const pxr::UsdPrim &prim, const USDImporterContext &context)
+{
+  USDObjectReader *result = nullptr;
+
+  if (prim.IsA<pxr::UsdGeomMesh>()) {
+    result = new USDMeshReader(prim, context);
+  }
+  else if (prim.IsA<pxr::UsdGeomXform>()) {
+    result = new USDTransformReader(prim, context);
+  }
+
+  return result;
+}
+
+void USDPrimIterator::create_readers(const pxr::UsdPrim &prim,
+                                     const USDImporterContext &context,
+                                     std::vector<USDObjectReader *> &r_readers,
+                                     std::vector<USDObjectReader *> &r_child_readers)
+{
+  if (!prim) {
+    return;
+  }
+
+  std::vector<USDObjectReader *> child_readers;
+
+  /* Recursively create readers for the child prims. */
+  pxr::UsdPrimSiblingRange child_prims = prim.GetFilteredChildren(
+      pxr::UsdTraverseInstanceProxies(pxr::UsdPrimDefaultPredicate));
+
+  for (const pxr::UsdPrim &child_prim : child_prims) {
+    create_readers(child_prim, context, r_readers, child_readers);
+  }
+
+  if (prim.IsPseudoRoot()) {
+    /* We're at the pseudo root, so we're done. */
+    return;
+  }
+
+  /* We prune away empty transform or scope hierarchies (we can add an import flag to make this
+   * behavior optional).  Therefore, we skip this prim if it's an Xform or Scope and if
+   * it has no corresponding child readers. */
+  if ((prim.IsA<pxr::UsdGeomXform>() || prim.IsA<pxr::UsdGeomScope>()) && child_readers.empty()) {
+    return;
+  }
+
+  /* If this is an Xform prim, see if we can merge with the child reader.
+   * We only merge if the child reader hasn't yet been merged
+   * and if it corresponds to a mesh prim.  The list of child types that
+   * can be merged will be expanded as we support more reader types
+   * (e.g., for lights, curves, etc.). */
+
+  if (prim.IsA<pxr::UsdGeomXform>() && child_readers.size() == 1 &&
+      !child_readers.front()->merged_with_parent() &&
+      child_readers.front()->prim().IsA<pxr::UsdGeomMesh>()) {
+    child_readers.front()->set_merged_with_parent(true);
+    /* Don't create a reader for the Xform but, instead, return the child
+     * that we merged. */
+    r_child_readers.push_back(child_readers.front());
+    return;
+  }
+
+  USDObjectReader *reader = get_reader(prim, context);
+
+  if (reader) {
+    for (USDObjectReader *child_reader : child_readers) {
+      child_reader->set_parent(reader);
+    }
+    r_child_readers.push_back(reader);
+    r_rea

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list