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