4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * Contributor(s): Michel Selten, Willian P. Germano, Stephen Swaney,
21 * Chris Keith, Chris Want, Ken Hughes, Campbell Barton
23 * ***** END GPL LICENSE BLOCK *****
26 /** \file blender/python/intern/bpy_interface.c
27 * \ingroup pythonintern
31 /* grr, python redefines */
32 #ifdef _POSIX_C_SOURCE
33 # undef _POSIX_C_SOURCE
38 #include "MEM_guardedalloc.h"
40 #include "RNA_types.h"
46 #include "bpy_traceback.h"
47 #include "bpy_intern_string.h"
49 #include "DNA_space_types.h"
50 #include "DNA_text_types.h"
52 #include "BLI_path_util.h"
53 #include "BLI_math_base.h"
54 #include "BLI_string.h"
55 #include "BLI_string_utf8.h"
56 #include "BLI_utildefines.h"
59 #include "BKE_context.h"
62 #include "BKE_global.h" /* only for script checking */
64 #include "BPY_extern.h"
66 #include "../generic/bpy_internal_import.h" // our own imports
67 #include "../generic/py_capi_utils.h"
69 /* inittab initialization functions */
70 #include "../generic/bgl.h"
71 #include "../generic/blf_py_api.h"
72 #include "../generic/noise_py_api.h"
73 #include "../mathutils/mathutils.h"
75 /* for internal use, when starting and ending python scripts */
77 /* incase a python script triggers another python call, stop bpy_context_clear from invalidating */
78 static int py_call_level= 0;
79 BPy_StructRNA *bpy_context_module= NULL; /* for fast access */
81 // #define TIME_PY_RUN // simple python tests. prints on exit.
85 static int bpy_timer_count= 0;
86 static double bpy_timer; /* time since python starts */
87 static double bpy_timer_run; /* time for each python script run */
88 static double bpy_timer_run_tot; /* accumulate python runs */
91 /* use for updating while a python script runs - in case of file load */
92 void bpy_context_update(bContext *C)
95 bpy_import_main_set(CTX_data_main(C));
96 BPY_modules_update(C); /* can give really bad results if this isnt here */
99 void bpy_context_set(bContext *C, PyGILState_STATE *gilstate)
104 *gilstate= PyGILState_Ensure();
106 if (py_call_level==1) {
107 bpy_context_update(C);
110 if (bpy_timer_count==0) {
111 /* record time from the beginning */
112 bpy_timer= PIL_check_seconds_timer();
113 bpy_timer_run= bpy_timer_run_tot= 0.0;
115 bpy_timer_run= PIL_check_seconds_timer();
123 /* context should be used but not now because it causes some bugs */
124 void bpy_context_clear(bContext *UNUSED(C), PyGILState_STATE *gilstate)
129 PyGILState_Release(*gilstate);
131 if (py_call_level < 0) {
132 fprintf(stderr, "ERROR: Python context internal state bug. this should not happen!\n");
134 else if (py_call_level==0) {
135 // XXX - Calling classes currently wont store the context :\, cant set NULL because of this. but this is very flakey still.
136 //BPy_SetContext(NULL);
137 //bpy_import_main_set(NULL);
140 bpy_timer_run_tot += PIL_check_seconds_timer() - bpy_timer_run;
147 void BPY_text_free_code(Text *text)
149 if (text->compiled) {
150 Py_DECREF((PyObject *)text->compiled);
151 text->compiled= NULL;
155 void BPY_modules_update(bContext *C)
157 #if 0 // slow, this runs all the time poll, draw etc 100's of time a sec.
158 PyObject *mod= PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL, 0);
159 PyModule_AddObject(mod, "data", BPY_rna_module());
160 PyModule_AddObject(mod, "types", BPY_rna_types()); // atm this does not need updating
163 /* refreshes the main struct */
164 BPY_update_rna_module();
165 bpy_context_module->ptr.data= (void *)C;
168 void BPY_context_set(bContext *C)
173 /* defined in AUD_C-API.cpp */
174 extern PyObject *AUD_initPython(void);
176 static struct _inittab bpy_internal_modules[]= {
177 {(char *)"noise", BPyInit_noise},
178 {(char *)"mathutils", PyInit_mathutils},
179 // {(char *)"mathutils.geometry", PyInit_mathutils_geometry},
180 {(char *)"bgl", BPyInit_bgl},
181 {(char *)"blf", BPyInit_blf},
182 #ifdef WITH_AUDASPACE
183 {(char *)"aud", AUD_initPython},
185 {(char *)"gpu", GPU_initPython},
189 /* call BPY_context_set first */
190 void BPY_python_start(int argc, const char **argv)
192 #ifndef WITH_PYTHON_MODULE
193 PyThreadState *py_tstate= NULL;
195 /* not essential but nice to set our name */
196 static wchar_t program_path_wchar[FILE_MAXDIR+FILE_MAXFILE]; /* python holds a reference */
197 BLI_strncpy_wchar_from_utf8(program_path_wchar, BLI_program_path(), sizeof(program_path_wchar) / sizeof(wchar_t));
198 Py_SetProgramName(program_path_wchar);
200 /* must run before python initializes */
201 PyImport_ExtendInittab(bpy_internal_modules);
203 /* allow to use our own included python */
204 PyC_SetHomePath(BLI_get_folder(BLENDER_SYSTEM_PYTHON, NULL));
206 /* Python 3.2 now looks for '2.58/python/include/python3.2d/pyconfig.h' to parse
207 * from the 'sysconfig' module which is used by 'site', so for now disable site.
208 * alternatively we could copy the file. */
213 // PySys_SetArgv(argc, argv); // broken in py3, not a huge deal
214 /* sigh, why do python guys not have a char** version anymore? :( */
217 PyObject *py_argv= PyList_New(argc);
218 for (i=0; i<argc; i++)
219 PyList_SET_ITEM(py_argv, i, PyC_UnicodeFromByte(argv[i])); /* should fix bug #20021 - utf path name problems, by replacing PyUnicode_FromString */
221 PySys_SetObject("argv", py_argv);
225 /* Initialize thread support (also acquires lock) */
226 PyEval_InitThreads();
231 /* must run before python initializes */
232 PyImport_ExtendInittab(bpy_internal_modules);
235 bpy_intern_string_init();
237 /* bpy.* and lets us import it */
240 bpy_import_init(PyEval_GetBuiltins());
244 BPY_atexit_register(); /* this can init any time */
246 #ifndef WITH_PYTHON_MODULE
247 py_tstate= PyGILState_GetThisThreadState();
248 PyEval_ReleaseThread(py_tstate);
252 void BPY_python_end(void)
254 // fprintf(stderr, "Ending Python!\n");
256 PyGILState_Ensure(); /* finalizing, no need to grab the state */
258 // free other python data.
261 /* clear all python data from structs */
263 bpy_intern_string_exit();
265 BPY_atexit_unregister(); /* without this we get recursive calls to WM_exit */
270 // measure time since py started
271 bpy_timer= PIL_check_seconds_timer() - bpy_timer;
273 printf("*bpy stats* - ");
274 printf("tot exec: %d, ", bpy_timer_count);
275 printf("tot run: %.4fsec, ", bpy_timer_run_tot);
276 if (bpy_timer_count>0)
277 printf("average run: %.6fsec, ", (bpy_timer_run_tot/bpy_timer_count));
280 printf("tot usage %.4f%%", (bpy_timer_run_tot/bpy_timer)*100.0);
284 // fprintf(stderr, "Ending Python Done!\n");
290 static void python_script_error_jump_text(struct Text *text)
294 python_script_error_jump(text->id.name+2, &lineno, &offset);
296 /* select the line with the error */
297 txt_move_to(text, lineno - 1, INT_MAX, FALSE);
298 txt_move_to(text, lineno - 1, offset, TRUE);
302 /* super annoying, undo _PyModule_Clear(), bug [#23871] */
303 #define PYMODULE_CLEAR_WORKAROUND
305 #ifdef PYMODULE_CLEAR_WORKAROUND
306 /* bad!, we should never do this, but currently only safe way I could find to keep namespace.
307 * from being cleared. - campbell */
311 /* ommit other values, we only want the dict. */
315 static int python_script_exec(bContext *C, const char *fn, struct Text *text, struct ReportList *reports, const short do_jump)
317 PyObject *main_mod= NULL;
318 PyObject *py_dict= NULL, *py_result= NULL;
319 PyGILState_STATE gilstate;
321 BLI_assert(fn || text);
323 if (fn==NULL && text==NULL) {
327 bpy_context_set(C, &gilstate);
329 PyC_MainModule_Backup(&main_mod);
332 char fn_dummy[FILE_MAXDIR];
333 bpy_text_filename_get(fn_dummy, sizeof(fn_dummy), text);
335 if (text->compiled == NULL) { /* if it wasn't already compiled, do it now */
336 char *buf= txt_to_buf(text);
338 text->compiled= Py_CompileString(buf, fn_dummy, Py_file_input);
342 if (PyErr_Occurred()) {
344 python_script_error_jump_text(text);
346 BPY_text_free_code(text);
350 if (text->compiled) {
351 py_dict= PyC_DefaultNameSpace(fn_dummy);
352 py_result= PyEval_EvalCode(text->compiled, py_dict, py_dict);
357 FILE *fp= fopen(fn, "r");
360 py_dict= PyC_DefaultNameSpace(fn);
363 /* Previously we used PyRun_File to run directly the code on a FILE
364 * object, but as written in the Python/C API Ref Manual, chapter 2,
365 * 'FILE structs for different C libraries can be different and
367 * So now we load the script file data to a buffer */
373 pystring= MEM_mallocN(strlen(fn) + 32, "pystring");
375 sprintf(pystring, "exec(open(r'%s').read())", fn);
376 py_result= PyRun_String(pystring, Py_file_input, py_dict, py_dict);
380 py_result= PyRun_File(fp, fn, Py_file_input, py_dict, py_dict);
385 PyErr_Format(PyExc_IOError,
386 "Python file \"%s\" could not be opened: %s",
387 fn, strerror(errno));
395 python_script_error_jump_text(text);
398 BPy_errors_to_report(reports);
401 Py_DECREF(py_result);
405 #ifdef PYMODULE_CLEAR_WORKAROUND
406 PyModuleObject *mmod= (PyModuleObject *)PyDict_GetItemString(PyThreadState_GET()->interp->modules, "__main__");
407 PyObject *dict_back= mmod->md_dict;
408 /* freeing the module will clear the namespace,
409 * gives problems running classes defined in this namespace being used later. */
411 Py_DECREF(dict_back);
414 #undef PYMODULE_CLEAR_WORKAROUND
417 PyC_MainModule_Restore(main_mod);
419 bpy_context_clear(C, &gilstate);
421 return (py_result != NULL);
424 /* Can run a file or text block */
425 int BPY_filepath_exec(bContext *C, const char *filepath, struct ReportList *reports)
427 return python_script_exec(C, filepath, NULL, reports, FALSE);
431 int BPY_text_exec(bContext *C, struct Text *text, struct ReportList *reports, const short do_jump)
433 return python_script_exec(C, NULL, text, reports, do_jump);
436 void BPY_DECREF(void *pyob_ptr)
438 PyGILState_STATE gilstate= PyGILState_Ensure();
439 Py_DECREF((PyObject *)pyob_ptr);
440 PyGILState_Release(gilstate);
443 /* return -1 on error, else 0 */
444 int BPY_button_exec(bContext *C, const char *expr, double *value, const short verbose)
446 PyGILState_STATE gilstate;
447 PyObject *py_dict, *mod, *retval;
449 PyObject *main_mod= NULL;
451 if (!value || !expr) return -1;
458 bpy_context_set(C, &gilstate);
460 PyC_MainModule_Backup(&main_mod);
462 py_dict= PyC_DefaultNameSpace("<blender button>");
464 mod= PyImport_ImportModule("math");
466 PyDict_Merge(py_dict, PyModule_GetDict(mod), 0); /* 0 - dont overwrite existing values */
469 else { /* highly unlikely but possibly */
474 retval= PyRun_String(expr, Py_eval_input, py_dict, py_dict);
476 if (retval == NULL) {
482 if (PyTuple_Check(retval)) {
483 /* Users my have typed in 10km, 2m
484 * add up all values */
488 for (i=0; i<PyTuple_GET_SIZE(retval); i++) {
489 val+= PyFloat_AsDouble(PyTuple_GET_ITEM(retval, i));
493 val= PyFloat_AsDouble(retval);
497 if (val==-1 && PyErr_Occurred()) {
500 else if (!finite(val)) {
510 BPy_errors_to_report(CTX_wm_reports(C));
517 PyC_MainModule_Backup(&main_mod);
519 bpy_context_clear(C, &gilstate);
524 int BPY_string_exec(bContext *C, const char *expr)
526 PyGILState_STATE gilstate;
527 PyObject *main_mod= NULL;
528 PyObject *py_dict, *retval;
530 Main *bmain_back; /* XXX, quick fix for release (Copy Settings crash), needs further investigation */
532 if (!expr) return -1;
538 bpy_context_set(C, &gilstate);
540 PyC_MainModule_Backup(&main_mod);
542 py_dict= PyC_DefaultNameSpace("<blender string>");
544 bmain_back= bpy_import_main_get();
545 bpy_import_main_set(CTX_data_main(C));
547 retval= PyRun_String(expr, Py_eval_input, py_dict, py_dict);
549 bpy_import_main_set(bmain_back);
551 if (retval == NULL) {
554 BPy_errors_to_report(CTX_wm_reports(C));
560 PyC_MainModule_Restore(main_mod);
562 bpy_context_clear(C, &gilstate);
568 void BPY_modules_load_user(bContext *C)
570 PyGILState_STATE gilstate;
571 Main *bmain= CTX_data_main(C);
574 /* can happen on file load */
578 /* update pointers since this can run from a nested script
581 bpy_context_update(C);
584 bpy_context_set(C, &gilstate);
586 for (text=CTX_data_main(C)->text.first; text; text= text->id.next) {
587 if (text->flags & TXT_ISSCRIPT && BLI_testextensie(text->id.name+2, ".py")) {
588 if (!(G.f & G_SCRIPT_AUTOEXEC)) {
589 printf("scripts disabled for \"%s\", skipping '%s'\n", bmain->name, text->id.name+2);
592 PyObject *module= bpy_text_import(text);
604 bpy_context_clear(C, &gilstate);
607 int BPY_context_member_get(bContext *C, const char *member, bContextDataResult *result)
609 PyObject *pyctx= (PyObject *)CTX_py_dict_get(C);
610 PyObject *item= PyDict_GetItemString(pyctx, member);
611 PointerRNA *ptr= NULL;
617 else if (item==Py_None) {
620 else if (BPy_StructRNA_Check(item)) {
621 ptr= &(((BPy_StructRNA *)item)->ptr);
623 //result->ptr= ((BPy_StructRNA *)item)->ptr;
624 CTX_data_pointer_set(result, ptr->id.data, ptr->type, ptr->data);
627 else if (PySequence_Check(item)) {
628 PyObject *seq_fast= PySequence_Fast(item, "bpy_context_get sequence conversion");
629 if (seq_fast==NULL) {
634 int len= PySequence_Fast_GET_SIZE(seq_fast);
636 for (i= 0; i < len; i++) {
637 PyObject *list_item= PySequence_Fast_GET_ITEM(seq_fast, i);
639 if (BPy_StructRNA_Check(list_item)) {
641 CollectionPointerLink *link= MEM_callocN(sizeof(CollectionPointerLink), "bpy_context_get");
642 link->ptr= ((BPy_StructRNA *)item)->ptr;
643 BLI_addtail(&result->list, link);
645 ptr= &(((BPy_StructRNA *)list_item)->ptr);
646 CTX_data_list_add(result, ptr->id.data, ptr->type, ptr->data);
649 printf("List item not a valid type\n");
660 if (item) printf("PyContext '%s' not a valid type\n", member);
661 else printf("PyContext '%s' not found\n", member);
665 printf("PyContext '%s' found\n", member);
673 #ifdef WITH_PYTHON_MODULE
674 #include "BLI_storage.h"
675 /* TODO, reloading the module isnt functional at the moment. */
677 static void bpy_module_free(void *mod);
678 extern int main_python_enter(int argc, const char **argv);
679 extern void main_python_exit(void);
680 static struct PyModuleDef bpy_proxy_def= {
681 PyModuleDef_HEAD_INIT,
685 NULL, /* m_methods */
687 NULL, /* m_traverse */
689 bpy_module_free, /* m_free */
694 /* Type-specific fields go here. */
698 /* call once __file__ is set */
699 void bpy_module_delay_init(PyObject *bpy_proxy)
703 PyObject *filename_obj= PyModule_GetFilenameObject(bpy_proxy); /* updating the module dict below will loose the reference to __file__ */
704 const char *filename_rel= _PyUnicode_AsString(filename_obj); /* can be relative */
705 char filename_abs[1024];
707 BLI_strncpy(filename_abs, filename_rel, sizeof(filename_abs));
708 BLI_path_cwd(filename_abs);
710 argv[0]= filename_abs;
713 // printf("module found %s\n", argv[0]);
715 main_python_enter(argc, argv);
717 /* initialized in BPy_init_modules() */
718 PyDict_Update(PyModule_GetDict(bpy_proxy), PyModule_GetDict(bpy_package_py));
721 static void dealloc_obj_dealloc(PyObject *self);
723 static PyTypeObject dealloc_obj_Type= {{{0}}};
725 /* use our own dealloc so we can free a property if we use one */
726 static void dealloc_obj_dealloc(PyObject *self)
728 bpy_module_delay_init(((dealloc_obj *)self)->mod);
730 /* Note, for subclassed PyObjects we cant just call PyObject_DEL() directly or it will crash */
731 dealloc_obj_Type.tp_free(self);
737 PyObject *bpy_proxy= PyModule_Create(&bpy_proxy_def);
740 * 1) this init function is expected to have a private member defined - 'md_def'
741 * but this is only set for C defined modules (not py packages)
742 * so we cant return 'bpy_package_py' as is.
744 * 2) there is a 'bpy' C module for python to load which is basically all of blender,
745 * and there is scripts/bpy/__init__.py,
746 * we may end up having to rename this module so there is no naming conflict here eg:
747 * 'from blender import bpy'
749 * 3) we dont know the filename at this point, workaround by assigning a dummy value
750 * which calls back when its freed so the real loading can take place.
753 /* assign an object which is freed after __file__ is assigned */
756 /* assign dummy type */
757 dealloc_obj_Type.tp_name= "dealloc_obj";
758 dealloc_obj_Type.tp_basicsize= sizeof(dealloc_obj);
759 dealloc_obj_Type.tp_dealloc= dealloc_obj_dealloc;
760 dealloc_obj_Type.tp_flags= Py_TPFLAGS_DEFAULT;
762 if (PyType_Ready(&dealloc_obj_Type) < 0)
765 dob= (dealloc_obj *) dealloc_obj_Type.tp_alloc(&dealloc_obj_Type, 0);
766 dob->mod= bpy_proxy; /* borrow */
767 PyModule_AddObject(bpy_proxy, "__file__", (PyObject *)dob); /* borrow */
772 static void bpy_module_free(void *UNUSED(mod))