8#include <pxr/usd/ar/asset.h>
9#include <pxr/usd/ar/packageUtils.h>
10#include <pxr/usd/ar/resolver.h>
11#include <pxr/usd/ar/writableAsset.h>
12#include <pxr/usd/usd/common.h>
13#include <pxr/usd/usd/stage.h>
49 for (
const std::string_view pattern : patterns) {
50 const std::string::size_type pos = path.find(pattern);
51 if (pos != std::string::npos) {
52 return {path.substr(0, pos), path.substr(pos + pattern.size())};
65 if (pxr::ArIsPackageRelativePath(src_path)) {
66 std::pair<std::string, std::string>
split = pxr::ArSplitPackageRelativePathInner(src_path);
67 if (
split.second.empty()) {
70 "%s: Couldn't determine package-relative file name from path %s",
86 const char *dest_dir_path,
93 BLI_path_join(dest_file_path,
sizeof(dest_file_path), dest_dir_path, base_name.c_str());
97 return dest_file_path;
100 if (!
copy_asset(src_path, dest_file_path, name_collision_mode, reports)) {
103 "%s: Couldn't copy file %s to %s",
110 return dest_file_path;
114 const char *dest_dir_path,
120 if (splitPath.first.empty() || splitPath.second.empty()) {
122 reports,
RPT_ERROR,
"%s: Couldn't split UDIM pattern %s", __func__, src_path.c_str());
134 const std::string src_udim = splitPath.first + std::to_string(i) + splitPath.second;
142 BLI_path_join(ret_udim_path,
sizeof(ret_udim_path), dest_dir_path, src_file_name.c_str());
148 if (splitPath.first.empty() || splitPath.second.empty()) {
150 return ret_udim_path;
153 return splitPath.first +
UDIM_PATTERN + splitPath.second;
157 const std::string &dst,
161 const pxr::ArResolver &ar = pxr::ArGetResolver();
164 if (!ar.Resolve(dst).IsEmpty()) {
167 reports,
RPT_INFO,
"%s: Will not overwrite existing asset %s", __func__, dst.c_str());
172 pxr::ArResolvedPath src_path = ar.Resolve(src);
174 if (src_path.IsEmpty()) {
179 pxr::ArResolvedPath dst_path = ar.ResolveForNewAsset(dst);
181 if (dst_path.IsEmpty()) {
183 reports,
RPT_ERROR,
"%s: Cannot resolve path %s for writing", __func__, dst.c_str());
187 if (src_path == dst_path) {
190 "%s: Cannot copy %s. The source and destination paths are the same",
192 src_path.GetPathString().c_str());
197 if (!ar.CanWriteAssetToPath(dst_path, &why_not)) {
200 "%s: Cannot write to asset %s: %s",
202 dst_path.GetPathString().c_str(),
207 std::shared_ptr<pxr::ArAsset> src_asset = ar.OpenAsset(src_path);
211 "%s: Cannot open source asset %s",
213 src_path.GetPathString().c_str());
217 const size_t size = src_asset->GetSize();
222 "%s: Will not copy zero size source asset %s",
224 src_path.GetPathString().c_str());
228 std::shared_ptr<const char> buf = src_asset->GetBuffer();
233 "%s: Null buffer for source asset %s",
235 src_path.GetPathString().c_str());
239 std::shared_ptr<pxr::ArWritableAsset> dst_asset = ar.OpenAssetForWrite(
240 dst_path, pxr::ArResolver::WriteMode::Replace);
244 "%s: Cannot open destination asset %s for writing",
246 src_path.GetPathString().c_str());
250 size_t bytes_written = dst_asset->Write(src_asset->GetBuffer().get(), src_asset->GetSize(), 0);
252 if (bytes_written == 0) {
255 "%s: Error writing to destination asset %s",
257 dst_path.GetPathString().c_str());
260 if (!dst_asset->Close()) {
263 "%s: Couldn't close destination asset %s",
265 dst_path.GetPathString().c_str());
269 return bytes_written > 0;
274 return !pxr::ArGetResolver().Resolve(path).IsEmpty();
278 const char *import_dir,
282 if (import_dir[0] ==
'\0') {
285 "%s: Texture import directory path empty, couldn't import %s",
292 STRNCPY(dest_dir_path, import_dir);
296 if (basepath[0] ==
'\0') {
299 "%s: import directory is relative "
300 "but the blend file path is empty. "
301 "Please save the blend file before importing the USD "
302 "or provide an absolute import directory path. "
309 STRNCPY(path_temp, dest_dir_path);
311 STRNCPY(dest_dir_path, path_temp);
319 "%s: Couldn't create texture import directory %s",
354 pxr::SdfLayerHandle
layer = stage->GetRootLayer();
356 if (
layer->IsAnonymous()) {
358 "%s: Cannot generate a textures directory path for anonymous stage",
363 const pxr::ArResolvedPath &stage_path =
layer->GetResolvedPath();
365 if (stage_path.empty()) {
370 const pxr::ArResolver &ar = pxr::ArGetResolver();
373 std::string textures_dir = ar.CreateIdentifierForNewAsset(
"./textures", stage_path);
394 if (pxr::ArIsPackageRelativePath(path)) {
405bool paths_equal(
const std::string &path1,
const std::string &path2)
408 "Paths arguments must be absolute");
410 const pxr::ArResolver &ar = pxr::ArGetResolver();
411 return ar.ResolveForNewAsset(path1) == ar.ResolveForNewAsset(path2);
416 static bool inited =
false;
435 const pxr::ArResolver &ar = pxr::ArGetResolver();
436 pxr::ArResolvedPath resolved_path = ar.ResolveForNewAsset(path);
438 if (resolved_path.IsEmpty()) {
444 if (!ar.CanWriteAssetToPath(resolved_path, &why_not)) {
447 "Cannot write to asset %s: %s",
448 resolved_path.GetPathString().c_str(),
453 std::shared_ptr<pxr::ArWritableAsset> dst_asset = ar.OpenAssetForWrite(
454 resolved_path, pxr::ArResolver::WriteMode::Replace);
458 "Cannot open destination asset %s for writing",
459 resolved_path.GetPathString().c_str());
463 size_t bytes_written = dst_asset->Write(
data, size, 0);
465 if (bytes_written == 0) {
468 "Error writing to destination asset %s",
469 resolved_path.GetPathString().c_str());
472 if (!dst_asset->Close()) {
475 "Couldn't close destination asset %s",
476 resolved_path.GetPathString().c_str());
480 return bytes_written > 0;
485 if (!
id || path.empty()) {
489 if (pxr::ArIsPackageRelativePath(path)) {
501 const StringRef prop_name =
"usd_source_path";
508 val.
string.str = path.c_str();
510 val.
string.len = path.size() + 1;
529 const StringRef prop_name =
"usd_source_path";
535 return static_cast<const char *
>(prop->
data.
pointer);
540 if (path.empty() || anchor.empty()) {
544 if (path == anchor) {
552 if (pxr::ArIsPackageRelativePath(path)) {
561 const bool walk_up =
true;
579 const pxr::ArResolver &ar = pxr::ArGetResolver();
581 std::string resolved_path = ar.Resolve(path);
582 std::string resolved_anchor = ar.Resolve(anchor);
584 if (resolved_path.empty() || resolved_anchor.empty()) {
588 std::string prefix = pxr::TfStringGetCommonPrefix(path, anchor);
589 if (prefix.empty()) {
593 std::replace(prefix.begin(), prefix.end(),
'\\',
'/');
595 size_t last_slash_pos = prefix.find_last_of(
'/');
596 if (last_slash_pos == std::string::npos) {
606 resolved_path =
"/root" + resolved_path.substr(last_slash_pos);
607 resolved_anchor =
"/root" + resolved_anchor.substr(last_slash_pos);
612 if (anchor_parent_dir[0] ==
'\0') {
616 const bool walk_up =
true;
619 resolved_path.c_str(), anchor_parent_dir, walk_up, result_path,
sizeof(result_path)))
626 return std::string(result_path);
632 pxr::ArResolvedPath resolved_path = for_import ? pxr::ArGetResolver().Resolve(path) :
633 pxr::ArGetResolver().ResolveForNewAsset(path);
635 const std::string &path_str = resolved_path.GetPathString();
637 if (!path_str.empty()) {
643 "In %s: resolved path %s exceeds path buffer length.",
File and directory operations.
#define BLI_path_join(...)
#define BLI_assert_msg(a, msg)
void split(const std::string &s, const char delim, std::vector< std::string > &tokens)
static std::string copy_udim_asset_to_directory(const std::string &src_path, const char *dest_dir_path, TexNameCollisionMode name_collision_mode, ReportList *reports)
const char * temp_textures_dir()
bool should_import_asset(const std::string &path)
bool asset_exists(const std::string &path)
bool is_udim_path(const std::string &path)
std::string get_relative_path(const std::string &path, const std::string &anchor)
static std::string get_asset_base_name(const std::string &src_path, ReportList *reports)
bool write_to_path(const void *data, size_t size, const std::string &path, ReportList *reports)
bool paths_equal(const std::string &path1, const std::string &path2)
static std::string copy_asset_to_directory(const std::string &src_path, const char *dest_dir_path, TexNameCollisionMode name_collision_mode, ReportList *reports)
constexpr int UDIM_START_TILE
constexpr char UDIM_PATTERN[]
void USD_path_abs(char *path, const char *basepath, bool for_import)
std::string get_export_textures_dir(const pxr::UsdStageRefPtr stage)
std::string import_asset(const std::string &src, const char *import_dir, TexNameCollisionMode name_collision_mode, ReportList *reports)
static std::pair< std::string, std::string > split_udim_pattern(const std::string &path)
constexpr int UDIM_END_TILE
std::string get_usd_source_path(ID *id)
void ensure_usd_source_path_prop(const std::string &path, ID *id)
constexpr char UDIM_PATTERN2[]
static bool parent_dir_exists_on_file_system(const std::string &path)
bool copy_asset(const std::string &src, const std::string &dst, TexNameCollisionMode name_collision_mode, ReportList *reports)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void WM_global_reportf(eReportType type, const char *format,...)
char * STRNCPY(char(&dst)[N], const char *src)
void BLI_path_slash_forward_from_native(char *path) ATTR_NONNULL(1)
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
bool BLI_dir_create_recursive(const char *dirname) ATTR_NONNULL()
bool IDP_AddToGroup(IDProperty *group, IDProperty *prop) ATTR_NONNULL()
IDProperty * IDP_GetPropertyFromGroup(const IDProperty *prop, StringRef name) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
const char * BKE_main_blendfile_path_from_global()
const char * BKE_tempdir_session() ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL
bool BLI_path_relative_to(const char *path, const char *base_dir, bool walk_up, char *r_path_relative, size_t r_path_relative_maxncpy) ATTR_NONNULL(1
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
void void BLI_path_split_dir_part(const char *filepath, char *dir, size_t dir_maxncpy) ATTR_NONNULL(1
BMesh const char void * data
int BLI_path_normalize(char *path) ATTR_NONNULL(1)
bool BLI_is_dir(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void void void BLI_path_split_file_part(const char *filepath, char *file, size_t file_maxncpy) ATTR_NONNULL(1
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
IDProperty * IDP_EnsureProperties(ID *id) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
IDProperty * IDP_New(char type, const IDPropertyTemplate *val, StringRef name, eIDPropertyFlag flags={}) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
bool BLI_is_file(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
struct blender::IDPropertyTemplate::@116303142222164257205355242364302075230051143265 string