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