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