[Bf-blender-cvs] [8ef0925c83a] universal-scene-description: USD export: avoid creating redundant root prim.

Michael Kowalski noreply at git.blender.org
Tue Dec 7 18:53:51 CET 2021


Commit: 8ef0925c83a8cfc422cd953a47ef0daeda956498
Author: Michael Kowalski
Date:   Tue Dec 7 12:25:07 2021 -0500
Branches: universal-scene-description
https://developer.blender.org/rB8ef0925c83a8cfc422cd953a47ef0daeda956498

USD export: avoid creating redundant root prim.

If a root prim path is set in the params, now checking
if a root object matching the root path name already
exists in the Blender scene.  Clearing the root prim
path in the params and printing a warning if it does.
This is to avoid prepending the root prim path redundantly.

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

M	source/blender/io/usd/intern/usd_capi_export.cc

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

diff --git a/source/blender/io/usd/intern/usd_capi_export.cc b/source/blender/io/usd/intern/usd_capi_export.cc
index 738c6c0931a..a7c1f57095c 100644
--- a/source/blender/io/usd/intern/usd_capi_export.cc
+++ b/source/blender/io/usd/intern/usd_capi_export.cc
@@ -107,6 +107,58 @@ static bool validate_params(const USDExportParams &params)
   return valid;
 }
 
+/* If a root prim path is set in the params, check if a
+ * root object matching the root path name already exists.
+ * If it does, clear the root prim path in the params.
+ * This is to avoid prepending the root prim path
+ * redundantly.
+ * TODO(makowalski): ideally, this functionality belongs
+ * in the USD hierarchy iterator, so that we don't iterate
+ * over the scene graph separately here. */
+static void validate_unique_root_prim_path(USDExportParams &params, Depsgraph *depsgraph)
+{
+  if (!depsgraph || strlen(params.root_prim_path) == 0) {
+    return;
+  }
+
+  pxr::SdfPath path(params.root_prim_path);
+
+  if (path.IsEmpty()) {
+    return;
+  }
+
+  pxr::SdfPath parent = path.GetParentPath();
+
+  while (!parent.IsEmpty() && !parent.IsAbsoluteRootPath()) {
+    path = parent;
+    parent = path.GetParentPath();
+  }
+
+  Object *match = nullptr;
+  std::string root_name = path.GetName();
+
+  DEG_OBJECT_ITER_BEGIN(depsgraph,
+    object,
+    DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY |
+    DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET) {
+
+    if (!match && !object->parent) {
+      /* We only care about root objects. */
+
+      if (pxr::TfMakeValidIdentifier(object->id.name + 2) == root_name) {
+        match = object;
+      }
+    }
+  }
+  DEG_OBJECT_ITER_END;
+
+  if (match) {
+    WM_reportf(
+      RPT_WARNING, "USD Export: the root prim will not be added because a root object named '%s' already exists", root_name.c_str());
+    params.root_prim_path[0] = '\0';
+  }
+}
+
 /* Create root prim if defined. */
 static void ensure_root_prim(pxr::UsdStageRefPtr stage, const USDExportParams &params)
 {
@@ -184,6 +236,8 @@ static void export_startjob(void *customdata,
   }
   BKE_scene_graph_update_tagged(data->depsgraph, data->bmain);
 
+  validate_unique_root_prim_path(data->params, data->depsgraph);
+
   *progress = 0.0f;
   *do_update = true;



More information about the Bf-blender-cvs mailing list