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