12 #include <frameobject.h>
54 #include "../generic/py_capi_utils.h"
57 #include "../bmesh/bmesh_py_api.h"
58 #include "../generic/bgl.h"
59 #include "../generic/bl_math_py_api.h"
60 #include "../generic/blf_py_api.h"
61 #include "../generic/idprop_py_api.h"
62 #include "../generic/imbuf_py_api.h"
63 #include "../gpu/gpu_py_api.h"
64 #include "../mathutils/mathutils.h"
85 static int bpy_timer_count = 0;
86 static double bpy_timer;
87 static double bpy_timer_run;
88 static double bpy_timer_run_tot;
109 *gilstate = PyGILState_Ensure();
116 if (bpy_timer_count == 0) {
119 bpy_timer_run = bpy_timer_run_tot = 0.0;
133 PyGILState_Release(*gilstate);
137 fprintf(stderr,
"ERROR: Python context internal state bug. this should not happen!\n");
163 const char *context_members[],
164 uint context_members_len)
166 PyGILState_STATE gilstate;
170 gilstate = PyGILState_Ensure();
174 if (*dict_p == dict_orig) {
175 *dict_p = PyDict_Copy(dict_orig);
178 PyObject *dict = *dict_p;
183 for (
uint i = 0; i < context_members_len; i++) {
184 PyObject *key = PyUnicode_FromString(context_members[i]);
185 PyObject *item = _PyDict_Pop(dict, key, Py_None);
191 PyGILState_Release(gilstate);
198 PyGILState_STATE gilstate;
202 gilstate = PyGILState_Ensure();
205 Py_DECREF((PyObject *)text->
compiled);
209 PyGILState_Release(gilstate);
217 PyObject *
mod = PyImport_ImportModuleLevel(
"bpy",
NULL,
NULL,
NULL, 0);
241 #ifdef WITH_AUDASPACE_PY
248 static PyObject *CCL_initPython(
void)
275 #ifdef WITH_AUDASPACE_PY
279 {
"_cycles", CCL_initPython},
286 #ifndef WITH_PYTHON_MODULE
298 if (
UNLIKELY(PyStatus_Exception(status))) {
299 fputs(
"Internal error initializing Python!\n", stderr);
301 Py_ExitStatusException(status);
308 #ifndef WITH_PYTHON_MODULE
312 PyPreConfig preconfig;
319 "Initializing %s support for the systems Python environment such as 'PYTHONPATH' and "
320 "the user-site directory.",
324 PyPreConfig_InitPythonConfig(&preconfig);
330 PyPreConfig_InitIsolatedConfig(&preconfig);
346 preconfig.utf8_mode =
true;
351 status = Py_PreInitialize(&preconfig);
362 bool has_python_executable =
false;
364 PyConfig_InitPythonConfig(&config);
368 config.pathconfig_warnings = 0;
374 config.parse_argv = 0;
375 status = PyConfig_SetBytesArgv(&config, argc, (
char *
const *)argv);
385 status = PyConfig_SetBytesString(&config, &config.program_name, program_path);
394 program_path,
sizeof(program_path), PY_MAJOR_VERSION, PY_MINOR_VERSION)) {
395 status = PyConfig_SetBytesString(&config, &config.executable, program_path);
397 has_python_executable =
true;
402 "Unable to find the python binary, "
403 "the multiprocessing module may not be functional!\n");
410 if (py_path_bundle !=
NULL) {
416 if (strchr(py_path_bundle,
':')) {
418 "Warning! Blender application is located in a path containing ':' or '/' chars\n"
419 "This may make python import function fail\n");
423 status = PyConfig_SetBytesString(&config, &config.home, py_path_bundle);
428 # if defined(__APPLE__) || defined(_WIN32)
430 "Bundled Python not found and is expected on this platform "
431 "(the 'install' target may have not been built)\n");
437 status = Py_InitializeFromConfig(&config);
440 if (!has_python_executable) {
441 PySys_SetObject(
"executable", Py_None);
448 Py_DECREF(PyImport_ImportModule(
"threading"));
462 #ifdef WITH_PYTHON_MODULE
465 struct _inittab *inittab_item;
466 PyObject *sys_modules = PyImport_GetModuleDict();
469 PyObject *
mod = inittab_item->initfunc();
471 PyDict_SetItemString(sys_modules, inittab_item->name,
mod);
490 #ifndef WITH_PYTHON_MODULE
495 PyEval_ReleaseThread(PyGILState_GetThisThreadState());
499 #ifdef WITH_PYTHON_MODULE
502 (
const char *[]){
"atexit",
"addon_utils",
NULL},
503 "atexit.register(addon_utils.disable_all)");
510 PyGILState_STATE gilstate;
513 gilstate = PyGILState_Ensure();
536 #ifndef WITH_PYTHON_MODULE
543 PyGILState_Release(gilstate);
550 printf(
"*bpy stats* - ");
551 printf(
"tot exec: %d, ", bpy_timer_count);
552 printf(
"tot run: %.4fsec, ", bpy_timer_run_tot);
553 if (bpy_timer_count > 0) {
554 printf(
"average run: %.6fsec, ", (bpy_timer_run_tot / bpy_timer_count));
557 if (bpy_timer > 0.0) {
558 printf(
"tot usage %.4f%%", (bpy_timer_run_tot / bpy_timer) * 100.0);
572 G.autoexec_fail[0] =
'\0';
587 fputs(
"\n# Python backtrace\n", fp);
590 if (!_PyThreadState_UncheckedGet()) {
593 PyFrameObject *frame;
594 if (!(frame = PyEval_GetFrame())) {
598 PyCodeObject *code = PyFrame_GetCode(frame);
599 const int line = PyFrame_GetLineNumber(frame);
600 const char *filepath = PyUnicode_AsUTF8(code->co_filename);
601 const char *funcname = PyUnicode_AsUTF8(code->co_name);
602 fprintf(fp,
" File \"%s\", line %d in %s\n", filepath, line, funcname);
603 }
while ((frame = PyFrame_GetBack(frame)));
608 const PyGILState_STATE gilstate = PyGILState_Ensure();
609 Py_DECREF((PyObject *)pyob_ptr);
610 PyGILState_Release(gilstate);
615 const PyGILState_STATE gilstate = PyGILState_Ensure();
616 const bool do_invalidate = (Py_REFCNT((PyObject *)pyob_ptr) > 1);
617 Py_DECREF((PyObject *)pyob_ptr);
621 PyGILState_Release(gilstate);
626 PyGILState_STATE gilstate;
650 printf(
"scripts disabled for \"%s\", skipping '%s'\n",
670 PyGILState_STATE gilstate;
679 gilstate = PyGILState_Ensure();
683 item = PyDict_GetItemString(pyctx, member);
688 else if (item == Py_None) {
699 else if (PySequence_Check(item)) {
700 PyObject *seq_fast = PySequence_Fast(item,
"bpy_context_get sequence conversion");
701 if (seq_fast ==
NULL) {
706 const int len = PySequence_Fast_GET_SIZE(seq_fast);
707 PyObject **seq_fast_items = PySequence_Fast_ITEMS(seq_fast);
710 for (i = 0; i <
len; i++) {
711 PyObject *list_item = seq_fast_items[i];
726 "'%s' list item not a valid type in sequence type '%s'",
728 Py_TYPE(item)->tp_name);
750 PyGILState_Release(gilstate);
756 #ifdef WITH_PYTHON_MODULE
759 static void bpy_module_free(
void *
mod);
762 extern int main_python_enter(
int argc,
const char **argv);
763 extern void main_python_exit(
void);
765 static struct PyModuleDef bpy_proxy_def = {
766 PyModuleDef_HEAD_INIT,
784 static void bpy_module_delay_init(PyObject *bpy_proxy)
790 PyObject *filepath_obj = PyModule_GetFilenameObject(bpy_proxy);
792 const char *filepath_rel = PyUnicode_AsUTF8(filepath_obj);
793 char filepath_abs[1024];
795 BLI_strncpy(filepath_abs, filepath_rel,
sizeof(filepath_abs));
797 Py_DECREF(filepath_obj);
799 argv[0] = filepath_abs;
804 main_python_enter(argc, argv);
807 PyDict_Update(PyModule_GetDict(bpy_proxy), PyModule_GetDict(
bpy_package_py));
810 static void dealloc_obj_dealloc(PyObject *
self);
812 static PyTypeObject dealloc_obj_Type;
815 static void dealloc_obj_dealloc(PyObject *
self)
817 bpy_module_delay_init(((dealloc_obj *)
self)->
mod);
820 dealloc_obj_Type.tp_free(
self);
823 PyMODINIT_FUNC PyInit_bpy(
void);
825 PyMODINIT_FUNC PyInit_bpy(
void)
827 PyObject *bpy_proxy = PyModule_Create(&bpy_proxy_def);
847 dealloc_obj_Type.tp_name =
"dealloc_obj";
848 dealloc_obj_Type.tp_basicsize =
sizeof(dealloc_obj);
849 dealloc_obj_Type.tp_dealloc = dealloc_obj_dealloc;
850 dealloc_obj_Type.tp_flags = Py_TPFLAGS_DEFAULT;
852 if (PyType_Ready(&dealloc_obj_Type) < 0) {
856 dob = (dealloc_obj *)dealloc_obj_Type.tp_alloc(&dealloc_obj_Type, 0);
857 dob->mod = bpy_proxy;
858 PyModule_AddObject(bpy_proxy,
"__file__", (PyObject *)dob);
863 static void bpy_module_free(
void *
UNUSED(
mod))
875 const char *kwlist[] = {
876 "False",
"None",
"True",
"and",
"as",
"assert",
"async",
"await",
"break",
877 "class",
"continue",
"def",
"del",
"elif",
"else",
"except",
"finally",
"for",
878 "from",
"global",
"if",
"import",
"in",
"is",
"lambda",
"nonlocal",
"not",
879 "or",
"pass",
"raise",
"return",
"try",
"while",
"with",
"yield",
NULL,
882 for (
int i = 0; kwlist[i]; i++) {
PyObject * AUD_initPython(void)
const char * BKE_appdir_folder_id(int folder_id, const char *subfolder)
const char * BKE_appdir_program_path(void)
bool BKE_appdir_program_python_search(char *fullpath, size_t fullpath_len, int version_major, int version_minor)
void * CTX_py_dict_get(const bContext *C)
@ CTX_DATA_TYPE_COLLECTION
void CTX_data_type_set(struct bContextDataResult *result, short type)
void CTX_data_list_add_ptr(bContextDataResult *result, const PointerRNA *ptr)
void CTX_data_pointer_set_ptr(bContextDataResult *result, const PointerRNA *ptr)
void CTX_wm_operator_poll_msg_clear(struct bContext *C)
struct Main * CTX_data_main(const bContext *C)
@ G_FLAG_SCRIPT_AUTOEXEC_FAIL_QUIET
@ G_FLAG_SCRIPT_AUTOEXEC_FAIL
const char * BKE_main_blendfile_path(const struct Main *bmain) ATTR_NONNULL()
bool text_check_identifier_nodigit(char ch)
bool text_check_identifier(char ch)
File and directory operations.
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
bool BLI_path_abs_from_cwd(char *path, size_t maxlen) ATTR_NONNULL()
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_NONNULL()
size_t BLI_snprintf(char *__restrict dst, size_t maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
int BLI_thread_is_main(void)
void BPY_driver_exit(void)
void BPY_driver_reset(void)
void BPY_app_handlers_reset(bool do_all)
struct CLG_LogRef * BPY_LOG_RNA
struct CLG_LogRef * BPY_LOG_CONTEXT
bool bool BPY_run_text(struct bContext *C, struct Text *text, struct ReportList *reports, bool do_jump) ATTR_NONNULL(1
bool BPY_run_string_eval(struct bContext *C, const char *imports[], const char *expr)
void * CCL_python_module_init(void)
#define CLOG_INFO(clg_ref, level,...)
Read Guarded memory(de)allocation.
Platform independent time functions.
PyObject * BPyInit_bgl(void)
PyMODINIT_FUNC BPyInit_bl_math(void)
PyObject * BPyInit_blf(void)
PyObject * BPyInit_bmesh(void)
PyObject * BPyInit_bmesh_geometry(void)
PyObject * BPyInit_bmesh_types(void)
PyObject * BPyInit_bmesh_utils(void)
void BPy_init_modules(struct bContext *C)
PyObject * bpy_package_py
void BPY_atexit_register(void)
struct CLG_LogRef * BPY_LOG_INTERFACE
void BPY_atexit_unregister(void)
void BPY_app_translations_end(void)
int text_check_identifier_nodigit_unicode(const uint ch)
bContext * BPY_context_get(void)
bool BPY_string_is_keyword(const char *str)
void BPY_context_dict_clear_members_array(void **dict_p, void *dict_orig, const char *context_members[], uint context_members_len)
int text_check_identifier_unicode(const uint ch)
int BPY_context_member_get(bContext *C, const char *member, bContextDataResult *result)
void bpy_context_set(bContext *C, PyGILState_STATE *gilstate)
void BPY_context_set(bContext *C)
void BPY_DECREF_RNA_INVALIDATE(void *pyob_ptr)
static void pystatus_exit_on_error(PyStatus status)
void BPY_python_backtrace(FILE *fp)
void BPY_python_use_system_env(void)
static void bpy_context_end(bContext *C)
void BPY_modules_update(void)
CLG_LOGREF_DECLARE_GLOBAL(BPY_LOG_CONTEXT, "bpy.context")
void BPY_python_reset(bContext *C)
static bool py_use_system_env
void BPY_context_update(bContext *C)
void BPY_python_end(void)
static struct _inittab bpy_internal_modules[]
void BPY_text_free_code(Text *text)
void bpy_context_clear(bContext *UNUSED(C), const PyGILState_STATE *gilstate)
void BPY_modules_load_user(bContext *C)
void BPY_python_start(bContext *C, int argc, const char **argv)
void BPY_DECREF(void *pyob_ptr)
void bpy_intern_string_init(void)
void bpy_intern_string_exit(void)
PyObject * BPyInit__bpy_path(void)
void BPY_rna_props_clear_all(void)
void pyrna_invalidate(BPy_DummyPointerRNA *self)
void pyrna_alloc_types(void)
void pyrna_free_types(void)
void BPY_update_rna_module(void)
PyObject * BPY_rna_module(void)
PyObject * BPY_rna_types(void)
BPy_StructRNA * bpy_context_module
#define BPy_StructRNA_Check(v)
SyclQueue void void size_t num_bytes void
PyObject * BPyInit_gpu(void)
PyObject * BPyInit_idprop(void)
PyObject * BPyInit_imbuf(void)
void *(* MEM_callocN)(size_t len, const char *str)
PyObject * Manta_initPython(void)
PyMODINIT_FUNC PyInit_mathutils(void)
PyMODINIT_FUNC PyInit_mathutils_geometry(void)
PyMODINIT_FUNC PyInit_mathutils_kdtree(void)
PyMODINIT_FUNC PyInit_mathutils_noise(void)
bool PyC_IsInterpreterActive(void)
PyObject_HEAD PointerRNA ptr
double PIL_check_seconds_timer(void)
ccl_device_inline int mod(int x, int m)