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