28 #include <frameobject.h>
70 #include "../generic/py_capi_utils.h"
73 #include "../bmesh/bmesh_py_api.h"
74 #include "../generic/bgl.h"
75 #include "../generic/bl_math_py_api.h"
76 #include "../generic/blf_py_api.h"
77 #include "../generic/idprop_py_api.h"
78 #include "../generic/imbuf_py_api.h"
79 #include "../gpu/gpu_py_api.h"
80 #include "../mathutils/mathutils.h"
100 static int bpy_timer_count = 0;
101 static double bpy_timer;
102 static double bpy_timer_run;
103 static double bpy_timer_run_tot;
125 *gilstate = PyGILState_Ensure();
132 if (bpy_timer_count == 0) {
135 bpy_timer_run = bpy_timer_run_tot = 0.0;
150 PyGILState_Release(*gilstate);
154 fprintf(stderr,
"ERROR: Python context internal state bug. this should not happen!\n");
182 const char *context_members[],
183 uint context_members_len)
185 PyGILState_STATE gilstate;
189 gilstate = PyGILState_Ensure();
193 if (*dict_p == dict_orig) {
194 *dict_p = PyDict_Copy(dict_orig);
197 PyObject *dict = *dict_p;
202 for (
uint i = 0; i < context_members_len; i++) {
203 PyObject *key = PyUnicode_FromString(context_members[i]);
204 PyObject *item = _PyDict_Pop(dict, key, Py_None);
210 PyGILState_Release(gilstate);
217 PyGILState_STATE gilstate;
221 gilstate = PyGILState_Ensure();
224 Py_DECREF((PyObject *)text->
compiled);
228 PyGILState_Release(gilstate);
239 PyObject *
mod = PyImport_ImportModuleLevel(
"bpy",
NULL,
NULL,
NULL, 0);
263 #ifdef WITH_AUDASPACE
270 static PyObject *CCL_initPython(
void)
297 #ifdef WITH_AUDASPACE
301 {
"_cycles", CCL_initPython},
308 #ifndef WITH_PYTHON_MODULE
320 if (
UNLIKELY(PyStatus_Exception(status))) {
321 fputs(
"Internal error initializing Python!\n", stderr);
323 Py_ExitStatusException(status);
331 #ifndef WITH_PYTHON_MODULE
335 PyPreConfig preconfig;
339 PyPreConfig_InitPythonConfig(&preconfig);
345 PyPreConfig_InitIsolatedConfig(&preconfig);
361 preconfig.utf8_mode =
true;
366 status = Py_PreInitialize(&preconfig);
377 bool has_python_executable =
false;
379 PyConfig_InitPythonConfig(&config);
383 config.pathconfig_warnings = 0;
389 config.parse_argv = 0;
390 status = PyConfig_SetBytesArgv(&config, argc, (
char *
const *)argv);
400 status = PyConfig_SetBytesString(&config, &config.program_name, program_path);
409 program_path,
sizeof(program_path), PY_MAJOR_VERSION, PY_MINOR_VERSION)) {
410 status = PyConfig_SetBytesString(&config, &config.executable, program_path);
412 has_python_executable =
true;
417 "Unable to find the python binary, "
418 "the multiprocessing module may not be functional!\n");
425 if (py_path_bundle !=
NULL) {
431 if (strchr(py_path_bundle,
':')) {
433 "Warning! Blender application is located in a path containing ':' or '/' chars\n"
434 "This may make python import function fail\n");
438 status = PyConfig_SetBytesString(&config, &config.home, py_path_bundle);
443 # if defined(__APPLE__) || defined(_WIN32)
445 "Bundled Python not found and is expected on this platform "
446 "(the 'install' target may have not been built)\n");
452 status = Py_InitializeFromConfig(&config);
455 if (!has_python_executable) {
456 PySys_SetObject(
"executable", Py_None);
463 Py_DECREF(PyImport_ImportModule(
"threading"));
477 #ifdef WITH_PYTHON_MODULE
480 struct _inittab *inittab_item;
481 PyObject *sys_modules = PyImport_GetModuleDict();
484 PyObject *
mod = inittab_item->initfunc();
486 PyDict_SetItemString(sys_modules, inittab_item->name,
mod);
502 #ifndef WITH_PYTHON_MODULE
507 PyEval_ReleaseThread(PyGILState_GetThisThreadState());
511 #ifdef WITH_PYTHON_MODULE
514 (
const char *[]){
"atexit",
"addon_utils",
NULL},
515 "atexit.register(addon_utils.disable_all)");
522 PyGILState_STATE gilstate;
525 gilstate = PyGILState_Ensure();
540 #ifndef WITH_PYTHON_MODULE
547 PyGILState_Release(gilstate);
554 printf(
"*bpy stats* - ");
555 printf(
"tot exec: %d, ", bpy_timer_count);
556 printf(
"tot run: %.4fsec, ", bpy_timer_run_tot);
557 if (bpy_timer_count > 0) {
558 printf(
"average run: %.6fsec, ", (bpy_timer_run_tot / bpy_timer_count));
561 if (bpy_timer > 0.0) {
562 printf(
"tot usage %.4f%%", (bpy_timer_run_tot / bpy_timer) * 100.0);
576 G.autoexec_fail[0] =
'\0';
591 fputs(
"\n# Python backtrace\n", fp);
592 PyThreadState *tstate = PyGILState_GetThisThreadState();
593 if (tstate !=
NULL && tstate->frame !=
NULL) {
594 PyFrameObject *frame = tstate->frame;
596 const int line = PyCode_Addr2Line(frame->f_code, frame->f_lasti);
597 const char *filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
598 const char *funcname = PyUnicode_AsUTF8(frame->f_code->co_name);
599 fprintf(fp,
" File \"%s\", line %d in %s\n", filename, line, funcname);
600 }
while ((frame = frame->f_back));
606 const PyGILState_STATE gilstate = PyGILState_Ensure();
607 Py_DECREF((PyObject *)pyob_ptr);
608 PyGILState_Release(gilstate);
613 const PyGILState_STATE gilstate = PyGILState_Ensure();
614 const bool do_invalidate = (Py_REFCNT((PyObject *)pyob_ptr) > 1);
615 Py_DECREF((PyObject *)pyob_ptr);
619 PyGILState_Release(gilstate);
624 PyGILState_STATE gilstate;
648 printf(
"scripts disabled for \"%s\", skipping '%s'\n",
668 PyGILState_STATE gilstate;
677 gilstate = PyGILState_Ensure();
681 item = PyDict_GetItemString(pyctx, member);
686 else if (item == Py_None) {
697 else if (PySequence_Check(item)) {
698 PyObject *seq_fast = PySequence_Fast(item,
"bpy_context_get sequence conversion");
699 if (seq_fast ==
NULL) {
704 const int len = PySequence_Fast_GET_SIZE(seq_fast);
705 PyObject **seq_fast_items = PySequence_Fast_ITEMS(seq_fast);
708 for (i = 0; i <
len; i++) {
709 PyObject *list_item = seq_fast_items[i];
724 "'%s' list item not a valid type in sequence type '%s'",
726 Py_TYPE(item)->tp_name);
748 PyGILState_Release(gilstate);
754 #ifdef WITH_PYTHON_MODULE
757 static void bpy_module_free(
void *
mod);
760 extern int main_python_enter(
int argc,
const char **argv);
761 extern void main_python_exit(
void);
763 static struct PyModuleDef bpy_proxy_def = {
764 PyModuleDef_HEAD_INIT,
782 static void bpy_module_delay_init(PyObject *bpy_proxy)
788 PyObject *filename_obj = PyModule_GetFilenameObject(bpy_proxy);
790 const char *filename_rel = PyUnicode_AsUTF8(filename_obj);
791 char filename_abs[1024];
793 BLI_strncpy(filename_abs, filename_rel,
sizeof(filename_abs));
795 Py_DECREF(filename_obj);
797 argv[0] = filename_abs;
802 main_python_enter(argc, argv);
805 PyDict_Update(PyModule_GetDict(bpy_proxy), PyModule_GetDict(
bpy_package_py));
808 static void dealloc_obj_dealloc(PyObject *
self);
810 static PyTypeObject dealloc_obj_Type;
813 static void dealloc_obj_dealloc(PyObject *
self)
815 bpy_module_delay_init(((dealloc_obj *)
self)->
mod);
818 dealloc_obj_Type.tp_free(
self);
821 PyMODINIT_FUNC PyInit_bpy(
void);
823 PyMODINIT_FUNC PyInit_bpy(
void)
825 PyObject *bpy_proxy = PyModule_Create(&bpy_proxy_def);
845 dealloc_obj_Type.tp_name =
"dealloc_obj";
846 dealloc_obj_Type.tp_basicsize =
sizeof(dealloc_obj);
847 dealloc_obj_Type.tp_dealloc = dealloc_obj_dealloc;
848 dealloc_obj_Type.tp_flags = Py_TPFLAGS_DEFAULT;
850 if (PyType_Ready(&dealloc_obj_Type) < 0) {
854 dob = (dealloc_obj *)dealloc_obj_Type.tp_alloc(&dealloc_obj_Type, 0);
855 dob->mod = bpy_proxy;
856 PyModule_AddObject(bpy_proxy,
"__file__", (PyObject *)dob);
861 static void bpy_module_free(
void *
UNUSED(
mod))
876 const char *kwlist[] = {
877 "False",
"None",
"True",
"and",
"as",
"assert",
"async",
"await",
"break",
878 "class",
"continue",
"def",
"del",
"elif",
"else",
"except",
"finally",
"for",
879 "from",
"global",
"if",
"import",
"in",
"is",
"lambda",
"nonlocal",
"not",
880 "or",
"pass",
"raise",
"return",
"try",
"while",
"with",
"yield",
NULL,
883 for (
int i = 0; kwlist[i]; i++) {
PyObject * AUD_initPython(void)
bool BKE_appdir_program_python_search(char *fullpath, const size_t fullpath_len, const int version_major, const int version_minor)
const char * BKE_appdir_folder_id(const int folder_id, const char *subfolder)
const char * BKE_appdir_program_path(void)
void CTX_data_pointer_set(bContextDataResult *result, struct ID *id, StructRNA *type, void *data)
void * CTX_py_dict_get(const bContext *C)
@ CTX_DATA_TYPE_COLLECTION
void CTX_data_list_add(bContextDataResult *result, struct ID *id, StructRNA *type, void *data)
void CTX_data_type_set(struct bContextDataResult *result, short type)
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(const char ch)
bool text_check_identifier_nodigit(const 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, const size_t maxlen) ATTR_NONNULL()
bool BLI_path_extension_check(const char *str, const char *ext) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT
size_t BLI_snprintf(char *__restrict dst, size_t maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy) ATTR_NONNULL()
int BLI_thread_is_main(void)
void BPY_app_handlers_reset(const short do_all)
void BPY_driver_reset(void)
struct CLG_LogRef * BPY_LOG_RNA
struct CLG_LogRef * BPY_LOG_CONTEXT
bool BPY_run_string_eval(struct bContext *C, const char *imports[], const char *expr)
bool BPY_run_text(struct bContext *C, struct Text *text, struct ReportList *reports, const bool do_jump)
#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)
CCL_NAMESPACE_END void * CCL_python_module_init()
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)
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)
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)