merged changes to revision 24446
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * Contributor(s): Michel Selten, Willian P. Germano, Stephen Swaney,
21  * Chris Keith, Chris Want, Ken Hughes, Campbell Barton
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25  
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/stat.h>
30
31
32 /* grr, python redefines */
33 #ifdef _POSIX_C_SOURCE
34 #undef _POSIX_C_SOURCE
35 #endif
36
37 #include <Python.h>
38 #include "compile.h"            /* for the PyCodeObject */
39 #include "eval.h"               /* for PyEval_EvalCode */
40
41 #include "bpy_rna.h"
42 #include "bpy_operator.h"
43 #include "bpy_ui.h"
44 #include "bpy_util.h"
45
46 #ifndef WIN32
47 #include <dirent.h>
48 #else
49 #include "BLI_winstuff.h"
50 #endif
51
52 #include "DNA_anim_types.h"
53 #include "DNA_space_types.h"
54 #include "DNA_text_types.h"
55
56 #include "MEM_guardedalloc.h"
57
58 #include "BLI_util.h"
59 #include "BLI_storage.h"
60 #include "BLI_fileops.h"
61 #include "BLI_string.h"
62
63 #include "BKE_context.h"
64 #include "BKE_fcurve.h"
65 #include "BKE_text.h"
66 #include "BKE_context.h"
67
68 #include "BPY_extern.h"
69
70 #include "BPy_Freestyle.h"
71
72 #include "../generic/bpy_internal_import.h" // our own imports
73 /* external util modules */
74
75 #include "../generic/Mathutils.h"
76 #include "../generic/Geometry.h"
77 #include "../generic/BGL.h"
78
79
80 /* for internal use, when starting and ending python scripts */
81
82 /* incase a python script triggers another python call, stop bpy_context_clear from invalidating */
83 static int py_call_level= 0;
84
85
86 // only for tests
87 #define TIME_PY_RUN
88
89 #ifdef TIME_PY_RUN
90 #include "PIL_time.h"
91 static int              bpy_timer_count = 0;
92 static double   bpy_timer; /* time since python starts */
93 static double   bpy_timer_run; /* time for each python script run */
94 static double   bpy_timer_run_tot; /* accumulate python runs */
95 #endif
96
97 void bpy_context_set(bContext *C, PyGILState_STATE *gilstate)
98 {
99         py_call_level++;
100
101         if(gilstate)
102                 *gilstate = PyGILState_Ensure();
103
104         if(py_call_level==1) {
105
106                 if(C) { // XXX - should always be true.
107                         BPy_SetContext(C);
108                         bpy_import_main_set(CTX_data_main(C));
109                 }
110                 else {
111                         fprintf(stderr, "ERROR: Python context called with a NULL Context. this should not happen!\n");
112                 }
113
114                 BPY_update_modules(); /* can give really bad results if this isnt here */
115
116 #ifdef TIME_PY_RUN
117                 if(bpy_timer_count==0) {
118                         /* record time from the beginning */
119                         bpy_timer= PIL_check_seconds_timer();
120                         bpy_timer_run = bpy_timer_run_tot = 0.0;
121                 }
122                 bpy_timer_run= PIL_check_seconds_timer();
123
124
125                 bpy_timer_count++;
126 #endif
127         }
128 }
129
130 void bpy_context_clear(bContext *C, PyGILState_STATE *gilstate)
131 {
132         py_call_level--;
133
134         if(gilstate)
135                 PyGILState_Release(*gilstate);
136
137         if(py_call_level < 0) {
138                 fprintf(stderr, "ERROR: Python context internal state bug. this should not happen!\n");
139         }
140         else if(py_call_level==0) {
141                 // XXX - Calling classes currently wont store the context :\, cant set NULL because of this. but this is very flakey still.
142                 //BPy_SetContext(NULL);
143                 //bpy_import_main_set(NULL);
144
145 #ifdef TIME_PY_RUN
146                 bpy_timer_run_tot += PIL_check_seconds_timer() - bpy_timer_run;
147                 bpy_timer_count++;
148 #endif
149
150         }
151 }
152
153 static void bpy_import_test(char *modname)
154 {
155         PyObject *mod= PyImport_ImportModuleLevel(modname, NULL, NULL, NULL, 0);
156         if(mod) {
157                 Py_DECREF(mod);
158         }
159         else {
160                 PyErr_Print();
161                 PyErr_Clear();
162         }       
163 }
164
165 void BPY_free_compiled_text( struct Text *text )
166 {
167         if( text->compiled ) {
168                 Py_DECREF( ( PyObject * ) text->compiled );
169                 text->compiled = NULL;
170         }
171 }
172
173 /*****************************************************************************
174 * Description: Creates the bpy module and adds it to sys.modules for importing
175 *****************************************************************************/
176 static BPy_StructRNA *bpy_context_module= NULL; /* for fast access */
177 static void bpy_init_modules( void )
178 {
179         PyObject *mod;
180         
181         /* Needs to be first since this dir is needed for future modules */
182         char *modpath= BLI_gethome_folder("scripts/modules", BLI_GETHOME_ALL);
183         if(modpath) {
184                 PyObject *sys_path= PySys_GetObject("path"); /* borrow */
185                 PyObject *py_modpath= PyUnicode_FromString(modpath);
186                 PyList_Insert(sys_path, 0, py_modpath); /* add first */
187                 Py_DECREF(py_modpath);
188         }
189         
190         mod = PyModule_New("bpy");
191
192         /* add the module so we can import it */
193         PyDict_SetItemString(PySys_GetObject("modules"), "bpy", mod);
194         Py_DECREF(mod);
195
196         /* run first, initializes rna types */
197         BPY_rna_init();
198
199         PyModule_AddObject( mod, "types", BPY_rna_types() ); /* needs to be first so bpy_types can run */
200         bpy_import_test("bpy_types");
201         PyModule_AddObject( mod, "data", BPY_rna_module() ); /* imports bpy_types by running this */
202         bpy_import_test("bpy_types");
203         /* PyModule_AddObject( mod, "doc", BPY_rna_doc() ); */
204         PyModule_AddObject( mod, "props", BPY_rna_props() );
205         PyModule_AddObject( mod, "__ops__", BPY_operator_module() ); /* ops is now a python module that does the conversion from SOME_OT_foo -> some.foo */
206         PyModule_AddObject( mod, "ui", BPY_ui_module() ); // XXX very experimental, consider this a test, especially PyCObject is not meant to be permanent
207
208
209
210         /* bpy context */
211         {
212                 bpy_context_module= ( BPy_StructRNA * ) PyObject_NEW( BPy_StructRNA, &pyrna_struct_Type );
213                 RNA_pointer_create(NULL, &RNA_Context, NULL, &bpy_context_module->ptr);
214
215                 PyModule_AddObject(mod, "context", (PyObject *)bpy_context_module);
216         }
217
218         /* stand alone utility modules not related to blender directly */
219         Geometry_Init();
220         Mathutils_Init();
221         BGL_Init();
222         Freestyle_Init();
223
224         /* add our own modules dir */
225         {
226                 bpy_import_test("bpy_ops"); /* adds its self to bpy.ops */
227                 bpy_import_test("bpy_utils"); /* adds its self to bpy.sys */
228         }
229 }
230
231 void BPY_update_modules( void )
232 {
233 #if 0 // slow, this runs all the time poll, draw etc 100's of time a sec.
234         PyObject *mod= PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL, 0);
235         PyModule_AddObject( mod, "data", BPY_rna_module() );
236         PyModule_AddObject( mod, "types", BPY_rna_types() ); // atm this does not need updating
237 #endif
238
239         /* refreshes the main struct */
240         BPY_update_rna_module();
241         bpy_context_module->ptr.data= (void *)BPy_GetContext();
242 }
243
244 /*****************************************************************************
245 * Description: This function creates a new Python dictionary object.
246 *****************************************************************************/
247 static PyObject *CreateGlobalDictionary( bContext *C )
248 {
249         PyObject *mod;
250         PyObject *dict = PyDict_New(  );
251         PyObject *item = PyUnicode_FromString( "__main__" );
252         PyDict_SetItemString( dict, "__builtins__", PyEval_GetBuiltins(  ) );
253         PyDict_SetItemString( dict, "__name__", item );
254         Py_DECREF(item);
255         
256         // XXX - put somewhere more logical
257         {
258                 PyMethodDef *ml;
259                 static PyMethodDef bpy_prop_meths[] = {
260                         {"FloatProperty", (PyCFunction)BPy_FloatProperty, METH_VARARGS|METH_KEYWORDS, ""},
261                         {"IntProperty", (PyCFunction)BPy_IntProperty, METH_VARARGS|METH_KEYWORDS, ""},
262                         {"BoolProperty", (PyCFunction)BPy_BoolProperty, METH_VARARGS|METH_KEYWORDS, ""},
263                         {"StringProperty", (PyCFunction)BPy_StringProperty, METH_VARARGS|METH_KEYWORDS, ""},
264                         {"EnumProperty", (PyCFunction)BPy_EnumProperty, METH_VARARGS|METH_KEYWORDS, ""},
265                         {"PointerProperty", (PyCFunction)BPy_PointerProperty, METH_VARARGS|METH_KEYWORDS, ""},
266                         {"CollectionProperty", (PyCFunction)BPy_CollectionProperty, METH_VARARGS|METH_KEYWORDS, ""},
267                         {NULL, NULL, 0, NULL}
268                 };
269                 
270                 for(ml = bpy_prop_meths; ml->ml_name; ml++) {
271                         PyDict_SetItemString( dict, ml->ml_name, PyCFunction_New(ml, NULL));
272                 }
273         }
274         
275         /* add bpy to global namespace */
276         mod= PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL, 0);
277         PyDict_SetItemString( dict, "bpy", mod );
278         Py_DECREF(mod);
279         
280         return dict;
281 }
282
283 /* must be called before Py_Initialize */
284 void BPY_start_python_path(void)
285 {
286         char *py_path_bundle= BLI_gethome_folder("python", BLI_GETHOME_ALL);
287
288         if(py_path_bundle==NULL)
289                 return;
290
291         /* set the environment path */
292         printf("found bundled python: %s\n", py_path_bundle);
293
294 #if 0
295         BLI_setenv("PYTHONHOME", py_path_bundle);
296         BLI_setenv("PYTHONPATH", py_path_bundle);
297 #endif
298
299         {
300                 static wchar_t py_path_bundle_wchar[FILE_MAXDIR];
301
302                 mbstowcs(py_path_bundle_wchar, py_path_bundle, FILE_MAXDIR);
303                 Py_SetPythonHome(py_path_bundle_wchar);
304         }
305 }
306
307
308 void BPY_start_python( int argc, char **argv )
309 {
310         PyThreadState *py_tstate = NULL;
311         
312         BPY_start_python_path(); /* allow to use our own included python */
313
314         Py_Initialize(  );
315         
316         // PySys_SetArgv( argc, argv); // broken in py3, not a huge deal
317         /* sigh, why do python guys not have a char** version anymore? :( */
318         {
319                 int i;
320                 PyObject *py_argv= PyList_New(argc);
321
322                 for (i=0; i<argc; i++)
323                         PyList_SET_ITEM(py_argv, i, PyUnicode_FromString(argv[i]));
324
325                 PySys_SetObject("argv", py_argv);
326                 Py_DECREF(py_argv);
327         }
328         
329         /* Initialize thread support (also acquires lock) */
330         PyEval_InitThreads();
331         
332         
333         /* bpy.* and lets us import it */
334         bpy_init_modules(); 
335
336         { /* our own import and reload functions */
337                 PyObject *item;
338                 //PyObject *m = PyImport_AddModule("__builtin__");
339                 //PyObject *d = PyModule_GetDict(m);
340                 PyObject *d = PyEval_GetBuiltins(  );
341                 PyDict_SetItemString(d, "reload",               item=PyCFunction_New(bpy_reload_meth, NULL));   Py_DECREF(item);
342                 PyDict_SetItemString(d, "__import__",   item=PyCFunction_New(bpy_import_meth, NULL));   Py_DECREF(item);
343                 
344                 /* a bit nasty but this prevents help() and input() from locking blender
345                  * Ideally we could have some way for the console to replace sys.stdin but
346                  * python would lock blender while waiting for a return value, not easy :| */
347                 PySys_SetObject("stdin", Py_None);
348         }
349         
350         pyrna_alloc_types();
351
352         py_tstate = PyGILState_GetThisThreadState();
353         PyEval_ReleaseThread(py_tstate);
354 }
355
356 void BPY_end_python( void )
357 {
358         // fprintf(stderr, "Ending Python!\n");
359
360         PyGILState_Ensure(); /* finalizing, no need to grab the state */
361         
362         // free other python data.
363         pyrna_free_types();
364
365         /* clear all python data from structs */
366         
367         Py_Finalize(  );
368         
369 #ifdef TIME_PY_RUN
370         // measure time since py started
371         bpy_timer = PIL_check_seconds_timer() - bpy_timer;
372
373         printf("*bpy stats* - ");
374         printf("tot exec: %d,  ", bpy_timer_count);
375         printf("tot run: %.4fsec,  ", bpy_timer_run_tot);
376         if(bpy_timer_count>0)
377                 printf("average run: %.6fsec,  ", (bpy_timer_run_tot/bpy_timer_count));
378
379         if(bpy_timer>0.0)
380                 printf("tot usage %.4f%%", (bpy_timer_run_tot/bpy_timer)*100.0);
381
382         printf("\n");
383
384         // fprintf(stderr, "Ending Python Done!\n");
385
386 #endif
387
388 }
389
390 /* Can run a file or text block */
391 int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struct ReportList *reports)
392 {
393         PyObject *py_dict, *py_result= NULL;
394         PyGILState_STATE gilstate;
395         
396         if (fn==NULL && text==NULL) {
397                 return 0;
398         }
399         
400         bpy_context_set(C, &gilstate);
401         
402         py_dict = CreateGlobalDictionary(C);
403
404         if (text) {
405                 
406                 if( !text->compiled ) { /* if it wasn't already compiled, do it now */
407                         char *buf = txt_to_buf( text );
408
409                         text->compiled =
410                                 Py_CompileString( buf, text->id.name+2, Py_file_input );
411
412                         MEM_freeN( buf );
413
414                         if( PyErr_Occurred(  ) ) {
415                                 BPY_free_compiled_text( text );
416                         }
417                 }
418                 if(text->compiled)
419                         py_result =  PyEval_EvalCode( text->compiled, py_dict, py_dict );
420                 
421         } else {
422 #if 0
423                 char *pystring;
424                 pystring= malloc(strlen(fn) + 32);
425                 pystring[0]= '\0';
426                 sprintf(pystring, "exec(open(r'%s').read())", fn);
427                 py_result = PyRun_String( pystring, Py_file_input, py_dict, py_dict );
428                 free(pystring);
429 #else
430                 FILE *fp= fopen(fn, "r");               
431                 if(fp) {
432                         py_result = PyRun_File(fp, fn, Py_file_input, py_dict, py_dict);
433                         fclose(fp);
434                 }
435                 else {
436                         PyErr_Format(PyExc_SystemError, "Python file \"%s\" could not be opened: %s", fn, strerror(errno));
437                         py_result= NULL;
438                 }
439 #endif
440         }
441         
442         if (!py_result) {
443                 BPy_errors_to_report(reports);
444         } else {
445                 Py_DECREF( py_result );
446         }
447         
448         Py_DECREF(py_dict);
449         
450         bpy_context_clear(C, &gilstate);
451
452         return py_result ? 1:0;
453 }
454
455
456 /* TODO - move into bpy_space.c ? */
457 /* GUI interface routines */
458
459 /* Copied from Draw.c */
460 static void exit_pydraw( SpaceScript * sc, short err )
461 {
462         Script *script = NULL;
463
464         if( !sc || !sc->script )
465                 return;
466
467         script = sc->script;
468
469         if( err ) {
470                 BPy_errors_to_report(NULL); // TODO, reports
471                 script->flags = 0;      /* mark script struct for deletion */
472                 SCRIPT_SET_NULL(script);
473                 script->scriptname[0] = '\0';
474                 script->scriptarg[0] = '\0';
475 // XXX 2.5              error_pyscript();
476 // XXX 2.5              scrarea_queue_redraw( sc->area );
477         }
478
479 #if 0 // XXX 2.5
480         BPy_Set_DrawButtonsList(sc->but_refs);
481         BPy_Free_DrawButtonsList(); /*clear all temp button references*/
482 #endif
483
484         sc->but_refs = NULL;
485         
486         Py_XDECREF( ( PyObject * ) script->py_draw );
487         Py_XDECREF( ( PyObject * ) script->py_event );
488         Py_XDECREF( ( PyObject * ) script->py_button );
489
490         script->py_draw = script->py_event = script->py_button = NULL;
491 }
492
493 static int bpy_run_script_init(bContext *C, SpaceScript * sc)
494 {
495         if (sc->script==NULL) 
496                 return 0;
497         
498         if (sc->script->py_draw==NULL && sc->script->scriptname[0] != '\0')
499                 BPY_run_python_script(C, sc->script->scriptname, NULL, NULL);
500                 
501         if (sc->script->py_draw==NULL)
502                 return 0;
503         
504         return 1;
505 }
506
507 int BPY_run_script_space_draw(const struct bContext *C, SpaceScript * sc)
508 {
509         if (bpy_run_script_init( (bContext *)C, sc)) {
510                 PyGILState_STATE gilstate = PyGILState_Ensure();
511                 PyObject *result = PyObject_CallObject( sc->script->py_draw, NULL );
512                 
513                 if (result==NULL)
514                         exit_pydraw(sc, 1);
515                         
516                 PyGILState_Release(gilstate);
517         }
518         return 1;
519 }
520
521 // XXX - not used yet, listeners dont get a context
522 int BPY_run_script_space_listener(bContext *C, SpaceScript * sc)
523 {
524         if (bpy_run_script_init(C, sc)) {
525                 PyGILState_STATE gilstate = PyGILState_Ensure();
526                 
527                 PyObject *result = PyObject_CallObject( sc->script->py_draw, NULL );
528                 
529                 if (result==NULL)
530                         exit_pydraw(sc, 1);
531                         
532                 PyGILState_Release(gilstate);
533         }
534         return 1;
535 }
536
537 void BPY_DECREF(void *pyob_ptr)
538 {
539         Py_DECREF((PyObject *)pyob_ptr);
540 }
541
542 #if 0
543 /* called from the the scripts window, assume context is ok */
544 int BPY_run_python_script_space(const char *modulename, const char *func)
545 {
546         PyObject *py_dict, *py_result= NULL;
547         char pystring[512];
548         PyGILState_STATE gilstate;
549         
550         /* for calling the module function */
551         PyObject *py_func, 
552         
553         gilstate = PyGILState_Ensure();
554         
555         py_dict = CreateGlobalDictionary(C);
556         
557         PyObject *module = PyImport_ImportModule(scpt->script.filename);
558         if (module==NULL) {
559                 PyErr_SetFormat(PyExc_SystemError, "could not import '%s'", scpt->script.filename);
560         }
561         else {
562                 py_func = PyObject_GetAttrString(modulename, func);
563                 if (py_func==NULL) {
564                         PyErr_SetFormat(PyExc_SystemError, "module has no function '%s.%s'\n", scpt->script.filename, func);
565                 }
566                 else {
567                         Py_DECREF(py_func);
568                         if (!PyCallable_Check(py_func)) {
569                                 PyErr_SetFormat(PyExc_SystemError, "module item is not callable '%s.%s'\n", scpt->script.filename, func);
570                         }
571                         else {
572                                 py_result= PyObject_CallObject(py_func, NULL); // XXX will need args eventually
573                         }
574                 }
575         }
576         
577         if (!py_result) {
578                 BPy_errors_to_report(NULL); // TODO - reports
579         } else
580                 Py_DECREF( py_result );
581         
582         Py_XDECREF(module);
583         
584         Py_DECREF(py_dict);
585         
586         PyGILState_Release(gilstate);
587         return 1;
588 }
589 #endif
590
591 // #define TIME_REGISTRATION
592
593 #ifdef TIME_REGISTRATION
594 #include "PIL_time.h"
595 #endif
596
597 /* for use by BPY_run_ui_scripts only */
598 static int bpy_import_module(char *modname, int reload)
599 {
600         PyObject *mod= PyImport_ImportModuleLevel(modname, NULL, NULL, NULL, 0);
601         if (mod) {
602                 if (reload) {
603                         PyObject *mod_orig= mod;
604                         mod= PyImport_ReloadModule(mod);
605                         Py_DECREF(mod_orig);
606                 }
607         }
608
609         if(mod) {
610                 Py_DECREF(mod); /* could be NULL from reloading */
611                 return 0;
612         } else {
613                 return -1;
614         }
615 }
616
617 /* XXX this is temporary, need a proper script registration system for 2.5 */
618 void BPY_run_ui_scripts(bContext *C, int reload)
619 {
620 #ifdef TIME_REGISTRATION
621         double time = PIL_check_seconds_timer();
622 #endif
623         DIR *dir; 
624         struct dirent *de;
625         char *file_extension;
626         char *dirname;
627         char path[FILE_MAX];
628         char *dirs[] = {"scripts/ui", "scripts/op", "scripts/io", NULL};
629         int path_flags[] = {BLI_GETHOME_LOCAL|BLI_GETHOME_SYSTEM, BLI_GETHOME_USER}; /* SYSTEM / NON-SYSTEM */
630         int a, err, flag_iter;
631         
632         PyGILState_STATE gilstate;
633         PyObject *sys_path;
634
635         bpy_context_set(C, &gilstate);
636
637         sys_path= PySys_GetObject("path"); /* borrow */
638         PyList_Insert(sys_path, 0, Py_None); /* place holder, resizes the list */
639
640         /* Scan system scripts first, then local/user */
641         for(flag_iter=0; flag_iter < sizeof(path_flags)/sizeof(int); flag_iter++) {
642                 
643                 for(a=0; dirs[a]; a++) {
644                         dirname= BLI_gethome_folder(dirs[a], path_flags[flag_iter]);
645
646                         if(!dirname)
647                                 continue;
648
649                         dir = opendir(dirname);
650
651                         if(!dir)
652                                 continue;
653                         
654                         /* set the first dir in the sys.path for fast importing of modules */
655                         PyList_SetItem(sys_path, 0, PyUnicode_FromString(dirname)); /* steals the ref */
656                                 
657                         while((de = readdir(dir)) != NULL) {
658                                 /* We could stat the file but easier just to let python
659                                  * import it and complain if theres a problem */
660                                 err = 0;
661
662                                 if (de->d_name[0] == '.') {
663                                         /* do nothing, probably .svn */
664                                 }
665                                 else if ((file_extension = strstr(de->d_name, ".py"))) {
666                                         /* normal py files? */
667                                         if(file_extension && file_extension[3] == '\0') {
668                                                 de->d_name[(file_extension - de->d_name)] = '\0';
669                                                 err= bpy_import_module(de->d_name, reload);
670                                         }
671                                 }
672 #ifndef __linux__
673                                 else if( BLI_join_dirfile(path, dirname, de->d_name), S_ISDIR(BLI_exist(path))) {
674 #else
675                                 else if(de->d_type==DT_DIR) {
676                                         BLI_join_dirfile(path, dirname, de->d_name);
677 #endif
678                                         /* support packages */
679                                         BLI_join_dirfile(path, path, "__init__.py");
680
681                                         if(BLI_exists(path)) {
682                                                 err= bpy_import_module(de->d_name, reload);
683                                         }
684                                 }
685
686                                 if(err==-1) {
687                                         BPy_errors_to_report(NULL);
688                                         fprintf(stderr, "unable to import %s/%s\n", dirname, de->d_name);
689                                 }
690                         }
691
692                         closedir(dir);
693                 }
694         }
695         
696         PyList_SetSlice(sys_path, 0, 1, NULL); /* remove the first item */
697
698         bpy_context_clear(C, &gilstate);
699         
700 #ifdef TIME_REGISTRATION
701         printf("script time %f\n", (PIL_check_seconds_timer()-time));
702 #endif
703
704         /* reset the timer so as not to take loading into the stats */
705         bpy_timer_count = 0;
706 }
707
708 /* ****************************************** */
709 /* Drivers - PyExpression Evaluation */
710
711 /* for pydrivers (drivers using one-line Python expressions to express relationships between targets) */
712 PyObject *bpy_pydriver_Dict = NULL;
713
714 /* For faster execution we keep a special dictionary for pydrivers, with
715  * the needed modules and aliases. 
716  */
717 static int bpy_pydriver_create_dict(void)
718 {
719         PyObject *d, *mod;
720         
721         /* validate namespace for driver evaluation */
722         if (bpy_pydriver_Dict) return -1;
723
724         d = PyDict_New();
725         if (d == NULL) 
726                 return -1;
727         else
728                 bpy_pydriver_Dict = d;
729
730         /* import some modules: builtins, bpy, math, (Blender.noise )*/
731         PyDict_SetItemString(d, "__builtins__", PyEval_GetBuiltins());
732
733         mod = PyImport_ImportModule("math");
734         if (mod) {
735                 PyDict_Merge(d, PyModule_GetDict(mod), 0); /* 0 - dont overwrite existing values */
736                 
737                 /* Only keep for backwards compat! - just import all math into root, they are standard */
738                 PyDict_SetItemString(d, "math", mod);
739                 PyDict_SetItemString(d, "m", mod);
740                 Py_DECREF(mod);
741         } 
742         
743         /* add bpy to global namespace */
744         mod= PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL, 0);
745         if (mod) {
746                 PyDict_SetItemString(bpy_pydriver_Dict, "bpy", mod);
747                 Py_DECREF(mod);
748         }
749         
750         
751 #if 0 // non existant yet
752         mod = PyImport_ImportModule("Blender.Noise");
753         if (mod) {
754                 PyDict_SetItemString(d, "noise", mod);
755                 PyDict_SetItemString(d, "n", mod);
756                 Py_DECREF(mod);
757         } else {
758                 PyErr_Clear();
759         }
760         
761         /* If there's a Blender text called pydrivers.py, import it.
762          * Users can add their own functions to this module. 
763          */
764         if (G.f & G_DOSCRIPTLINKS) {
765                 mod = importText("pydrivers"); /* can also use PyImport_Import() */
766                 if (mod) {
767                         PyDict_SetItemString(d, "pydrivers", mod);
768                         PyDict_SetItemString(d, "p", mod);
769                         Py_DECREF(mod);
770                 } else {
771                         PyErr_Clear();
772                 }
773         }
774 #endif // non existant yet
775         
776         return 0;
777 }
778
779 /* Update function, it gets rid of pydrivers global dictionary, forcing
780  * BPY_pydriver_eval to recreate it. This function is used to force
781  * reloading the Blender text module "pydrivers.py", if available, so
782  * updates in it reach pydriver evaluation. 
783  */
784 void BPY_pydriver_update(void)
785 {
786         PyGILState_STATE gilstate = PyGILState_Ensure();
787
788         if (bpy_pydriver_Dict) { /* free the global dict used by pydrivers */
789                 PyDict_Clear(bpy_pydriver_Dict);
790                 Py_DECREF(bpy_pydriver_Dict);
791                 bpy_pydriver_Dict = NULL;
792         }
793
794         PyGILState_Release(gilstate);
795
796         return;
797 }
798
799 /* error return function for BPY_eval_pydriver */
800 static float pydriver_error(ChannelDriver *driver) 
801 {
802         if (bpy_pydriver_Dict) { /* free the global dict used by pydrivers */
803                 PyDict_Clear(bpy_pydriver_Dict);
804                 Py_DECREF(bpy_pydriver_Dict);
805                 bpy_pydriver_Dict = NULL;
806         }
807
808         driver->flag |= DRIVER_FLAG_INVALID; /* py expression failed */
809         fprintf(stderr, "\nError in Driver: The following Python expression failed:\n\t'%s'\n\n", driver->expression);
810         
811         BPy_errors_to_report(NULL); // TODO - reports
812
813         return 0.0f;
814 }
815
816 /* This evals py driver expressions, 'expr' is a Python expression that
817  * should evaluate to a float number, which is returned. 
818  */
819 float BPY_pydriver_eval (ChannelDriver *driver)
820 {
821         PyObject *driver_vars=NULL;
822         PyObject *retval;
823         PyGILState_STATE gilstate;
824         
825         DriverTarget *dtar;
826         float result = 0.0f; /* default return */
827         char *expr = NULL;
828         short targets_ok= 1;
829         
830         /* sanity checks - should driver be executed? */
831         if ((driver == NULL) /*|| (G.f & G_DOSCRIPTLINKS)==0*/) 
832                 return result;
833         
834         /* get the py expression to be evaluated */
835         expr = driver->expression; 
836         if ((expr == NULL) || (expr[0]=='\0')) 
837                 return result;
838
839         gilstate = PyGILState_Ensure();
840         
841         /* init global dictionary for py-driver evaluation settings */
842         if (!bpy_pydriver_Dict) {
843                 if (bpy_pydriver_create_dict() != 0) {
844                         fprintf(stderr, "Pydriver error: couldn't create Python dictionary");
845                         PyGILState_Release(gilstate);
846                         return result;
847                 }
848         }
849         
850         /* add target values to a dict that will be used as '__locals__' dict */
851         driver_vars = PyDict_New(); // XXX do we need to decref this?
852         for (dtar= driver->targets.first; dtar; dtar= dtar->next) {
853                 PyObject *driver_arg = NULL;
854                 float tval = 0.0f;
855                 
856                 /* try to get variable value */
857                 tval= driver_get_target_value(driver, dtar);
858                 driver_arg= PyFloat_FromDouble((double)tval);
859                 
860                 /* try to add to dictionary */
861                 if (PyDict_SetItemString(driver_vars, dtar->name, driver_arg)) {
862                         /* this target failed - bad name */
863                         if (targets_ok) {
864                                 /* first one - print some extra info for easier identification */
865                                 fprintf(stderr, "\nBPY_pydriver_eval() - Error while evaluating PyDriver:\n");
866                                 targets_ok= 0;
867                         }
868                         
869                         fprintf(stderr, "\tBPY_pydriver_eval() - couldn't add variable '%s' to namespace \n", dtar->name);
870                         BPy_errors_to_report(NULL); // TODO - reports
871                 }
872         }
873         
874         /* execute expression to get a value */
875         retval = PyRun_String(expr, Py_eval_input, bpy_pydriver_Dict, driver_vars);
876         
877         /* decref the driver vars first...  */
878         Py_DECREF(driver_vars);
879         
880         /* process the result */
881         if (retval == NULL) {
882                 result = pydriver_error(driver);
883                 PyGILState_Release(gilstate);
884                 return result;
885         }
886
887         result = (float)PyFloat_AsDouble(retval);
888         Py_DECREF(retval);
889         
890         if ((result == -1) && PyErr_Occurred()) {
891                 result = pydriver_error(driver);
892                 PyGILState_Release(gilstate);
893                 return result;
894         }
895         
896         /* all fine, make sure the "invalid expression" flag is cleared */
897         driver->flag &= ~DRIVER_FLAG_INVALID;
898
899         PyGILState_Release(gilstate);
900
901         return result;
902 }
903
904 int BPY_button_eval(bContext *C, char *expr, double *value)
905 {
906         PyGILState_STATE gilstate;
907         PyObject *dict, *mod, *retval;
908         int error_ret = 0;
909         
910         if (!value || !expr || expr[0]=='\0') return -1;
911         
912         bpy_context_set(C, &gilstate);
913         
914         dict= CreateGlobalDictionary(C);
915         
916         /* import some modules: builtins,math*/
917         PyDict_SetItemString(dict, "__builtins__", PyEval_GetBuiltins());
918
919         mod = PyImport_ImportModule("math");
920         if (mod) {
921                 PyDict_Merge(dict, PyModule_GetDict(mod), 0); /* 0 - dont overwrite existing values */
922                 
923                 /* Only keep for backwards compat! - just import all math into root, they are standard */
924                 PyDict_SetItemString(dict, "math", mod);
925                 PyDict_SetItemString(dict, "m", mod);
926                 Py_DECREF(mod);
927         } 
928         
929         retval = PyRun_String(expr, Py_eval_input, dict, dict);
930         
931         if (retval == NULL) {
932                 error_ret= -1;
933         }
934         else {
935                 double val;
936
937                 if(PyTuple_Check(retval)) {
938                         /* Users my have typed in 10km, 2m
939                          * add up all values */
940                         int i;
941                         val= 0.0;
942
943                         for(i=0; i<PyTuple_GET_SIZE(retval); i++) {
944                                 val+= PyFloat_AsDouble(PyTuple_GET_ITEM(retval, i));
945                         }
946                 }
947                 else {
948                         val = PyFloat_AsDouble(retval);
949                 }
950                 Py_DECREF(retval);
951                 
952                 if(val==-1 && PyErr_Occurred()) {
953                         error_ret= -1;
954                 }
955                 else {
956                         *value= val;
957                 }
958         }
959         
960         if(error_ret) {
961                 BPy_errors_to_report(CTX_wm_reports(C));
962         }
963         
964         Py_DECREF(dict);
965         bpy_context_clear(C, &gilstate);
966         
967         return error_ret;
968 }
969
970
971
972 int bpy_context_get(bContext *C, const char *member, bContextDataResult *result)
973 {
974         PyObject *pyctx= (PyObject *)CTX_py_dict_get(C);
975         PyObject *item= PyDict_GetItemString(pyctx, member);
976         PointerRNA *ptr= NULL;
977         int done= 0;
978
979         if(item==NULL) {
980                 /* pass */
981         }
982         else if(item==Py_None) {
983                 /* pass */
984         }
985         else if(BPy_StructRNA_Check(item)) {
986                 ptr= &(((BPy_StructRNA *)item)->ptr);
987
988                 //result->ptr= ((BPy_StructRNA *)item)->ptr;
989                 CTX_data_pointer_set(result, ptr->id.data, ptr->type, ptr->data);
990                 done= 1;
991         }
992         else if (PySequence_Check(item)) {
993                 PyObject *seq_fast= PySequence_Fast(item, "bpy_context_get sequence conversion");
994                 if (seq_fast==NULL) {
995                         PyErr_Print();
996                         PyErr_Clear();
997                 }
998                 else {
999                         int len= PySequence_Fast_GET_SIZE(seq_fast);
1000                         int i;
1001                         for(i = 0; i < len; i++) {
1002                                 PyObject *list_item= PySequence_Fast_GET_ITEM(seq_fast, i);
1003
1004                                 if(BPy_StructRNA_Check(list_item)) {
1005                                         /*
1006                                         CollectionPointerLink *link= MEM_callocN(sizeof(CollectionPointerLink), "bpy_context_get");
1007                                         link->ptr= ((BPy_StructRNA *)item)->ptr;
1008                                         BLI_addtail(&result->list, link);
1009                                         */
1010                                         ptr= &(((BPy_StructRNA *)list_item)->ptr);
1011                                         CTX_data_list_add(result, ptr->id.data, ptr->type, ptr->data);
1012                                 }
1013                                 else {
1014                                         printf("List item not a valid type\n");
1015                                 }
1016
1017                         }
1018                         Py_DECREF(seq_fast);
1019
1020                         done= 1;
1021                 }
1022         }
1023
1024         if(done==0) {
1025                 if (item)       printf("Context '%s' not found\n", member);
1026                 else            printf("Context '%s' not a valid type\n", member);
1027         }
1028
1029         return done;
1030 }
1031