#99807: Add support for exporting to USDZ - ultimate fixes #105185

Merged
Bastien Montagne merged 12 commits from mont29/blender:usdz_export_fixes into main 2023-02-26 16:37:02 +01:00
4 changed files with 106 additions and 2 deletions
Showing only changes of commit 96aa7ab367 - Show all commits

View File

@ -142,6 +142,18 @@ double BLI_dir_free_space(const char *dir) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(
*/
char *BLI_current_working_dir(char *dir, size_t maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
eFileAttributes BLI_file_attributes(const char *path);
/**
* Changes the current working directory to the provided path.
*
* Usage of this function is strongly discouraged as it is not thread safe. It will likely cause
* issues if there is an operation on another thread that does not expect the current working
* directory to change. This has been added to support USDZ export, which has a problematic
* "feature" described in this task https://developer.blender.org/D15623. It will be removed if it
* is possible to resolve that issue upstream in the USD library.
*
* \return true on success, false otherwise.
*/
bool BLI_change_working_dir(const char *dir);
/** \} */
@ -313,6 +325,8 @@ void BLI_file_free_lines(struct LinkNode *lines);
* Giving a path without leading `~` is not an error.
*/
const char *BLI_expand_tilde(const char *path_with_tilde);
const char *BLI_apple_getcwd(const size_t max_length);
int BLI_apple_chdir(const char *dir);
#endif
/* This weirdo pops up in two places. */
#if !defined(WIN32)

View File

@ -54,8 +54,33 @@
#include "BLI_linklist.h"
#include "BLI_path_util.h"
#include "BLI_string.h"
#include "BLI_threads.h"
#include "BLI_utildefines.h"
bool BLI_change_working_dir(const char *dir)
{
BLI_assert(BLI_thread_is_main());
if (!BLI_is_dir(dir)) {
return false;
}
#if defined(WIN32)
wchar_t wdir[FILE_MAX];
if (conv_utf_8_to_16(dir, wdir, ARRAY_SIZE(wdir)) != 0) {
return false;
}
return _wchdir(wdir) == 0;
#elif defined(__APPLE__)
return BLI_apple_chdir(dir) == 0;
#else
int result = chdir(dir);
if (result == 0) {
BLI_setenv("PWD", dir);
}
return result == 0;
#endif
}
char *BLI_current_working_dir(char *dir, const size_t maxncpy)
{
#if defined(WIN32)
@ -66,6 +91,9 @@ char *BLI_current_working_dir(char *dir, const size_t maxncpy)
}
}
return NULL;
#elif defined(__APPLE__)
BLI_strncpy(dir, BLI_apple_getcwd(maxncpy), maxncpy);
return dir;
#else
const char *pwd = BLI_getenv("PWD");
if (pwd) {

View File

@ -185,3 +185,27 @@ const char *BLI_expand_tilde(const char *path_with_tilde)
}
return path_expanded;
}
const char *BLI_apple_getcwd(const size_t max_length) {
static char path_expanded[PATH_MAX];
@autoreleasepool {
NSString *path = [[NSFileManager defaultManager] currentDirectoryPath];
const size_t length = max_length > PATH_MAX ? PATH_MAX : max_length;
[path getCString:path_expanded maxLength: length encoding:NSUTF8StringEncoding];
return path_expanded;
}
}
int BLI_apple_chdir(const char* dir) {
@autoreleasepool {
NSString* path = [[NSString alloc] initWithUTF8String: dir];
if ([[NSFileManager defaultManager] changeCurrentDirectoryPath: path] == YES) {
return 0;
}
else {
return -1;
}
}
}

View File

@ -1,9 +1,12 @@
/* SPDX-License-Identifier: Apache-2.0 */
#include "BLI_fileops.hh"
#include "testing/testing.h"
#include "BLI_fileops.hh"
#include "BLI_path_util.h"
#include "BLI_string.h"
#include "BLI_threads.h"
namespace blender::tests {
TEST(fileops, fstream_open_string_filename)
@ -37,4 +40,39 @@ TEST(fileops, fstream_open_charptr_filename)
/* Reading the file not tested here. That's deferred to `std::fstream` anyway. */
}
TEST(fileops, change_working_directory)
{
BLI_threadapi_init();
char original_wd[FILE_MAX];
BLI_current_working_dir(original_wd, FILE_MAX);
const std::string test_temp_dir = blender::tests::flags_test_asset_dir() + "/" +
"fileops_test_temp_новый";
if (BLI_exists(test_temp_dir.c_str())) {
BLI_delete(test_temp_dir.c_str(), true, false);
}
bool result = BLI_change_working_dir(test_temp_dir.c_str());
ASSERT_FALSE(result);
BLI_dir_create_recursive(test_temp_dir.c_str());
result = BLI_change_working_dir(test_temp_dir.c_str());
ASSERT_TRUE(result);
char cwd[FILE_MAX];
BLI_current_working_dir(cwd, FILE_MAX);
ASSERT_TRUE(BLI_path_cmp_normalized(cwd, test_temp_dir.c_str()) == 0);
result = BLI_change_working_dir(original_wd);
ASSERT_TRUE(result);
BLI_delete(test_temp_dir.c_str(), true, false);
BLI_threadapi_exit();
}
} // namespace blender::tests