svn merge -r 21041:21243 https://svn.blender.org/svnroot/bf-blender/branches/blender2...
[blender.git] / source / blender / python / intern / bpy_interface.c
1
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <sys/stat.h>
6
7 #ifndef WIN32
8 #include <dirent.h>
9 #else
10 #include "BLI_winstuff.h"
11 #endif
12
13 #include <Python.h>
14 #include "compile.h"            /* for the PyCodeObject */
15 #include "eval.h"               /* for PyEval_EvalCode */
16
17 #include "bpy_compat.h"
18
19 #include "bpy_rna.h"
20 #include "bpy_operator.h"
21 #include "bpy_ui.h"
22 #include "bpy_util.h"
23
24 #include "DNA_anim_types.h"
25 #include "DNA_space_types.h"
26 #include "DNA_text_types.h"
27
28 #include "MEM_guardedalloc.h"
29
30 #include "BLI_util.h"
31 #include "BLI_string.h"
32
33 #include "BKE_context.h"
34 #include "BKE_fcurve.h"
35 #include "BKE_text.h"
36
37 #include "BPY_extern.h"
38
39 #include "../generic/bpy_internal_import.h" // our own imports
40 /* external util modukes */
41
42 #include "../generic/Mathutils.h"
43 #include "../generic/Geometry.h"
44 #include "../generic/BGL.h"
45
46
47 void BPY_free_compiled_text( struct Text *text )
48 {
49         if( text->compiled ) {
50                 Py_DECREF( ( PyObject * ) text->compiled );
51                 text->compiled = NULL;
52         }
53 }
54
55 /*****************************************************************************
56 * Description: Creates the bpy module and adds it to sys.modules for importing
57 *****************************************************************************/
58 static void bpy_init_modules( void )
59 {
60         PyObject *mod;
61         
62         mod = PyModule_New("bpy");
63         
64         PyModule_AddObject( mod, "data", BPY_rna_module() );
65         /* PyModule_AddObject( mod, "doc", BPY_rna_doc() ); */
66         PyModule_AddObject( mod, "types", BPY_rna_types() );
67         PyModule_AddObject( mod, "props", BPY_rna_props() );
68         PyModule_AddObject( mod, "ops", BPY_operator_module() );
69         PyModule_AddObject( mod, "ui", BPY_ui_module() ); // XXX very experimental, consider this a test, especially PyCObject is not meant to be permanent
70         
71         /* add the module so we can import it */
72         PyDict_SetItemString(PySys_GetObject("modules"), "bpy", mod);
73         Py_DECREF(mod);
74
75
76         /* stand alone utility modules not related to blender directly */
77         Geometry_Init("Geometry");
78         Mathutils_Init("Mathutils");
79         BGL_Init("BGL");
80 }
81
82 #if (PY_VERSION_HEX < 0x02050000)
83 PyObject *PyImport_ImportModuleLevel(char *name, void *a, void *b, void *c, int d)
84 {
85         return PyImport_ImportModule(name);
86 }
87 #endif
88
89 void BPY_update_modules( void )
90 {
91         PyObject *mod= PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL, 0);
92         PyModule_AddObject( mod, "data", BPY_rna_module() );
93         PyModule_AddObject( mod, "types", BPY_rna_types() );
94 }
95
96 /*****************************************************************************
97 * Description: This function creates a new Python dictionary object.
98 *****************************************************************************/
99 static PyObject *CreateGlobalDictionary( bContext *C )
100 {
101         PyObject *mod;
102         PyObject *dict = PyDict_New(  );
103         PyObject *item = PyUnicode_FromString( "__main__" );
104         PyDict_SetItemString( dict, "__builtins__", PyEval_GetBuiltins(  ) );
105         PyDict_SetItemString( dict, "__name__", item );
106         Py_DECREF(item);
107         
108         // XXX - evil, need to access context
109         BPy_SetContext(C);
110         
111         // XXX - put somewhere more logical
112         {
113                 PyMethodDef *ml;
114                 static PyMethodDef bpy_prop_meths[] = {
115                         {"FloatProperty", (PyCFunction)BPy_FloatProperty, METH_VARARGS|METH_KEYWORDS, ""},
116                         {"IntProperty", (PyCFunction)BPy_IntProperty, METH_VARARGS|METH_KEYWORDS, ""},
117                         {"BoolProperty", (PyCFunction)BPy_BoolProperty, METH_VARARGS|METH_KEYWORDS, ""},
118                         {"StringProperty", (PyCFunction)BPy_StringProperty, METH_VARARGS|METH_KEYWORDS, ""},
119                         {NULL, NULL, 0, NULL}
120                 };
121                 
122                 for(ml = bpy_prop_meths; ml->ml_name; ml++) {
123                         PyDict_SetItemString( dict, ml->ml_name, PyCFunction_New(ml, NULL));
124                 }
125         }
126         
127         /* add bpy to global namespace */
128         mod= PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL, 0);
129         PyDict_SetItemString( dict, "bpy", mod );
130         Py_DECREF(mod);
131         
132         return dict;
133 }
134
135 void BPY_start_python( int argc, char **argv )
136 {
137         PyThreadState *py_tstate = NULL;
138         
139         Py_Initialize(  );
140         
141         //PySys_SetArgv( argc_copy, argv_copy );
142         
143         /* Initialize thread support (also acquires lock) */
144         PyEval_InitThreads();
145         
146         
147         /* bpy.* and lets us import it */
148         bpy_init_modules(); 
149
150         { /* our own import and reload functions */
151                 PyObject *item;
152                 //PyObject *m = PyImport_AddModule("__builtin__");
153                 //PyObject *d = PyModule_GetDict(m);
154                 PyObject *d = PyEval_GetBuiltins(  );
155                 PyDict_SetItemString(d, "reload",               item=PyCFunction_New(bpy_reload_meth, NULL));   Py_DECREF(item);
156                 PyDict_SetItemString(d, "__import__",   item=PyCFunction_New(bpy_import_meth, NULL));   Py_DECREF(item);
157         }
158         
159         py_tstate = PyGILState_GetThisThreadState();
160         PyEval_ReleaseThread(py_tstate);
161 }
162
163 void BPY_end_python( void )
164 {
165         PyGILState_Ensure(); /* finalizing, no need to grab the state */
166         
167         // free other python data.
168         //BPY_rna_free_types();
169         
170         Py_Finalize(  );
171         
172         return;
173 }
174
175 /* Can run a file or text block */
176 int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struct ReportList *reports)
177 {
178         PyObject *py_dict, *py_result;
179         PyGILState_STATE gilstate;
180         
181         if (fn==NULL && text==NULL) {
182                 return 0;
183         }
184         
185         //BPY_start_python();
186         
187         gilstate = PyGILState_Ensure();
188
189         BPY_update_modules(); /* can give really bad results if this isnt here */
190         bpy_import_main_set(CTX_data_main(C));
191         
192         py_dict = CreateGlobalDictionary(C);
193
194         if (text) {
195                 
196                 if( !text->compiled ) { /* if it wasn't already compiled, do it now */
197                         char *buf = txt_to_buf( text );
198
199                         text->compiled =
200                                 Py_CompileString( buf, text->id.name+2, Py_file_input );
201
202                         MEM_freeN( buf );
203
204                         if( PyErr_Occurred(  ) ) {
205                                 BPy_errors_to_report(reports);
206                                 BPY_free_compiled_text( text );
207                                 PyGILState_Release(gilstate);
208                                 return 0;
209                         }
210                 }
211                 py_result =  PyEval_EvalCode( text->compiled, py_dict, py_dict );
212                 
213         } else {
214                 char pystring[512];
215                 /* TODO - look into a better way to run a file */
216                 sprintf(pystring, "exec(open(r'%s').read())", fn);      
217                 py_result = PyRun_String( pystring, Py_file_input, py_dict, py_dict );                  
218         }
219         
220         if (!py_result) {
221                 BPy_errors_to_report(reports);
222         } else {
223                 Py_DECREF( py_result );
224         }
225         
226         Py_DECREF(py_dict);
227         PyGILState_Release(gilstate);
228         bpy_import_main_set(NULL);
229         
230         //BPY_end_python();
231         return py_result ? 1:0;
232 }
233
234
235 /* TODO - move into bpy_space.c ? */
236 /* GUI interface routines */
237
238 /* Copied from Draw.c */
239 static void exit_pydraw( SpaceScript * sc, short err )
240 {
241         Script *script = NULL;
242
243         if( !sc || !sc->script )
244                 return;
245
246         script = sc->script;
247
248         if( err ) {
249                 BPy_errors_to_report(NULL); // TODO, reports
250                 script->flags = 0;      /* mark script struct for deletion */
251                 SCRIPT_SET_NULL(script);
252                 script->scriptname[0] = '\0';
253                 script->scriptarg[0] = '\0';
254 // XXX 2.5              error_pyscript();
255 // XXX 2.5              scrarea_queue_redraw( sc->area );
256         }
257
258 #if 0 // XXX 2.5
259         BPy_Set_DrawButtonsList(sc->but_refs);
260         BPy_Free_DrawButtonsList(); /*clear all temp button references*/
261 #endif
262
263         sc->but_refs = NULL;
264         
265         Py_XDECREF( ( PyObject * ) script->py_draw );
266         Py_XDECREF( ( PyObject * ) script->py_event );
267         Py_XDECREF( ( PyObject * ) script->py_button );
268
269         script->py_draw = script->py_event = script->py_button = NULL;
270 }
271
272 static int bpy_run_script_init(bContext *C, SpaceScript * sc)
273 {
274         if (sc->script==NULL) 
275                 return 0;
276         
277         if (sc->script->py_draw==NULL && sc->script->scriptname[0] != '\0')
278                 BPY_run_python_script(C, sc->script->scriptname, NULL, NULL);
279                 
280         if (sc->script->py_draw==NULL)
281                 return 0;
282         
283         return 1;
284 }
285
286 int BPY_run_script_space_draw(struct bContext *C, SpaceScript * sc)
287 {
288         if (bpy_run_script_init(C, sc)) {
289                 PyGILState_STATE gilstate = PyGILState_Ensure();
290                 PyObject *result = PyObject_CallObject( sc->script->py_draw, NULL );
291                 
292                 if (result==NULL)
293                         exit_pydraw(sc, 1);
294                         
295                 PyGILState_Release(gilstate);
296         }
297         return 1;
298 }
299
300 // XXX - not used yet, listeners dont get a context
301 int BPY_run_script_space_listener(bContext *C, SpaceScript * sc)
302 {
303         if (bpy_run_script_init(C, sc)) {
304                 PyGILState_STATE gilstate = PyGILState_Ensure();
305                 
306                 PyObject *result = PyObject_CallObject( sc->script->py_draw, NULL );
307                 
308                 if (result==NULL)
309                         exit_pydraw(sc, 1);
310                         
311                 PyGILState_Release(gilstate);
312         }
313         return 1;
314 }
315
316 void BPY_DECREF(void *pyob_ptr)
317 {
318         Py_DECREF((PyObject *)pyob_ptr);
319 }
320
321 #if 0
322 /* called from the the scripts window, assume context is ok */
323 int BPY_run_python_script_space(const char *modulename, const char *func)
324 {
325         PyObject *py_dict, *py_result= NULL;
326         char pystring[512];
327         PyGILState_STATE gilstate;
328         
329         /* for calling the module function */
330         PyObject *py_func, 
331         
332         gilstate = PyGILState_Ensure();
333         
334         py_dict = CreateGlobalDictionary(C);
335         
336         PyObject *module = PyImport_ImportModule(scpt->script.filename);
337         if (module==NULL) {
338                 PyErr_SetFormat(PyExc_SystemError, "could not import '%s'", scpt->script.filename);
339         }
340         else {
341                 py_func = PyObject_GetAttrString(modulename, func);
342                 if (py_func==NULL) {
343                         PyErr_SetFormat(PyExc_SystemError, "module has no function '%s.%s'\n", scpt->script.filename, func);
344                 }
345                 else {
346                         Py_DECREF(py_func);
347                         if (!PyCallable_Check(py_func)) {
348                                 PyErr_SetFormat(PyExc_SystemError, "module item is not callable '%s.%s'\n", scpt->script.filename, func);
349                         }
350                         else {
351                                 py_result= PyObject_CallObject(py_func, NULL); // XXX will need args eventually
352                         }
353                 }
354         }
355         
356         if (!py_result) {
357                 BPy_errors_to_report(NULL); // TODO - reports
358         } else
359                 Py_DECREF( py_result );
360         
361         Py_XDECREF(module);
362         
363         Py_DECREF(py_dict);
364         
365         PyGILState_Release(gilstate);
366         return 1;
367 }
368 #endif
369
370 // #define TIME_REGISTRATION
371
372 #ifdef TIME_REGISTRATION
373 #include "PIL_time.h"
374 #endif
375
376 /* XXX this is temporary, need a proper script registration system for 2.5 */
377 void BPY_run_ui_scripts(bContext *C, int reload)
378 {
379 #ifdef TIME_REGISTRATION
380         double time = PIL_check_seconds_timer();
381 #endif
382         DIR *dir; 
383         struct dirent *de;
384         char *file_extension;
385         char *dirname;
386         char path[FILE_MAX];
387         char *dirs[] = {"io", "ui", NULL};
388         int a, filelen; /* filename length */
389         
390         PyGILState_STATE gilstate;
391         PyObject *mod;
392         PyObject *sys_path_orig;
393         PyObject *sys_path_new;
394
395         gilstate = PyGILState_Ensure();
396         
397         // XXX - evil, need to access context
398         BPy_SetContext(C);
399         bpy_import_main_set(CTX_data_main(C));
400
401         for(a=0; dirs[a]; a++) {
402                 dirname= BLI_gethome_folder(dirs[a]);
403
404                 if(!dirname)
405                         continue;
406
407                 dir = opendir(dirname);
408
409                 if(!dir)
410                         continue;
411
412                 /* backup sys.path */
413                 sys_path_orig= PySys_GetObject("path");
414                 Py_INCREF(sys_path_orig); /* dont free it */
415                 
416                 sys_path_new= PyList_New(1);
417                 PyList_SET_ITEM(sys_path_new, 0, PyUnicode_FromString(dirname));
418                 PySys_SetObject("path", sys_path_new);
419                 Py_DECREF(sys_path_new);
420                         
421                 while((de = readdir(dir)) != NULL) {
422                         /* We could stat the file but easier just to let python
423                          * import it and complain if theres a problem */
424                         
425                         file_extension = strstr(de->d_name, ".py");
426                         
427                         if(file_extension && *(file_extension + 3) == '\0') {
428                                 filelen = strlen(de->d_name);
429                                 BLI_strncpy(path, de->d_name, filelen-2); /* cut off the .py on copy */
430                                 
431                                 mod= PyImport_ImportModuleLevel(path, NULL, NULL, NULL, 0);
432                                 if (mod) {
433                                         if (reload) {
434                                                 PyObject *mod_orig= mod;
435                                                 mod= PyImport_ReloadModule(mod);
436                                                 Py_DECREF(mod_orig);
437                                         }
438                                 }
439                                 
440                                 if(mod) {
441                                         Py_DECREF(mod); /* could be NULL from reloading */
442                                 } else {
443                                         BPy_errors_to_report(NULL); // TODO - reports
444                                         fprintf(stderr, "unable to import \"%s\"  %s/%s\n", path, dirname, de->d_name);
445                                 }
446
447                         }
448                 }
449
450                 closedir(dir);
451
452                 PySys_SetObject("path", sys_path_orig);
453                 Py_DECREF(sys_path_orig);
454         }
455         
456         bpy_import_main_set(NULL);
457         
458         PyGILState_Release(gilstate);
459 #ifdef TIME_REGISTRATION
460         printf("script time %f\n", (PIL_check_seconds_timer()-time));
461 #endif
462 }
463
464 /* ****************************************** */
465 /* Drivers - PyExpression Evaluation */
466
467 /* for pydrivers (drivers using one-line Python expressions to express relationships between targets) */
468 PyObject *bpy_pydriver_Dict = NULL;
469
470 /* For faster execution we keep a special dictionary for pydrivers, with
471  * the needed modules and aliases. 
472  */
473 static int bpy_pydriver_create_dict(void)
474 {
475         PyObject *d, *mod;
476         
477         /* validate namespace for driver evaluation */
478         if (bpy_pydriver_Dict) return -1;
479
480         d = PyDict_New();
481         if (d == NULL) 
482                 return -1;
483         else
484                 bpy_pydriver_Dict = d;
485
486         /* import some modules: builtins, bpy, math, (Blender.noise )*/
487         PyDict_SetItemString(d, "__builtins__", PyEval_GetBuiltins());
488
489         mod = PyImport_ImportModule("math");
490         if (mod) {
491                 PyDict_Merge(d, PyModule_GetDict(mod), 0); /* 0 - dont overwrite existing values */
492                 
493                 /* Only keep for backwards compat! - just import all math into root, they are standard */
494                 PyDict_SetItemString(d, "math", mod);
495                 PyDict_SetItemString(d, "m", mod);
496                 Py_DECREF(mod);
497         } 
498         
499         /* add bpy to global namespace */
500         mod= PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL, 0);
501         if (mod) {
502                 PyDict_SetItemString(bpy_pydriver_Dict, "bpy", mod);
503                 Py_DECREF(mod);
504         }
505         
506         
507 #if 0 // non existant yet
508         mod = PyImport_ImportModule("Blender.Noise");
509         if (mod) {
510                 PyDict_SetItemString(d, "noise", mod);
511                 PyDict_SetItemString(d, "n", mod);
512                 Py_DECREF(mod);
513         } else {
514                 PyErr_Clear();
515         }
516         
517         /* If there's a Blender text called pydrivers.py, import it.
518          * Users can add their own functions to this module. 
519          */
520         if (G.f & G_DOSCRIPTLINKS) {
521                 mod = importText("pydrivers"); /* can also use PyImport_Import() */
522                 if (mod) {
523                         PyDict_SetItemString(d, "pydrivers", mod);
524                         PyDict_SetItemString(d, "p", mod);
525                         Py_DECREF(mod);
526                 } else {
527                         PyErr_Clear();
528                 }
529         }
530 #endif // non existant yet
531         
532         return 0;
533 }
534
535 /* Update function, it gets rid of pydrivers global dictionary, forcing
536  * BPY_pydriver_eval to recreate it. This function is used to force
537  * reloading the Blender text module "pydrivers.py", if available, so
538  * updates in it reach pydriver evaluation. 
539  */
540 void BPY_pydriver_update(void)
541 {
542         PyGILState_STATE gilstate = PyGILState_Ensure();
543
544         if (bpy_pydriver_Dict) { /* free the global dict used by pydrivers */
545                 PyDict_Clear(bpy_pydriver_Dict);
546                 Py_DECREF(bpy_pydriver_Dict);
547                 bpy_pydriver_Dict = NULL;
548         }
549
550         PyGILState_Release(gilstate);
551
552         return;
553 }
554
555 /* error return function for BPY_eval_pydriver */
556 static float pydriver_error(ChannelDriver *driver) 
557 {
558         if (bpy_pydriver_Dict) { /* free the global dict used by pydrivers */
559                 PyDict_Clear(bpy_pydriver_Dict);
560                 Py_DECREF(bpy_pydriver_Dict);
561                 bpy_pydriver_Dict = NULL;
562         }
563
564         driver->flag |= DRIVER_FLAG_INVALID; /* py expression failed */
565         fprintf(stderr, "\nError in Driver: The following Python expression failed:\n\t'%s'\n\n", driver->expression);
566         
567         BPy_errors_to_report(NULL); // TODO - reports
568
569         return 0.0f;
570 }
571
572 /* This evals py driver expressions, 'expr' is a Python expression that
573  * should evaluate to a float number, which is returned. 
574  */
575 float BPY_pydriver_eval (ChannelDriver *driver)
576 {
577         PyObject *driver_vars=NULL;
578         PyObject *retval;
579         PyGILState_STATE gilstate;
580         
581         DriverTarget *dtar;
582         float result = 0.0f; /* default return */
583         char *expr = NULL;
584         short targets_ok= 1;
585         
586         /* sanity checks - should driver be executed? */
587         if ((driver == NULL) /*|| (G.f & G_DOSCRIPTLINKS)==0*/) 
588                 return result;
589         
590         /* get the py expression to be evaluated */
591         expr = driver->expression; 
592         if ((expr == NULL) || (expr[0]=='\0')) 
593                 return result;
594
595         gilstate = PyGILState_Ensure();
596         
597         /* init global dictionary for py-driver evaluation settings */
598         if (!bpy_pydriver_Dict) {
599                 if (bpy_pydriver_create_dict() != 0) {
600                         fprintf(stderr, "Pydriver error: couldn't create Python dictionary");
601                         PyGILState_Release(gilstate);
602                         return result;
603                 }
604         }
605         
606         /* add target values to a dict that will be used as '__locals__' dict */
607         driver_vars = PyDict_New(); // XXX do we need to decref this?
608         for (dtar= driver->targets.first; dtar; dtar= dtar->next) {
609                 PyObject *driver_arg = NULL;
610                 float tval = 0.0f;
611                 
612                 /* try to get variable value */
613                 tval= driver_get_target_value(driver, dtar);
614                 driver_arg= PyFloat_FromDouble((double)tval);
615                 
616                 /* try to add to dictionary */
617                 if (PyDict_SetItemString(driver_vars, dtar->name, driver_arg)) {
618                         /* this target failed - bad name */
619                         if (targets_ok) {
620                                 /* first one - print some extra info for easier identification */
621                                 fprintf(stderr, "\nBPY_pydriver_eval() - Error while evaluating PyDriver:\n");
622                                 targets_ok= 0;
623                         }
624                         
625                         fprintf(stderr, "\tBPY_pydriver_eval() - couldn't add variable '%s' to namespace \n", dtar->name);
626                         BPy_errors_to_report(NULL); // TODO - reports
627                 }
628         }
629         
630         /* execute expression to get a value */
631         retval = PyRun_String(expr, Py_eval_input, bpy_pydriver_Dict, driver_vars);
632         
633         /* decref the driver vars first...  */
634         Py_DECREF(driver_vars);
635         
636         /* process the result */
637         if (retval == NULL) {
638                 result = pydriver_error(driver);
639                 PyGILState_Release(gilstate);
640                 return result;
641         }
642
643         result = (float)PyFloat_AsDouble(retval);
644         Py_DECREF(retval);
645         
646         if ((result == -1) && PyErr_Occurred()) {
647                 result = pydriver_error(driver);
648                 PyGILState_Release(gilstate);
649                 return result;
650         }
651         
652         /* all fine, make sure the "invalid expression" flag is cleared */
653         driver->flag &= ~DRIVER_FLAG_INVALID;
654
655         PyGILState_Release(gilstate);
656
657         return result;
658 }