Fix Blender as a Python module for WIN32

BKE_appdir_program_path_init would override the module path extracted
from the Python module, replacing it with the Python executable.

This caused the data files not to be found and the module not to load.
This commit is contained in:
2022-09-08 20:27:03 +10:00
parent 82fc52ffc8
commit 5e2d139ee3
3 changed files with 40 additions and 13 deletions

View File

@@ -105,8 +105,11 @@ void BKE_appdir_app_templates(struct ListBase *templates);
/**
* Initialize path to program executable.
*
* \param strict: When true, use `argv0` unmodified (besides making absolute & normalizing).
* Otherwise other methods may be used to find the program path, including searching `$PATH`.
*/
void BKE_appdir_program_path_init(const char *argv0);
void BKE_appdir_program_path_init(const char *argv0, bool strict);
/**
* Path to executable

View File

@@ -794,11 +794,11 @@ const char *BKE_appdir_folder_id_version(const int folder_id,
* (must be #FILE_MAX minimum)
* \param name: The name of the executable (usually `argv[0]`) to be checked
*/
static void where_am_i(char *fullname, const size_t maxlen, const char *name)
static void where_am_i(char *fullname, const size_t maxlen, const char *name, const bool strict)
{
#ifdef WITH_BINRELOC
/* Linux uses `binreloc` since `argv[0]` is not reliable, call `br_init(NULL)` first. */
{
if (!strict) {
const char *path = NULL;
path = br_find_exe(NULL);
if (path) {
@@ -810,7 +810,7 @@ static void where_am_i(char *fullname, const size_t maxlen, const char *name)
#endif
#ifdef _WIN32
{
if (!strict) {
wchar_t *fullname_16 = MEM_mallocN(maxlen * sizeof(wchar_t), "ProgramPath");
if (GetModuleFileNameW(0, fullname_16, maxlen)) {
conv_utf_16_to_8(fullname_16, fullname, maxlen);
@@ -834,18 +834,24 @@ static void where_am_i(char *fullname, const size_t maxlen, const char *name)
if (name[0] == '.') {
BLI_path_abs_from_cwd(fullname, maxlen);
#ifdef _WIN32
BLI_path_program_extensions_add_win32(fullname, maxlen);
if (!strict) {
BLI_path_program_extensions_add_win32(fullname, maxlen);
}
#endif
}
else if (BLI_path_slash_rfind(name)) {
/* Full path. */
BLI_strncpy(fullname, name, maxlen);
#ifdef _WIN32
BLI_path_program_extensions_add_win32(fullname, maxlen);
if (!strict) {
BLI_path_program_extensions_add_win32(fullname, maxlen);
}
#endif
}
else {
BLI_path_program_search(fullname, maxlen, name);
if (!strict) {
BLI_path_program_search(fullname, maxlen, name);
}
}
/* Remove "/./" and "/../" so string comparisons can be used on the path. */
BLI_path_normalize(NULL, fullname);
@@ -858,9 +864,9 @@ static void where_am_i(char *fullname, const size_t maxlen, const char *name)
}
}
void BKE_appdir_program_path_init(const char *argv0)
void BKE_appdir_program_path_init(const char *argv0, const bool strict)
{
where_am_i(g_app.program_filepath, sizeof(g_app.program_filepath), argv0);
where_am_i(g_app.program_filepath, sizeof(g_app.program_filepath), argv0, strict);
BLI_split_dir_part(g_app.program_filepath, g_app.program_dirname, sizeof(g_app.program_dirname));
}