[Bf-blender-cvs] [9a5b4be8d5d] usd-importer-T81257: WIP: new UsdCameraReader class.

Michael A. Kowalski noreply at git.blender.org
Fri Nov 13 23:51:24 CET 2020


Commit: 9a5b4be8d5da0cc0421f8c27b743d6eee24b0bca
Author: Michael A. Kowalski
Date:   Wed Nov 11 22:19:47 2020 -0500
Branches: usd-importer-T81257
https://developer.blender.org/rB9a5b4be8d5da0cc0421f8c27b743d6eee24b0bca

WIP: new UsdCameraReader class.

Also added logic to instantiate the camera reader
when traversing the USD stage.  Not all camera
parameters are being set yet.

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

M	source/blender/io/usd/CMakeLists.txt
M	source/blender/io/usd/import/usd_prim_iterator.cc
A	source/blender/io/usd/import/usd_reader_camera.cc
A	source/blender/io/usd/import/usd_reader_camera.h
M	source/blender/io/usd/import/usd_reader_mesh.cc
M	source/blender/io/usd/import/usd_reader_mesh_base.cc
M	source/blender/io/usd/import/usd_reader_xform.h
M	source/blender/io/usd/import/usd_reader_xformable.h

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

diff --git a/source/blender/io/usd/CMakeLists.txt b/source/blender/io/usd/CMakeLists.txt
index f554c1aaf5e..c965a613bd0 100644
--- a/source/blender/io/usd/CMakeLists.txt
+++ b/source/blender/io/usd/CMakeLists.txt
@@ -56,6 +56,7 @@ set(SRC
 
   import/usd_import_util.cc
   import/usd_prim_iterator.cc
+  import/usd_reader_camera.cc
   import/usd_reader_mesh.cc
   import/usd_reader_mesh_base.cc
   import/usd_reader_prim.cc
@@ -75,6 +76,7 @@ set(SRC
   import/usd_importer_context.h
   import/usd_import_util.h
   import/usd_prim_iterator.h
+  import/usd_reader_camera.h
   import/usd_reader_mesh.h
   import/usd_reader_mesh_base.h
   import/usd_reader_prim.h
diff --git a/source/blender/io/usd/import/usd_prim_iterator.cc b/source/blender/io/usd/import/usd_prim_iterator.cc
index 491103e6135..08daa651465 100644
--- a/source/blender/io/usd/import/usd_prim_iterator.cc
+++ b/source/blender/io/usd/import/usd_prim_iterator.cc
@@ -21,6 +21,7 @@
 
 #include "usd.h"
 #include "usd_importer_context.h"
+#include "usd_reader_camera.h"
 #include "usd_reader_mesh.h"
 #include "usd_reader_xform.h"
 #include "usd_reader_xformable.h"
@@ -30,6 +31,7 @@
 #include <pxr/usd/usd/prim.h>
 #include <pxr/usd/usd/primRange.h>
 #include <pxr/usd/usd/stage.h>
+#include <pxr/usd/usdGeom/camera.h>
 #include <pxr/usd/usdGeom/mesh.h>
 #include <pxr/usd/usdGeom/scope.h>
 #include <pxr/usd/usdGeom/tokens.h>
@@ -89,7 +91,10 @@ USDXformableReader *USDPrimIterator::get_object_reader(const pxr::UsdPrim &prim,
 {
   USDXformableReader *result = nullptr;
 
-  if (prim.IsA<pxr::UsdGeomMesh>()) {
+  if (prim.IsA<pxr::UsdGeomCamera>()) {
+    result = new USDCameraReader(prim, context);
+  }
+  else if (prim.IsA<pxr::UsdGeomMesh>()) {
     result = new USDMeshReader(prim, context);
   }
   else if (prim.IsA<pxr::UsdGeomXform>()) {
@@ -130,15 +135,11 @@ void USDPrimIterator::create_object_readers(const pxr::UsdPrim &prim,
     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 this is an Xform prim, see if we can merge with the child reader. */
 
   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()->can_merge_with_parent()) {
     child_readers.front()->set_merged_with_parent(true);
     /* Don't create a reader for the Xform but, instead, return the child
      * that we merged. */
diff --git a/source/blender/io/usd/import/usd_reader_camera.cc b/source/blender/io/usd/import/usd_reader_camera.cc
new file mode 100644
index 00000000000..4f774bc69ad
--- /dev/null
+++ b/source/blender/io/usd/import/usd_reader_camera.cc
@@ -0,0 +1,87 @@
+/*
+ * 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_reader_camera.h"
+
+#include "DNA_camera_types.h"
+#include "DNA_object_types.h"
+
+#include "BKE_camera.h"
+#include "BKE_object.h"
+
+#include <iostream>
+
+namespace blender::io::usd {
+
+USDCameraReader::USDCameraReader(const pxr::UsdPrim &prim, const USDImporterContext &context)
+    : USDXformableReader(prim, context), camera_(prim)
+{
+}
+
+bool USDCameraReader::valid() const
+{
+  return static_cast<bool>(camera_);
+}
+
+void USDCameraReader::create_object(Main *bmain, double time)
+{
+  if (!this->valid()) {
+    return;
+  }
+
+  /* Determine prim visibility.
+   * TODO(makowalski): Consider optimizations to avoid this expensive call,
+   * for example, by determining visibility during stage traversal. */
+  pxr::TfToken vis_tok = this->camera_.ComputeVisibility();
+
+  if (vis_tok == pxr::UsdGeomTokens->invisible) {
+    return;
+  }
+
+  std::string obj_name = get_object_name();
+
+  if (obj_name.empty()) {
+    /* Sanity check. */
+    std::cerr << "Warning: couldn't determine object name for " << this->prim_path() << std::endl;
+  }
+
+  this->object_ = BKE_object_add_only_object(bmain, OB_CAMERA, obj_name.c_str());
+
+  std::string cam_name = get_data_name();
+
+  if (cam_name.empty()) {
+    /* Sanity check. */
+    std::cerr << "Warning: couldn't determine camera name for " << this->prim_path() << std::endl;
+  }
+
+  Camera *bcam = static_cast<Camera *>(BKE_camera_add(bmain, cam_name.c_str()));
+
+  pxr::GfCamera usd_cam = camera_.GetCamera(time);
+
+  bcam->lens = usd_cam.GetFocalLength();
+
+  pxr::GfRange1f usd_clip_range = usd_cam.GetClippingRange();
+
+  bcam->clip_start = usd_clip_range.GetMin();
+  bcam->clip_end = usd_clip_range.GetMax();
+
+  this->object_->data = bcam;
+}
+
+}  // namespace blender::io::usd
diff --git a/source/blender/io/usd/import/usd_reader_xform.h b/source/blender/io/usd/import/usd_reader_camera.h
similarity index 79%
copy from source/blender/io/usd/import/usd_reader_xform.h
copy to source/blender/io/usd/import/usd_reader_camera.h
index 893bd55d56f..194fd5909d3 100644
--- a/source/blender/io/usd/import/usd_reader_xform.h
+++ b/source/blender/io/usd/import/usd_reader_camera.h
@@ -20,18 +20,18 @@
 
 #include "usd_reader_xformable.h"
 
-#include <pxr/usd/usdGeom/xform.h>
+#include <pxr/usd/usdGeom/camera.h>
 
 namespace blender::io::usd {
 
-/* Wraps the UsdGeomXform schema. Creates a Blender Empty object. */
+/* Wraps the UsdGeomCamera schema. Creates a Blender Camera object. */
 
-class USDXformReader : public USDXformableReader {
+class USDCameraReader : public USDXformableReader {
 
-  pxr::UsdGeomXform xform_;
+  pxr::UsdGeomCamera camera_;
 
  public:
-  USDXformReader(const pxr::UsdPrim &prim, const USDImporterContext &context);
+  USDCameraReader(const pxr::UsdPrim &prim, const USDImporterContext &context);
 
   bool valid() const override;
 
diff --git a/source/blender/io/usd/import/usd_reader_mesh.cc b/source/blender/io/usd/import/usd_reader_mesh.cc
index d2226158ae3..4f97a901c81 100644
--- a/source/blender/io/usd/import/usd_reader_mesh.cc
+++ b/source/blender/io/usd/import/usd_reader_mesh.cc
@@ -356,7 +356,7 @@ Mesh *USDMeshReader::create_mesh(Main *bmain, double time)
     return nullptr;
   }
 
-  std::string mesh_name = this->prim_name();
+  std::string mesh_name = this->get_data_name();
 
   if (mesh_name.empty()) {
     /* Sanity check. */
diff --git a/source/blender/io/usd/import/usd_reader_mesh_base.cc b/source/blender/io/usd/import/usd_reader_mesh_base.cc
index 409b90096cd..954ffcfc8a5 100644
--- a/source/blender/io/usd/import/usd_reader_mesh_base.cc
+++ b/source/blender/io/usd/import/usd_reader_mesh_base.cc
@@ -68,7 +68,7 @@ void USDMeshReaderBase::create_object(Main *bmain, double time)
     return;
   }
 
-  std::string obj_name = merged_with_parent_ ? this->parent_prim_name() : this->prim_name();
+  std::string obj_name = get_object_name();
 
   if (obj_name.empty()) {
     /* Sanity check. */
diff --git a/source/blender/io/usd/import/usd_reader_xform.h b/source/blender/io/usd/import/usd_reader_xform.h
index 893bd55d56f..4be0f2d1273 100644
--- a/source/blender/io/usd/import/usd_reader_xform.h
+++ b/source/blender/io/usd/import/usd_reader_xform.h
@@ -35,6 +35,11 @@ class USDXformReader : public USDXformableReader {
 
   bool valid() const override;
 
+  bool can_merge_with_parent() const override
+  {
+    return false;
+  }
+
   void create_object(Main *bmain, double time) override;
 };
 
diff --git a/source/blender/io/usd/import/usd_reader_xformable.h b/source/blender/io/usd/import/usd_reader_xformable.h
index 11ec5b022b8..8e8cecacc0e 100644
--- a/source/blender/io/usd/import/usd_reader_xformable.h
+++ b/source/blender/io/usd/import/usd_reader_xformable.h
@@ -70,10 +70,25 @@ class USDXformableReader : public USDPrimReader {
     return merged_with_parent_;
   }
 
+  std::string get_object_name() const
+  {
+    return merged_with_parent_ ? this->parent_prim_name() : this->prim_name();
+  }
+
+  std::string get_data_name() const
+  {
+    return this->prim_name();
+  }
+
   virtual bool valid() const = 0;
 
   virtual void create_object(Main *bmain, double time) = 0;
 
+  virtual bool can_merge_with_parent() const
+  {
+    return true;
+  }
+
   void set_object_transform(const double time);
 
   void read_matrix(float r_mat[4][4], const double time, const float scale, bool &is_constant);



More information about the Bf-blender-cvs mailing list