[Bf-blender-cvs] [4d9b898e0ec] tmp-dynamic-usd: USD IO: invoke resolver to write USDZ.

Michael Kowalski noreply at git.blender.org
Tue Nov 29 01:16:47 CET 2022


Commit: 4d9b898e0ecf7303d3799a7703b203dc45bd9443
Author: Michael Kowalski
Date:   Fri Nov 11 14:35:03 2022 -0500
Branches: tmp-dynamic-usd
https://developer.blender.org/rB4d9b898e0ecf7303d3799a7703b203dc45bd9443

USD IO: invoke resolver to write USDZ.

Added new copy_usd_asset() function to invoke the USD
asset resolver to copy a file.  Updated the USDZ export
logic to use this function when copying the temporary
USDZ file to its final destination.

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

M	source/blender/io/usd/CMakeLists.txt
A	source/blender/io/usd/intern/usd_asset_utils.cc
A	source/blender/io/usd/intern/usd_asset_utils.h
M	source/blender/io/usd/intern/usd_capi_export.cc

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

diff --git a/source/blender/io/usd/CMakeLists.txt b/source/blender/io/usd/CMakeLists.txt
index 1cbec8c471f..7a7539089df 100644
--- a/source/blender/io/usd/CMakeLists.txt
+++ b/source/blender/io/usd/CMakeLists.txt
@@ -65,6 +65,7 @@ set(INC_SYS
 )
 
 set(SRC
+  intern/usd_asset_utils.cc
   intern/usd_capi_export.cc
   intern/usd_capi_import.cc
   intern/usd_common.cc
@@ -107,6 +108,7 @@ set(SRC
 
   usd.h
 
+  intern/usd_asset_utils.h
   intern/usd_common.h
   intern/usd_exporter_context.h
   intern/usd_hierarchy_iterator.h
diff --git a/source/blender/io/usd/intern/usd_asset_utils.cc b/source/blender/io/usd/intern/usd_asset_utils.cc
new file mode 100644
index 00000000000..a01e8ae953f
--- /dev/null
+++ b/source/blender/io/usd/intern/usd_asset_utils.cc
@@ -0,0 +1,114 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later
+ * Copyright 2022 NVIDIA Corportation. All rights reserved. */
+
+#include "usd_asset_utils.h"
+
+#include <pxr/usd/ar/asset.h>
+#include <pxr/usd/ar/resolver.h>
+#include <pxr/usd/ar/writableAsset.h>
+
+#include "WM_api.h"
+#include "WM_types.h"
+
+#include <iostream>
+
+namespace blender::io::usd {
+
+bool copy_usd_asset(const char *src, const char *dst, bool overwrite)
+{
+  if (!(src && dst)) {
+    return false;
+  }
+
+  pxr::ArResolver &ar = pxr::ArGetResolver();
+
+  if (!overwrite) {
+    if (!ar.Resolve(dst).IsEmpty()) {
+      /* The asset exists, so this is a no-op. */
+      WM_reportf(RPT_INFO, "copy_usd_asset(): Will not overwrite existing asset %s", dst);
+      return true;
+    }
+  }
+
+  pxr::ArResolvedPath src_path = ar.Resolve(src);
+
+  if (src_path.IsEmpty()) {
+    WM_reportf(RPT_ERROR, "copy_usd_asset(): Can't resolve path %s.", src);
+    return false;
+  }
+
+  pxr::ArResolvedPath dst_path = ar.ResolveForNewAsset(dst);
+
+  if (dst_path.IsEmpty()) {
+    WM_reportf(RPT_ERROR, "copy_usd_asset(): Can't resolve path %s for writing.", dst);
+    return false;
+  }
+
+  std::string why_not;
+  if (!ar.CanWriteAssetToPath(dst_path, &why_not)) {
+    WM_reportf(RPT_ERROR,
+               "copy_usd_asset(): Can't write to asset %s.  %s.",
+               dst_path.GetPathString().c_str(),
+               why_not.c_str());
+    return false;
+  }
+
+  std::shared_ptr<pxr::ArAsset> src_asset = ar.OpenAsset(src_path);
+  if (!src_asset) {
+    WM_reportf(
+        RPT_ERROR, "copy_usd_asset(): Can't open source asset %s.", src_path.GetPathString().c_str());
+    return false;
+  }
+
+  const size_t size = src_asset->GetSize();
+
+  if (size == 0) {
+    WM_reportf(RPT_WARNING,
+               "copy_usd_asset(): Will not copy zero size source asset %s.",
+               src_path.GetPathString().c_str());
+    return false;
+  }
+
+  std::shared_ptr<const char> buf = src_asset->GetBuffer();
+
+  if (!buf) {
+    WM_reportf(RPT_ERROR,
+               "copy_usd_asset(): Null buffer for source asset %s.",
+               src_path.GetPathString().c_str());
+    return false;
+  }
+
+  std::shared_ptr<pxr::ArWritableAsset> dst_asset = ar.OpenAssetForWrite(
+      dst_path, pxr::ArResolver::WriteMode::Replace);
+  if (!dst_asset) {
+    WM_reportf(RPT_ERROR,
+               "copy_usd_asset(): Can't open destination asset %s for writing.",
+               src_path.GetPathString().c_str());
+    return false;
+  }
+
+  size_t bytes_written = dst_asset->Write(src_asset->GetBuffer().get(), src_asset->GetSize(), 0);
+
+  if (bytes_written == 0) {
+    WM_reportf(RPT_ERROR,
+               "copy_usd_asset(): Error writing to destination asset %s.",
+               dst_path.GetPathString().c_str());
+  }
+  else {
+    WM_reportf(RPT_INFO,
+               "copy_usd_asset(): Copied %s to %s.",
+               src_path.GetPathString().c_str(), dst_path.GetPathString().c_str());
+  }
+
+  if (!dst_asset->Close()) {
+    WM_reportf(RPT_ERROR,
+               "copy_usd_asset(): Couldn't close destination asset %s.",
+               dst_path.GetPathString().c_str());
+    return false;
+  }
+
+  return bytes_written > 0;
+}
+
+
+}  // namespace blender::io::usd
diff --git a/source/blender/io/usd/intern/usd_asset_utils.h b/source/blender/io/usd/intern/usd_asset_utils.h
new file mode 100644
index 00000000000..1a4fe83c59d
--- /dev/null
+++ b/source/blender/io/usd/intern/usd_asset_utils.h
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later
+ * Copyright 2021  NVIDIA Corporation. All rights reserved. */
+#pragma once
+
+namespace blender::io::usd {
+
+  /* Invoke the the USD asset resolver to copy assets. */
+  bool copy_usd_asset(const char *src, const char *dst, bool overwrite);
+
+}  // namespace blender::io::usd
diff --git a/source/blender/io/usd/intern/usd_capi_export.cc b/source/blender/io/usd/intern/usd_capi_export.cc
index 72c20f28b62..20030a63989 100644
--- a/source/blender/io/usd/intern/usd_capi_export.cc
+++ b/source/blender/io/usd/intern/usd_capi_export.cc
@@ -2,6 +2,7 @@
  * Copyright 2019 Blender Foundation. All rights reserved. */
 
 #include "usd.h"
+#include "usd_asset_utils.h"
 #include "usd_common.h"
 #include "usd_hierarchy_iterator.h"
 #include "usd_light_convert.h"
@@ -309,10 +310,9 @@ static bool perform_usdz_conversion(const ExportJobData *data)
       return false;
     }
   }
-  result = BLI_rename(usdz_temp_dirfile, data->usdz_filepath);
-  if (result != 0) {
+  if (!copy_usd_asset(usdz_temp_dirfile, data->usdz_filepath, true /* overwrite */)) {
     WM_reportf(RPT_ERROR,
-               "USD Export: Couldn't move new usdz file from temporary location %s to %s",
+               "USD Export: Couldn't copy new usdz file from temporary location %s to %s",
                usdz_temp_dirfile,
                data->usdz_filepath);
     return false;



More information about the Bf-blender-cvs mailing list