2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2011-02-27 20:10:08 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup pythonintern
|
2011-11-05 08:21:12 +00:00
|
|
|
*
|
|
|
|
|
* This file deals with embedding the python interpreter within blender,
|
|
|
|
|
* starting and stopping python and exposing blender/python modules so they can
|
|
|
|
|
* be accesses from scripts.
|
2011-02-27 20:10:08 +00:00
|
|
|
*/
|
|
|
|
|
|
2010-10-27 06:05:22 +00:00
|
|
|
#include <Python.h>
|
2020-08-17 17:21:11 +10:00
|
|
|
#include <frameobject.h>
|
2025-09-19 06:48:07 +00:00
|
|
|
#include <optional>
|
2008-11-29 13:36:08 +00:00
|
|
|
|
2022-09-08 14:49:42 +10:00
|
|
|
#ifdef WITH_PYTHON_MODULE
|
|
|
|
|
# include "pylifecycle.h" /* For `Py_Version`. */
|
|
|
|
|
#endif
|
2025-06-20 04:19:35 +00:00
|
|
|
#include "../generic/python_compat.hh" /* IWYU pragma: keep. */
|
2022-09-08 14:49:42 +10:00
|
|
|
|
2018-04-02 11:05:15 +02:00
|
|
|
#include "CLG_log.h"
|
|
|
|
|
|
2024-09-26 21:13:39 +10:00
|
|
|
#include "BLI_path_utils.hh"
|
2025-09-19 06:48:07 +00:00
|
|
|
#include "BLI_string.h"
|
2025-07-27 02:47:45 +00:00
|
|
|
#include "BLI_string_utf8.h"
|
2013-01-07 05:26:12 +00:00
|
|
|
#include "BLI_threads.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BLI_utildefines.h"
|
2025-08-05 12:08:24 +10:00
|
|
|
#ifdef WITH_PYTHON_MODULE
|
|
|
|
|
# include "BLI_string.h"
|
|
|
|
|
#endif
|
2013-01-07 05:26:12 +00:00
|
|
|
|
2024-02-09 18:59:42 +01:00
|
|
|
#include "BLT_translation.hh"
|
2023-12-30 15:35:10 +01:00
|
|
|
|
2023-08-10 22:40:27 +02:00
|
|
|
#include "RNA_types.hh"
|
2011-03-02 04:51:43 +00:00
|
|
|
|
2024-09-24 12:23:25 +02:00
|
|
|
#include "bpy.hh"
|
2024-09-24 15:25:36 +02:00
|
|
|
#include "bpy_capi_utils.hh"
|
2024-09-24 12:23:25 +02:00
|
|
|
#include "bpy_intern_string.hh"
|
|
|
|
|
#include "bpy_path.hh"
|
|
|
|
|
#include "bpy_props.hh"
|
2024-09-24 11:30:38 +02:00
|
|
|
#include "bpy_rna.hh"
|
2009-02-28 13:27:45 +00:00
|
|
|
|
2024-09-24 12:05:13 +02:00
|
|
|
#include "bpy_app_translations.hh"
|
2015-04-01 09:34:01 +02:00
|
|
|
|
2009-03-04 13:26:33 +00:00
|
|
|
#include "DNA_text_types.h"
|
2009-04-11 02:18:24 +00:00
|
|
|
|
2024-01-21 19:42:13 +01:00
|
|
|
#include "BKE_appdir.hh"
|
2023-11-16 11:41:55 +01:00
|
|
|
#include "BKE_context.hh"
|
2024-02-10 18:25:14 +01:00
|
|
|
#include "BKE_global.hh" /* Only for script checking. */
|
2023-12-01 19:43:16 +01:00
|
|
|
#include "BKE_main.hh"
|
2018-11-07 15:37:31 +01:00
|
|
|
#include "BKE_text.h"
|
2009-04-11 02:18:24 +00:00
|
|
|
|
2019-04-16 13:57:03 +02:00
|
|
|
#ifdef WITH_CYCLES
|
|
|
|
|
# include "CCL_api.h"
|
|
|
|
|
#endif
|
2012-01-09 16:58:01 +00:00
|
|
|
|
2024-09-24 17:07:49 +02:00
|
|
|
#include "BPY_extern.hh"
|
|
|
|
|
#include "BPY_extern_python.hh"
|
|
|
|
|
#include "BPY_extern_run.hh"
|
2.5
More cleanup!
- removed old UI font completely, including from uiBeginBlock
- emboss hints for uiBlock only have three types now;
Regular, Pulldown, or "Nothing" (only icon/text)
- removed old font path from Userdef
- removed all old button theme hinting
- removed old "auto block" to merge buttons in groups
(was only in use for radiosity buttons)
And went over all warnings. One hooray for make giving clean output :)
Well, we need uniform definitions for warnings, so people at least fix
them... here's the real bad bugs I found:
- in mesh code, a call to editmesh mixed *em and *me
- in armature, ED_util.h was not included, so no warnings for wrong call
to ED_undo_push()
- The extern Py api .h was not included in the bpy_interface.c, showing
a several calls using different args.
Further just added the missing includes, and removed unused vars.
2009-04-14 15:59:52 +00:00
|
|
|
|
2024-09-24 15:25:36 +02:00
|
|
|
#include "../generic/py_capi_utils.hh"
|
2009-06-17 20:33:34 +00:00
|
|
|
|
2023-09-19 15:00:21 +10:00
|
|
|
/* `inittab` initialization functions. */
|
2024-09-24 16:27:18 +02:00
|
|
|
#include "../bmesh/bmesh_py_api.hh"
|
2024-09-24 15:25:36 +02:00
|
|
|
#include "../generic/bl_math_py_api.hh"
|
|
|
|
|
#include "../generic/blf_py_api.hh"
|
|
|
|
|
#include "../generic/idprop_py_api.hh"
|
|
|
|
|
#include "../generic/imbuf_py_api.hh"
|
2024-03-23 10:06:45 -04:00
|
|
|
#include "../gpu/gpu_py_api.hh"
|
2024-09-24 12:56:16 +02:00
|
|
|
#include "../mathutils/mathutils.hh"
|
2011-02-14 04:15:25 +00:00
|
|
|
|
2018-04-02 11:05:15 +02:00
|
|
|
/* Logging types to use anywhere in the Python modules. */
|
2021-12-10 21:40:53 +11:00
|
|
|
|
2020-11-02 19:33:32 +11:00
|
|
|
CLG_LOGREF_DECLARE_GLOBAL(BPY_LOG_INTERFACE, "bpy.interface");
|
2018-04-02 11:05:15 +02:00
|
|
|
CLG_LOGREF_DECLARE_GLOBAL(BPY_LOG_RNA, "bpy.rna");
|
2012-04-15 14:54:15 +00:00
|
|
|
|
2025-09-23 21:15:42 -04:00
|
|
|
extern CLG_LogRef *BKE_LOG_CONTEXT;
|
|
|
|
|
|
2023-09-19 15:00:21 +10:00
|
|
|
/* For internal use, when starting and ending Python scripts. */
|
2009-08-07 16:20:19 +00:00
|
|
|
|
2023-09-19 15:00:21 +10:00
|
|
|
/* In case a Python script triggers another Python call,
|
|
|
|
|
* stop #bpy_context_clear from invalidating. */
|
2011-12-26 12:26:11 +00:00
|
|
|
static int py_call_level = 0;
|
2009-08-09 13:20:12 +00:00
|
|
|
|
2020-01-16 21:11:05 +11:00
|
|
|
/* Set by command line arguments before Python starts. */
|
|
|
|
|
static bool py_use_system_env = false;
|
|
|
|
|
|
2023-09-19 15:00:21 +10:00
|
|
|
// #define TIME_PY_RUN /* Simple python tests. prints on exit. */
|
2009-08-09 13:20:12 +00:00
|
|
|
|
|
|
|
|
#ifdef TIME_PY_RUN
|
2024-01-19 14:32:28 +01:00
|
|
|
# include "BLI_time.h"
|
2012-03-26 20:41:54 +00:00
|
|
|
static int bpy_timer_count = 0;
|
2023-09-19 15:00:21 +10:00
|
|
|
/** Time since python starts. */
|
|
|
|
|
static double bpy_timer;
|
|
|
|
|
/** Time for each Python script run. */
|
|
|
|
|
static double bpy_timer_run;
|
|
|
|
|
/** Accumulate Python runs. */
|
|
|
|
|
static double bpy_timer_run_tot;
|
2009-08-09 13:20:12 +00:00
|
|
|
#endif
|
|
|
|
|
|
2012-05-08 22:07:06 +00:00
|
|
|
void BPY_context_update(bContext *C)
|
2011-08-20 13:29:42 +00:00
|
|
|
{
|
2023-09-19 15:00:21 +10:00
|
|
|
/* Don't do this from a non-main (e.g. render) thread, it can cause a race
|
|
|
|
|
* condition on `C->data.recursion`. Ideal solution would be to disable
|
|
|
|
|
* context entirely from non-main threads, but that's more complicated. */
|
2019-03-30 06:12:48 +11:00
|
|
|
if (!BLI_thread_is_main()) {
|
2012-04-13 16:03:52 +00:00
|
|
|
return;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-10-15 18:12:03 +11:00
|
|
|
BPY_context_set(C);
|
2023-09-19 15:00:21 +10:00
|
|
|
|
|
|
|
|
/* Can give really bad results if this isn't here. */
|
|
|
|
|
BPY_modules_update();
|
2011-08-20 13:29:42 +00:00
|
|
|
}
|
|
|
|
|
|
2009-08-07 16:20:19 +00:00
|
|
|
void bpy_context_set(bContext *C, PyGILState_STATE *gilstate)
|
|
|
|
|
{
|
|
|
|
|
py_call_level++;
|
|
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (gilstate) {
|
2011-12-26 12:26:11 +00:00
|
|
|
*gilstate = PyGILState_Ensure();
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2009-08-07 16:20:19 +00:00
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
if (py_call_level == 1) {
|
2012-05-08 22:07:06 +00:00
|
|
|
BPY_context_update(C);
|
2009-11-05 11:17:09 +00:00
|
|
|
|
2009-08-09 13:20:12 +00:00
|
|
|
#ifdef TIME_PY_RUN
|
2011-12-26 12:26:11 +00:00
|
|
|
if (bpy_timer_count == 0) {
|
2023-09-19 15:00:21 +10:00
|
|
|
/* Record time from the beginning. */
|
2024-02-15 13:15:56 +11:00
|
|
|
bpy_timer = BLI_time_now_seconds();
|
2011-12-26 12:26:11 +00:00
|
|
|
bpy_timer_run = bpy_timer_run_tot = 0.0;
|
2009-08-09 13:20:12 +00:00
|
|
|
}
|
2024-02-15 13:15:56 +11:00
|
|
|
bpy_timer_run = BLI_time_now_seconds();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2009-08-09 13:20:12 +00:00
|
|
|
bpy_timer_count++;
|
|
|
|
|
#endif
|
2009-08-07 16:20:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
void bpy_context_clear(bContext * /*C*/, const PyGILState_STATE *gilstate)
|
2009-08-07 16:20:19 +00:00
|
|
|
{
|
|
|
|
|
py_call_level--;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (gilstate) {
|
2009-08-07 16:20:19 +00:00
|
|
|
PyGILState_Release(*gilstate);
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-10-13 01:29:08 +00:00
|
|
|
if (py_call_level < 0) {
|
2009-08-07 16:20:19 +00:00
|
|
|
fprintf(stderr, "ERROR: Python context internal state bug. this should not happen!\n");
|
|
|
|
|
}
|
2011-12-26 12:26:11 +00:00
|
|
|
else if (py_call_level == 0) {
|
2023-09-19 15:00:21 +10:00
|
|
|
/* NOTE: Unfortunately calling classes currently won't store the context.
|
2025-05-22 13:45:02 +10:00
|
|
|
* Can't set nullptr because of this - but this is very unreliable still. */
|
2011-11-26 15:18:30 +00:00
|
|
|
#if 0
|
2023-07-21 02:18:59 +02:00
|
|
|
BPY_context_set(nullptr);
|
2011-11-26 15:18:30 +00:00
|
|
|
#endif
|
2009-08-09 13:20:12 +00:00
|
|
|
|
|
|
|
|
#ifdef TIME_PY_RUN
|
2024-02-15 13:15:56 +11:00
|
|
|
bpy_timer_run_tot += BLI_time_now_seconds() - bpy_timer_run;
|
2009-08-09 13:20:12 +00:00
|
|
|
bpy_timer_count++;
|
|
|
|
|
#endif
|
2009-08-07 16:20:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-20 11:57:28 +10:00
|
|
|
static void bpy_context_end(bContext *C)
|
|
|
|
|
{
|
2023-07-21 02:18:59 +02:00
|
|
|
if (UNLIKELY(C == nullptr)) {
|
2021-04-20 11:57:28 +10:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
CTX_wm_operator_poll_msg_clear(C);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-17 18:23:12 +10:00
|
|
|
void BPY_context_dict_clear_members_array(void **dict_p,
|
|
|
|
|
void *dict_orig,
|
|
|
|
|
const char *context_members[],
|
|
|
|
|
uint context_members_len)
|
|
|
|
|
{
|
|
|
|
|
PyGILState_STATE gilstate;
|
|
|
|
|
const bool use_gil = !PyC_IsInterpreterActive();
|
|
|
|
|
if (use_gil) {
|
|
|
|
|
gilstate = PyGILState_Ensure();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Copy on write. */
|
|
|
|
|
if (*dict_p == dict_orig) {
|
2023-07-21 02:18:59 +02:00
|
|
|
*dict_p = PyDict_Copy(static_cast<PyObject *>(dict_orig));
|
2020-09-17 18:23:12 +10:00
|
|
|
}
|
|
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
PyObject *dict = static_cast<PyObject *>(*dict_p);
|
2020-09-17 18:23:12 +10:00
|
|
|
BLI_assert(PyDict_Check(dict));
|
2021-01-07 14:31:23 +11:00
|
|
|
|
|
|
|
|
/* Use #PyDict_Pop instead of #PyDict_DelItemString to avoid setting the exception,
|
|
|
|
|
* while supported it's good to avoid for low level functions like this that run often. */
|
2020-09-17 18:23:12 +10:00
|
|
|
for (uint i = 0; i < context_members_len; i++) {
|
2021-01-07 14:31:23 +11:00
|
|
|
PyObject *key = PyUnicode_FromString(context_members[i]);
|
2025-06-30 15:55:19 +10:00
|
|
|
PyObject *item;
|
|
|
|
|
|
|
|
|
|
#if PY_VERSION_HEX >= 0x030d0000
|
|
|
|
|
switch (PyDict_Pop(dict, key, &item)) {
|
|
|
|
|
case 1: {
|
|
|
|
|
Py_DECREF(item);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case -1: {
|
|
|
|
|
/* Not expected, but allow for an error. */
|
|
|
|
|
BLI_assert(false);
|
|
|
|
|
PyErr_Clear();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#else /* Remove when Python 3.12 support is dropped. */
|
|
|
|
|
item = _PyDict_Pop(dict, key, Py_None);
|
2021-01-07 14:31:23 +11:00
|
|
|
Py_DECREF(item);
|
2025-06-30 15:55:19 +10:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
Py_DECREF(key);
|
2020-09-17 18:23:12 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (use_gil) {
|
|
|
|
|
PyGILState_Release(gilstate);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-05 02:08:54 +00:00
|
|
|
void BPY_text_free_code(Text *text)
|
2009-03-04 13:26:33 +00:00
|
|
|
{
|
2011-10-13 01:29:08 +00:00
|
|
|
if (text->compiled) {
|
2012-09-10 23:32:46 +00:00
|
|
|
PyGILState_STATE gilstate;
|
2020-08-20 16:10:13 +10:00
|
|
|
const bool use_gil = !PyC_IsInterpreterActive();
|
2012-09-10 23:32:46 +00:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (use_gil) {
|
2012-09-10 23:32:46 +00:00
|
|
|
gilstate = PyGILState_Ensure();
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2012-09-10 23:32:46 +00:00
|
|
|
|
2011-03-19 11:12:48 +00:00
|
|
|
Py_DECREF((PyObject *)text->compiled);
|
2023-07-21 02:18:59 +02:00
|
|
|
text->compiled = nullptr;
|
2012-09-10 23:32:46 +00:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (use_gil) {
|
2012-09-10 23:32:46 +00:00
|
|
|
PyGILState_Release(gilstate);
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2009-03-04 13:26:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
2008-11-29 13:36:08 +00:00
|
|
|
|
2023-07-21 10:59:54 +10:00
|
|
|
void BPY_modules_update()
|
2009-04-11 16:17:39 +00:00
|
|
|
{
|
2023-09-19 15:00:21 +10:00
|
|
|
/* Correct but slow, this runs all the time operator poll, panel draw etc
|
|
|
|
|
* (100's of time a second). */
|
|
|
|
|
#if 0
|
2023-07-21 02:18:59 +02:00
|
|
|
PyObject *mod = PyImport_ImportModuleLevel("bpy", nullptr, nullptr, nullptr, 0);
|
2011-03-19 11:12:48 +00:00
|
|
|
PyModule_AddObject(mod, "data", BPY_rna_module());
|
2021-09-23 22:06:49 +10:00
|
|
|
PyModule_AddObject(mod, "types", BPY_rna_types()); /* This does not need updating. */
|
2009-08-15 09:53:38 +00:00
|
|
|
#endif
|
|
|
|
|
|
2023-09-19 15:00:21 +10:00
|
|
|
/* Refreshes the main struct. */
|
2009-08-15 09:53:38 +00:00
|
|
|
BPY_update_rna_module();
|
2020-10-15 18:12:03 +11:00
|
|
|
}
|
|
|
|
|
|
2023-07-21 10:59:54 +10:00
|
|
|
bContext *BPY_context_get()
|
2020-10-15 18:12:03 +11:00
|
|
|
{
|
RNA: Make the `PointerRNA` struct non-trivial.
For now, PointerRNA is made non-trivial by giving explicit default
values to its members.
Besides of BPY python binding code, the change is relatively trivial.
The main change (besides the creation/deletion part) is the replacement
of `memset` by zero-initialized assignment (using `{}`).
makesrna required changes are quite small too.
The big piece of this PR is the refactor of the BPY RNA code.
It essentially brings back allocation and deletion of the BPy_StructRNA,
BPy_Pointer etc. python objects into 'cannonical process', using `__new__`,
and `__init__` callbacks (and there matching CAPI functions).
Existing code was doing very low-level manipulations to create these
data, which is not really easy to understand, and AFAICT incompatible
with handling C++ data that needs to be constructed and destructed.
Unfortunately, similar change in destruction code (using `__del__` and
matching `tp_finalize` CAPI callback) is not possible, because of technical
low-level implementation details in CPython (see [1] for details).
`std::optional` pointer management is used to encapsulate PointerRNA
data. This allows to keep control on _when_ actual RNA creation is done,
and to have a safe destruction in `tp_dealloc` callbacks.
Note that a critical change in Blender's Python API will be that classes
inherinting from `bpy_struct` etc. will now have to properly call the
base class `__new__` and/or `__init__`if they define them.
Implements #122431.
[1] https://discuss.python.org/t/cpython-usage-of-tp-finalize-in-c-defined-static-types-with-no-custom-tp-dealloc/64100
2024-10-30 15:08:37 +01:00
|
|
|
return static_cast<bContext *>(bpy_context_module->ptr->data);
|
2009-04-11 16:17:39 +00:00
|
|
|
}
|
|
|
|
|
|
2011-01-05 02:08:54 +00:00
|
|
|
void BPY_context_set(bContext *C)
|
2009-11-13 09:28:05 +00:00
|
|
|
{
|
RNA: Make the `PointerRNA` struct non-trivial.
For now, PointerRNA is made non-trivial by giving explicit default
values to its members.
Besides of BPY python binding code, the change is relatively trivial.
The main change (besides the creation/deletion part) is the replacement
of `memset` by zero-initialized assignment (using `{}`).
makesrna required changes are quite small too.
The big piece of this PR is the refactor of the BPY RNA code.
It essentially brings back allocation and deletion of the BPy_StructRNA,
BPy_Pointer etc. python objects into 'cannonical process', using `__new__`,
and `__init__` callbacks (and there matching CAPI functions).
Existing code was doing very low-level manipulations to create these
data, which is not really easy to understand, and AFAICT incompatible
with handling C++ data that needs to be constructed and destructed.
Unfortunately, similar change in destruction code (using `__del__` and
matching `tp_finalize` CAPI callback) is not possible, because of technical
low-level implementation details in CPython (see [1] for details).
`std::optional` pointer management is used to encapsulate PointerRNA
data. This allows to keep control on _when_ actual RNA creation is done,
and to have a safe destruction in `tp_dealloc` callbacks.
Note that a critical change in Blender's Python API will be that classes
inherinting from `bpy_struct` etc. will now have to properly call the
base class `__new__` and/or `__init__`if they define them.
Implements #122431.
[1] https://discuss.python.org/t/cpython-usage-of-tp-finalize-in-c-defined-static-types-with-no-custom-tp-dealloc/64100
2024-10-30 15:08:37 +01:00
|
|
|
bpy_context_module->ptr->data = (void *)C;
|
2009-11-13 09:28:05 +00:00
|
|
|
}
|
|
|
|
|
|
2019-12-16 15:50:14 +01:00
|
|
|
#ifdef WITH_FLUID
|
2023-09-19 15:00:21 +10:00
|
|
|
/* Defined in `manta` module. */
|
2025-01-07 11:26:06 +11:00
|
|
|
extern "C" PyObject *Manta_initPython();
|
2019-12-16 15:50:14 +01:00
|
|
|
#endif
|
|
|
|
|
|
2022-05-20 11:17:34 +10:00
|
|
|
#ifdef WITH_AUDASPACE_PY
|
2023-09-19 15:00:21 +10:00
|
|
|
/* Defined in `AUD_C-API.cpp`. */
|
2025-01-07 11:26:06 +11:00
|
|
|
extern "C" PyObject *AUD_initPython();
|
2019-12-16 15:50:14 +01:00
|
|
|
#endif
|
2012-01-09 16:58:01 +00:00
|
|
|
|
|
|
|
|
#ifdef WITH_CYCLES
|
2023-09-19 15:00:21 +10:00
|
|
|
/* Defined in `cycles` module. */
|
2023-07-21 10:59:54 +10:00
|
|
|
static PyObject *CCL_initPython()
|
2012-01-09 16:58:01 +00:00
|
|
|
{
|
2012-03-26 20:41:54 +00:00
|
|
|
return (PyObject *)CCL_python_module_init();
|
2012-01-09 16:58:01 +00:00
|
|
|
}
|
|
|
|
|
#endif
|
2010-10-29 22:59:39 +00:00
|
|
|
|
2023-08-04 15:10:48 +02:00
|
|
|
#ifdef WITH_HYDRA
|
2023-09-19 15:00:21 +10:00
|
|
|
/* Defined in `render_hydra` module. */
|
2023-08-04 15:10:48 +02:00
|
|
|
PyObject *BPyInit_hydra();
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-07-21 10:59:54 +10:00
|
|
|
static _inittab bpy_internal_modules[] = {
|
2014-07-01 13:39:36 +10:00
|
|
|
{"mathutils", PyInit_mathutils},
|
|
|
|
|
#if 0
|
2019-04-17 08:24:14 +02:00
|
|
|
{"mathutils.geometry", PyInit_mathutils_geometry},
|
|
|
|
|
{"mathutils.noise", PyInit_mathutils_noise},
|
|
|
|
|
{"mathutils.kdtree", PyInit_mathutils_kdtree},
|
2014-07-01 13:39:36 +10:00
|
|
|
#endif
|
|
|
|
|
{"_bpy_path", BPyInit__bpy_path},
|
|
|
|
|
{"blf", BPyInit_blf},
|
2020-07-04 13:20:59 +03:00
|
|
|
{"bl_math", BPyInit_bl_math},
|
2018-06-07 07:41:49 +02:00
|
|
|
{"imbuf", BPyInit_imbuf},
|
2014-07-01 13:39:36 +10:00
|
|
|
{"bmesh", BPyInit_bmesh},
|
|
|
|
|
#if 0
|
2019-04-17 08:24:14 +02:00
|
|
|
{"bmesh.types", BPyInit_bmesh_types},
|
|
|
|
|
{"bmesh.utils", BPyInit_bmesh_utils},
|
|
|
|
|
{"bmesh.utils", BPyInit_bmesh_geometry},
|
2014-07-01 13:39:36 +10:00
|
|
|
#endif
|
2019-12-16 15:50:14 +01:00
|
|
|
#ifdef WITH_FLUID
|
|
|
|
|
{"manta", Manta_initPython},
|
|
|
|
|
#endif
|
2022-05-20 11:17:34 +10:00
|
|
|
#ifdef WITH_AUDASPACE_PY
|
2014-07-01 13:39:36 +10:00
|
|
|
{"aud", AUD_initPython},
|
2011-08-16 16:15:34 +00:00
|
|
|
#endif
|
|
|
|
|
#ifdef WITH_CYCLES
|
2014-07-01 13:39:36 +10:00
|
|
|
{"_cycles", CCL_initPython},
|
2011-06-23 09:27:56 +00:00
|
|
|
#endif
|
2018-09-05 21:10:42 -03:00
|
|
|
{"gpu", BPyInit_gpu},
|
2014-07-01 13:39:36 +10:00
|
|
|
{"idprop", BPyInit_idprop},
|
2023-08-04 15:10:48 +02:00
|
|
|
#ifdef WITH_HYDRA
|
|
|
|
|
{"_bpy_hydra", BPyInit_hydra},
|
|
|
|
|
#endif
|
2023-07-21 02:18:59 +02:00
|
|
|
{nullptr, nullptr},
|
2010-10-29 22:59:39 +00:00
|
|
|
};
|
|
|
|
|
|
2021-03-10 23:13:03 +11:00
|
|
|
#ifndef WITH_PYTHON_MODULE
|
2021-02-12 08:08:17 +11:00
|
|
|
/**
|
|
|
|
|
* Convenience function for #BPY_python_start.
|
|
|
|
|
*
|
|
|
|
|
* These should happen so rarely that having comprehensive errors isn't needed.
|
|
|
|
|
* For example if `sys.argv` fails to allocate memory.
|
|
|
|
|
*
|
|
|
|
|
* Show an error just to avoid silent failure in the unlikely event something goes wrong,
|
|
|
|
|
* in this case a developer will need to track down the root cause.
|
|
|
|
|
*/
|
2024-03-28 17:13:28 +11:00
|
|
|
static void pystatus_exit_on_error(const PyStatus &status)
|
2021-02-12 08:08:17 +11:00
|
|
|
{
|
|
|
|
|
if (UNLIKELY(PyStatus_Exception(status))) {
|
|
|
|
|
fputs("Internal error initializing Python!\n", stderr);
|
|
|
|
|
/* This calls `exit`. */
|
|
|
|
|
Py_ExitStatusException(status);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-10 23:13:03 +11:00
|
|
|
#endif
|
2021-02-12 08:08:17 +11:00
|
|
|
|
2020-10-15 18:12:03 +11:00
|
|
|
void BPY_python_start(bContext *C, int argc, const char **argv)
|
2008-11-29 13:36:08 +00:00
|
|
|
{
|
2011-02-20 23:39:29 +00:00
|
|
|
#ifndef WITH_PYTHON_MODULE
|
2024-04-05 11:41:12 +11:00
|
|
|
BLI_assert_msg(Py_IsInitialized() == 0, "Python has already been initialized");
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-06-24 15:56:58 +10:00
|
|
|
/* #PyPreConfig (early-configuration). */
|
2020-07-23 13:52:52 +10:00
|
|
|
{
|
2021-02-12 08:08:17 +11:00
|
|
|
PyPreConfig preconfig;
|
|
|
|
|
PyStatus status;
|
|
|
|
|
|
2023-02-12 14:37:16 +11:00
|
|
|
/* To narrow down reports where the systems Python is inexplicably used, see: #98131. */
|
2025-06-12 02:20:03 +02:00
|
|
|
CLOG_DEBUG(
|
2022-05-18 11:31:04 +10:00
|
|
|
BPY_LOG_INTERFACE,
|
|
|
|
|
"Initializing %s support for the systems Python environment such as 'PYTHONPATH' and "
|
|
|
|
|
"the user-site directory.",
|
|
|
|
|
py_use_system_env ? "*with*" : "*without*");
|
|
|
|
|
|
2021-02-12 08:08:17 +11:00
|
|
|
if (py_use_system_env) {
|
|
|
|
|
PyPreConfig_InitPythonConfig(&preconfig);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* Only use the systems environment variables and site when explicitly requested.
|
2023-02-12 14:37:16 +11:00
|
|
|
* Since an incorrect 'PYTHONPATH' causes difficult to debug errors, see: #72807.
|
2021-02-12 08:08:17 +11:00
|
|
|
* An alternative to setting `preconfig.use_environment = 0` */
|
|
|
|
|
PyPreConfig_InitIsolatedConfig(&preconfig);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-17 09:18:03 +10:00
|
|
|
/* Force UTF8 on all platforms, since this is what's used for Blender's internal strings,
|
2021-02-12 08:08:17 +11:00
|
|
|
* providing consistent encoding behavior across all Blender installations.
|
|
|
|
|
*
|
|
|
|
|
* This also uses the `surrogateescape` error handler ensures any unexpected bytes are escaped
|
|
|
|
|
* instead of raising an error.
|
|
|
|
|
*
|
|
|
|
|
* Without this `sys.getfilesystemencoding()` and `sys.stdout` for example may be set to ASCII
|
2025-05-17 09:18:03 +10:00
|
|
|
* or some other encoding - where printing some UTF8 values will raise an error.
|
2021-02-12 08:08:17 +11:00
|
|
|
*
|
|
|
|
|
* This can cause scripts to fail entirely on some systems.
|
|
|
|
|
*
|
|
|
|
|
* This assignment is the equivalent of enabling the `PYTHONUTF8` environment variable.
|
|
|
|
|
* See `PEP-540` for details on exactly what this changes. */
|
|
|
|
|
preconfig.utf8_mode = true;
|
|
|
|
|
|
|
|
|
|
/* Note that there is no reason to call #Py_PreInitializeFromBytesArgs here
|
|
|
|
|
* as this is only used so that command line arguments can be handled by Python itself,
|
|
|
|
|
* not for setting `sys.argv` (handled below). */
|
|
|
|
|
status = Py_PreInitialize(&preconfig);
|
|
|
|
|
pystatus_exit_on_error(status);
|
2020-07-23 13:52:52 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-02-12 08:08:17 +11:00
|
|
|
/* Must run before python initializes, but after #PyPreConfig. */
|
2010-10-29 22:59:39 +00:00
|
|
|
PyImport_ExtendInittab(bpy_internal_modules);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-02-12 08:08:17 +11:00
|
|
|
/* #PyConfig (initialize Python). */
|
2020-11-02 19:33:32 +11:00
|
|
|
{
|
2021-02-12 08:08:17 +11:00
|
|
|
PyConfig config;
|
|
|
|
|
PyStatus status;
|
|
|
|
|
bool has_python_executable = false;
|
|
|
|
|
|
2024-05-03 15:27:10 +10:00
|
|
|
if (py_use_system_env) {
|
|
|
|
|
PyConfig_InitPythonConfig(&config);
|
2024-11-27 20:32:51 +11:00
|
|
|
|
|
|
|
|
BLI_assert(config.install_signal_handlers);
|
2024-05-03 15:27:10 +10:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
PyConfig_InitIsolatedConfig(&config);
|
2025-05-29 14:41:20 -04:00
|
|
|
/* Python's isolated config disables its own signal overrides.
|
2024-11-27 20:32:51 +11:00
|
|
|
* While it makes sense not to interfering with other components of the process,
|
|
|
|
|
* the signal handlers are needed for Python's own error handling to work properly.
|
|
|
|
|
* Without this a `SIGPIPE` signal will crash Blender, see: #129657. */
|
|
|
|
|
config.install_signal_handlers = 1;
|
2024-05-03 15:27:10 +10:00
|
|
|
}
|
2021-02-12 08:08:17 +11:00
|
|
|
|
|
|
|
|
/* Suppress error messages when calculating the module search path.
|
|
|
|
|
* While harmless, it's noisy. */
|
|
|
|
|
config.pathconfig_warnings = 0;
|
|
|
|
|
|
2025-07-29 11:45:12 +00:00
|
|
|
{
|
|
|
|
|
/* NOTE: running scripts directly uses the default behavior *but* the default
|
|
|
|
|
* warning filter doesn't show warnings form module besides `__main__`.
|
|
|
|
|
* Use the default behavior unless debugging Python. See: !139487. */
|
|
|
|
|
bool show_python_warnings = false;
|
|
|
|
|
|
|
|
|
|
# ifdef NDEBUG
|
|
|
|
|
show_python_warnings = G.debug & G_DEBUG_PYTHON;
|
|
|
|
|
# else
|
|
|
|
|
/* Always show warnings for debug builds so developers are made aware
|
|
|
|
|
* of outdated API use before any breakages occur. */
|
|
|
|
|
show_python_warnings = true;
|
|
|
|
|
# endif
|
|
|
|
|
|
|
|
|
|
if (show_python_warnings) {
|
|
|
|
|
/* Don't overwrite warning settings if they have been set by the environment. */
|
|
|
|
|
if (!(py_use_system_env && BLI_getenv("PYTHONWARNINGS"))) {
|
|
|
|
|
/* Confusingly `default` is not the default.
|
|
|
|
|
* Setting to `default` without any module names shows warnings for all modules.
|
|
|
|
|
* Useful for development since most functionality occurs outside of `__main__`. */
|
|
|
|
|
PyWideStringList_Append(&config.warnoptions, L"default");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-25 12:41:59 +11:00
|
|
|
/* Allow the user site directory because this is used
|
2023-02-12 14:37:16 +11:00
|
|
|
* when PIP installing packages from Blender, see: #104000.
|
2023-01-25 12:41:59 +11:00
|
|
|
*
|
2023-02-09 11:30:25 +11:00
|
|
|
* NOTE(@ideasman42): While an argument can be made for isolating Blender's Python
|
2023-01-25 12:41:59 +11:00
|
|
|
* from the users home directory entirely, an alternative directory should be used in that
|
|
|
|
|
* case - so PIP can be used to install packages. Otherwise PIP will install packages to a
|
|
|
|
|
* directory which us not in the users `sys.path`, see `site.USER_BASE` for details. */
|
|
|
|
|
// config.user_site_directory = py_use_system_env;
|
2021-02-12 08:08:17 +11:00
|
|
|
|
|
|
|
|
/* While `sys.argv` is set, we don't want Python to interpret it. */
|
|
|
|
|
config.parse_argv = 0;
|
|
|
|
|
status = PyConfig_SetBytesArgv(&config, argc, (char *const *)argv);
|
|
|
|
|
pystatus_exit_on_error(status);
|
|
|
|
|
|
|
|
|
|
/* Needed for Python's initialization for portable Python installations.
|
|
|
|
|
* We could use #Py_SetPath, but this overrides Python's internal logic
|
2021-05-21 22:19:46 +10:00
|
|
|
* for calculating its own module search paths.
|
2021-02-12 08:08:17 +11:00
|
|
|
*
|
|
|
|
|
* `sys.executable` is overwritten after initialization to the Python binary. */
|
|
|
|
|
{
|
|
|
|
|
const char *program_path = BKE_appdir_program_path();
|
|
|
|
|
status = PyConfig_SetBytesString(&config, &config.program_name, program_path);
|
|
|
|
|
pystatus_exit_on_error(status);
|
2020-11-02 19:33:32 +11:00
|
|
|
}
|
2021-02-12 08:08:17 +11:00
|
|
|
|
|
|
|
|
/* Setting the program name is important so the 'multiprocessing' module
|
|
|
|
|
* can launch new Python instances. */
|
|
|
|
|
{
|
|
|
|
|
char program_path[FILE_MAX];
|
|
|
|
|
if (BKE_appdir_program_python_search(
|
|
|
|
|
program_path, sizeof(program_path), PY_MAJOR_VERSION, PY_MINOR_VERSION))
|
|
|
|
|
{
|
|
|
|
|
status = PyConfig_SetBytesString(&config, &config.executable, program_path);
|
|
|
|
|
pystatus_exit_on_error(status);
|
|
|
|
|
has_python_executable = true;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* Set to `sys.executable = None` below (we can't do before Python is initialized). */
|
|
|
|
|
fprintf(stderr,
|
2023-05-14 15:23:43 +02:00
|
|
|
"Unable to find the Python binary, "
|
2021-02-12 08:08:17 +11:00
|
|
|
"the multiprocessing module may not be functional!\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
/* Allow to use our own included Python. `py_path_bundle` may be nullptr. */
|
2021-02-12 08:08:17 +11:00
|
|
|
{
|
2024-01-23 18:38:15 +01:00
|
|
|
const std::optional<std::string> py_path_bundle = BKE_appdir_folder_id(BLENDER_SYSTEM_PYTHON,
|
|
|
|
|
nullptr);
|
|
|
|
|
if (py_path_bundle.has_value()) {
|
2021-02-22 21:20:48 +11:00
|
|
|
|
|
|
|
|
# ifdef __APPLE__
|
|
|
|
|
/* Mac-OS allows file/directory names to contain `:` character
|
|
|
|
|
* (represented as `/` in the Finder) but current Python lib (as of release 3.1.1)
|
|
|
|
|
* doesn't handle these correctly. */
|
2024-01-23 18:38:15 +01:00
|
|
|
if (strchr(py_path_bundle->c_str(), ':')) {
|
2021-02-22 21:20:48 +11:00
|
|
|
fprintf(stderr,
|
|
|
|
|
"Warning! Blender application is located in a path containing ':' or '/' chars\n"
|
2023-05-14 15:23:43 +02:00
|
|
|
"This may make Python import function fail\n");
|
2021-02-22 21:20:48 +11:00
|
|
|
}
|
|
|
|
|
# endif /* __APPLE__ */
|
|
|
|
|
|
2024-01-23 18:38:15 +01:00
|
|
|
status = PyConfig_SetBytesString(&config, &config.home, py_path_bundle->c_str());
|
2021-02-12 08:08:17 +11:00
|
|
|
pystatus_exit_on_error(status);
|
2023-10-17 19:05:40 +11:00
|
|
|
|
|
|
|
|
# ifdef PYTHON_SSL_CERT_FILE
|
|
|
|
|
/* Point to the portable SSL certificate to support HTTPS access, see: #102300. */
|
|
|
|
|
const char *ssl_cert_file_env = "SSL_CERT_FILE";
|
|
|
|
|
if (BLI_getenv(ssl_cert_file_env) == nullptr) {
|
|
|
|
|
const char *ssl_cert_file_suffix = PYTHON_SSL_CERT_FILE;
|
|
|
|
|
char ssl_cert_file[FILE_MAX];
|
|
|
|
|
BLI_path_join(
|
2024-01-23 18:38:15 +01:00
|
|
|
ssl_cert_file, sizeof(ssl_cert_file), py_path_bundle->c_str(), ssl_cert_file_suffix);
|
2023-10-17 19:05:40 +11:00
|
|
|
BLI_setenv(ssl_cert_file_env, ssl_cert_file);
|
|
|
|
|
}
|
|
|
|
|
# endif /* PYTHON_SSL_CERT_FILE */
|
2021-02-12 08:08:17 +11:00
|
|
|
}
|
|
|
|
|
else {
|
2023-07-21 02:18:59 +02:00
|
|
|
/* Common enough to use the system Python on Linux/Unix, warn on other systems. */
|
2020-11-02 19:33:32 +11:00
|
|
|
# if defined(__APPLE__) || defined(_WIN32)
|
2021-02-12 08:08:17 +11:00
|
|
|
fprintf(stderr,
|
|
|
|
|
"Bundled Python not found and is expected on this platform "
|
|
|
|
|
"(the 'install' target may have not been built)\n");
|
2020-11-02 19:33:32 +11:00
|
|
|
# endif
|
2021-02-12 08:08:17 +11:00
|
|
|
}
|
2020-11-02 19:33:32 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-02-12 08:08:17 +11:00
|
|
|
/* Initialize Python (also acquires lock). */
|
|
|
|
|
status = Py_InitializeFromConfig(&config);
|
2024-10-29 18:25:21 +11:00
|
|
|
PyConfig_Clear(&config);
|
|
|
|
|
|
2021-02-12 08:08:17 +11:00
|
|
|
pystatus_exit_on_error(status);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-02-12 08:08:17 +11:00
|
|
|
if (!has_python_executable) {
|
|
|
|
|
PySys_SetObject("executable", Py_None);
|
2020-10-29 09:33:15 +11:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-17 07:27:09 +11:00
|
|
|
# ifdef WITH_FLUID
|
|
|
|
|
/* Required to prevent assertion error, see:
|
|
|
|
|
* https://stackoverflow.com/questions/27844676 */
|
2019-12-16 15:50:14 +01:00
|
|
|
Py_DECREF(PyImport_ImportModule("threading"));
|
2019-12-17 07:27:09 +11:00
|
|
|
# endif
|
|
|
|
|
|
2023-05-30 11:45:21 +10:00
|
|
|
#else /* WITH_PYTHON_MODULE */
|
2011-02-20 23:39:29 +00:00
|
|
|
(void)argc;
|
|
|
|
|
(void)argv;
|
2011-03-25 01:55:00 +00:00
|
|
|
|
2023-05-30 11:45:21 +10:00
|
|
|
/* NOTE(ideasman42): unfortunately the `inittab` can only be used
|
|
|
|
|
* before Python has been initialized.
|
|
|
|
|
* When built as a Python module, Python will have been initialized
|
|
|
|
|
* and using the `inittab` isn't supported.
|
|
|
|
|
* So it's necessary to load all modules as soon as `bpy` is imported. */
|
2013-02-06 13:14:11 +00:00
|
|
|
// PyImport_ExtendInittab(bpy_internal_modules);
|
2023-05-30 11:45:21 +10:00
|
|
|
|
|
|
|
|
#endif /* WITH_PYTHON_MODULE */
|
2011-02-20 23:39:29 +00:00
|
|
|
|
2011-07-03 19:15:46 +00:00
|
|
|
bpy_intern_string_init();
|
2013-02-06 13:14:11 +00:00
|
|
|
|
|
|
|
|
#ifdef WITH_PYTHON_MODULE
|
|
|
|
|
{
|
|
|
|
|
/* Manually load all modules */
|
|
|
|
|
struct _inittab *inittab_item;
|
|
|
|
|
PyObject *sys_modules = PyImport_GetModuleDict();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-02-06 13:14:11 +00:00
|
|
|
for (inittab_item = bpy_internal_modules; inittab_item->name; inittab_item++) {
|
|
|
|
|
PyObject *mod = inittab_item->initfunc();
|
|
|
|
|
if (mod) {
|
|
|
|
|
PyDict_SetItemString(sys_modules, inittab_item->name, mod);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
PyErr_Print();
|
|
|
|
|
}
|
2023-09-19 15:00:21 +10:00
|
|
|
// Py_DECREF(mod); /* Ideally would decref, but in this case we never want to free. */
|
2013-02-06 13:14:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-08-11 17:29:26 +10:00
|
|
|
/* Run first, initializes RNA types. */
|
|
|
|
|
BPY_rna_init();
|
|
|
|
|
|
|
|
|
|
/* Defines `bpy.*` and lets us import it. */
|
2020-10-15 18:12:03 +11:00
|
|
|
BPy_init_modules(C);
|
2009-04-11 16:17:39 +00:00
|
|
|
|
2009-08-15 09:53:38 +00:00
|
|
|
pyrna_alloc_types();
|
|
|
|
|
|
2012-01-17 15:19:11 +00:00
|
|
|
#ifndef WITH_PYTHON_MODULE
|
2023-09-19 15:00:21 +10:00
|
|
|
/* Python module runs `atexit` when `bpy` is freed. */
|
|
|
|
|
BPY_atexit_register(); /* This can initialize any time. */
|
2011-09-20 12:22:19 +00:00
|
|
|
|
2021-02-12 08:08:17 +11:00
|
|
|
/* Free the lock acquired (implicitly) when Python is initialized. */
|
|
|
|
|
PyEval_ReleaseThread(PyGILState_GetThisThreadState());
|
|
|
|
|
|
2011-02-20 23:39:29 +00:00
|
|
|
#endif
|
2021-01-28 14:38:08 +11:00
|
|
|
|
|
|
|
|
#ifdef WITH_PYTHON_MODULE
|
2023-02-12 14:37:16 +11:00
|
|
|
/* Disable all add-ons at exit, not essential, it just avoids resource leaks, see #71362. */
|
2023-07-21 02:18:59 +02:00
|
|
|
const char *imports[] = {"atexit", "addon_utils", nullptr};
|
|
|
|
|
BPY_run_string_eval(C, imports, "atexit.register(addon_utils.disable_all)");
|
2021-01-28 14:38:08 +11:00
|
|
|
#endif
|
2008-11-29 13:36:08 +00:00
|
|
|
}
|
|
|
|
|
|
2023-09-19 15:38:55 +10:00
|
|
|
void BPY_python_end(const bool do_python_exit)
|
2008-11-29 13:36:08 +00:00
|
|
|
{
|
2024-05-06 22:33:44 +03:00
|
|
|
#ifndef WITH_PYTHON_MODULE
|
2024-04-05 11:41:12 +11:00
|
|
|
BLI_assert_msg(Py_IsInitialized() != 0, "Python must be initialized");
|
2024-05-06 22:33:44 +03:00
|
|
|
#endif
|
2024-04-05 11:41:12 +11:00
|
|
|
|
2023-09-19 15:00:21 +10:00
|
|
|
/* Finalizing, no need to grab the state, except when we are a module. */
|
2025-02-13 17:56:42 +11:00
|
|
|
PyGILState_STATE gilstate = PyGILState_Ensure();
|
2018-06-04 08:54:17 +02:00
|
|
|
|
2023-09-19 15:00:21 +10:00
|
|
|
/* Frees the Python-driver name-space & cached data. */
|
2022-07-07 12:30:45 +10:00
|
|
|
BPY_driver_exit();
|
|
|
|
|
|
2021-04-20 11:57:28 +10:00
|
|
|
/* Clear Python values in the context so freeing the context after Python exits doesn't crash. */
|
|
|
|
|
bpy_context_end(BPY_context_get());
|
|
|
|
|
|
2021-03-16 12:18:56 +11:00
|
|
|
/* Decrement user counts of all callback functions. */
|
|
|
|
|
BPY_rna_props_clear_all();
|
|
|
|
|
|
2023-09-19 15:00:21 +10:00
|
|
|
/* Free other Python data. */
|
2024-10-11 18:33:29 +02:00
|
|
|
RNA_bpy_exit();
|
2009-08-14 12:29:55 +00:00
|
|
|
|
2021-08-11 17:29:26 +10:00
|
|
|
BPY_rna_exit();
|
|
|
|
|
|
2023-09-19 15:00:21 +10:00
|
|
|
/* Clear all Python data from structs. */
|
2011-06-18 08:45:45 +00:00
|
|
|
|
|
|
|
|
bpy_intern_string_exit();
|
|
|
|
|
|
2023-09-19 15:00:21 +10:00
|
|
|
/* `bpy.app` modules that need cleanup. */
|
2015-04-01 09:34:01 +02:00
|
|
|
BPY_app_translations_end();
|
|
|
|
|
|
2012-01-17 15:19:11 +00:00
|
|
|
#ifndef WITH_PYTHON_MODULE
|
2023-09-19 15:38:55 +10:00
|
|
|
/* Without this we get recursive calls to #WM_exit_ex. */
|
2023-09-19 15:00:21 +10:00
|
|
|
BPY_atexit_unregister();
|
2011-09-20 15:17:24 +00:00
|
|
|
|
2023-09-19 15:38:55 +10:00
|
|
|
if (do_python_exit) {
|
|
|
|
|
Py_Finalize();
|
|
|
|
|
}
|
2014-02-14 10:55:19 +11:00
|
|
|
(void)gilstate;
|
2014-02-13 06:53:42 +11:00
|
|
|
#else
|
|
|
|
|
PyGILState_Release(gilstate);
|
2023-09-19 15:38:55 +10:00
|
|
|
(void)do_python_exit;
|
2014-02-13 06:53:42 +11:00
|
|
|
#endif
|
|
|
|
|
|
2009-08-09 13:20:12 +00:00
|
|
|
#ifdef TIME_PY_RUN
|
2023-09-19 15:00:21 +10:00
|
|
|
/* Measure time since Python started. */
|
2024-02-15 13:15:56 +11:00
|
|
|
bpy_timer = BLI_time_now_seconds() - bpy_timer;
|
2009-08-09 13:20:12 +00:00
|
|
|
|
|
|
|
|
printf("*bpy stats* - ");
|
|
|
|
|
printf("tot exec: %d, ", bpy_timer_count);
|
|
|
|
|
printf("tot run: %.4fsec, ", bpy_timer_run_tot);
|
2019-03-30 06:12:48 +11:00
|
|
|
if (bpy_timer_count > 0) {
|
2012-03-26 20:41:54 +00:00
|
|
|
printf("average run: %.6fsec, ", (bpy_timer_run_tot / bpy_timer_count));
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2009-08-09 13:20:12 +00:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (bpy_timer > 0.0) {
|
2012-03-26 20:41:54 +00:00
|
|
|
printf("tot usage %.4f%%", (bpy_timer_run_tot / bpy_timer) * 100.0);
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2009-08-09 13:20:12 +00:00
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
|
#endif
|
2008-11-29 13:36:08 +00:00
|
|
|
}
|
|
|
|
|
|
2013-06-09 23:31:53 +00:00
|
|
|
void BPY_python_reset(bContext *C)
|
|
|
|
|
{
|
2024-04-05 11:41:12 +11:00
|
|
|
BLI_assert_msg(Py_IsInitialized() != 0, "Python must be initialized");
|
|
|
|
|
|
2023-09-19 15:00:21 +10:00
|
|
|
/* Unrelated security stuff. */
|
2019-02-02 13:39:51 +11:00
|
|
|
G.f &= ~(G_FLAG_SCRIPT_AUTOEXEC_FAIL | G_FLAG_SCRIPT_AUTOEXEC_FAIL_QUIET);
|
2013-06-10 00:42:16 +00:00
|
|
|
G.autoexec_fail[0] = '\0';
|
|
|
|
|
|
2013-06-09 23:31:53 +00:00
|
|
|
BPY_driver_reset();
|
|
|
|
|
BPY_app_handlers_reset(false);
|
|
|
|
|
BPY_modules_load_user(C);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-21 10:59:54 +10:00
|
|
|
void BPY_python_use_system_env()
|
2020-01-16 21:11:05 +11:00
|
|
|
{
|
|
|
|
|
BLI_assert(!Py_IsInitialized());
|
|
|
|
|
py_use_system_env = true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-31 11:34:44 +10:00
|
|
|
bool BPY_python_use_system_env_get()
|
|
|
|
|
{
|
|
|
|
|
return py_use_system_env;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-17 17:21:11 +10:00
|
|
|
void BPY_python_backtrace(FILE *fp)
|
2020-08-06 15:34:55 +10:00
|
|
|
{
|
|
|
|
|
fputs("\n# Python backtrace\n", fp);
|
2022-08-19 16:20:20 +10:00
|
|
|
|
|
|
|
|
/* Can happen in rare cases. */
|
2025-06-20 04:19:35 +00:00
|
|
|
if (!PyThreadState_GetUnchecked()) {
|
2022-08-19 16:20:20 +10:00
|
|
|
return;
|
|
|
|
|
}
|
2023-09-19 15:00:21 +10:00
|
|
|
PyFrameObject *frame = PyEval_GetFrame();
|
|
|
|
|
if (frame == nullptr) {
|
2022-07-05 13:41:55 +10:00
|
|
|
return;
|
2020-08-17 17:21:11 +10:00
|
|
|
}
|
2022-07-05 13:41:55 +10:00
|
|
|
do {
|
|
|
|
|
PyCodeObject *code = PyFrame_GetCode(frame);
|
|
|
|
|
const int line = PyFrame_GetLineNumber(frame);
|
|
|
|
|
const char *filepath = PyUnicode_AsUTF8(code->co_filename);
|
|
|
|
|
const char *funcname = PyUnicode_AsUTF8(code->co_name);
|
|
|
|
|
fprintf(fp, " File \"%s\", line %d in %s\n", filepath, line, funcname);
|
|
|
|
|
} while ((frame = PyFrame_GetBack(frame)));
|
2020-08-06 15:34:55 +10:00
|
|
|
}
|
|
|
|
|
|
2009-04-01 12:43:07 +00:00
|
|
|
void BPY_DECREF(void *pyob_ptr)
|
|
|
|
|
{
|
2020-08-20 16:10:13 +10:00
|
|
|
const PyGILState_STATE gilstate = PyGILState_Ensure();
|
2009-04-01 12:43:07 +00:00
|
|
|
Py_DECREF((PyObject *)pyob_ptr);
|
2009-12-08 10:36:46 +00:00
|
|
|
PyGILState_Release(gilstate);
|
2009-04-01 12:43:07 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-26 10:54:02 +00:00
|
|
|
void BPY_DECREF_RNA_INVALIDATE(void *pyob_ptr)
|
2012-10-26 10:33:57 +00:00
|
|
|
{
|
2020-08-20 16:10:13 +10:00
|
|
|
const PyGILState_STATE gilstate = PyGILState_Ensure();
|
2021-01-09 19:00:05 +11:00
|
|
|
const bool do_invalidate = (Py_REFCNT((PyObject *)pyob_ptr) > 1);
|
2012-10-26 10:33:57 +00:00
|
|
|
Py_DECREF((PyObject *)pyob_ptr);
|
|
|
|
|
if (do_invalidate) {
|
2023-07-21 02:18:59 +02:00
|
|
|
pyrna_invalidate(static_cast<BPy_DummyPointerRNA *>(pyob_ptr));
|
2012-10-26 10:33:57 +00:00
|
|
|
}
|
|
|
|
|
PyGILState_Release(gilstate);
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-05 02:08:54 +00:00
|
|
|
void BPY_modules_load_user(bContext *C)
|
2009-11-20 15:01:09 +00:00
|
|
|
{
|
|
|
|
|
PyGILState_STATE gilstate;
|
2011-12-26 12:26:11 +00:00
|
|
|
Main *bmain = CTX_data_main(C);
|
2009-11-20 15:01:09 +00:00
|
|
|
Text *text;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-09-19 15:00:21 +10:00
|
|
|
/* Can happen on file load. */
|
2023-07-21 02:18:59 +02:00
|
|
|
if (bmain == nullptr) {
|
2010-01-10 20:01:13 +00:00
|
|
|
return;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-09-19 15:00:21 +10:00
|
|
|
/* Update pointers since this can run from a nested script on file load. */
|
2011-10-13 01:29:08 +00:00
|
|
|
if (py_call_level) {
|
2012-05-08 22:07:06 +00:00
|
|
|
BPY_context_update(C);
|
2011-08-20 13:29:42 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2009-11-20 15:01:09 +00:00
|
|
|
bpy_context_set(C, &gilstate);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
for (text = static_cast<Text *>(bmain->texts.first); text;
|
|
|
|
|
text = static_cast<Text *>(text->id.next))
|
|
|
|
|
{
|
2021-08-13 15:51:08 +10:00
|
|
|
if (text->flags & TXT_ISSCRIPT) {
|
2019-02-02 13:39:51 +11:00
|
|
|
if (!(G.f & G_FLAG_SCRIPT_AUTOEXEC)) {
|
|
|
|
|
if (!(G.f & G_FLAG_SCRIPT_AUTOEXEC_FAIL_QUIET)) {
|
|
|
|
|
G.f |= G_FLAG_SCRIPT_AUTOEXEC_FAIL;
|
2025-07-27 02:47:45 +00:00
|
|
|
SNPRINTF_UTF8(G.autoexec_fail, RPT_("Text '%s'"), text->id.name + 2);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-06-05 15:10:33 +02:00
|
|
|
printf("scripts disabled for \"%s\", skipping '%s'\n",
|
|
|
|
|
BKE_main_blendfile_path(bmain),
|
|
|
|
|
text->id.name + 2);
|
2009-11-20 15:01:09 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2009-11-20 15:01:09 +00:00
|
|
|
else {
|
2023-07-21 02:18:59 +02:00
|
|
|
BPY_run_text(C, text, nullptr, false);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-04-18 14:45:23 +02:00
|
|
|
/* Check if the script loaded a new file. */
|
2012-10-01 14:14:21 +00:00
|
|
|
if (bmain != CTX_data_main(C)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2009-11-20 15:01:09 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
bpy_context_clear(C, &gilstate);
|
|
|
|
|
}
|
2009-10-29 09:25:11 +00:00
|
|
|
|
2025-09-23 21:15:42 -04:00
|
|
|
/** Helper function for logging context member access errors with both CLI and Python support */
|
|
|
|
|
static void bpy_context_log_member_error(const bContext *C, const char *message)
|
|
|
|
|
{
|
|
|
|
|
const bool use_logging_info = CLOG_CHECK(BKE_LOG_CONTEXT, CLG_LEVEL_INFO);
|
|
|
|
|
const bool use_logging_member = C && CTX_member_logging_get(C);
|
|
|
|
|
if (!(use_logging_info || use_logging_member)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<std::string> python_location = BPY_python_current_file_and_line();
|
|
|
|
|
const char *location = python_location ? python_location->c_str() : "unknown:0";
|
|
|
|
|
|
|
|
|
|
if (use_logging_info) {
|
|
|
|
|
CLOG_INFO(BKE_LOG_CONTEXT, "%s: %s", location, message);
|
|
|
|
|
}
|
|
|
|
|
else if (use_logging_member) {
|
|
|
|
|
CLOG_AT_LEVEL_NOCHECK(BKE_LOG_CONTEXT, CLG_LEVEL_INFO, "%s: %s", location, message);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
BLI_assert_unreachable();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-26 14:27:17 +10:00
|
|
|
bool BPY_context_member_get(bContext *C, const char *member, bContextDataResult *result)
|
2009-10-29 09:25:11 +00:00
|
|
|
{
|
2012-09-21 22:31:02 +00:00
|
|
|
PyGILState_STATE gilstate;
|
2020-08-20 16:10:13 +10:00
|
|
|
const bool use_gil = !PyC_IsInterpreterActive();
|
2025-02-13 17:56:42 +11:00
|
|
|
if (use_gil) {
|
|
|
|
|
gilstate = PyGILState_Ensure();
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-09-21 22:31:02 +00:00
|
|
|
PyObject *pyctx;
|
|
|
|
|
PyObject *item;
|
2023-07-21 02:18:59 +02:00
|
|
|
PointerRNA *ptr = nullptr;
|
2013-01-07 05:26:12 +00:00
|
|
|
bool done = false;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-09-21 22:31:02 +00:00
|
|
|
pyctx = (PyObject *)CTX_py_dict_get(C);
|
|
|
|
|
item = PyDict_GetItemString(pyctx, member);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
if (item == nullptr) {
|
2023-09-19 15:00:21 +10:00
|
|
|
/* Pass. */
|
2009-10-29 09:25:11 +00:00
|
|
|
}
|
2011-12-26 12:26:11 +00:00
|
|
|
else if (item == Py_None) {
|
2013-06-10 12:17:36 +00:00
|
|
|
done = true;
|
2009-10-29 09:25:11 +00:00
|
|
|
}
|
2011-10-13 01:29:08 +00:00
|
|
|
else if (BPy_StructRNA_Check(item)) {
|
RNA: Make the `PointerRNA` struct non-trivial.
For now, PointerRNA is made non-trivial by giving explicit default
values to its members.
Besides of BPY python binding code, the change is relatively trivial.
The main change (besides the creation/deletion part) is the replacement
of `memset` by zero-initialized assignment (using `{}`).
makesrna required changes are quite small too.
The big piece of this PR is the refactor of the BPY RNA code.
It essentially brings back allocation and deletion of the BPy_StructRNA,
BPy_Pointer etc. python objects into 'cannonical process', using `__new__`,
and `__init__` callbacks (and there matching CAPI functions).
Existing code was doing very low-level manipulations to create these
data, which is not really easy to understand, and AFAICT incompatible
with handling C++ data that needs to be constructed and destructed.
Unfortunately, similar change in destruction code (using `__del__` and
matching `tp_finalize` CAPI callback) is not possible, because of technical
low-level implementation details in CPython (see [1] for details).
`std::optional` pointer management is used to encapsulate PointerRNA
data. This allows to keep control on _when_ actual RNA creation is done,
and to have a safe destruction in `tp_dealloc` callbacks.
Note that a critical change in Blender's Python API will be that classes
inherinting from `bpy_struct` etc. will now have to properly call the
base class `__new__` and/or `__init__`if they define them.
Implements #122431.
[1] https://discuss.python.org/t/cpython-usage-of-tp-finalize-in-c-defined-static-types-with-no-custom-tp-dealloc/64100
2024-10-30 15:08:37 +01:00
|
|
|
ptr = &reinterpret_cast<BPy_StructRNA *>(item)->ptr.value();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-05-01 11:09:22 +10:00
|
|
|
// result->ptr = ((BPy_StructRNA *)item)->ptr;
|
2021-08-31 12:45:45 +10:00
|
|
|
CTX_data_pointer_set_ptr(result, ptr);
|
2025-09-19 08:21:30 +10:00
|
|
|
CTX_data_type_set(result, ContextDataType::Pointer);
|
2013-01-07 05:26:12 +00:00
|
|
|
done = true;
|
2009-10-29 09:25:11 +00:00
|
|
|
}
|
2009-10-29 10:03:34 +00:00
|
|
|
else if (PySequence_Check(item)) {
|
2011-12-26 12:26:11 +00:00
|
|
|
PyObject *seq_fast = PySequence_Fast(item, "bpy_context_get sequence conversion");
|
2023-07-21 02:18:59 +02:00
|
|
|
if (seq_fast == nullptr) {
|
2009-10-29 10:03:34 +00:00
|
|
|
PyErr_Print();
|
|
|
|
|
}
|
|
|
|
|
else {
|
2020-08-20 16:10:13 +10:00
|
|
|
const int len = PySequence_Fast_GET_SIZE(seq_fast);
|
2015-07-29 09:58:10 +10:00
|
|
|
PyObject **seq_fast_items = PySequence_Fast_ITEMS(seq_fast);
|
2009-10-29 10:03:34 +00:00
|
|
|
int i;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
for (i = 0; i < len; i++) {
|
2015-07-29 09:58:10 +10:00
|
|
|
PyObject *list_item = seq_fast_items[i];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-10-13 01:29:08 +00:00
|
|
|
if (BPy_StructRNA_Check(list_item)) {
|
RNA: Make the `PointerRNA` struct non-trivial.
For now, PointerRNA is made non-trivial by giving explicit default
values to its members.
Besides of BPY python binding code, the change is relatively trivial.
The main change (besides the creation/deletion part) is the replacement
of `memset` by zero-initialized assignment (using `{}`).
makesrna required changes are quite small too.
The big piece of this PR is the refactor of the BPY RNA code.
It essentially brings back allocation and deletion of the BPy_StructRNA,
BPy_Pointer etc. python objects into 'cannonical process', using `__new__`,
and `__init__` callbacks (and there matching CAPI functions).
Existing code was doing very low-level manipulations to create these
data, which is not really easy to understand, and AFAICT incompatible
with handling C++ data that needs to be constructed and destructed.
Unfortunately, similar change in destruction code (using `__del__` and
matching `tp_finalize` CAPI callback) is not possible, because of technical
low-level implementation details in CPython (see [1] for details).
`std::optional` pointer management is used to encapsulate PointerRNA
data. This allows to keep control on _when_ actual RNA creation is done,
and to have a safe destruction in `tp_dealloc` callbacks.
Note that a critical change in Blender's Python API will be that classes
inherinting from `bpy_struct` etc. will now have to properly call the
base class `__new__` and/or `__init__`if they define them.
Implements #122431.
[1] https://discuss.python.org/t/cpython-usage-of-tp-finalize-in-c-defined-static-types-with-no-custom-tp-dealloc/64100
2024-10-30 15:08:37 +01:00
|
|
|
ptr = &reinterpret_cast<BPy_StructRNA *>(list_item)->ptr.value();
|
2021-08-31 12:45:45 +10:00
|
|
|
CTX_data_list_add_ptr(result, ptr);
|
2009-10-29 10:03:34 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2025-09-23 21:15:42 -04:00
|
|
|
/* Log invalid list item type */
|
|
|
|
|
std::string message = std::string("'") + member +
|
|
|
|
|
"' list item not a valid type in sequence type '" +
|
|
|
|
|
Py_TYPE(list_item)->tp_name + "'";
|
|
|
|
|
bpy_context_log_member_error(C, message.c_str());
|
2009-10-29 09:25:11 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2009-10-29 10:03:34 +00:00
|
|
|
Py_DECREF(seq_fast);
|
2025-09-19 08:21:30 +10:00
|
|
|
CTX_data_type_set(result, ContextDataType::Collection);
|
2013-01-07 05:26:12 +00:00
|
|
|
done = true;
|
2009-10-29 09:25:11 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2013-02-07 04:04:12 +00:00
|
|
|
if (done == false) {
|
2013-09-02 19:28:44 +00:00
|
|
|
if (item) {
|
2025-09-23 21:15:42 -04:00
|
|
|
/* Log invalid member type */
|
|
|
|
|
std::string message = std::string("'") + member + "' not a valid type";
|
|
|
|
|
bpy_context_log_member_error(C, message.c_str());
|
2013-09-02 19:28:44 +00:00
|
|
|
}
|
2009-10-29 09:25:11 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (use_gil) {
|
2012-09-21 22:31:02 +00:00
|
|
|
PyGILState_Release(gilstate);
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2009-10-29 09:25:11 +00:00
|
|
|
return done;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-19 06:48:07 +00:00
|
|
|
std::optional<std::string> BPY_python_current_file_and_line()
|
|
|
|
|
{
|
|
|
|
|
/* Early return if Python is not initialized, usually during startup.
|
|
|
|
|
* This function shouldn't operate if Python isn't initialized yet.
|
|
|
|
|
*
|
|
|
|
|
* In most cases this shouldn't be done, make an exception as it's needed for logging. */
|
|
|
|
|
if (!Py_IsInitialized()) {
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PyGILState_STATE gilstate;
|
|
|
|
|
const bool use_gil = !PyC_IsInterpreterActive();
|
|
|
|
|
std::optional<std::string> result = std::nullopt;
|
|
|
|
|
if (use_gil) {
|
|
|
|
|
gilstate = PyGILState_Ensure();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char *filename = nullptr;
|
|
|
|
|
int lineno = -1;
|
|
|
|
|
PyC_FileAndNum_Safe(&filename, &lineno);
|
|
|
|
|
|
|
|
|
|
if (filename) {
|
|
|
|
|
result = std::string(filename) + ":" + std::to_string(lineno);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (use_gil) {
|
|
|
|
|
PyGILState_Release(gilstate);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-20 23:39:29 +00:00
|
|
|
#ifdef WITH_PYTHON_MODULE
|
2021-07-03 23:08:40 +10:00
|
|
|
/* TODO: reloading the module isn't functional at the moment. */
|
2011-02-20 23:39:29 +00:00
|
|
|
|
2011-07-10 18:54:02 +00:00
|
|
|
static void bpy_module_free(void *mod);
|
2020-08-02 13:11:30 +10:00
|
|
|
|
|
|
|
|
/* Defined in 'creator.c' when building as a Python module. */
|
2023-08-02 08:08:36 -06:00
|
|
|
extern int main_python_enter(int argc, const char **argv);
|
2023-09-17 09:01:46 +10:00
|
|
|
extern void main_python_exit();
|
2020-08-02 13:11:30 +10:00
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
static struct PyModuleDef bpy_proxy_def = {
|
2023-07-16 17:43:31 +10:00
|
|
|
/*m_base*/ PyModuleDef_HEAD_INIT,
|
2022-11-08 11:13:58 +11:00
|
|
|
/*m_name*/ "bpy",
|
2023-07-21 02:18:59 +02:00
|
|
|
/*m_doc*/ nullptr,
|
2022-11-08 11:13:58 +11:00
|
|
|
/*m_size*/ 0,
|
2023-07-21 02:18:59 +02:00
|
|
|
/*m_methods*/ nullptr,
|
|
|
|
|
/*m_slots*/ nullptr,
|
|
|
|
|
/*m_traverse*/ nullptr,
|
|
|
|
|
/*m_clear*/ nullptr,
|
2022-11-08 11:13:58 +11:00
|
|
|
/*m_free*/ bpy_module_free,
|
2011-07-10 18:54:02 +00:00
|
|
|
};
|
2011-02-20 23:39:29 +00:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
struct dealloc_obj {
|
2011-04-21 13:11:51 +00:00
|
|
|
PyObject_HEAD
|
2021-06-24 17:10:22 +10:00
|
|
|
/* Type-specific fields go here. */
|
|
|
|
|
PyObject *mod;
|
2023-07-21 02:18:59 +02:00
|
|
|
};
|
2011-02-22 14:19:09 +00:00
|
|
|
|
2023-09-19 15:00:21 +10:00
|
|
|
/* Call once `__file__` is set. */
|
2013-01-02 16:15:45 +00:00
|
|
|
static void bpy_module_delay_init(PyObject *bpy_proxy)
|
2011-02-20 23:39:29 +00:00
|
|
|
{
|
2011-12-26 12:26:11 +00:00
|
|
|
const int argc = 1;
|
2011-02-22 14:19:09 +00:00
|
|
|
const char *argv[2];
|
2011-11-26 15:18:30 +00:00
|
|
|
|
2023-09-19 15:00:21 +10:00
|
|
|
/* Updating the module dict below will lose the reference to `__file__`. */
|
2022-05-17 11:38:05 +10:00
|
|
|
PyObject *filepath_obj = PyModule_GetFilenameObject(bpy_proxy);
|
2011-11-26 15:18:30 +00:00
|
|
|
|
2023-09-19 15:00:21 +10:00
|
|
|
/* The file can be a relative path. */
|
|
|
|
|
const char *filepath_rel = PyUnicode_AsUTF8(filepath_obj);
|
2022-05-17 11:38:05 +10:00
|
|
|
char filepath_abs[1024];
|
2011-02-22 14:19:09 +00:00
|
|
|
|
2023-05-13 17:34:21 +10:00
|
|
|
STRNCPY(filepath_abs, filepath_rel);
|
2022-05-17 11:38:05 +10:00
|
|
|
BLI_path_abs_from_cwd(filepath_abs, sizeof(filepath_abs));
|
|
|
|
|
Py_DECREF(filepath_obj);
|
2011-03-09 04:58:44 +00:00
|
|
|
|
2022-05-17 11:38:05 +10:00
|
|
|
argv[0] = filepath_abs;
|
2023-07-21 02:18:59 +02:00
|
|
|
argv[1] = nullptr;
|
2018-06-04 08:54:17 +02:00
|
|
|
|
2011-07-10 18:54:02 +00:00
|
|
|
main_python_enter(argc, argv);
|
2011-02-20 23:39:29 +00:00
|
|
|
|
2023-09-19 15:00:21 +10:00
|
|
|
/* Initialized in #BPy_init_modules(). */
|
2011-02-22 14:19:09 +00:00
|
|
|
PyDict_Update(PyModule_GetDict(bpy_proxy), PyModule_GetDict(bpy_package_py));
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-08 14:49:42 +10:00
|
|
|
/**
|
|
|
|
|
* Raise an error and return false if the Python version used to compile Blender
|
|
|
|
|
* isn't compatible with the interpreter loading the `bpy` module.
|
|
|
|
|
*/
|
2023-09-17 09:01:46 +10:00
|
|
|
static bool bpy_module_ensure_compatible_version()
|
2022-09-08 14:49:42 +10:00
|
|
|
{
|
2024-01-10 10:04:10 +11:00
|
|
|
/* First check the Python version used matches the major version that Blender was built with.
|
|
|
|
|
* While this isn't essential, the error message in this case may be cryptic and misleading.
|
|
|
|
|
* NOTE: using `Py_LIMITED_API` would remove the need for this, in practice it's
|
|
|
|
|
* unlikely Blender will ever used the limited API though. */
|
2022-09-08 14:49:42 +10:00
|
|
|
const uint version_runtime = Py_Version;
|
|
|
|
|
|
|
|
|
|
uint version_compile_major = PY_VERSION_HEX >> 24;
|
|
|
|
|
uint version_compile_minor = ((PY_VERSION_HEX & 0x00ff0000) >> 16);
|
|
|
|
|
uint version_runtime_major = version_runtime >> 24;
|
|
|
|
|
uint version_runtime_minor = ((version_runtime & 0x00ff0000) >> 16);
|
|
|
|
|
if ((version_compile_major != version_runtime_major) ||
|
|
|
|
|
(version_compile_minor != version_runtime_minor))
|
|
|
|
|
{
|
|
|
|
|
PyErr_Format(PyExc_ImportError,
|
|
|
|
|
"The version of \"bpy\" was compiled with: "
|
|
|
|
|
"(%u.%u) is incompatible with: (%u.%u) used by the interpreter!",
|
|
|
|
|
version_compile_major,
|
|
|
|
|
version_compile_minor,
|
|
|
|
|
version_runtime_major,
|
|
|
|
|
version_runtime_minor);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-22 14:19:09 +00:00
|
|
|
static void dealloc_obj_dealloc(PyObject *self);
|
|
|
|
|
|
2015-04-16 22:26:20 +10:00
|
|
|
static PyTypeObject dealloc_obj_Type;
|
2011-02-22 14:19:09 +00:00
|
|
|
|
2023-09-19 15:00:21 +10:00
|
|
|
/* Use our own `dealloc` so we can free a property if we use one. */
|
2011-02-22 14:19:09 +00:00
|
|
|
static void dealloc_obj_dealloc(PyObject *self)
|
|
|
|
|
{
|
|
|
|
|
bpy_module_delay_init(((dealloc_obj *)self)->mod);
|
|
|
|
|
|
2023-09-19 15:00:21 +10:00
|
|
|
/* NOTE: for sub-classed `PyObject` objects
|
|
|
|
|
* we can't call #PyObject_DEL() directly or it will crash. */
|
2011-02-22 14:19:09 +00:00
|
|
|
dealloc_obj_Type.tp_free(self);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-17 09:01:46 +10:00
|
|
|
PyMODINIT_FUNC PyInit_bpy();
|
2013-01-02 16:15:45 +00:00
|
|
|
|
2023-09-17 09:01:46 +10:00
|
|
|
PyMODINIT_FUNC PyInit_bpy()
|
2011-02-22 14:19:09 +00:00
|
|
|
{
|
2022-09-08 14:49:42 +10:00
|
|
|
if (!bpy_module_ensure_compatible_version()) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr; /* The error has been set. */
|
2022-09-08 14:49:42 +10:00
|
|
|
}
|
|
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
PyObject *bpy_proxy = PyModule_Create(&bpy_proxy_def);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-02-22 14:19:09 +00:00
|
|
|
/* Problem:
|
2023-09-19 15:00:21 +10:00
|
|
|
* 1) This initializing function is expected to have a private member defined - `md_def`
|
|
|
|
|
* but this is only set for CAPI defined modules (not Python packages)
|
|
|
|
|
* so we can't return `bpy_package_py` as is.
|
2011-02-22 14:19:09 +00:00
|
|
|
*
|
2023-09-19 15:00:21 +10:00
|
|
|
* 2) There is a `bpy` CAPI module for python to load which is basically all of blender,
|
2022-05-17 11:38:05 +10:00
|
|
|
* and there is `scripts/bpy/__init__.py`,
|
2011-02-22 14:19:09 +00:00
|
|
|
* we may end up having to rename this module so there is no naming conflict here eg:
|
2023-09-19 15:00:21 +10:00
|
|
|
* `from blender import bpy`
|
2011-02-22 14:19:09 +00:00
|
|
|
*
|
2023-09-19 15:00:21 +10:00
|
|
|
* 3) We don't know the file-path at this point, workaround by assigning a dummy value
|
2011-02-22 14:19:09 +00:00
|
|
|
* which calls back when its freed so the real loading can take place.
|
|
|
|
|
*/
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-09-19 15:00:21 +10:00
|
|
|
/* Assign an object which is freed after `__file__` is assigned. */
|
2011-02-22 14:19:09 +00:00
|
|
|
dealloc_obj *dob;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-09-19 15:00:21 +10:00
|
|
|
/* Assign dummy type. */
|
2011-12-26 12:26:11 +00:00
|
|
|
dealloc_obj_Type.tp_name = "dealloc_obj";
|
|
|
|
|
dealloc_obj_Type.tp_basicsize = sizeof(dealloc_obj);
|
|
|
|
|
dealloc_obj_Type.tp_dealloc = dealloc_obj_dealloc;
|
|
|
|
|
dealloc_obj_Type.tp_flags = Py_TPFLAGS_DEFAULT;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (PyType_Ready(&dealloc_obj_Type) < 0) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
dob = (dealloc_obj *)dealloc_obj_Type.tp_alloc(&dealloc_obj_Type, 0);
|
|
|
|
|
dob->mod = bpy_proxy; /* borrow */
|
2011-02-22 14:19:09 +00:00
|
|
|
PyModule_AddObject(bpy_proxy, "__file__", (PyObject *)dob); /* borrow */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-02-22 14:19:09 +00:00
|
|
|
return bpy_proxy;
|
2011-02-20 23:39:29 +00:00
|
|
|
}
|
2011-02-22 14:19:09 +00:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
static void bpy_module_free(void * /*mod*/)
|
2011-07-10 18:54:02 +00:00
|
|
|
{
|
|
|
|
|
main_python_exit();
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-20 23:39:29 +00:00
|
|
|
#endif
|
2013-02-19 16:13:41 +00:00
|
|
|
|
2016-04-01 09:43:11 +11:00
|
|
|
bool BPY_string_is_keyword(const char *str)
|
|
|
|
|
{
|
2023-09-19 15:00:21 +10:00
|
|
|
/* List is from: `", ".join(['"%s"' % kw for kw in __import__("keyword").kwlist])`. */
|
2016-04-01 09:43:11 +11:00
|
|
|
const char *kwlist[] = {
|
2018-11-11 09:37:26 +11:00
|
|
|
"False", "None", "True", "and", "as", "assert", "async", "await", "break",
|
2016-04-01 09:43:11 +11:00
|
|
|
"class", "continue", "def", "del", "elif", "else", "except", "finally", "for",
|
|
|
|
|
"from", "global", "if", "import", "in", "is", "lambda", "nonlocal", "not",
|
2023-07-21 02:18:59 +02:00
|
|
|
"or", "pass", "raise", "return", "try", "while", "with", "yield", nullptr,
|
2016-04-01 09:43:11 +11:00
|
|
|
};
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-04-01 09:43:11 +11:00
|
|
|
for (int i = 0; kwlist[i]; i++) {
|
|
|
|
|
if (STREQ(str, kwlist[i])) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-04-01 09:43:11 +11:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-02 17:27:09 +11:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Character Classification
|
|
|
|
|
*
|
|
|
|
|
* Define `text.cc` functions here (declared in `BKE_text.h`),
|
|
|
|
|
* This could be removed if Blender gets its own unicode library.
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2020-02-20 15:38:58 +11:00
|
|
|
int text_check_identifier_unicode(const uint ch)
|
2013-02-19 16:13:41 +00:00
|
|
|
{
|
2023-07-21 10:59:54 +10:00
|
|
|
return (ch < 255 && text_check_identifier(char(ch))) || Py_UNICODE_ISALNUM(ch);
|
2013-02-19 16:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
2020-02-20 15:38:58 +11:00
|
|
|
int text_check_identifier_nodigit_unicode(const uint ch)
|
2013-02-19 16:13:41 +00:00
|
|
|
{
|
2023-07-21 10:59:54 +10:00
|
|
|
return (ch < 255 && text_check_identifier_nodigit(char(ch))) || Py_UNICODE_ISALPHA(ch);
|
2013-02-19 16:13:41 +00:00
|
|
|
}
|
2024-11-02 17:27:09 +11:00
|
|
|
|
|
|
|
|
/** \} */
|