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