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