merge with trunk at r27259 and commit of a patch by anthony jones to fix msvc (though...
[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 #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         /*convert argv to wchar_t*/
254         // PySys_SetArgv( argc, argv); // broken in py3, not a huge deal
255         
256         /*temporarily set argv*/
257         /* sigh, why do python guys not have a char** version anymore? :( */
258         {
259                 int i;
260 #if 0
261                 PyObject *py_argv= PyList_New(argc);
262                 for (i=0; i<argc; i++)
263                         PyList_SET_ITEM(py_argv, i, PyUnicode_FromString(argv[i]));
264
265 #else   // should fix bug #20021 - utf path name problems
266                 PyObject *py_argv= PyList_New(0);
267                 for (i=0; i<argc; i++) {
268                         PyObject *item= PyUnicode_Decode(argv[i], strlen(argv[i]), Py_FileSystemDefaultEncoding, NULL);
269                         if(item==NULL) { // should never happen
270                                 PyErr_Print();
271                                 PyErr_Clear();
272                         }
273                         else {
274                                 PyList_Append(py_argv, item);
275                                 Py_DECREF(item);
276                         }
277                 }
278 #endif
279                 PySys_SetObject("argv", py_argv);
280                 Py_DECREF(py_argv);
281         }
282         
283         /* Initialize thread support (also acquires lock) */
284         PyEval_InitThreads();
285         
286         
287         /* bpy.* and lets us import it */
288         BPy_init_modules();
289
290         { /* our own import and reload functions */
291                 PyObject *item;
292                 //PyObject *m = PyImport_AddModule("__builtin__");
293                 //PyObject *d = PyModule_GetDict(m);
294                 PyObject *d = PyEval_GetBuiltins(  );
295                 PyDict_SetItemString(d, "reload",               item=PyCFunction_New(bpy_reload_meth, NULL));   Py_DECREF(item);
296                 PyDict_SetItemString(d, "__import__",   item=PyCFunction_New(bpy_import_meth, NULL));   Py_DECREF(item);
297         }
298         
299         pyrna_alloc_types();
300
301         py_tstate = PyGILState_GetThisThreadState();
302         PyEval_ReleaseThread(py_tstate);
303 }
304
305 void BPY_end_python( void )
306 {
307         // fprintf(stderr, "Ending Python!\n");
308
309         PyGILState_Ensure(); /* finalizing, no need to grab the state */
310         
311         // free other python data.
312         pyrna_free_types();
313
314         /* clear all python data from structs */
315         
316         Py_Finalize(  );
317         
318 #ifdef TIME_PY_RUN
319         // measure time since py started
320         bpy_timer = PIL_check_seconds_timer() - bpy_timer;
321
322         printf("*bpy stats* - ");
323         printf("tot exec: %d,  ", bpy_timer_count);
324         printf("tot run: %.4fsec,  ", bpy_timer_run_tot);
325         if(bpy_timer_count>0)
326                 printf("average run: %.6fsec,  ", (bpy_timer_run_tot/bpy_timer_count));
327
328         if(bpy_timer>0.0)
329                 printf("tot usage %.4f%%", (bpy_timer_run_tot/bpy_timer)*100.0);
330
331         printf("\n");
332
333         // fprintf(stderr, "Ending Python Done!\n");
334
335 #endif
336
337 }
338
339 /* Can run a file or text block */
340 int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struct ReportList *reports)
341 {
342         PyObject *py_dict, *py_result= NULL;
343         PyGILState_STATE gilstate;
344         
345         if (fn==NULL && text==NULL) {
346                 return 0;
347         }
348         
349         bpy_context_set(C, &gilstate);
350         
351         py_dict = CreateGlobalDictionary(C, text?text->id.name+2:fn);
352
353         if (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, text->id.name+2, Py_file_input );
360
361                         MEM_freeN( buf );
362
363                         if( PyErr_Occurred(  ) ) {
364                                 BPY_free_compiled_text( text );
365                         }
366                 }
367                 if(text->compiled)
368                         py_result =  PyEval_EvalCode( text->compiled, py_dict, py_dict );
369                 
370         } else {
371                 FILE *fp= fopen(fn, "r");               
372                 if(fp) {
373 #ifdef _WIN32
374                         /* Previously we used PyRun_File to run directly the code on a FILE 
375                          * object, but as written in the Python/C API Ref Manual, chapter 2,
376                          * 'FILE structs for different C libraries can be different and 
377                          * incompatible'.
378                          * So now we load the script file data to a buffer */
379                         char *pystring;
380
381                         fclose(fp);
382
383                         pystring= MEM_mallocN(strlen(fn) + 32, "pystring");
384                         pystring[0]= '\0';
385                         sprintf(pystring, "exec(open(r'%s').read())", fn);
386                         py_result = PyRun_String( pystring, Py_file_input, py_dict, py_dict );
387                         MEM_freeN(pystring);
388 #else
389                         py_result = PyRun_File(fp, fn, Py_file_input, py_dict, py_dict);
390                         fclose(fp);
391 #endif
392                 }
393                 else {
394                         PyErr_Format(PyExc_SystemError, "Python file \"%s\" could not be opened: %s", fn, strerror(errno));
395                         py_result= NULL;
396                 }
397         }
398         
399         if (!py_result) {
400                 BPy_errors_to_report(reports);
401         } else {
402                 Py_DECREF( py_result );
403         }
404         
405         Py_DECREF(py_dict);
406         
407         bpy_context_clear(C, &gilstate);
408
409         return py_result ? 1:0;
410 }
411
412
413 /* TODO - move into bpy_space.c ? */
414 /* GUI interface routines */
415
416 /* Copied from Draw.c */
417 static void exit_pydraw( SpaceScript * sc, short err )
418 {
419         Script *script = NULL;
420
421         if( !sc || !sc->script )
422                 return;
423
424         script = sc->script;
425
426         if( err ) {
427                 BPy_errors_to_report(NULL); // TODO, reports
428                 script->flags = 0;      /* mark script struct for deletion */
429                 SCRIPT_SET_NULL(script);
430                 script->scriptname[0] = '\0';
431                 script->scriptarg[0] = '\0';
432 // XXX 2.5              error_pyscript();
433 // XXX 2.5              scrarea_queue_redraw( sc->area );
434         }
435
436 #if 0 // XXX 2.5
437         BPy_Set_DrawButtonsList(sc->but_refs);
438         BPy_Free_DrawButtonsList(); /*clear all temp button references*/
439 #endif
440
441         sc->but_refs = NULL;
442         
443         Py_XDECREF( ( PyObject * ) script->py_draw );
444         Py_XDECREF( ( PyObject * ) script->py_event );
445         Py_XDECREF( ( PyObject * ) script->py_button );
446
447         script->py_draw = script->py_event = script->py_button = NULL;
448 }
449
450 static int bpy_run_script_init(bContext *C, SpaceScript * sc)
451 {
452         if (sc->script==NULL) 
453                 return 0;
454         
455         if (sc->script->py_draw==NULL && sc->script->scriptname[0] != '\0')
456                 BPY_run_python_script(C, sc->script->scriptname, NULL, NULL);
457                 
458         if (sc->script->py_draw==NULL)
459                 return 0;
460         
461         return 1;
462 }
463
464 int BPY_run_script_space_draw(const struct bContext *C, SpaceScript * sc)
465 {
466         if (bpy_run_script_init( (bContext *)C, sc)) {
467                 PyGILState_STATE gilstate = PyGILState_Ensure();
468                 PyObject *result = PyObject_CallObject( sc->script->py_draw, NULL );
469                 
470                 if (result==NULL)
471                         exit_pydraw(sc, 1);
472                         
473                 PyGILState_Release(gilstate);
474         }
475         return 1;
476 }
477
478 // XXX - not used yet, listeners dont get a context
479 int BPY_run_script_space_listener(bContext *C, SpaceScript * sc)
480 {
481         if (bpy_run_script_init(C, sc)) {
482                 PyGILState_STATE gilstate = PyGILState_Ensure();
483                 
484                 PyObject *result = PyObject_CallObject( sc->script->py_draw, NULL );
485                 
486                 if (result==NULL)
487                         exit_pydraw(sc, 1);
488                         
489                 PyGILState_Release(gilstate);
490         }
491         return 1;
492 }
493
494 void BPY_DECREF(void *pyob_ptr)
495 {
496         PyGILState_STATE gilstate = PyGILState_Ensure();
497         Py_DECREF((PyObject *)pyob_ptr);
498         PyGILState_Release(gilstate);
499 }
500
501 #if 0
502 /* called from the the scripts window, assume context is ok */
503 int BPY_run_python_script_space(const char *modulename, const char *func)
504 {
505         PyObject *py_dict, *py_result= NULL;
506         char pystring[512];
507         PyGILState_STATE gilstate;
508         
509         /* for calling the module function */
510         PyObject *py_func, 
511         
512         gilstate = PyGILState_Ensure();
513         
514         py_dict = CreateGlobalDictionary(C);
515         
516         PyObject *module = PyImport_ImportModule(scpt->script.filename);
517         if (module==NULL) {
518                 PyErr_SetFormat(PyExc_SystemError, "could not import '%s'", scpt->script.filename);
519         }
520         else {
521                 py_func = PyObject_GetAttrString(modulename, func);
522                 if (py_func==NULL) {
523                         PyErr_SetFormat(PyExc_SystemError, "module has no function '%s.%s'\n", scpt->script.filename, func);
524                 }
525                 else {
526                         Py_DECREF(py_func);
527                         if (!PyCallable_Check(py_func)) {
528                                 PyErr_SetFormat(PyExc_SystemError, "module item is not callable '%s.%s'\n", scpt->script.filename, func);
529                         }
530                         else {
531                                 py_result= PyObject_CallObject(py_func, NULL); // XXX will need args eventually
532                         }
533                 }
534         }
535         
536         if (!py_result) {
537                 BPy_errors_to_report(NULL); // TODO - reports
538         } else
539                 Py_DECREF( py_result );
540         
541         Py_XDECREF(module);
542         
543         Py_DECREF(py_dict);
544         
545         PyGILState_Release(gilstate);
546         return 1;
547 }
548 #endif
549
550 // #define TIME_REGISTRATION
551
552 #ifdef TIME_REGISTRATION
553 #include "PIL_time.h"
554 #endif
555
556
557 int BPY_button_eval(bContext *C, char *expr, double *value)
558 {
559         PyGILState_STATE gilstate;
560         PyObject *dict, *mod, *retval;
561         int error_ret = 0;
562         
563         if (!value || !expr) return -1;
564
565         if(expr[0]=='\0') {
566                 *value= 0.0;
567                 return error_ret;
568         }
569
570         bpy_context_set(C, &gilstate);
571         
572         dict= CreateGlobalDictionary(C, NULL);
573
574         mod = PyImport_ImportModule("math");
575         if (mod) {
576                 PyDict_Merge(dict, PyModule_GetDict(mod), 0); /* 0 - dont overwrite existing values */
577                 Py_DECREF(mod);
578         }
579         else { /* highly unlikely but possibly */
580                 PyErr_Print();
581                 PyErr_Clear();
582         }
583         
584         retval = PyRun_String(expr, Py_eval_input, dict, dict);
585         
586         if (retval == NULL) {
587                 error_ret= -1;
588         }
589         else {
590                 double val;
591
592                 if(PyTuple_Check(retval)) {
593                         /* Users my have typed in 10km, 2m
594                          * add up all values */
595                         int i;
596                         val= 0.0;
597
598                         for(i=0; i<PyTuple_GET_SIZE(retval); i++) {
599                                 val+= PyFloat_AsDouble(PyTuple_GET_ITEM(retval, i));
600                         }
601                 }
602                 else {
603                         val = PyFloat_AsDouble(retval);
604                 }
605                 Py_DECREF(retval);
606                 
607                 if(val==-1 && PyErr_Occurred()) {
608                         error_ret= -1;
609                 }
610                 else {
611                         *value= val;
612                 }
613         }
614         
615         if(error_ret) {
616                 BPy_errors_to_report(CTX_wm_reports(C));
617         }
618         
619         Py_DECREF(dict);
620         bpy_context_clear(C, &gilstate);
621         
622         return error_ret;
623 }
624
625 void BPY_load_user_modules(bContext *C)
626 {
627         PyGILState_STATE gilstate;
628         Main *bmain= CTX_data_main(C);
629         Text *text;
630
631         /* can happen on file load */
632         if(bmain==NULL)
633                 return;
634
635         bpy_context_set(C, &gilstate);
636
637         for(text=CTX_data_main(C)->text.first; text; text= text->id.next) {
638                 if(text->flags & TXT_ISSCRIPT && BLI_testextensie(text->id.name+2, ".py")) {
639                         if(!(G.f & G_SCRIPT_AUTOEXEC)) {
640                                 printf("scripts disabled for \"%s\", skipping '%s'\n", bmain->name, text->id.name+2);
641                         }
642                         else {
643                                 PyObject *module= bpy_text_import(text);
644
645                                 if (module==NULL) {
646                                         PyErr_Print();
647                                         PyErr_Clear();
648                                 }
649                                 else {
650                                         Py_DECREF(module);
651                                 }
652                         }
653                 }
654         }
655         bpy_context_clear(C, &gilstate);
656 }
657
658 int BPY_context_get(bContext *C, const char *member, bContextDataResult *result)
659 {
660         PyObject *pyctx= (PyObject *)CTX_py_dict_get(C);
661         PyObject *item= PyDict_GetItemString(pyctx, member);
662         PointerRNA *ptr= NULL;
663         int done= 0;
664
665         if(item==NULL) {
666                 /* pass */
667         }
668         else if(item==Py_None) {
669                 /* pass */
670         }
671         else if(BPy_StructRNA_Check(item)) {
672                 ptr= &(((BPy_StructRNA *)item)->ptr);
673
674                 //result->ptr= ((BPy_StructRNA *)item)->ptr;
675                 CTX_data_pointer_set(result, ptr->id.data, ptr->type, ptr->data);
676                 done= 1;
677         }
678         else if (PySequence_Check(item)) {
679                 PyObject *seq_fast= PySequence_Fast(item, "bpy_context_get sequence conversion");
680                 if (seq_fast==NULL) {
681                         PyErr_Print();
682                         PyErr_Clear();
683                 }
684                 else {
685                         int len= PySequence_Fast_GET_SIZE(seq_fast);
686                         int i;
687                         for(i = 0; i < len; i++) {
688                                 PyObject *list_item= PySequence_Fast_GET_ITEM(seq_fast, i);
689
690                                 if(BPy_StructRNA_Check(list_item)) {
691                                         /*
692                                         CollectionPointerLink *link= MEM_callocN(sizeof(CollectionPointerLink), "bpy_context_get");
693                                         link->ptr= ((BPy_StructRNA *)item)->ptr;
694                                         BLI_addtail(&result->list, link);
695                                         */
696                                         ptr= &(((BPy_StructRNA *)list_item)->ptr);
697                                         CTX_data_list_add(result, ptr->id.data, ptr->type, ptr->data);
698                                 }
699                                 else {
700                                         printf("List item not a valid type\n");
701                                 }
702
703                         }
704                         Py_DECREF(seq_fast);
705
706                         done= 1;
707                 }
708         }
709
710         if(done==0) {
711                 if (item)       printf("Context '%s' not a valid type\n", member);
712                 else            printf("Context '%s' not found\n", member);
713         }
714         else {
715                 printf("Context '%s' found\n", member);
716         }
717
718         return done;
719 }
720