[Bf-blender-cvs] [19dd90686b2] usd-importer-T81257: USD importer: import shapes as empties.

makowalski noreply at git.blender.org
Sun Dec 27 22:18:13 CET 2020


Commit: 19dd90686b253243cd3af19bed653279758df0d7
Author: makowalski
Date:   Thu Dec 10 21:02:57 2020 -0500
Branches: usd-importer-T81257
https://developer.blender.org/rB19dd90686b253243cd3af19bed653279758df0d7

USD importer: import shapes as empties.

Added support for importing USD shapes (e.g., sphere, cube, etc.)
as Blender empty objects, to allow visual debugging of
transforms.  Eventually, this logic will be extended to create
meshes corresponding to the actual shapes.

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

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_shape.cc
A	source/blender/io/usd/import/usd_reader_shape.h

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

diff --git a/source/blender/io/usd/CMakeLists.txt b/source/blender/io/usd/CMakeLists.txt
index 6d693401b6d..3e94517ee47 100644
--- a/source/blender/io/usd/CMakeLists.txt
+++ b/source/blender/io/usd/CMakeLists.txt
@@ -63,6 +63,7 @@ set(SRC
   import/usd_reader_mesh.cc
   import/usd_reader_mesh_base.cc
   import/usd_reader_prim.cc
+  import/usd_reader_shape.cc
   import/usd_reader_xform.cc
   import/usd_reader_xformable.cc
   intern/usd_capi.cc
@@ -86,6 +87,7 @@ set(SRC
   import/usd_reader_mesh.h
   import/usd_reader_mesh_base.h
   import/usd_reader_prim.h
+  import/usd_reader_shape.h
   import/usd_reader_xform.h
   import/usd_reader_xformable.h
   intern/usd_exporter_context.h
diff --git a/source/blender/io/usd/import/usd_prim_iterator.cc b/source/blender/io/usd/import/usd_prim_iterator.cc
index 4cf353bf8a3..92b71dafe32 100644
--- a/source/blender/io/usd/import/usd_prim_iterator.cc
+++ b/source/blender/io/usd/import/usd_prim_iterator.cc
@@ -25,6 +25,7 @@
 #include "usd_reader_camera.h"
 #include "usd_reader_light.h"
 #include "usd_reader_mesh.h"
+#include "usd_reader_shape.h"
 #include "usd_reader_xform.h"
 #include "usd_reader_xformable.h"
 
@@ -40,8 +41,13 @@
 #include <pxr/usd/usd/primRange.h>
 #include <pxr/usd/usd/stage.h>
 #include <pxr/usd/usdGeom/camera.h>
+#include <pxr/usd/usdGeom/capsule.h>
+#include <pxr/usd/usdGeom/cone.h>
+#include <pxr/usd/usdGeom/cube.h>
+#include <pxr/usd/usdGeom/cylinder.h>
 #include <pxr/usd/usdGeom/mesh.h>
 #include <pxr/usd/usdGeom/scope.h>
+#include <pxr/usd/usdGeom/sphere.h>
 #include <pxr/usd/usdGeom/tokens.h>
 #include <pxr/usd/usdGeom/xformable.h>
 #include <pxr/usd/usdLux/light.h>
@@ -124,6 +130,11 @@ USDXformableReader *USDPrimIterator::get_object_reader(const pxr::UsdPrim &prim,
   else if (prim.IsA<pxr::UsdGeomMesh>()) {
     result = new USDMeshReader(prim, context);
   }
+  else if (prim.IsA<pxr::UsdGeomCapsule>() || prim.IsA<pxr::UsdGeomCone>() ||
+           prim.IsA<pxr::UsdGeomCube>() || prim.IsA<pxr::UsdGeomCylinder>() ||
+           prim.IsA<pxr::UsdGeomSphere>()) {
+    result = new USDShapeReader(prim, context);
+  }
   else if (prim.IsA<pxr::UsdGeomXform>()) {
     result = new USDXformReader(prim, context);
   }
diff --git a/source/blender/io/usd/import/usd_reader_shape.cc b/source/blender/io/usd/import/usd_reader_shape.cc
new file mode 100644
index 00000000000..0969b692523
--- /dev/null
+++ b/source/blender/io/usd/import/usd_reader_shape.cc
@@ -0,0 +1,97 @@
+/*
+ * 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_shape.h"
+
+#include "BKE_object.h"
+#include "DNA_object_types.h"
+
+#include <pxr/usd/usdGeom/cube.h>
+#include <pxr/usd/usdGeom/gprim.h>
+#include <pxr/usd/usdGeom/imageable.h>
+#include <pxr/usd/usdGeom/sphere.h>
+
+#include <iostream>
+
+namespace blender::io::usd {
+
+USDShapeReader::USDShapeReader(const pxr::UsdPrim &prim, const USDImporterContext &context)
+    : USDXformableReader(prim, context)
+{
+}
+
+USDShapeReader::~USDShapeReader()
+{
+}
+
+bool USDShapeReader::valid() const
+{
+  return static_cast<bool>(pxr::UsdGeomGprim(prim_));
+}
+
+void USDShapeReader::create_object(Main *bmain, double time, USDDataCache *data_cache)
+{
+  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::UsdGeomImageable imageable(this->prim_);
+
+  if (!imageable) {
+    return;  // Should never happen.
+  }
+
+  pxr::TfToken vis_tok = imageable.ComputeVisibility();
+
+  if (vis_tok == pxr::UsdGeomTokens->invisible) {
+    return;
+  }
+
+  this->object_ = BKE_object_add_only_object(bmain, OB_EMPTY, this->prim_name().c_str());
+
+  if (this->prim_.IsA<pxr::UsdGeomCube>()) {
+    this->object_->empty_drawtype = OB_CUBE;
+
+    pxr::UsdGeomCube cube(this->prim_);
+
+    if (cube) {
+      double size = 1.0;
+      cube.GetSizeAttr().Get<double>(&size);
+      this->object_->empty_drawsize = static_cast<float>(0.5 * size);
+    }
+  }
+  else if (this->prim_.IsA<pxr::UsdGeomSphere>()) {
+    this->object_->empty_drawtype = OB_EMPTY_SPHERE;
+
+    pxr::UsdGeomSphere sphere(this->prim_);
+
+    if (sphere) {
+      double rad = 1.0;
+      sphere.GetRadiusAttr().Get<double>(&rad);
+      this->object_->empty_drawsize = static_cast<float>(rad);
+    }
+  }
+
+  this->object_->data = nullptr;
+}
+
+}  // namespace blender::io::usd
diff --git a/source/blender/io/usd/import/usd_reader_shape.h b/source/blender/io/usd/import/usd_reader_shape.h
new file mode 100644
index 00000000000..bc48ab4ac6c
--- /dev/null
+++ b/source/blender/io/usd/import/usd_reader_shape.h
@@ -0,0 +1,51 @@
+/*
+ * 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.
+ */
+#pragma once
+
+#include "usd_reader_xformable.h"
+
+namespace blender::io::usd {
+
+/* Creates a Blender empty object as a placeholder for USD geom
+ * sphere, cube, cylinder, cone and capsule schemas. Currently,
+ * the viewport display is limited only to the draw modes for
+ * empty objects, but is still useful for visualizing transform
+ * hierarchies.  Eventually, we could extend this class to
+ * derive from USDMeshReaderBase to generate mesh geometry for
+ * an accurate representation of the shapes. */
+
+class USDShapeReader : public USDXformableReader {
+ protected:
+
+ public:
+  USDShapeReader(const pxr::UsdPrim &prim, const USDImporterContext &context);
+
+  virtual ~USDShapeReader();
+
+  bool valid() const override;
+
+  virtual bool can_merge_with_parent() const override
+  {
+    return true;
+  }
+
+  void create_object(Main *bmain, double time, USDDataCache *data_cache) override;
+};
+
+}  // namespace blender::io::usd



More information about the Bf-blender-cvs mailing list