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