edits for BPY_extern.h functions, no functional changes
[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 /* grr, python redefines */
27 #ifdef _POSIX_C_SOURCE
28 #undef _POSIX_C_SOURCE
29 #endif
30
31 #include <Python.h>
32
33 #include "bpy.h"
34 #include "bpy_rna.h"
35 #include "bpy_util.h"
36
37 #include "DNA_space_types.h"
38 #include "DNA_text_types.h"
39
40 #include "MEM_guardedalloc.h"
41 #include "BLI_path_util.h"
42 #include "BLI_math_base.h"
43 #include "BLI_string.h"
44
45 #include "BKE_utildefines.h"
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_modules_update(C); /* 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 /* context should be used but not now because it causes some bugs */
108 void bpy_context_clear(bContext *UNUSED(C), PyGILState_STATE *gilstate)
109 {
110         py_call_level--;
111
112         if(gilstate)
113                 PyGILState_Release(*gilstate);
114
115         if(py_call_level < 0) {
116                 fprintf(stderr, "ERROR: Python context internal state bug. this should not happen!\n");
117         }
118         else if(py_call_level==0) {
119                 // XXX - Calling classes currently wont store the context :\, cant set NULL because of this. but this is very flakey still.
120                 //BPy_SetContext(NULL);
121                 //bpy_import_main_set(NULL);
122
123 #ifdef TIME_PY_RUN
124                 bpy_timer_run_tot += PIL_check_seconds_timer() - bpy_timer_run;
125                 bpy_timer_count++;
126 #endif
127
128         }
129 }
130
131 void BPY_text_free_code(Text *text)
132 {
133         if( text->compiled ) {
134                 Py_DECREF( ( PyObject * ) text->compiled );
135                 text->compiled = NULL;
136         }
137 }
138
139 void BPY_modules_update(bContext *C)
140 {
141 #if 0 // slow, this runs all the time poll, draw etc 100's of time a sec.
142         PyObject *mod= PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL, 0);
143         PyModule_AddObject( mod, "data", BPY_rna_module() );
144         PyModule_AddObject( mod, "types", BPY_rna_types() ); // atm this does not need updating
145 #endif
146
147         /* refreshes the main struct */
148         BPY_update_rna_module();
149         bpy_context_module->ptr.data= (void *)C;
150 }
151
152 /* must be called before Py_Initialize */
153 void BPY_python_start_path(void)
154 {
155         char *py_path_bundle= BLI_get_folder(BLENDER_PYTHON, NULL);
156
157         if(py_path_bundle==NULL)
158                 return;
159
160         /* set the environment path */
161         printf("found bundled python: %s\n", py_path_bundle);
162
163 #ifdef __APPLE__
164         /* OSX allow file/directory names to contain : character (represented as / in the Finder)
165          but current Python lib (release 3.1.1) doesn't handle these correctly */
166         if(strchr(py_path_bundle, ':'))
167                 printf("Warning : Blender application is located in a path containing : or / chars\
168                            \nThis may make python import function fail\n");
169 #endif
170         
171 #ifdef _WIN32
172         /* cmake/MSVC debug build crashes without this, why only
173            in this case is unknown.. */
174         {
175                 BLI_setenv("PYTHONPATH", py_path_bundle);       
176         }
177 #endif
178
179         {
180                 static wchar_t py_path_bundle_wchar[FILE_MAX];
181
182                 /* cant use this, on linux gives bug: #23018, TODO: try LANG="en_US.UTF-8" /usr/bin/blender, suggested 22008 */
183                 /* mbstowcs(py_path_bundle_wchar, py_path_bundle, FILE_MAXDIR); */
184
185                 utf8towchar(py_path_bundle_wchar, py_path_bundle);
186
187                 Py_SetPythonHome(py_path_bundle_wchar);
188                 // printf("found python (wchar_t) '%ls'\n", py_path_bundle_wchar);
189         }
190 }
191
192
193
194 void BPY_context_set(bContext *C)
195 {
196         BPy_SetContext(C);
197 }
198
199 /* init-tab */
200 extern PyObject *BPyInit_noise(void);
201 extern PyObject *BPyInit_mathutils(void);
202 // extern PyObject *BPyInit_mathutils_geometry(void); // BPyInit_mathutils calls, py doesnt work with thos :S
203 extern PyObject *BPyInit_bgl(void);
204 extern PyObject *BPyInit_blf(void);
205 extern PyObject *AUD_initPython(void);
206
207 static struct _inittab bpy_internal_modules[]= {
208         {(char *)"noise", BPyInit_noise},
209         {(char *)"mathutils", BPyInit_mathutils},
210 //      {(char *)"mathutils.geometry", BPyInit_mathutils_geometry},
211         {(char *)"bgl", BPyInit_bgl},
212         {(char *)"blf", BPyInit_blf},
213         {(char *)"aud", AUD_initPython},
214         {NULL, NULL}
215 };
216
217 /* call BPY_context_set first */
218 void BPY_python_start( int argc, char **argv )
219 {
220         PyThreadState *py_tstate = NULL;
221         
222         /* not essential but nice to set our name */
223         static wchar_t bprogname_wchar[FILE_MAXDIR+FILE_MAXFILE]; /* python holds a reference */
224         utf8towchar(bprogname_wchar, bprogname);
225         Py_SetProgramName(bprogname_wchar);
226
227         /* builtin modules */
228         PyImport_ExtendInittab(bpy_internal_modules);
229
230         BPY_python_start_path(); /* allow to use our own included python */
231
232         Py_Initialize(  );
233         
234         // PySys_SetArgv( argc, argv); // broken in py3, not a huge deal
235         /* sigh, why do python guys not have a char** version anymore? :( */
236         {
237                 int i;
238                 PyObject *py_argv= PyList_New(argc);
239                 for (i=0; i<argc; i++)
240                         PyList_SET_ITEM(py_argv, i, PyC_UnicodeFromByte(argv[i])); /* should fix bug #20021 - utf path name problems, by replacing PyUnicode_FromString */
241
242                 PySys_SetObject("argv", py_argv);
243                 Py_DECREF(py_argv);
244         }
245         
246         /* Initialize thread support (also acquires lock) */
247         PyEval_InitThreads();
248         
249         
250         /* bpy.* and lets us import it */
251         BPy_init_modules();
252
253         { /* our own import and reload functions */
254                 PyObject *item;
255                 PyObject *mod;
256                 //PyObject *m = PyImport_AddModule("__builtin__");
257                 //PyObject *d = PyModule_GetDict(m);
258                 PyObject *d = PyEval_GetBuiltins(  );
259 //              PyDict_SetItemString(d, "reload",               item=PyCFunction_New(&bpy_reload_meth, NULL));  Py_DECREF(item);
260                 PyDict_SetItemString(d, "__import__",   item=PyCFunction_New(&bpy_import_meth, NULL));  Py_DECREF(item);
261
262                 /* move reload here
263                  * XXX, use import hooks */
264                 mod= PyImport_ImportModuleLevel((char *)"imp", NULL, NULL, NULL, 0);
265                 if(mod) {
266                         PyDict_SetItemString(PyModule_GetDict(mod), "reload",           item=PyCFunction_New(&bpy_reload_meth, NULL));  Py_DECREF(item);
267                         Py_DECREF(mod);
268                 }
269                 else {
270                         BKE_assert(!"unable to load 'imp' module.");
271                 }
272
273         }
274         
275         pyrna_alloc_types();
276
277         py_tstate = PyGILState_GetThisThreadState();
278         PyEval_ReleaseThread(py_tstate);
279 }
280
281 void BPY_python_end(void)
282 {
283         // fprintf(stderr, "Ending Python!\n");
284
285         PyGILState_Ensure(); /* finalizing, no need to grab the state */
286         
287         // free other python data.
288         pyrna_free_types();
289
290         /* clear all python data from structs */
291         
292         Py_Finalize(  );
293         
294 #ifdef TIME_PY_RUN
295         // measure time since py started
296         bpy_timer = PIL_check_seconds_timer() - bpy_timer;
297
298         printf("*bpy stats* - ");
299         printf("tot exec: %d,  ", bpy_timer_count);
300         printf("tot run: %.4fsec,  ", bpy_timer_run_tot);
301         if(bpy_timer_count>0)
302                 printf("average run: %.6fsec,  ", (bpy_timer_run_tot/bpy_timer_count));
303
304         if(bpy_timer>0.0)
305                 printf("tot usage %.4f%%", (bpy_timer_run_tot/bpy_timer)*100.0);
306
307         printf("\n");
308
309         // fprintf(stderr, "Ending Python Done!\n");
310
311 #endif
312
313 }
314
315 static int python_script_exec(bContext *C, const char *fn, struct Text *text, struct ReportList *reports)
316 {
317         PyObject *py_dict= NULL, *py_result= NULL;
318         PyGILState_STATE gilstate;
319
320         BKE_assert(fn || text);
321
322         if (fn==NULL && text==NULL) {
323                 return 0;
324         }
325
326         bpy_context_set(C, &gilstate);
327
328         if (text) {
329                 char fn_dummy[FILE_MAXDIR];
330                 bpy_text_filename_get(fn_dummy, text);
331
332                 if( !text->compiled ) { /* if it wasn't already compiled, do it now */
333                         char *buf = txt_to_buf( text );
334
335                         text->compiled =
336                                 Py_CompileString( buf, fn_dummy, Py_file_input );
337
338                         MEM_freeN( buf );
339
340                         if(PyErr_Occurred()) {
341                                 BPY_text_free_code(text);
342                         }
343                 }
344
345                 if(text->compiled) {
346                         py_dict = PyC_DefaultNameSpace(fn_dummy);
347                         py_result =  PyEval_EvalCode(text->compiled, py_dict, py_dict);
348                 }
349
350         }
351         else {
352                 FILE *fp= fopen(fn, "r");
353
354                 if(fp) {
355                         py_dict = PyC_DefaultNameSpace(fn);
356
357 #ifdef _WIN32
358                         /* Previously we used PyRun_File to run directly the code on a FILE
359                          * object, but as written in the Python/C API Ref Manual, chapter 2,
360                          * 'FILE structs for different C libraries can be different and
361                          * incompatible'.
362                          * So now we load the script file data to a buffer */
363                         {
364                                 char *pystring;
365
366                                 fclose(fp);
367
368                                 pystring= MEM_mallocN(strlen(fn) + 32, "pystring");
369                                 pystring[0]= '\0';
370                                 sprintf(pystring, "exec(open(r'%s').read())", fn);
371                                 py_result = PyRun_String( pystring, Py_file_input, py_dict, py_dict );
372                                 MEM_freeN(pystring);
373                         }
374 #else
375                         py_result = PyRun_File(fp, fn, Py_file_input, py_dict, py_dict);
376                         fclose(fp);
377 #endif
378                 }
379                 else {
380                         PyErr_Format(PyExc_SystemError, "Python file \"%s\" could not be opened: %s", fn, strerror(errno));
381                         py_result= NULL;
382                 }
383         }
384
385         if (!py_result) {
386                 BPy_errors_to_report(reports);
387         } else {
388                 Py_DECREF( py_result );
389         }
390
391 /* super annoying, undo _PyModule_Clear() */
392 #define PYMODULE_CLEAR_WORKAROUND
393
394         if(py_dict) {
395 #ifdef PYMODULE_CLEAR_WORKAROUND
396                 PyObject *py_dict_back= PyDict_Copy(py_dict);
397                 Py_INCREF(py_dict);
398 #endif
399                 /* normal */
400                 PyDict_SetItemString(PyThreadState_GET()->interp->modules, "__main__", Py_None);
401 #ifdef PYMODULE_CLEAR_WORKAROUND
402                 PyDict_Clear(py_dict);
403                 PyDict_Update(py_dict, py_dict_back);
404                 Py_DECREF(py_dict);
405                 Py_DECREF(py_dict_back);
406 #endif
407 #undef PYMODULE_CLEAR_WORKAROUND
408         }
409
410         bpy_context_clear(C, &gilstate);
411
412         return (py_result != NULL);
413 }
414
415 /* Can run a file or text block */
416 int BPY_filepath_exec(bContext *C, const char *filepath, struct ReportList *reports)
417 {
418         return python_script_exec(C, filepath, NULL, reports);
419 }
420
421
422 int BPY_text_exec(bContext *C, struct Text *text, struct ReportList *reports)
423 {
424         return python_script_exec(C, NULL, text, reports);
425 }
426
427 void BPY_DECREF(void *pyob_ptr)
428 {
429         PyGILState_STATE gilstate = PyGILState_Ensure();
430         Py_DECREF((PyObject *)pyob_ptr);
431         PyGILState_Release(gilstate);
432 }
433
434 int BPY_button_exec(bContext *C, const char *expr, double *value)
435 {
436         PyGILState_STATE gilstate;
437         PyObject *py_dict, *mod, *retval;
438         int error_ret = 0;
439         
440         if (!value || !expr) return -1;
441
442         if(expr[0]=='\0') {
443                 *value= 0.0;
444                 return error_ret;
445         }
446
447         bpy_context_set(C, &gilstate);
448         
449         py_dict= PyC_DefaultNameSpace("<blender button>");
450
451         mod = PyImport_ImportModule("math");
452         if (mod) {
453                 PyDict_Merge(py_dict, PyModule_GetDict(mod), 0); /* 0 - dont overwrite existing values */
454                 Py_DECREF(mod);
455         }
456         else { /* highly unlikely but possibly */
457                 PyErr_Print();
458                 PyErr_Clear();
459         }
460         
461         retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict);
462         
463         if (retval == NULL) {
464                 error_ret= -1;
465         }
466         else {
467                 double val;
468
469                 if(PyTuple_Check(retval)) {
470                         /* Users my have typed in 10km, 2m
471                          * add up all values */
472                         int i;
473                         val= 0.0;
474
475                         for(i=0; i<PyTuple_GET_SIZE(retval); i++) {
476                                 val+= PyFloat_AsDouble(PyTuple_GET_ITEM(retval, i));
477                         }
478                 }
479                 else {
480                         val = PyFloat_AsDouble(retval);
481                 }
482                 Py_DECREF(retval);
483                 
484                 if(val==-1 && PyErr_Occurred()) {
485                         error_ret= -1;
486                 }
487                 else if (!finite(val)) {
488                         *value= 0.0;
489                 }
490                 else {
491                         *value= val;
492                 }
493         }
494         
495         if(error_ret) {
496                 BPy_errors_to_report(CTX_wm_reports(C));
497         }
498
499         PyDict_SetItemString(PyThreadState_GET()->interp->modules, "__main__", Py_None);
500         
501         bpy_context_clear(C, &gilstate);
502         
503         return error_ret;
504 }
505
506 int BPY_string_exec(bContext *C, const char *expr)
507 {
508         PyGILState_STATE gilstate;
509         PyObject *py_dict, *retval;
510         int error_ret = 0;
511
512         if (!expr) return -1;
513
514         if(expr[0]=='\0') {
515                 return error_ret;
516         }
517
518         bpy_context_set(C, &gilstate);
519
520         py_dict= PyC_DefaultNameSpace("<blender string>");
521
522         retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict);
523
524         if (retval == NULL) {
525                 error_ret= -1;
526
527                 BPy_errors_to_report(CTX_wm_reports(C));
528         }
529         else {
530                 Py_DECREF(retval);
531         }
532
533         PyDict_SetItemString(PyThreadState_GET()->interp->modules, "__main__", Py_None);
534         
535         bpy_context_clear(C, &gilstate);
536         
537         return error_ret;
538 }
539
540
541 void BPY_modules_load_user(bContext *C)
542 {
543         PyGILState_STATE gilstate;
544         Main *bmain= CTX_data_main(C);
545         Text *text;
546
547         /* can happen on file load */
548         if(bmain==NULL)
549                 return;
550
551         bpy_context_set(C, &gilstate);
552
553         for(text=CTX_data_main(C)->text.first; text; text= text->id.next) {
554                 if(text->flags & TXT_ISSCRIPT && BLI_testextensie(text->id.name+2, ".py")) {
555                         if(!(G.f & G_SCRIPT_AUTOEXEC)) {
556                                 printf("scripts disabled for \"%s\", skipping '%s'\n", bmain->name, text->id.name+2);
557                         }
558                         else {
559                                 PyObject *module= bpy_text_import(text);
560
561                                 if (module==NULL) {
562                                         PyErr_Print();
563                                         PyErr_Clear();
564                                 }
565                                 else {
566                                         Py_DECREF(module);
567                                 }
568                         }
569                 }
570         }
571         bpy_context_clear(C, &gilstate);
572 }
573
574 int BPY_context_member_get(bContext *C, const char *member, bContextDataResult *result)
575 {
576         PyObject *pyctx= (PyObject *)CTX_py_dict_get(C);
577         PyObject *item= PyDict_GetItemString(pyctx, member);
578         PointerRNA *ptr= NULL;
579         int done= 0;
580
581         if(item==NULL) {
582                 /* pass */
583         }
584         else if(item==Py_None) {
585                 /* pass */
586         }
587         else if(BPy_StructRNA_Check(item)) {
588                 ptr= &(((BPy_StructRNA *)item)->ptr);
589
590                 //result->ptr= ((BPy_StructRNA *)item)->ptr;
591                 CTX_data_pointer_set(result, ptr->id.data, ptr->type, ptr->data);
592                 done= 1;
593         }
594         else if (PySequence_Check(item)) {
595                 PyObject *seq_fast= PySequence_Fast(item, "bpy_context_get sequence conversion");
596                 if (seq_fast==NULL) {
597                         PyErr_Print();
598                         PyErr_Clear();
599                 }
600                 else {
601                         int len= PySequence_Fast_GET_SIZE(seq_fast);
602                         int i;
603                         for(i = 0; i < len; i++) {
604                                 PyObject *list_item= PySequence_Fast_GET_ITEM(seq_fast, i);
605
606                                 if(BPy_StructRNA_Check(list_item)) {
607                                         /*
608                                         CollectionPointerLink *link= MEM_callocN(sizeof(CollectionPointerLink), "bpy_context_get");
609                                         link->ptr= ((BPy_StructRNA *)item)->ptr;
610                                         BLI_addtail(&result->list, link);
611                                         */
612                                         ptr= &(((BPy_StructRNA *)list_item)->ptr);
613                                         CTX_data_list_add(result, ptr->id.data, ptr->type, ptr->data);
614                                 }
615                                 else {
616                                         printf("List item not a valid type\n");
617                                 }
618
619                         }
620                         Py_DECREF(seq_fast);
621
622                         done= 1;
623                 }
624         }
625
626         if(done==0) {
627                 if (item)       printf("Context '%s' not a valid type\n", member);
628                 else            printf("Context '%s' not found\n", member);
629         }
630         else {
631                 printf("Context '%s' found\n", member);
632         }
633
634         return done;
635 }
636