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