Blender V5.3
bpy_interface.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
12
13#include <Python.h>
14#include <frameobject.h>
15#include <optional>
16
17#ifdef WITH_PYTHON_MODULE
18# include "pylifecycle.h" /* For `Py_Version`. */
19#endif
20#include "../generic/python_compat.hh" /* IWYU pragma: keep. */
21
22#include "CLG_log.h"
23
24#include "BLI_path_utils.hh"
25#include "BLI_string.hh"
26#include "BLI_string_utf8.hh"
27#include "BLI_threads.hh"
28#include "BLI_utildefines.hh"
29#ifdef WITH_PYTHON_MODULE
30# include "BLI_string.hh"
31#endif
32
33#include "BLT_translation.hh"
34
35#include "RNA_types.hh"
36
37#include "bpy.hh"
38#include "bpy_audaspace.hh"
39#include "bpy_capi_utils.hh"
40#include "bpy_intern_string.hh"
41#include "bpy_path.hh"
42#include "bpy_props.hh"
43#include "bpy_rna.hh"
44
46
47#include "DNA_freestyle_types.h"
48#include "DNA_layer_types.h"
49#include "DNA_scene_types.h"
50#include "DNA_text_types.h"
51
52#include "BKE_appdir.hh"
53#include "BKE_context.hh"
54#include "BKE_global.hh" /* Only for script checking. */
55#include "BKE_main.hh"
56#include "BKE_text.h"
57
58#ifdef WITH_CYCLES
59# include "CCL_api.h"
60#endif
61
62#include "BPY_extern.hh"
63#include "BPY_extern_python.hh"
64#include "BPY_extern_run.hh"
65
67
68/* `inittab` initialization functions. */
74#include "../gpu/gpu_py_api.hh"
76
77namespace blender {
78
79/* Logging types to use anywhere in the Python modules. */
80
83
85
86/* For internal use, when starting and ending Python scripts. */
87
88/* In case a Python script triggers another Python call,
89 * stop #bpy_context_clear from invalidating. */
90static int py_call_level = 0;
91
92/* Set by command line arguments before Python starts. */
93static bool py_use_system_env = false;
94static bool py_use_user_env = false;
95
96// #define TIME_PY_RUN /* Simple python tests. prints on exit. */
97
98#ifdef TIME_PY_RUN
99# include "BLI_time.hh"
100static int bpy_timer_count = 0;
102static double bpy_timer;
104static double bpy_timer_run;
106static double bpy_timer_run_tot;
107#endif
108
110{
111 /* Don't do this from a non-main (e.g. render) thread, it can cause a race
112 * condition on `C->data.recursion`. Ideal solution would be to disable
113 * context entirely from non-main threads, but that's more complicated. */
114 if (!BLI_thread_is_main()) {
115 return;
116 }
117
119
120 /* Can give really bad results if this isn't here. */
122}
123
131 PyGILState_STATE *gilstate,
132 const bool allow_null_context)
133{
134 bool context_set = false;
136
137 if (gilstate) {
138 *gilstate = PyGILState_Ensure();
139 }
140
141 if (py_call_level == 1) {
142 if (!allow_null_context) {
143 BLI_assert_msg(C != nullptr, "bpy: Trying to set invalid nullptr context");
144 }
145
147 context_set = true;
148
149 if (C != nullptr) {
151 }
152
153#ifdef TIME_PY_RUN
154 if (bpy_timer_count == 0) {
155 /* Record time from the beginning. */
156 bpy_timer = BLI_time_now_seconds();
157 bpy_timer_run = bpy_timer_run_tot = 0.0;
158 }
159 bpy_timer_run = BLI_time_now_seconds();
160
161 bpy_timer_count++;
162#endif
163 }
164
165 return context_set;
166}
167
168bool bpy_context_set(bContext *C, PyGILState_STATE *gilstate)
169{
170 return bpy_context_set_ex(C, gilstate, false);
171}
172
173bool bpy_context_set_allow_null(bContext *C, PyGILState_STATE *gilstate)
174{
175 return bpy_context_set_ex(C, gilstate, true);
176}
177
178void bpy_context_clear(bContext *C, const PyGILState_STATE *gilstate)
179{
181
182 if (gilstate) {
183 PyGILState_Release(*gilstate);
184 }
185
186 if (py_call_level < 0) {
187 fprintf(stderr, "ERROR: Python context internal state bug. this should not happen!\n");
188 }
189 else if (py_call_level == 0) {
190 /* NOTE: Unfortunately calling classes currently won't store the context.
191 * Can't set nullptr because of this - but this is very unreliable still. */
192#if 0
193 BPY_context_set(nullptr);
194#endif
195
196 /* See previous comment regarding null check in #bpy_context_set. */
197 if (C != nullptr) {
199 }
200
201#ifdef TIME_PY_RUN
202 bpy_timer_run_tot += BLI_time_now_seconds() - bpy_timer_run;
203 bpy_timer_count++;
204#endif
205 }
206}
207
209{
210 if (C == nullptr) [[unlikely]] {
211 return;
212 }
214}
215
217 void *dict_orig,
218 const char *context_members[],
219 uint context_members_len)
220{
221 PyGILState_STATE gilstate;
222 const bool use_gil = !PyC_IsInterpreterActive();
223 if (use_gil) {
224 gilstate = PyGILState_Ensure();
225 }
226
227 /* Copy on write. */
228 if (*dict_p == dict_orig) {
229 *dict_p = PyDict_Copy(static_cast<PyObject *>(dict_orig));
230 }
231
232 PyObject *dict = static_cast<PyObject *>(*dict_p);
233 BLI_assert(PyDict_Check(dict));
234
235 /* Use #PyDict_Pop instead of #PyDict_DelItemString to avoid setting the exception,
236 * while supported it's good to avoid for low level functions like this that run often. */
237 for (uint i = 0; i < context_members_len; i++) {
238 PyObject *key = PyUnicode_FromString(context_members[i]);
239 PyObject *item;
240
241 switch (PyDict_Pop(dict, key, &item)) {
242 case 1: {
243 Py_DECREF(item);
244 break;
245 }
246 case -1: {
247 /* Not expected, but allow for an error. */
248 BLI_assert(false);
249 PyErr_Clear();
250 break;
251 }
252 }
253
254 Py_DECREF(key);
255 }
256
257 if (use_gil) {
258 PyGILState_Release(gilstate);
259 }
260}
261
263{
264 if (text->compiled) {
265 PyGILState_STATE gilstate;
266 const bool use_gil = !PyC_IsInterpreterActive();
267
268 if (use_gil) {
269 gilstate = PyGILState_Ensure();
270 }
271
272 Py_DECREF((PyObject *)text->compiled);
273 text->compiled = nullptr;
274
275 if (use_gil) {
276 PyGILState_Release(gilstate);
277 }
278 }
279}
280
282{
283 /* Correct but slow, this runs all the time operator poll, panel draw etc
284 * (100's of time a second). */
285#if 0
286 PyObject *mod = PyImport_ImportModuleLevel("bpy", nullptr, nullptr, nullptr, 0);
287 PyModule_AddObject(mod, "data", BPY_rna_module());
288 PyModule_AddObject(mod, "types", BPY_rna_types()); /* This does not need updating. */
289#endif
290
291 /* Refreshes the main struct. */
293}
294
296{
297 return static_cast<bContext *>(bpy_context_module->ptr->data);
298}
299
301{
302 bpy_context_module->ptr->data = static_cast<void *>(C);
303}
304
305#ifdef WITH_FLUID
306/* Defined in `manta` module. */
307extern "C" PyObject *Manta_initPython();
308#endif
309
310#ifdef WITH_CYCLES
311/* Defined in `cycles` module. */
312static PyObject *CCL_initPython()
313{
314 return (PyObject *)CCL_python_module_init();
315}
316#endif
317
318#ifdef WITH_HYDRA
319/* Defined in `render_hydra` module. */
320PyObject *BPyInit_hydra();
321#endif
322
323static _inittab bpy_internal_modules[] = {
324 {"mathutils", PyInit_mathutils},
325#if 0
326 {"mathutils.geometry", PyInit_mathutils_geometry},
327 {"mathutils.noise", PyInit_mathutils_noise},
328 {"mathutils.kdtree", PyInit_mathutils_kdtree},
329#endif
330 {"_bpy_path", BPyInit__bpy_path},
331 {"blf", BPyInit_blf},
332 {"bl_math", BPyInit_bl_math},
333 {"imbuf", BPyInit_imbuf},
334 {"bmesh", BPyInit_bmesh},
335#if 0
336 {"bmesh.types", BPyInit_bmesh_types},
337 {"bmesh.utils", BPyInit_bmesh_utils},
338 {"bmesh.utils", BPyInit_bmesh_geometry},
339#endif
340#ifdef WITH_FLUID
341 {"manta", Manta_initPython},
342#endif
343#ifdef WITH_AUDASPACE_PY
344 {"aud", BPyInit_audaspace},
345#endif
346#ifdef WITH_CYCLES
347 {"_cycles", CCL_initPython},
348#endif
349 {"gpu", BPyInit_gpu},
350 {"idprop", BPyInit_idprop},
351#ifdef WITH_HYDRA
352 {"_bpy_hydra", BPyInit_hydra},
353#endif
354 {nullptr, nullptr},
355};
356
357#ifndef WITH_PYTHON_MODULE
367static void pystatus_exit_on_error(const PyStatus &status)
368{
369 if (PyStatus_Exception(status)) [[unlikely]] {
370 fputs("Internal error initializing Python!\n", stderr);
371 /* This calls `exit`. */
372 Py_ExitStatusException(status);
373 }
374}
375#endif
376
377void BPY_python_start(bContext *C, int argc, const char **argv)
378{
379#ifndef WITH_PYTHON_MODULE
380 BLI_assert_msg(Py_IsInitialized() == 0, "Python has already been initialized");
381
382 /* It's necessary to disable isolation so `user-site-packages` can be used.
383 * Leave everything else disabled (mainly environment variables). */
384 const std::optional<bool> isolated_override = ((py_use_system_env == false) &&
385 (py_use_user_env == true)) ?
386 std::optional(false) :
387 std::nullopt;
388
389 /* #PyPreConfig (early-configuration). */
390 {
391 PyPreConfig preconfig;
392 PyStatus status;
393
394 /* To narrow down reports where the systems Python is inexplicably used, see: #98131. */
396 "Initializing %s support for the systems Python environment such as 'PYTHONPATH', "
397 "%s support for the user-site directory.",
398 py_use_system_env ? "*with*" : "*without*",
399 py_use_user_env ? "*with*" : "*without*");
400
401 if (py_use_system_env) {
402 PyPreConfig_InitPythonConfig(&preconfig);
403 }
404 else {
405 /* Only use the systems environment variables and site when explicitly requested.
406 * Since an incorrect 'PYTHONPATH' causes difficult to debug errors, see: #72807.
407 * An alternative to setting `preconfig.use_environment = 0` */
408 PyPreConfig_InitIsolatedConfig(&preconfig);
409 }
410
411 if (isolated_override) {
412 preconfig.isolated = isolated_override.value();
413 }
414
415 /* Force UTF8 on all platforms, since this is what's used for Blender's internal strings,
416 * providing consistent encoding behavior across all Blender installations.
417 *
418 * This also uses the `surrogateescape` error handler ensures any unexpected bytes are escaped
419 * instead of raising an error.
420 *
421 * Without this `sys.getfilesystemencoding()` and `sys.stdout` for example may be set to ASCII
422 * or some other encoding - where printing some UTF8 values will raise an error.
423 *
424 * This can cause scripts to fail entirely on some systems.
425 *
426 * This assignment is the equivalent of enabling the `PYTHONUTF8` environment variable.
427 * See `PEP-540` for details on exactly what this changes. */
428 preconfig.utf8_mode = true;
429
430 /* Note that there is no reason to call #Py_PreInitializeFromBytesArgs here
431 * as this is only used so that command line arguments can be handled by Python itself,
432 * not for setting `sys.argv` (handled below). */
433 status = Py_PreInitialize(&preconfig);
435 }
436
437 /* Must run before python initializes, but after #PyPreConfig. */
438 PyImport_ExtendInittab(bpy_internal_modules);
439
440 /* #PyConfig (initialize Python). */
441 {
442 PyConfig config;
443 PyStatus status;
444 bool has_python_executable = false;
445
446 if (py_use_system_env) {
447 PyConfig_InitPythonConfig(&config);
448
449 BLI_assert(config.install_signal_handlers);
450 }
451 else {
452 PyConfig_InitIsolatedConfig(&config);
453 /* Python's isolated config disables its own signal overrides.
454 * While it makes sense not to interfering with other components of the process,
455 * the signal handlers are needed for Python's own error handling to work properly.
456 * Without this a `SIGPIPE` signal will crash Blender, see: #129657. */
457 config.install_signal_handlers = 1;
458 }
459
460 if (isolated_override) {
461 config.isolated = isolated_override.value();
462 }
463
464 /* Suppress error messages when calculating the module search path.
465 * While harmless, it's noisy. */
466 config.pathconfig_warnings = 0;
467
468 {
469 /* NOTE: running scripts directly uses the default behavior *but* the default
470 * warning filter doesn't show warnings form module besides `__main__`.
471 * Use the default behavior unless debugging Python. See: !139487. */
472 bool show_python_warnings = false;
473
474# ifdef NDEBUG
475 show_python_warnings = G.debug & G_DEBUG_PYTHON;
476# else
477 /* Always show warnings for debug builds so developers are made aware
478 * of outdated API use before any breakages occur. */
479 show_python_warnings = true;
480# endif
481
482 if (show_python_warnings) {
483 /* Don't overwrite warning settings if they have been set by the environment. */
484 if (!(py_use_system_env && BLI_getenv("PYTHONWARNINGS"))) {
485 /* Confusingly `default` is not the default.
486 * Setting to `default` without any module names shows warnings for all modules.
487 * Useful for development since most functionality occurs outside of `__main__`. */
488 PyWideStringList_Append(&config.warnoptions, L"default");
489 }
490 }
491 }
492
493 /* By default, use an isolated environment unless the user passes in:
494 * `--python-use-user-env`.
495 *
496 * This is a somewhat contentious issue since Python developers may want to access
497 * modules from their user path. The problem with this is it's possible for this path
498 * to contain modules that override Blender's bundled Python modules and user modules
499 * may not be binary compatible with Blender.
500 * So it's possible for the existence of user modules to "break" Blender.
501 * Given this situation, default to an isolated environment with command line arguments
502 * to enable *user* and *system* Python settings, see: #107137.
503 *
504 * - See also related reports about users site packages failing to load, see: #104000, #106963.
505 * - See `site.USER_BASE` for the location PIP will install user packages
506 * this could be customized if we want to support a separate "blender-user" user path.
507 */
508 config.user_site_directory = py_use_user_env;
509
510 /* While `sys.argv` is set, we don't want Python to interpret it. */
511 config.parse_argv = 0;
512 status = PyConfig_SetBytesArgv(&config, argc, const_cast<char *const *>(argv));
514
515 /* Needed for Python's initialization for portable Python installations.
516 * We could use #Py_SetPath, but this overrides Python's internal logic
517 * for calculating its own module search paths.
518 *
519 * `sys.executable` is overwritten after initialization to the Python binary. */
520 {
521 const char *program_path = BKE_appdir_program_path();
522 status = PyConfig_SetBytesString(&config, &config.program_name, program_path);
524 }
525
526 /* Setting the program name is important so the 'multiprocessing' module
527 * can launch new Python instances. */
528 {
529 char program_path[FILE_MAX];
531 program_path, sizeof(program_path), PY_MAJOR_VERSION, PY_MINOR_VERSION))
532 {
533 status = PyConfig_SetBytesString(&config, &config.executable, program_path);
535 has_python_executable = true;
536 }
537 else {
538 /* Set to `sys.executable = None` below (we can't do before Python is initialized). */
539 fprintf(stderr,
540 "Unable to find the Python binary, "
541 "the multiprocessing module may not be functional!\n");
542 }
543 }
544
545 /* Allow to use our own included Python. `py_path_bundle` may be nullptr. */
546 {
547 const std::optional<std::string> py_path_bundle = BKE_appdir_folder_id(BLENDER_SYSTEM_PYTHON,
548 nullptr);
549 if (py_path_bundle.has_value()) {
550
551# ifdef __APPLE__
552 /* Mac-OS allows file/directory names to contain `:` character
553 * (represented as `/` in the Finder) but current Python lib (as of release 3.1.1)
554 * doesn't handle these correctly. */
555 if (strchr(py_path_bundle->c_str(), ':')) {
556 fprintf(stderr,
557 "Warning! Blender application is located in a path containing ':' or '/' chars\n"
558 "This may make Python import function fail\n");
559 }
560# endif /* __APPLE__ */
561
562 status = PyConfig_SetBytesString(&config, &config.home, py_path_bundle->c_str());
564
565# ifdef PYTHON_SSL_CERT_FILE
566 /* Point to the portable SSL certificate to support HTTPS access, see: #102300. */
567 const char *ssl_cert_file_env = "SSL_CERT_FILE";
568 if (BLI_getenv(ssl_cert_file_env) == nullptr) {
569 const char *ssl_cert_file_suffix = PYTHON_SSL_CERT_FILE;
570 char ssl_cert_file[FILE_MAX];
572 ssl_cert_file, sizeof(ssl_cert_file), py_path_bundle->c_str(), ssl_cert_file_suffix);
573 BLI_setenv(ssl_cert_file_env, ssl_cert_file);
574 }
575# endif /* PYTHON_SSL_CERT_FILE */
576 }
577 else {
578/* Common enough to use the system Python on Linux/Unix, warn on other systems. */
579# if defined(__APPLE__) || defined(_WIN32)
580 fprintf(stderr,
581 "Bundled Python not found and is expected on this platform "
582 "(the 'install' target may have not been built)\n");
583# endif
584 }
585 }
586
587 /* Initialize Python (also acquires lock). */
588 status = Py_InitializeFromConfig(&config);
589 PyConfig_Clear(&config);
590
592
593 if (!has_python_executable) {
594 PySys_SetObject("executable", Py_None);
595 }
596 }
597
598# ifdef WITH_FLUID
599 /* Required to prevent assertion error, see:
600 * https://stackoverflow.com/questions/27844676 */
601 Py_DECREF(PyImport_ImportModule("threading"));
602# endif
603
604#else /* WITH_PYTHON_MODULE */
605 (void)argc;
606 (void)argv;
607
608 /* NOTE(ideasman42): unfortunately the `inittab` can only be used
609 * before Python has been initialized.
610 * When built as a Python module, Python will have been initialized
611 * and using the `inittab` isn't supported.
612 * So it's necessary to load all modules as soon as `bpy` is imported. */
613 // PyImport_ExtendInittab(bpy_internal_modules);
614
615#endif /* WITH_PYTHON_MODULE */
616
618
619#ifdef WITH_PYTHON_MODULE
620 {
621 /* Manually load all modules */
622 struct _inittab *inittab_item;
623 PyObject *sys_modules = PyImport_GetModuleDict();
624
625 for (inittab_item = bpy_internal_modules; inittab_item->name; inittab_item++) {
626 PyObject *mod = inittab_item->initfunc();
627 if (mod) {
628 PyDict_SetItemString(sys_modules, inittab_item->name, mod);
629 }
630 else {
631 PyErr_Print();
632 }
633 // Py_DECREF(mod); /* Ideally would decref, but in this case we never want to free. */
634 }
635 }
636#endif
637
638 /* Run first, initializes RNA types. */
639 BPY_rna_init();
640
641 /* Defines `bpy.*` and lets us import it. */
643
645
646#ifndef WITH_PYTHON_MODULE
647 /* Python module runs `atexit` when `bpy` is freed. */
648 BPY_atexit_register(); /* This can initialize any time. */
649
650 /* Free the lock acquired (implicitly) when Python is initialized. */
651 PyEval_ReleaseThread(PyGILState_GetThisThreadState());
652
653#endif
654
655#ifdef WITH_PYTHON_MODULE
656 /* Disable all add-ons at exit, not essential, it just avoids resource leaks, see #71362. */
657 const char *imports[] = {"atexit", "addon_utils", nullptr};
658 BPY_run_string_eval(C, imports, "atexit.register(addon_utils.disable_all)");
659#endif
660}
661
662void BPY_python_end(const bool do_python_exit)
663{
664#ifndef WITH_PYTHON_MODULE
665 BLI_assert_msg(Py_IsInitialized() != 0, "Python must be initialized");
666#endif
667
668 /* Finalizing, no need to grab the state, except when we are a module. */
669 PyGILState_STATE gilstate = PyGILState_Ensure();
670
671 /* Frees the Python-driver name-space & cached data. */
673
674 /* Clear Python values in the context so freeing the context after Python exits doesn't crash. */
676
677 /* Decrement user counts of all callback functions. */
679
680 /* Free other Python data. */
681 RNA_bpy_exit();
682
683 BPY_rna_exit();
684
685 /* Clear all Python data from structs. */
686
688
689 /* `bpy.app` modules that need cleanup. */
691
692#ifndef WITH_PYTHON_MODULE
693 /* Without this we get recursive calls to #WM_exit_ex. */
695
696 if (do_python_exit) {
697 Py_Finalize();
698 }
699 (void)gilstate;
700#else
701 PyGILState_Release(gilstate);
702 (void)do_python_exit;
703#endif
704
705#ifdef TIME_PY_RUN
706 /* Measure time since Python started. */
707 bpy_timer = BLI_time_now_seconds() - bpy_timer;
708
709 printf("*bpy stats* - ");
710 printf("tot exec: %d, ", bpy_timer_count);
711 printf("tot run: %.4fsec, ", bpy_timer_run_tot);
712 if (bpy_timer_count > 0) {
713 printf("average run: %.6fsec, ", (bpy_timer_run_tot / bpy_timer_count));
714 }
715
716 if (bpy_timer > 0.0) {
717 printf("tot usage %.4f%%", (bpy_timer_run_tot / bpy_timer) * 100.0);
718 }
719
720 printf("\n");
721#endif
722}
723
725{
726 BLI_assert_msg(Py_IsInitialized() != 0, "Python must be initialized");
727
728 /* Unrelated security stuff. */
730 G.autoexec_fail[0] = '\0';
731
735}
736
738{
739 BLI_assert(!Py_IsInitialized());
740 py_use_system_env = true;
741
742 /* NOTE: it's debatable if enabling the system-environment should enable the user-environment.
743 *
744 * While in principle it's possible a developer wants to access user site-packages
745 * in an otherwise isolated environment. The intent with the system-environment was
746 * to disable all isolation, so Python developers have a convenient way to access
747 * the full Python environment.
748 *
749 * Having to pass in multiple arguments to achieve this goes against the original intention.
750 *
751 * If a developer wants to enable the system-environment and disable user-environment
752 * this can be achieved by running with the environment variable `PYTHONNOUSERSITE=1`
753 * along with the argument `--python-use-system-env`. */
754 py_use_user_env = true;
755}
756
758{
759 BLI_assert(!Py_IsInitialized());
760 py_use_user_env = true;
761}
762
767
769{
770 fputs("\n# Python backtrace\n", fp);
771
772 /* Can happen in rare cases. */
774 return;
775 }
776 PyFrameObject *frame = PyEval_GetFrame();
777 if (frame == nullptr) {
778 return;
779 }
780 /* The reference is borrowed, increase since #PyFrame_GetBack is *not* borrowed,
781 * and this simplifies handling reference counts in the loop. */
782 Py_INCREF(frame);
783 do {
784 PyCodeObject *code = PyFrame_GetCode(frame);
785 const int line = PyFrame_GetLineNumber(frame);
786 const char *filepath = PyUnicode_AsUTF8(code->co_filename);
787 const char *funcname = PyUnicode_AsUTF8(code->co_name);
788 fprintf(fp, " File \"%s\", line %d in %s\n", filepath, line, funcname);
789 Py_DECREF(code);
790 PyFrameObject *frame_next = PyFrame_GetBack(frame);
791 Py_DECREF(frame);
792 frame = frame_next;
793 } while (frame);
794}
795
796void BPY_DECREF(void *pyob_ptr)
797{
798 const PyGILState_STATE gilstate = PyGILState_Ensure();
799 Py_DECREF((PyObject *)pyob_ptr);
800 PyGILState_Release(gilstate);
801}
802
803void BPY_DECREF_RNA_INVALIDATE(void *pyob_ptr)
804{
805 const PyGILState_STATE gilstate = PyGILState_Ensure();
806 const bool do_invalidate = (Py_REFCNT((PyObject *)pyob_ptr) > 1);
807 Py_DECREF((PyObject *)pyob_ptr);
808 if (do_invalidate) {
809 pyrna_invalidate(static_cast<BPy_DummyPointerRNA *>(pyob_ptr));
810 }
811 PyGILState_Release(gilstate);
812}
813
815{
816 PyGILState_STATE gilstate;
817 Main *bmain = CTX_data_main(C);
818 Text *text;
819
820 /* Can happen on file load. */
821 if (bmain == nullptr) {
822 return;
823 }
824
825 /* Update pointers since this can run from a nested script on file load. */
826 if (py_call_level) {
828 }
829
830 bpy_context_set(C, &gilstate);
831
834 {
835 for (Scene &scene : bmain->scenes) {
836 for (ViewLayer &view_layer : scene.view_layers) {
837 for (const FreestyleModuleConfig &module_conf : view_layer.freestyle_config.modules) {
838 if (module_conf.script) {
840 SNPRINTF_UTF8(G.autoexec_fail,
841 RPT_("Freestyle style module '%s'"),
842 module_conf.script->id.name + 2);
843 }
844 }
845 }
846 }
847 }
848
849 for (text = bmain->texts.first(); text; text = static_cast<Text *>(text->id.next)) {
850 if (text->flags & TXT_ISSCRIPT) {
851 if (!(G.f & G_FLAG_SCRIPT_AUTOEXEC)) {
854 SNPRINTF_UTF8(G.autoexec_fail, RPT_("Text '%s'"), text->id.name + 2);
855
856 printf("scripts disabled for \"%s\", skipping '%s'\n",
858 text->id.name + 2);
859 }
860 }
861 else {
862 BPY_run_text(C, text, nullptr, false);
863
864 /* Check if the script loaded a new file. */
865 if (bmain != CTX_data_main(C)) {
866 break;
867 }
868 }
869 }
870 }
871
872 bpy_context_clear(C, &gilstate);
873}
874
876static void bpy_context_log_member_error(const bContext *C, const char *message)
877{
878 const bool use_logging_info = CLOG_CHECK(BKE_LOG_CONTEXT, CLG_LEVEL_INFO);
879 const bool use_logging_member = C && CTX_member_logging_get(C);
880 if (!(use_logging_info || use_logging_member)) {
881 return;
882 }
883
884 std::optional<std::string> python_location = BPY_python_current_file_and_line();
885 const char *location = python_location ? python_location->c_str() : "unknown:0";
886
887 if (use_logging_info) {
888 CLOG_INFO(BKE_LOG_CONTEXT, "%s: %s", location, message);
889 }
890 else if (use_logging_member) {
891 CLOG_AT_LEVEL_NOCHECK(BKE_LOG_CONTEXT, CLG_LEVEL_INFO, "%s: %s", location, message);
892 }
893 else {
895 }
896}
897
899{
900 PyGILState_STATE gilstate;
901 const bool use_gil = !PyC_IsInterpreterActive();
902 if (use_gil) {
903 gilstate = PyGILState_Ensure();
904 }
905
906 PyObject *pyctx;
907 PyObject *item;
908 PointerRNA *ptr = nullptr;
909 bool done = false;
910
911 pyctx = static_cast<PyObject *>(CTX_py_dict_get(C));
912 item = PyDict_GetItemString(pyctx, member);
913
914 if (item == nullptr) {
915 /* Pass. */
916 }
917 else if (item == Py_None) {
918 done = true;
919 }
920 else if (BPy_StructRNA_Check(item)) {
921 ptr = &reinterpret_cast<BPy_StructRNA *>(item)->ptr.value();
922
923 // result->ptr = ((BPy_StructRNA *)item)->ptr;
926 done = true;
927 }
928 else if (PySequence_Check(item)) {
929 PyObject *seq_fast = PySequence_Fast(item, "bpy_context_get sequence conversion");
930 if (seq_fast == nullptr) {
931 PyErr_Print();
932 }
933 else {
934 const int len = PySequence_Fast_GET_SIZE(seq_fast);
935 PyObject **seq_fast_items = PySequence_Fast_ITEMS(seq_fast);
936 int i;
937
938 for (i = 0; i < len; i++) {
939 PyObject *list_item = seq_fast_items[i];
940
941 if (BPy_StructRNA_Check(list_item)) {
942 ptr = &reinterpret_cast<BPy_StructRNA *>(list_item)->ptr.value();
944 }
945 else {
946 /* Log invalid list item type */
947 std::string message = std::string("'") + member +
948 "' list item not a valid type in sequence type '" +
949 Py_TYPE(list_item)->tp_name + "'";
950 bpy_context_log_member_error(C, message.c_str());
951 }
952 }
953 Py_DECREF(seq_fast);
955 done = true;
956 }
957 }
958
959 if (done == false) {
960 if (item) {
961 /* Log invalid member type */
962 std::string message = std::string("'") + member + "' not a valid type";
963 bpy_context_log_member_error(C, message.c_str());
964 }
965 }
966
967 if (use_gil) {
968 PyGILState_Release(gilstate);
969 }
970
971 return done;
972}
973
974std::optional<std::string> BPY_python_current_file_and_line()
975{
976 /* Early return if Python is not initialized, usually during startup.
977 * This function shouldn't operate if Python isn't initialized yet.
978 *
979 * In most cases this shouldn't be done, make an exception as it's needed for logging. */
980 if (!Py_IsInitialized()) {
981 return std::nullopt;
982 }
983
984 PyGILState_STATE gilstate;
985 const bool use_gil = !PyC_IsInterpreterActive();
986 std::optional<std::string> result = std::nullopt;
987 if (use_gil) {
988 gilstate = PyGILState_Ensure();
989 }
990
991 const char *filename = nullptr;
992 int lineno = -1;
993 PyC_FileAndNum_Safe(&filename, &lineno);
994
995 if (filename) {
996 result = std::string(filename) + ":" + std::to_string(lineno);
997 }
998
999 if (use_gil) {
1000 PyGILState_Release(gilstate);
1001 }
1002 return result;
1003}
1004
1005#ifdef WITH_PYTHON_MODULE
1006
1007/* -------------------------------------------------------------------- */
1019
1020static void main_python_exit_ensure();
1021
1022static void bpy_detect_exit_singleton_cleanup(PyObject * /*capsule*/)
1023{
1024 main_python_exit_ensure();
1025}
1026
1027static void bpy_detect_exit_singleton_add_to_module(PyObject *mod)
1028{
1029 static PyObject *singleton = nullptr;
1030
1031 /* Note that Python's API docs state that:
1032 * - If this capsule will be stored as an attribute of a module,
1033 * the name should be specified as `modulename.attributename`.
1034 * This is ignored here because the capsule is not intended for script author access.
1035 * It also wouldn't make sense as it is stored in multiple modules. */
1036 const char *bpy_detect_exit_singleton_id = "_bpy_detect_exit_singleton";
1037 if (singleton == nullptr) {
1038 /* This is ignored, but must be non-null,
1039 * set an address that is non-null and easily identifiable. */
1040 void *pointer = reinterpret_cast<void *>(uintptr_t(-1));
1041 singleton = PyCapsule_New(
1042 pointer, bpy_detect_exit_singleton_id, bpy_detect_exit_singleton_cleanup);
1043 BLI_assert(singleton);
1044 }
1045 else {
1046 Py_INCREF(singleton);
1047 }
1048 PyModule_AddObject(mod, bpy_detect_exit_singleton_id, singleton);
1049}
1050
1052
1053/* TODO: reloading the module isn't functional at the moment. */
1054
1055static void bpy_module_free(void *mod);
1056
1057/* Defined in 'creator.c' when building as a Python module. */
1058extern int main_python_enter(int argc, const char **argv);
1059extern void main_python_exit();
1060
1061static void main_python_exit_ensure()
1062{
1063 static bool exit = false;
1064 if (exit) {
1065 return;
1066 }
1067 exit = true;
1068 main_python_exit();
1069}
1070
1071static struct PyModuleDef bpy_proxy_def = {
1072 /*m_base*/ PyModuleDef_HEAD_INIT,
1073 /*m_name*/ "bpy",
1074 /*m_doc*/ nullptr,
1075 /*m_size*/ 0,
1076 /*m_methods*/ nullptr,
1077 /*m_slots*/ nullptr,
1078 /*m_traverse*/ nullptr,
1079 /*m_clear*/ nullptr,
1080 /*m_free*/ bpy_module_free,
1081};
1082
1083struct dealloc_obj {
1084 PyObject_HEAD
1085 /* Type-specific fields go here. */
1086 PyObject *mod;
1087};
1088
1089/* Call once `__file__` is set. */
1090static void bpy_module_delay_init(PyObject *bpy_proxy)
1091{
1092 const int argc = 1;
1093 const char *argv[2];
1094
1095 /* Updating the module dict below will lose the reference to `__file__`. */
1096 PyObject *filepath_obj = PyModule_GetFilenameObject(bpy_proxy);
1097
1098 /* The file can be a relative path. */
1099 const char *filepath_rel = PyUnicode_AsUTF8(filepath_obj);
1100 char filepath_abs[1024];
1101
1102 STRNCPY(filepath_abs, filepath_rel);
1103 BLI_path_abs_from_cwd(filepath_abs, sizeof(filepath_abs));
1104 Py_DECREF(filepath_obj);
1105
1106 argv[0] = filepath_abs;
1107 argv[1] = nullptr;
1108
1109 main_python_enter(argc, argv);
1110
1111 /* Initialized in #BPy_init_modules(). */
1112 PyDict_Update(PyModule_GetDict(bpy_proxy), PyModule_GetDict(bpy_package_py));
1113
1114 {
1115 /* Modules which themselves require access to Blender
1116 * allocated resources to be freed should be included in this list.
1117 * Once the last module has been cleared, the singleton will be de-allocated
1118 * which calls #main_python_exit.
1119 *
1120 * Note that, other modules can be here as needed. */
1121 const char *bpy_modules_array[] = {
1122 "bpy.types",
1123 /* Not technically required however as this is created early on
1124 * in Blender's module initialization, it's likely to be cleared later,
1125 * since module cleanup runs in the reverse of the order added to `sys.modules`. */
1126 "_bpy",
1127 };
1128 PyObject *sys_modules = PyImport_GetModuleDict();
1129 for (int i = 0; i < ARRAY_SIZE(bpy_modules_array); i++) {
1130 PyObject *mod = PyDict_GetItemString(sys_modules, bpy_modules_array[i]);
1131 BLI_assert(mod);
1132 bpy_detect_exit_singleton_add_to_module(mod);
1133 }
1134 }
1135}
1136
1141static bool bpy_module_ensure_compatible_version()
1142{
1143 /* First check the Python version used matches the major version that Blender was built with.
1144 * While this isn't essential, the error message in this case may be cryptic and misleading.
1145 * NOTE: using `Py_LIMITED_API` would remove the need for this, in practice it's
1146 * unlikely Blender will ever used the limited API though. */
1147 const uint version_runtime = Py_Version;
1148
1149 uint version_compile_major = PY_VERSION_HEX >> 24;
1150 uint version_compile_minor = ((PY_VERSION_HEX & 0x00ff0000) >> 16);
1151 uint version_runtime_major = version_runtime >> 24;
1152 uint version_runtime_minor = ((version_runtime & 0x00ff0000) >> 16);
1153 if ((version_compile_major != version_runtime_major) ||
1154 (version_compile_minor != version_runtime_minor))
1155 {
1156 PyErr_Format(PyExc_ImportError,
1157 "The version of \"bpy\" was compiled with: "
1158 "(%u.%u) is incompatible with: (%u.%u) used by the interpreter!",
1159 version_compile_major,
1160 version_compile_minor,
1161 version_runtime_major,
1162 version_runtime_minor);
1163 return false;
1164 }
1165 return true;
1166}
1167
1168static void dealloc_obj_dealloc(PyObject *self);
1169
1170static PyTypeObject dealloc_obj_Type;
1171
1172/* Use our own `dealloc` so we can free a property if we use one. */
1173static void dealloc_obj_dealloc(PyObject *self)
1174{
1175 bpy_module_delay_init(((dealloc_obj *)self)->mod);
1176
1177 /* NOTE: for sub-classed `PyObject` objects
1178 * we can't call #PyObject_DEL() directly or it will crash. */
1179 dealloc_obj_Type.tp_free(self);
1180}
1181
1182PyMODINIT_FUNC PyInit_bpy();
1183
1184PyMODINIT_FUNC PyInit_bpy()
1185{
1186 if (!bpy_module_ensure_compatible_version()) {
1187 return nullptr; /* The error has been set. */
1188 }
1189
1190 /* Assign dummy type. */
1191 dealloc_obj_Type.tp_name = "dealloc_obj";
1192 dealloc_obj_Type.tp_basicsize = sizeof(dealloc_obj);
1193 dealloc_obj_Type.tp_dealloc = dealloc_obj_dealloc;
1194 dealloc_obj_Type.tp_flags = Py_TPFLAGS_DEFAULT;
1195
1196 if (PyType_Ready(&dealloc_obj_Type) < 0) {
1197 return nullptr;
1198 }
1199
1200 PyObject *bpy_proxy = PyModule_Create(&bpy_proxy_def);
1201
1202 /* Problem:
1203 * 1) This initializing function is expected to have a private member defined - `md_def`
1204 * but this is only set for CAPI defined modules (not Python packages)
1205 * so we can't return `bpy_package_py` as is.
1206 *
1207 * 2) There is a `bpy` CAPI module for python to load which is basically all of blender,
1208 * and there is `scripts/bpy/__init__.py`,
1209 * we may end up having to rename this module so there is no naming conflict here eg:
1210 * `from blender import bpy`
1211 *
1212 * 3) We don't know the file-path at this point, workaround by assigning a dummy value
1213 * which calls back when its freed so the real loading can take place.
1214 */
1215
1216 /* Assign an object which is freed after `__file__` is assigned. */
1217 dealloc_obj *dob;
1218
1219 dob = (dealloc_obj *)dealloc_obj_Type.tp_alloc(&dealloc_obj_Type, 0);
1220 dob->mod = bpy_proxy; /* borrow */
1221 PyModule_AddObject(bpy_proxy, "__file__", (PyObject *)dob); /* borrow */
1222
1223 return bpy_proxy;
1224}
1225
1226static void bpy_module_free(void * /*mod*/)
1227{
1228 main_python_exit_ensure();
1229}
1230
1231#endif
1232
1234{
1235 /* List is from: `", ".join(['"%s"' % kw for kw in __import__("keyword").kwlist])`. */
1236 const char *kwlist[] = {
1237 "False", "None", "True", "and", "as", "assert", "async", "await", "break",
1238 "class", "continue", "def", "del", "elif", "else", "except", "finally", "for",
1239 "from", "global", "if", "import", "in", "is", "lambda", "nonlocal", "not",
1240 "or", "pass", "raise", "return", "try", "while", "with", "yield", nullptr,
1241 };
1242
1243 for (int i = 0; kwlist[i]; i++) {
1244 if (STREQ(str, kwlist[i])) {
1245 return true;
1246 }
1247 }
1248
1249 return false;
1250}
1251
1252/* -------------------------------------------------------------------- */
1258
1260{
1261 return (ch < 255 && text_check_identifier(char(ch))) || Py_UNICODE_ISALNUM(ch);
1262}
1263
1265{
1266 return (ch < 255 && text_check_identifier_nodigit(char(ch))) || Py_UNICODE_ISALPHA(ch);
1267}
1268
1270
1271} // namespace blender
#define FILE_MAX
#define BLI_path_join(...)
#define SNPRINTF_UTF8(dst, format,...)
Platform independent time functions.
#define RPT_(msgid)
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
#define CLOG_AT_LEVEL_NOCHECK(clg_ref, verbose_level,...)
Definition CLG_log.h:159
#define CLOG_CHECK(clg_ref, verbose_level,...)
Definition CLG_log.h:147
@ CLG_LEVEL_INFO
Definition CLG_log.h:60
#define CLOG_INFO(clg_ref,...)
Definition CLG_log.h:190
#define C
Definition RandGen.cpp:29
#define BLI_assert_unreachable()
#define BLI_assert_msg(a, msg)
#define STREQ(a, b)
#define BPy_StructRNA_Check(v)
Definition bpy_rna.hh:76
#define str(s)
VecBase< float, D > constexpr mod(VecOp< float, D >, VecOp< float, D >) RET
PyObject * Manta_initPython(void)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
#define L
std::string filename(const std::string_view &str, size_t pos)
BMesh Iterators.
PyObject * BPyInit_idprop()
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static void bpy_context_log_member_error(const bContext *C, const char *message)
void BPY_DECREF_RNA_INVALIDATE(void *pyob_ptr)
bool PyC_IsInterpreterActive()
bool text_check_identifier(char ch)
Definition text.cc:2323
void BPY_python_use_user_env()
void * CTX_py_dict_get(const bContext *C)
char * STRNCPY(char(&dst)[N], const char *src)
PyObject * BPY_rna_module()
Definition bpy_rna.cc:9081
void BPY_modules_update()
static int py_call_level
static bool py_use_user_env
unsigned int uint
Definition BKE_text.h:55
PyMODINIT_FUNC PyInit_mathutils_noise()
void BPY_context_dict_clear_members_array(void **dict_p, void *dict_orig, const char *context_members[], uint context_members_len)
void BPY_python_backtrace(FILE *fp)
BPy_StructRNA * bpy_context_module
Definition bpy_rna.cc:100
void pyrna_context_clear(bContext *C)
Definition bpy_rna.cc:534
std::optional< std::string > BPY_python_current_file_and_line()
void BPY_rna_exit()
Definition bpy_rna.cc:9058
PyObject * BPyInit_bmesh()
void pyrna_context_init(bContext *C)
Definition bpy_rna.cc:529
bool BPY_python_use_system_env_get()
bool BPY_string_is_keyword(const char *str)
void BPY_app_handlers_reset(bool do_all)
PyObject * bpy_package_py
Definition bpy.cc:68
bool text_check_identifier_nodigit(char ch)
Definition text.cc:2346
void BPY_DECREF(void *pyob_ptr)
PyObject * BPyInit_bmesh_types()
bool CTX_member_logging_get(const bContext *C)
bool bool BPY_run_text(bContext *C, Text *text, ReportList *reports, bool do_jump) ATTR_NONNULL(1
void BPY_text_free_code(Text *text)
void PyC_FileAndNum_Safe(const char **r_filename, int *r_lineno)
void BPY_python_start(bContext *C, int argc, const char **argv)
CLG_LogRef * BKE_LOG_CONTEXT
void BLI_setenv(const char *env, const char *val) ATTR_NONNULL(1)
void BPY_context_set(bContext *C)
@ G_DEBUG_PYTHON
Global G
Definition blender.cc:57
const char * BKE_main_blendfile_path(const Main *bmain) ATTR_NONNULL()
Definition main.cc:1058
void RNA_bpy_exit()
static bool bpy_context_set_ex(bContext *C, PyGILState_STATE *gilstate, const bool allow_null_context)
std::optional< std::string > BKE_appdir_folder_id(int folder_id, const char *subfolder) ATTR_WARN_UNUSED_RESULT
Definition appdir.cc:762
int text_check_identifier_nodigit_unicode(unsigned int ch)
Definition text.cc:2372
PyMODINIT_FUNC PyInit_mathutils()
Definition mathutils.cc:846
PyObject * BPyInit__bpy_path()
Definition bpy_path.cc:128
PyObject * BPyInit_imbuf()
struct bContext * BPY_context_get()
void CTX_data_type_set(bContextDataResult *result, ContextDataType type)
CLG_LogRef * BPY_LOG_INTERFACE
PyMODINIT_FUNC BPyInit_bl_math()
void CTX_data_pointer_set_ptr(bContextDataResult *result, const PointerRNA *ptr)
static bool py_use_system_env
void BPY_atexit_register()
bool BKE_appdir_program_python_search(char *program_filepath, size_t program_filepath_maxncpy, int version_major, int version_minor) ATTR_NONNULL(1)
Definition appdir.cc:1015
bool BLI_path_abs_from_cwd(char *path, size_t path_maxncpy) ATTR_NONNULL(1)
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
CLG_LogRef * BPY_LOG_RNA
Definition bpy.hh:29
void BPY_python_reset(bContext *C)
bool BPY_run_string_eval(bContext *C, const char *imports[], const char *expr)
PyObject * BPY_rna_types()
Definition bpy_rna.cc:9246
void BPY_driver_reset()
int BLI_thread_is_main()
Definition threads.cc:184
bool BPY_context_member_get(bContext *C, const char *member, bContextDataResult *result)
void BPY_rna_props_clear_all()
bool bpy_context_set_allow_null(struct bContext *C, PyGILState_STATE *gilstate)
void BPY_app_translations_end()
int text_check_identifier_unicode(unsigned int ch)
Definition text.cc:2367
void BPY_modules_load_user(bContext *C)
PyObject * BPyInit_gpu()
_W64 unsigned int uintptr_t
Definition BKE_text.h:120
PyObject * BPyInit_blf()
static void bpy_context_end(bContext *C)
void BPY_driver_exit()
void pyrna_invalidate(BPy_DummyPointerRNA *self)
Definition bpy_rna.cc:211
static void pystatus_exit_on_error(const PyStatus &status)
@ G_FLAG_SCRIPT_AUTOEXEC_FAIL_QUIET
@ G_FLAG_SCRIPT_AUTOEXEC_FAIL
@ G_FLAG_SCRIPT_AUTOEXEC
Main * CTX_data_main(const bContext *C)
PyObject * BPyInit_audaspace()
PyObject * BPyInit_hydra()
Definition python.cc:255
bool bpy_context_set(struct bContext *C, PyGILState_STATE *gilstate) ATTR_NONNULL(1)
void BPy_init_modules(bContext *C)
Definition bpy.cc:761
static _inittab bpy_internal_modules[]
void BPY_python_end(bool do_python_exit)
PyMODINIT_FUNC PyInit_mathutils_kdtree()
void pyrna_alloc_types()
Definition bpy_rna.cc:10448
void CTX_data_list_add_ptr(bContextDataResult *result, const PointerRNA *ptr)
double BLI_time_now_seconds()
Definition time.cc:30
PyObject * BPyInit_bmesh_geometry()
const char * BKE_appdir_program_path() ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL
Definition appdir.cc:1001
PyMODINIT_FUNC PyInit_mathutils_geometry()
void * CCL_python_module_init()
Definition python.cpp:958
void BPY_context_update(bContext *C)
@ BLENDER_SYSTEM_PYTHON
void bpy_context_clear(struct bContext *C, const PyGILState_STATE *gilstate)
void BPY_atexit_unregister()
void BPY_rna_init()
Definition bpy_rna.cc:9005
void BPY_python_use_system_env()
void CTX_wm_operator_poll_msg_clear(bContext *C)
void bpy_intern_string_exit()
CLG_LOGREF_DECLARE_GLOBAL(BKE_LOG_CONTEXT, "context")
const char * BLI_getenv(const char *env) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
void bpy_intern_string_init()
void BPY_update_rna_module()
Definition bpy_rna.cc:9093
PyObject * BPyInit_bmesh_utils()
header-only compatibility defines.
#define PyThreadState_GetUnchecked
PyObject_HEAD std::optional< PointerRNA > ptr
Definition bpy_rna.hh:133
ListBaseT< FreestyleModuleConfig > modules
ListBaseT< Text > texts
Definition BKE_main.hh:402
ListBaseT< Scene > scenes
Definition BKE_main.hh:386
ListBaseT< ViewLayer > view_layers
struct FreestyleConfig freestyle_config