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