From 5e2d139ee30d88b474d3e7d65d5ff37d0be086b6 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Thu, 8 Sep 2022 20:27:03 +1000 Subject: 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. --- source/creator/creator.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'source/creator') diff --git a/source/creator/creator.c b/source/creator/creator.c index e7a803d383f..e7e9eeed79a 100644 --- a/source/creator/creator.c +++ b/source/creator/creator.c @@ -233,6 +233,12 @@ void gmp_blender_init_allocator() /** \name Main Function * \{ */ +/* When building as a Python module, don't use special argument handling + * so the module loading logic can control the `argv` & `argc`. */ +#if defined(WIN32) && !defined(WITH_PYTHON_MODULE) +# define USE_WIN32_UNICODE_ARGS +#endif + /** * Blender's main function responsibilities are: * - setup subsystems. @@ -241,7 +247,7 @@ void gmp_blender_init_allocator() * or exit immediately when running in background-mode. */ int main(int argc, -#ifdef WIN32 +#ifdef USE_WIN32_UNICODE_ARGS const char **UNUSED(argv_c) #else const char **argv @@ -254,7 +260,7 @@ int main(int argc, bArgs *ba; #endif -#ifdef WIN32 +#ifdef USE_WIN32_UNICODE_ARGS char **argv; int argv_num; #endif @@ -284,6 +290,7 @@ int main(int argc, /* NOTE: cannot use `guardedalloc` allocation here, as it's not yet initialized * (it depends on the arguments passed in, which is what we're getting here!) */ +# ifdef USE_WIN32_UNICODE_ARGS { wchar_t **argv_16 = CommandLineToArgvW(GetCommandLineW(), &argc); argv = malloc(argc * sizeof(char *)); @@ -296,7 +303,8 @@ int main(int argc, app_init_data.argv = argv; app_init_data.argv_num = argv_num; } -#endif /* WIN32 */ +# endif /* USE_WIN32_UNICODE_ARGS */ +#endif /* WIN32 */ /* NOTE: Special exception for guarded allocator type switch: * we need to perform switch from lock-free to fully @@ -388,7 +396,17 @@ int main(int argc, #endif /* Initialize path to executable. */ - BKE_appdir_program_path_init(argv[0]); + { +#ifdef WITH_PYTHON_MODULE + /* NOTE(@campbellbarton): Always use `argv[0]` as is, when building as a Python module. + * Otherwise other methods of detecting the binary that override this argument + * which must point to the Python module for data-files to be detected. */ + const bool strict = true; +#else + const bool strict = false; +#endif + BKE_appdir_program_path_init(argv[0], strict); + } BLI_threadapi_init(); -- cgit v1.2.3