4ae1b41bb8996d55a578f4d75ba8d681658f9713
[blender.git] / source / blender / python / intern / bpy_interface.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
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.
10  *
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.
15  *
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.
19  *
20  * Contributor(s): Michel Selten, Willian P. Germano, Stephen Swaney,
21  * Chris Keith, Chris Want, Ken Hughes, Campbell Barton
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25  
26 /* grr, python redefines */
27 #ifdef _POSIX_C_SOURCE
28 #undef _POSIX_C_SOURCE
29 #endif
30
31 #include <Python.h>
32
33 #include "MEM_guardedalloc.h"
34
35 #include "bpy.h"
36 #include "bpy_rna.h"
37 #include "bpy_util.h"
38
39 #include "DNA_space_types.h"
40 #include "DNA_text_types.h"
41
42 #include "BLI_path_util.h"
43 #include "BLI_math_base.h"
44 #include "BLI_string.h"
45 #include "BLI_utildefines.h"
46
47
48 #include "BKE_context.h"
49 #include "BKE_text.h"
50 #include "BKE_font.h" /* only for utf8towchar */
51 #include "BKE_main.h"
52 #include "BKE_global.h" /* only for script checking */
53
54 #include "BPY_extern.h"
55
56 #include "../generic/bpy_internal_import.h" // our own imports
57 #include "../generic/py_capi_utils.h"
58
59 /* inittab initialization functions */
60 #include "../generic/noise_py_api.h"
61 #include "../generic/mathutils.h"
62 #include "../generic/bgl.h"
63 #include "../generic/blf_py_api.h"
64
65 /* for internal use, when starting and ending python scripts */
66
67 /* incase a python script triggers another python call, stop bpy_context_clear from invalidating */
68 static int py_call_level= 0;
69 BPy_StructRNA *bpy_context_module= NULL; /* for fast access */
70
71 // #define TIME_PY_RUN // simple python tests. prints on exit.
72
73 #ifdef TIME_PY_RUN
74 #include "PIL_time.h"
75 static int              bpy_timer_count = 0;
76 static double   bpy_timer; /* time since python starts */
77 static double   bpy_timer_run; /* time for each python script run */
78 static double   bpy_timer_run_tot; /* accumulate python runs */
79 #endif
80
81 void bpy_context_set(bContext *C, PyGILState_STATE *gilstate)
82 {
83         py_call_level++;
84
85         if(gilstate)
86                 *gilstate = PyGILState_Ensure();
87
88         if(py_call_level==1) {
89
90                 if(C) { // XXX - should always be true.
91                         BPy_SetContext(C);
92                         bpy_import_main_set(CTX_data_main(C));
93                 }
94                 else {
95                         fprintf(stderr, "ERROR: Python context called with a NULL Context. this should not happen!\n");
96                 }
97
98                 BPY_modules_update(C); /* can give really bad results if this isnt here */
99
100 #ifdef TIME_PY_RUN
101                 if(bpy_timer_count==0) {
102                         /* record time from the beginning */
103                         bpy_timer= PIL_check_seconds_timer();
104                         bpy_timer_run = bpy_timer_run_tot = 0.0;
105                 }
106                 bpy_timer_run= PIL_check_seconds_timer();
107
108
109                 bpy_timer_count++;
110 #endif
111         }
112 }
113
114 /* context should be used but not now because it causes some bugs */
115 void bpy_context_clear(bContext *UNUSED(C), PyGILState_STATE *gilstate)
116 {
117         py_call_level--;
118
119         if(gilstate)
120                 PyGILState_Release(*gilstate);
121
122         if(py_call_level < 0) {
123                 fprintf(stderr, "ERROR: Python context internal state bug. this should not happen!\n");
124         }
125         else if(py_call_level==0) {
126                 // XXX - Calling classes currently wont store the context :\, cant set NULL because of this. but this is very flakey still.
127                 //BPy_SetContext(NULL);
128                 //bpy_import_main_set(NULL);
129
130 #ifdef TIME_PY_RUN
131                 bpy_timer_run_tot += PIL_check_seconds_timer() - bpy_timer_run;
132                 bpy_timer_count++;
133 #endif
134
135         }
136 }
137
138 void BPY_text_free_code(Text *text)
139 {
140         if( text->compiled ) {
141                 Py_DECREF( ( PyObject * ) text->compiled );
142                 text->compiled = NULL;
143         }
144 }
145
146 void BPY_modules_update(bContext *C)
147 {
148 #if 0 // slow, this runs all the time poll, draw etc 100's of time a sec.
149         PyObject *mod= PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL, 0);
150         PyModule_AddObject( mod, "data", BPY_rna_module() );
151         PyModule_AddObject( mod, "types", BPY_rna_types() ); // atm this does not need updating
152 #endif
153
154         /* refreshes the main struct */
155         BPY_update_rna_module();
156         bpy_context_module->ptr.data= (void *)C;
157 }
158
159 /* must be called before Py_Initialize */
160 static void bpy_python_start_path(void)
161 {
162         char *py_path_bundle= BLI_get_folder(BLENDER_PYTHON, NULL);
163
164         if(py_path_bundle==NULL)
165                 return;
166
167         /* set the environment path */
168         printf("found bundled python: %s\n", py_path_bundle);
169
170 #ifdef __APPLE__
171         /* OSX allow file/directory names to contain : character (represented as / in the Finder)
172          but current Python lib (release 3.1.1) doesn't handle these correctly */
173         if(strchr(py_path_bundle, ':'))
174                 printf("Warning : Blender application is located in a path containing : or / chars\
175                            \nThis may make python import function fail\n");
176 #endif
177         
178 #ifdef _WIN32
179         /* cmake/MSVC debug build crashes without this, why only
180            in this case is unknown.. */
181         {
182                 BLI_setenv("PYTHONPATH", py_path_bundle);       
183         }
184 #endif
185
186         {
187                 static wchar_t py_path_bundle_wchar[FILE_MAX];
188
189                 /* cant use this, on linux gives bug: #23018, TODO: try LANG="en_US.UTF-8" /usr/bin/blender, suggested 22008 */
190                 /* mbstowcs(py_path_bundle_wchar, py_path_bundle, FILE_MAXDIR); */
191
192                 utf8towchar(py_path_bundle_wchar, py_path_bundle);
193
194                 Py_SetPythonHome(py_path_bundle_wchar);
195                 // printf("found python (wchar_t) '%ls'\n", py_path_bundle_wchar);
196         }
197 }
198
199
200
201 void BPY_context_set(bContext *C)
202 {
203         BPy_SetContext(C);
204 }
205
206 /* defined in AUD_C-API.cpp */
207 extern PyObject *AUD_initPython(void);
208
209 static struct _inittab bpy_internal_modules[]= {
210         {(char *)"noise", BPyInit_noise},
211         {(char *)"mathutils", BPyInit_mathutils},
212 //      {(char *)"mathutils.geometry", BPyInit_mathutils_geometry},
213         {(char *)"bgl", BPyInit_bgl},
214         {(char *)"blf", BPyInit_blf},
215         {(char *)"aud", AUD_initPython},
216         {NULL, NULL}
217 };
218
219 /* call BPY_context_set first */
220 void BPY_python_start(int argc, const char **argv)
221 {
222         PyThreadState *py_tstate = NULL;
223         
224         /* not essential but nice to set our name */
225         static wchar_t bprogname_wchar[FILE_MAXDIR+FILE_MAXFILE]; /* python holds a reference */
226         utf8towchar(bprogname_wchar, bprogname);
227         Py_SetProgramName(bprogname_wchar);
228
229         /* builtin modules */
230         PyImport_ExtendInittab(bpy_internal_modules);
231
232         bpy_python_start_path(); /* allow to use our own included python */
233
234         /* Python 3.2 now looks for '2.56/python/include/python3.2d/pyconfig.h' to parse
235          * from the 'sysconfig' module which is used by 'site', so for now disable site.
236          * alternatively we could copy the file. */
237         Py_NoSiteFlag= 1;
238
239         Py_Initialize(  );
240         
241         // PySys_SetArgv( argc, argv); // broken in py3, not a huge deal
242         /* sigh, why do python guys not have a char** version anymore? :( */
243         {
244                 int i;
245                 PyObject *py_argv= PyList_New(argc);
246                 for (i=0; i<argc; i++)
247                         PyList_SET_ITEM(py_argv, i, PyC_UnicodeFromByte(argv[i])); /* should fix bug #20021 - utf path name problems, by replacing PyUnicode_FromString */
248
249                 PySys_SetObject("argv", py_argv);
250                 Py_DECREF(py_argv);
251         }
252         
253         /* Initialize thread support (also acquires lock) */
254         PyEval_InitThreads();
255         
256         
257         /* bpy.* and lets us import it */
258         BPy_init_modules();
259
260         { /* our own import and reload functions */
261                 PyObject *item;
262                 PyObject *mod;
263                 //PyObject *m = PyImport_AddModule("__builtin__");
264                 //PyObject *d = PyModule_GetDict(m);
265                 PyObject *d = PyEval_GetBuiltins(  );
266 //              PyDict_SetItemString(d, "reload",               item=PyCFunction_New(&bpy_reload_meth, NULL));  Py_DECREF(item);
267                 PyDict_SetItemString(d, "__import__",   item=PyCFunction_New(&bpy_import_meth, NULL));  Py_DECREF(item);
268
269                 /* move reload here
270                  * XXX, use import hooks */
271                 mod= PyImport_ImportModuleLevel((char *)"imp", NULL, NULL, NULL, 0);
272                 if(mod) {
273                         PyDict_SetItemString(PyModule_GetDict(mod), "reload",           item=PyCFunction_New(&bpy_reload_meth, NULL));  Py_DECREF(item);
274                         Py_DECREF(mod);
275                 }
276                 else {
277                         BLI_assert(!"unable to load 'imp' module.");
278                 }
279
280         }
281         
282         pyrna_alloc_types();
283
284         py_tstate = PyGILState_GetThisThreadState();
285         PyEval_ReleaseThread(py_tstate);
286 }
287
288 void BPY_python_end(void)
289 {
290         // fprintf(stderr, "Ending Python!\n");
291
292         PyGILState_Ensure(); /* finalizing, no need to grab the state */
293         
294         // free other python data.
295         pyrna_free_types();
296
297         /* clear all python data from structs */
298         
299         Py_Finalize(  );
300         
301 #ifdef TIME_PY_RUN
302         // measure time since py started
303         bpy_timer = PIL_check_seconds_timer() - bpy_timer;
304
305         printf("*bpy stats* - ");
306         printf("tot exec: %d,  ", bpy_timer_count);
307         printf("tot run: %.4fsec,  ", bpy_timer_run_tot);
308         if(bpy_timer_count>0)
309                 printf("average run: %.6fsec,  ", (bpy_timer_run_tot/bpy_timer_count));
310
311         if(bpy_timer>0.0)
312                 printf("tot usage %.4f%%", (bpy_timer_run_tot/bpy_timer)*100.0);
313
314         printf("\n");
315
316         // fprintf(stderr, "Ending Python Done!\n");
317
318 #endif
319
320 }
321
322 /* super annoying, undo _PyModule_Clear(), bug [#23871] */
323 #define PYMODULE_CLEAR_WORKAROUND
324
325 #ifdef PYMODULE_CLEAR_WORKAROUND
326 /* bad!, we should never do this, but currently only safe way I could find to keep namespace.
327  * from being cleared. - campbell */
328 typedef struct {
329         PyObject_HEAD
330         PyObject *md_dict;
331         /* ommit other values, we only want the dict. */
332 } PyModuleObject;
333 #endif
334
335 static int python_script_exec(bContext *C, const char *fn, struct Text *text, struct ReportList *reports)
336 {
337         PyObject *main_mod= NULL;
338         PyObject *py_dict= NULL, *py_result= NULL;
339         PyGILState_STATE gilstate;
340
341         BLI_assert(fn || text);
342
343         if (fn==NULL && text==NULL) {
344                 return 0;
345         }
346
347         bpy_context_set(C, &gilstate);
348
349         PyC_MainModule_Backup(&main_mod);
350
351         if (text) {
352                 char fn_dummy[FILE_MAXDIR];
353                 bpy_text_filename_get(fn_dummy, text);
354
355                 if( !text->compiled ) { /* if it wasn't already compiled, do it now */
356                         char *buf = txt_to_buf( text );
357
358                         text->compiled =
359                                 Py_CompileString( buf, fn_dummy, Py_file_input );
360
361                         MEM_freeN( buf );
362
363                         if(PyErr_Occurred()) {
364                                 BPY_text_free_code(text);
365                         }
366                 }
367
368                 if(text->compiled) {
369                         py_dict = PyC_DefaultNameSpace(fn_dummy);
370                         py_result =  PyEval_EvalCode(text->compiled, py_dict, py_dict);
371                 }
372
373         }
374         else {
375                 FILE *fp= fopen(fn, "r");
376
377                 if(fp) {
378                         py_dict = PyC_DefaultNameSpace(fn);
379
380 #ifdef _WIN32
381                         /* Previously we used PyRun_File to run directly the code on a FILE
382                          * object, but as written in the Python/C API Ref Manual, chapter 2,
383                          * 'FILE structs for different C libraries can be different and
384                          * incompatible'.
385                          * So now we load the script file data to a buffer */
386                         {
387                                 char *pystring;
388
389                                 fclose(fp);
390
391                                 pystring= MEM_mallocN(strlen(fn) + 32, "pystring");
392                                 pystring[0]= '\0';
393                                 sprintf(pystring, "exec(open(r'%s').read())", fn);
394                                 py_result = PyRun_String( pystring, Py_file_input, py_dict, py_dict );
395                                 MEM_freeN(pystring);
396                         }
397 #else
398                         py_result = PyRun_File(fp, fn, Py_file_input, py_dict, py_dict);
399                         fclose(fp);
400 #endif
401                 }
402                 else {
403                         PyErr_Format(PyExc_IOError, "Python file \"%s\" could not be opened: %s", fn, strerror(errno));
404                         py_result= NULL;
405                 }
406         }
407
408         if (!py_result) {
409                 BPy_errors_to_report(reports);
410         } else {
411                 Py_DECREF( py_result );
412         }
413
414         if(py_dict) {
415 #ifdef PYMODULE_CLEAR_WORKAROUND
416                 PyModuleObject *mmod= (PyModuleObject *)PyDict_GetItemString(PyThreadState_GET()->interp->modules, "__main__");
417                 PyObject *dict_back = mmod->md_dict;
418                 /* freeing the module will clear the namespace,
419                  * gives problems running classes defined in this namespace being used later. */
420                 mmod->md_dict= NULL;
421                 Py_DECREF(dict_back);
422 #endif
423
424 #undef PYMODULE_CLEAR_WORKAROUND
425         }
426
427         PyC_MainModule_Restore(main_mod);
428
429         bpy_context_clear(C, &gilstate);
430
431         return (py_result != NULL);
432 }
433
434 /* Can run a file or text block */
435 int BPY_filepath_exec(bContext *C, const char *filepath, struct ReportList *reports)
436 {
437         return python_script_exec(C, filepath, NULL, reports);
438 }
439
440
441 int BPY_text_exec(bContext *C, struct Text *text, struct ReportList *reports)
442 {
443         return python_script_exec(C, NULL, text, reports);
444 }
445
446 void BPY_DECREF(void *pyob_ptr)
447 {
448         PyGILState_STATE gilstate = PyGILState_Ensure();
449         Py_DECREF((PyObject *)pyob_ptr);
450         PyGILState_Release(gilstate);
451 }
452
453 int BPY_button_exec(bContext *C, const char *expr, double *value)
454 {
455         PyGILState_STATE gilstate;
456         PyObject *py_dict, *mod, *retval;
457         int error_ret = 0;
458         PyObject *main_mod= NULL;
459         
460         if (!value || !expr) return -1;
461
462         if(expr[0]=='\0') {
463                 *value= 0.0;
464                 return error_ret;
465         }
466
467         bpy_context_set(C, &gilstate);
468
469         PyC_MainModule_Backup(&main_mod);
470
471         py_dict= PyC_DefaultNameSpace("<blender button>");
472
473         mod = PyImport_ImportModule("math");
474         if (mod) {
475                 PyDict_Merge(py_dict, PyModule_GetDict(mod), 0); /* 0 - dont overwrite existing values */
476                 Py_DECREF(mod);
477         }
478         else { /* highly unlikely but possibly */
479                 PyErr_Print();
480                 PyErr_Clear();
481         }
482         
483         retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict);
484         
485         if (retval == NULL) {
486                 error_ret= -1;
487         }
488         else {
489                 double val;
490
491                 if(PyTuple_Check(retval)) {
492                         /* Users my have typed in 10km, 2m
493                          * add up all values */
494                         int i;
495                         val= 0.0;
496
497                         for(i=0; i<PyTuple_GET_SIZE(retval); i++) {
498                                 val+= PyFloat_AsDouble(PyTuple_GET_ITEM(retval, i));
499                         }
500                 }
501                 else {
502                         val = PyFloat_AsDouble(retval);
503                 }
504                 Py_DECREF(retval);
505                 
506                 if(val==-1 && PyErr_Occurred()) {
507                         error_ret= -1;
508                 }
509                 else if (!finite(val)) {
510                         *value= 0.0;
511                 }
512                 else {
513                         *value= val;
514                 }
515         }
516         
517         if(error_ret) {
518                 BPy_errors_to_report(CTX_wm_reports(C));
519         }
520
521         PyC_MainModule_Backup(&main_mod);
522         
523         bpy_context_clear(C, &gilstate);
524         
525         return error_ret;
526 }
527
528 int BPY_string_exec(bContext *C, const char *expr)
529 {
530         PyGILState_STATE gilstate;
531         PyObject *main_mod= NULL;
532         PyObject *py_dict, *retval;
533         int error_ret = 0;
534
535         if (!expr) return -1;
536
537         if(expr[0]=='\0') {
538                 return error_ret;
539         }
540
541         bpy_context_set(C, &gilstate);
542
543         PyC_MainModule_Backup(&main_mod);
544
545         py_dict= PyC_DefaultNameSpace("<blender string>");
546
547         retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict);
548
549         if (retval == NULL) {
550                 error_ret= -1;
551
552                 BPy_errors_to_report(CTX_wm_reports(C));
553         }
554         else {
555                 Py_DECREF(retval);
556         }
557
558         PyC_MainModule_Restore(main_mod);
559
560         bpy_context_clear(C, &gilstate);
561         
562         return error_ret;
563 }
564
565
566 void BPY_modules_load_user(bContext *C)
567 {
568         PyGILState_STATE gilstate;
569         Main *bmain= CTX_data_main(C);
570         Text *text;
571
572         /* can happen on file load */
573         if(bmain==NULL)
574                 return;
575
576         bpy_context_set(C, &gilstate);
577
578         for(text=CTX_data_main(C)->text.first; text; text= text->id.next) {
579                 if(text->flags & TXT_ISSCRIPT && BLI_testextensie(text->id.name+2, ".py")) {
580                         if(!(G.f & G_SCRIPT_AUTOEXEC)) {
581                                 printf("scripts disabled for \"%s\", skipping '%s'\n", bmain->name, text->id.name+2);
582                         }
583                         else {
584                                 PyObject *module= bpy_text_import(text);
585
586                                 if (module==NULL) {
587                                         PyErr_Print();
588                                         PyErr_Clear();
589                                 }
590                                 else {
591                                         Py_DECREF(module);
592                                 }
593                         }
594                 }
595         }
596         bpy_context_clear(C, &gilstate);
597 }
598
599 int BPY_context_member_get(bContext *C, const char *member, bContextDataResult *result)
600 {
601         PyObject *pyctx= (PyObject *)CTX_py_dict_get(C);
602         PyObject *item= PyDict_GetItemString(pyctx, member);
603         PointerRNA *ptr= NULL;
604         int done= 0;
605
606         if(item==NULL) {
607                 /* pass */
608         }
609         else if(item==Py_None) {
610                 /* pass */
611         }
612         else if(BPy_StructRNA_Check(item)) {
613                 ptr= &(((BPy_StructRNA *)item)->ptr);
614
615                 //result->ptr= ((BPy_StructRNA *)item)->ptr;
616                 CTX_data_pointer_set(result, ptr->id.data, ptr->type, ptr->data);
617                 done= 1;
618         }
619         else if (PySequence_Check(item)) {
620                 PyObject *seq_fast= PySequence_Fast(item, "bpy_context_get sequence conversion");
621                 if (seq_fast==NULL) {
622                         PyErr_Print();
623                         PyErr_Clear();
624                 }
625                 else {
626                         int len= PySequence_Fast_GET_SIZE(seq_fast);
627                         int i;
628                         for(i = 0; i < len; i++) {
629                                 PyObject *list_item= PySequence_Fast_GET_ITEM(seq_fast, i);
630
631                                 if(BPy_StructRNA_Check(list_item)) {
632                                         /*
633                                         CollectionPointerLink *link= MEM_callocN(sizeof(CollectionPointerLink), "bpy_context_get");
634                                         link->ptr= ((BPy_StructRNA *)item)->ptr;
635                                         BLI_addtail(&result->list, link);
636                                         */
637                                         ptr= &(((BPy_StructRNA *)list_item)->ptr);
638                                         CTX_data_list_add(result, ptr->id.data, ptr->type, ptr->data);
639                                 }
640                                 else {
641                                         printf("List item not a valid type\n");
642                                 }
643
644                         }
645                         Py_DECREF(seq_fast);
646
647                         done= 1;
648                 }
649         }
650
651         if(done==0) {
652                 if (item)       printf("Context '%s' not a valid type\n", member);
653                 else            printf("Context '%s' not found\n", member);
654         }
655         else {
656                 printf("Context '%s' found\n", member);
657         }
658
659         return done;
660 }
661