Add bpy.app.binary_path_python

Access to the python binary distributed with Blender,
fallback to system python executable (matching Blender's version).
This commit is contained in:
Campbell Barton 2015-05-06 11:07:15 +10:00
parent c246e0c3b6
commit bc2f77e1da
Notes: blender-bot 2023-02-14 09:33:09 +01:00
Referenced by issue #44957, Buildbot Builds Crash (Gooseberry)
Referenced by issue #44631, Custom Normal Data Transfer crash.
Referenced by issue #44616, Cycles : Texture Loading : 42k by 21k textures crash Cycles
Referenced by issue #44621, Convert Hair Particules (Whole Group) doesn't respect duplicated Group Hierarchy
Referenced by issue #43486, Include Python executable with Blender.
3 changed files with 74 additions and 1 deletions

View File

@ -38,6 +38,11 @@ void BKE_appdir_program_path_init(const char *argv0);
const char *BKE_appdir_program_path(void);
const char *BKE_appdir_program_dir(void);
/* find python executable */
bool BKE_appdir_program_python_search(
char *fullpath, const size_t fullpath_len,
const int version_major, const int version_minor);
/* Initialize path to temporary directory. */
void BKE_tempdir_init(char *userdir);
void BKE_tempdir_system_init(char *dir);

View File

@ -599,6 +599,45 @@ const char *BKE_appdir_program_dir(void)
return bprogdir;
}
bool BKE_appdir_program_python_search(
char *fullpath, const size_t fullpath_len,
const int version_major, const int version_minor)
{
const char *basename = "python";
bool is_found = false;
{
const char *python_bin_dir = BKE_appdir_folder_id(BLENDER_SYSTEM_PYTHON, "bin");
if (python_bin_dir) {
BLI_join_dirfile(fullpath, fullpath_len, python_bin_dir, basename);
if (
#ifdef _WIN32
BLI_path_program_extensions_add_win32(fullpath, fullpath_len)
#else
BLI_exists(fullpath)
#endif
)
{
is_found = true;
}
}
}
if (is_found == false) {
char python_ver[16];
BLI_snprintf(python_ver, sizeof(python_ver), "%s%d.%d", basename, version_major, version_minor);
if (BLI_path_program_search(fullpath, fullpath_len, python_ver)) {
is_found = true;
}
}
if (is_found == false) {
*fullpath = '\0';
}
return is_found;
}
/**
* Gets the temp directory when blender first runs.
* If the default path is not found, use try $TEMP

View File

@ -220,6 +220,33 @@ static int bpy_app_debug_set(PyObject *UNUSED(self), PyObject *value, void *clos
return 0;
}
PyDoc_STRVAR(bpy_app_binary_path_python_doc,
"String, the path to the python executable (read-only)"
);
static PyObject *bpy_app_binary_path_python_get(PyObject *UNUSED(self), void *UNUSED(closure))
{
/* refcount is held in BlenderAppType.tp_dict */
static PyObject *ret = NULL;
if (ret == NULL) {
/* only run once */
char fullpath[1024];
BKE_appdir_program_python_search(
fullpath, sizeof(fullpath),
PY_MAJOR_VERSION, PY_MINOR_VERSION);
ret = PyC_UnicodeFromByte(fullpath);
PyDict_SetItemString(BlenderAppType.tp_dict, "binary_path_python", ret);
}
else {
Py_INCREF(ret);
}
return ret;
}
PyDoc_STRVAR(bpy_app_debug_value_doc,
"Int, number which can be set to non-zero values for testing purposes"
);
@ -287,7 +314,9 @@ static PyGetSetDef bpy_app_getsets[] = {
{(char *)"debug_wm", bpy_app_debug_get, bpy_app_debug_set, (char *)bpy_app_debug_doc, (void *)G_DEBUG_WM},
{(char *)"debug_depsgraph", bpy_app_debug_get, bpy_app_debug_set, (char *)bpy_app_debug_doc, (void *)G_DEBUG_DEPSGRAPH},
{(char *)"debug_simdata", bpy_app_debug_get, bpy_app_debug_set, (char *)bpy_app_debug_doc, (void *)G_DEBUG_SIMDATA},
{(char *)"debug_gpumem", bpy_app_debug_get, bpy_app_debug_set, (char *)bpy_app_debug_doc, (void *)G_DEBUG_GPU_MEM},
{(char *)"debug_gpumem", bpy_app_debug_get, bpy_app_debug_set, (char *)bpy_app_debug_doc, (void *)G_DEBUG_GPU_MEM},
{(char *)"binary_path_python", bpy_app_binary_path_python_get, NULL, (char *)bpy_app_binary_path_python_doc, NULL},
{(char *)"debug_value", bpy_app_debug_value_get, bpy_app_debug_value_set, (char *)bpy_app_debug_value_doc, NULL},
{(char *)"tempdir", bpy_app_tempdir_get, NULL, (char *)bpy_app_tempdir_doc, NULL},