Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'source/blender/io/usd/intern/usd_reader_stage.cc')
-rw-r--r--source/blender/io/usd/intern/usd_reader_stage.cc324
1 files changed, 324 insertions, 0 deletions
diff --git a/source/blender/io/usd/intern/usd_reader_stage.cc b/source/blender/io/usd/intern/usd_reader_stage.cc
new file mode 100644
index 00000000000..d3693f783ec
--- /dev/null
+++ b/source/blender/io/usd/intern/usd_reader_stage.cc
@@ -0,0 +1,324 @@
+/*
+ * 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) 2021 Tangent Animation and
+ * NVIDIA Corporation. All rights reserved.
+ */
+
+#include "usd_reader_stage.h"
+#include "usd_reader_camera.h"
+#include "usd_reader_curve.h"
+#include "usd_reader_instance.h"
+#include "usd_reader_light.h"
+#include "usd_reader_mesh.h"
+#include "usd_reader_nurbs.h"
+#include "usd_reader_prim.h"
+#include "usd_reader_volume.h"
+#include "usd_reader_xform.h"
+
+#include <pxr/pxr.h>
+#include <pxr/usd/usdGeom/camera.h>
+#include <pxr/usd/usdGeom/curves.h>
+#include <pxr/usd/usdGeom/mesh.h>
+#include <pxr/usd/usdGeom/nurbsCurves.h>
+#include <pxr/usd/usdGeom/scope.h>
+#include <pxr/usd/usdLux/light.h>
+
+#include <iostream>
+
+namespace blender::io::usd {
+
+USDStageReader::USDStageReader(pxr::UsdStageRefPtr stage,
+ const USDImportParams &params,
+ const ImportSettings &settings)
+ : stage_(stage), params_(params), settings_(settings)
+{
+}
+
+USDStageReader::~USDStageReader()
+{
+ clear_readers();
+}
+
+bool USDStageReader::valid() const
+{
+ return stage_;
+}
+
+USDPrimReader *USDStageReader::create_reader_if_allowed(const pxr::UsdPrim &prim)
+{
+ if (params_.import_cameras && prim.IsA<pxr::UsdGeomCamera>()) {
+ return new USDCameraReader(prim, params_, settings_);
+ }
+ if (params_.import_curves && prim.IsA<pxr::UsdGeomBasisCurves>()) {
+ return new USDCurvesReader(prim, params_, settings_);
+ }
+ if (params_.import_curves && prim.IsA<pxr::UsdGeomNurbsCurves>()) {
+ return new USDNurbsReader(prim, params_, settings_);
+ }
+ if (params_.import_meshes && prim.IsA<pxr::UsdGeomMesh>()) {
+ return new USDMeshReader(prim, params_, settings_);
+ }
+ if (params_.import_lights && prim.IsA<pxr::UsdLuxLight>()) {
+ return new USDLightReader(prim, params_, settings_);
+ }
+ if (params_.import_volumes && prim.IsA<pxr::UsdVolVolume>()) {
+ return new USDVolumeReader(prim, params_, settings_);
+ }
+ if (prim.IsA<pxr::UsdGeomImageable>()) {
+ return new USDXformReader(prim, params_, settings_);
+ }
+
+ return nullptr;
+}
+
+USDPrimReader *USDStageReader::create_reader(const pxr::UsdPrim &prim)
+{
+ if (prim.IsA<pxr::UsdGeomCamera>()) {
+ return new USDCameraReader(prim, params_, settings_);
+ }
+ if (prim.IsA<pxr::UsdGeomBasisCurves>()) {
+ return new USDCurvesReader(prim, params_, settings_);
+ }
+ if (prim.IsA<pxr::UsdGeomNurbsCurves>()) {
+ return new USDNurbsReader(prim, params_, settings_);
+ }
+ if (prim.IsA<pxr::UsdGeomMesh>()) {
+ return new USDMeshReader(prim, params_, settings_);
+ }
+ if (prim.IsA<pxr::UsdLuxLight>()) {
+ return new USDLightReader(prim, params_, settings_);
+ }
+ if (prim.IsA<pxr::UsdVolVolume>()) {
+ return new USDVolumeReader(prim, params_, settings_);
+ }
+ if (prim.IsA<pxr::UsdGeomImageable>()) {
+ return new USDXformReader(prim, params_, settings_);
+ }
+ return nullptr;
+}
+
+/* Returns true if the given prim should be included in the
+ * traversal based on the import options and the prim's visibility
+ * attribute. Note that the prim will be trivially included
+ * if it has no visibility attribute or if the visibility
+ * is inherited. */
+bool USDStageReader::include_by_visibility(const pxr::UsdGeomImageable &imageable) const
+{
+ if (!params_.import_visible_only) {
+ /* Invisible prims are allowed. */
+ return true;
+ }
+
+ pxr::UsdAttribute visibility_attr = imageable.GetVisibilityAttr();
+
+ if (!visibility_attr) {
+ /* No visibility attribute, so allow. */
+ return true;
+ }
+
+ /* Include if the prim has an animating visibility attribute or is not invisible. */
+
+ if (visibility_attr.ValueMightBeTimeVarying()) {
+ return true;
+ }
+
+ pxr::TfToken visibility;
+ visibility_attr.Get(&visibility);
+ return visibility != pxr::UsdGeomTokens->invisible;
+}
+
+/* Returns true if the given prim should be included in the
+ * traversal based on the import options and the prim's purpose
+ * attribute. E.g., return false (to exclude the prim) if the prim
+ * represents guide geometry and the 'Import Guide' option is
+ * toggled off. */
+bool USDStageReader::include_by_purpose(const pxr::UsdGeomImageable &imageable) const
+{
+ if (params_.import_guide && params_.import_proxy && params_.import_render) {
+ /* The options allow any purpose, so we trivially include the prim. */
+ return true;
+ }
+
+ pxr::UsdAttribute purpose_attr = imageable.GetPurposeAttr();
+
+ if (!purpose_attr) {
+ /* No purpose attribute, so trivially include the prim. */
+ return true;
+ }
+
+ pxr::TfToken purpose;
+ purpose_attr.Get(&purpose);
+
+ if (purpose == pxr::UsdGeomTokens->guide) {
+ return params_.import_guide;
+ }
+ if (purpose == pxr::UsdGeomTokens->proxy) {
+ return params_.import_proxy;
+ }
+ if (purpose == pxr::UsdGeomTokens->render) {
+ return params_.import_render;
+ }
+
+ return true;
+}
+
+/* Determine if the given reader can use the parent of the encapsulated USD prim
+ * to compute the Blender object's transform. If so, the reader is appropriately
+ * flagged and the function returns true. Otherwise, the function returns false. */
+static bool merge_with_parent(USDPrimReader *reader)
+{
+ USDXformReader *xform_reader = dynamic_cast<USDXformReader *>(reader);
+
+ if (!xform_reader) {
+ return false;
+ }
+
+ /* Check if the Xform reader is already merged. */
+ if (xform_reader->use_parent_xform()) {
+ return false;
+ }
+
+ /* Only merge if the parent is an Xform. */
+ if (!xform_reader->prim().GetParent().IsA<pxr::UsdGeomXform>()) {
+ return false;
+ }
+
+ /* Don't merge Xform and Scope prims. */
+ if (xform_reader->prim().IsA<pxr::UsdGeomXform>() ||
+ xform_reader->prim().IsA<pxr::UsdGeomScope>()) {
+ return false;
+ }
+
+ /* Don't merge if the prim has authored transform ops. */
+ if (xform_reader->prim_has_xform_ops()) {
+ return false;
+ }
+
+ /* Flag the Xform reader as merged. */
+ xform_reader->set_use_parent_xform(true);
+
+ return true;
+}
+
+USDPrimReader *USDStageReader::collect_readers(Main *bmain, const pxr::UsdPrim &prim)
+{
+ if (prim.IsA<pxr::UsdGeomImageable>()) {
+ pxr::UsdGeomImageable imageable(prim);
+
+ if (!include_by_purpose(imageable)) {
+ return nullptr;
+ }
+
+ if (!include_by_visibility(imageable)) {
+ return nullptr;
+ }
+ }
+
+ pxr::Usd_PrimFlagsPredicate filter_predicate = pxr::UsdPrimDefaultPredicate;
+
+ if (params_.import_instance_proxies) {
+ filter_predicate = pxr::UsdTraverseInstanceProxies(filter_predicate);
+ }
+
+ pxr::UsdPrimSiblingRange children = prim.GetFilteredChildren(filter_predicate);
+
+ std::vector<USDPrimReader *> child_readers;
+
+ for (const auto &childPrim : children) {
+ if (USDPrimReader *child_reader = collect_readers(bmain, childPrim)) {
+ child_readers.push_back(child_reader);
+ }
+ }
+
+ if (prim.IsPseudoRoot()) {
+ return nullptr;
+ }
+
+ /* Check if we can merge an Xform with its child prim. */
+ if (child_readers.size() == 1) {
+
+ USDPrimReader *child_reader = child_readers.front();
+
+ if (merge_with_parent(child_reader)) {
+ return child_reader;
+ }
+ }
+
+ USDPrimReader *reader = create_reader_if_allowed(prim);
+
+ if (!reader) {
+ return nullptr;
+ }
+
+ reader->create_object(bmain, 0.0);
+
+ readers_.push_back(reader);
+ reader->incref();
+
+ /* Set each child reader's parent. */
+ for (USDPrimReader *child_reader : child_readers) {
+ child_reader->parent(reader);
+ }
+
+ return reader;
+}
+
+void USDStageReader::collect_readers(Main *bmain)
+{
+ if (!valid()) {
+ return;
+ }
+
+ clear_readers();
+
+ /* Iterate through the stage. */
+ pxr::UsdPrim root = stage_->GetPseudoRoot();
+
+ std::string prim_path_mask(params_.prim_path_mask);
+
+ if (!prim_path_mask.empty()) {
+ pxr::UsdPrim prim = stage_->GetPrimAtPath(pxr::SdfPath(prim_path_mask));
+ if (prim.IsValid()) {
+ root = prim;
+ }
+ else {
+ std::cerr << "WARNING: Prim Path Mask " << prim_path_mask
+ << " does not specify a valid prim.\n";
+ }
+ }
+
+ stage_->SetInterpolationType(pxr::UsdInterpolationType::UsdInterpolationTypeHeld);
+ collect_readers(bmain, root);
+}
+
+void USDStageReader::clear_readers()
+{
+ for (USDPrimReader *reader : readers_) {
+ if (!reader) {
+ continue;
+ }
+
+ reader->decref();
+
+ if (reader->refcount() == 0) {
+ delete reader;
+ }
+ }
+
+ readers_.clear();
+}
+
+} // Namespace blender::io::usd