667dd14283f0d1b8bdd545286e8291ea5679829e
[blender-staging.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_space_types.h"
24 #include "DNA_text_types.h"
25
26 #include "MEM_guardedalloc.h"
27
28 #include "BLI_util.h"
29
30 #include "BKE_context.h"
31 #include "BKE_text.h"
32
33 void BPY_free_compiled_text( struct Text *text )
34 {
35         if( text->compiled ) {
36                 Py_DECREF( ( PyObject * ) text->compiled );
37                 text->compiled = NULL;
38         }
39 }
40
41 /*****************************************************************************
42 * Description: This function creates a new Python dictionary object.
43 *****************************************************************************/
44
45 static PyObject *CreateGlobalDictionary( bContext *C )
46 {
47         PyObject *mod;
48         PyObject *dict = PyDict_New(  );
49         PyObject *item = PyUnicode_FromString( "__main__" );
50         PyDict_SetItemString( dict, "__builtins__", PyEval_GetBuiltins(  ) );
51         PyDict_SetItemString( dict, "__name__", item );
52         Py_DECREF(item);
53         
54         /* add bpy to global namespace */
55         mod = PyModule_New("bpy");
56         PyDict_SetItemString( dict, "bpy", mod );
57         Py_DECREF(mod);
58         
59         PyModule_AddObject( mod, "data", BPY_rna_module() );
60         /* PyModule_AddObject( mod, "doc", BPY_rna_doc() ); */
61         PyModule_AddObject( mod, "types", BPY_rna_types() );
62         PyModule_AddObject( mod, "ops", BPY_operator_module(C) );
63         PyModule_AddObject( mod, "ui", BPY_ui_module() ); // XXX very experemental, consider this a test, especially PyCObject is not meant to be perminant
64         
65         // XXX - evil, need to access context
66         item = PyCObject_FromVoidPtr( C, NULL );
67         PyDict_SetItemString( dict, "__bpy_context__", item );
68         Py_DECREF(item);
69         
70         
71         // XXX - put somewhere more logical
72         {
73                 PyMethodDef *ml;
74                 static PyMethodDef bpy_prop_meths[] = {
75                         {"FloatProperty", (PyCFunction)BPy_FloatProperty, METH_VARARGS|METH_KEYWORDS, ""},
76                         {"IntProperty", (PyCFunction)BPy_IntProperty, METH_VARARGS|METH_KEYWORDS, ""},
77                         {"BoolProperty", (PyCFunction)BPy_BoolProperty, METH_VARARGS|METH_KEYWORDS, ""},
78                         {NULL, NULL, 0, NULL}
79                 };
80                 
81                 for(ml = bpy_prop_meths; ml->ml_name; ml++) {
82                         PyDict_SetItemString( dict, ml->ml_name, PyCFunction_New(ml, NULL));
83                 }
84         }
85         
86         return dict;
87 }
88
89 void BPY_start_python( void )
90 {
91         PyThreadState *py_tstate = NULL;
92
93         Py_Initialize(  );
94         
95         //PySys_SetArgv( argc_copy, argv_copy );
96         
97         /* Initialize thread support (also acquires lock) */
98         PyEval_InitThreads();
99         
100         // todo - sys paths - our own imports
101         
102         py_tstate = PyGILState_GetThisThreadState();
103         PyEval_ReleaseThread(py_tstate);
104         
105 }
106
107 void BPY_end_python( void )
108 {
109         PyGILState_Ensure(); /* finalizing, no need to grab the state */
110         
111         // free other python data.
112         //BPY_rna_free_types();
113         
114         Py_Finalize(  );
115         
116         return;
117 }
118
119 /* Can run a file or text block */
120 int BPY_run_python_script( bContext *C, const char *fn, struct Text *text )
121 {
122         PyObject *py_dict, *py_result;
123         PyGILState_STATE gilstate;
124         
125         if (fn==NULL && text==NULL) {
126                 return 0;
127         }
128         
129         //BPY_start_python();
130         
131         gilstate = PyGILState_Ensure();
132
133         py_dict = CreateGlobalDictionary(C);
134
135         if (text) {
136                 
137                 if( !text->compiled ) { /* if it wasn't already compiled, do it now */
138                         char *buf = txt_to_buf( text );
139
140                         text->compiled =
141                                 Py_CompileString( buf, text->id.name+2, Py_file_input );
142
143                         MEM_freeN( buf );
144
145                         if( PyErr_Occurred(  ) ) {
146                                 PyErr_Print();
147                                 BPY_free_compiled_text( text );
148                                 PyGILState_Release(gilstate);
149                                 return 0;
150                         }
151                 }
152                 py_result =  PyEval_EvalCode( text->compiled, py_dict, py_dict );
153                 
154         } else {
155                 char pystring[512];
156                 /* TODO - look into a better way to run a file */
157                 sprintf(pystring, "exec(open(r'%s').read())", fn);      
158                 py_result = PyRun_String( pystring, Py_file_input, py_dict, py_dict );                  
159         }
160         
161         if (!py_result) {
162                 PyErr_Print();
163         } else {
164                 Py_DECREF( py_result );
165         }
166         PyGILState_Release(gilstate);
167         
168         //BPY_end_python();
169         return py_result ? 1:0;
170 }
171
172
173 /* TODO - move into bpy_space.c ? */
174 /* GUI interface routines */
175
176 /* Copied from Draw.c */
177 static void exit_pydraw( SpaceScript * sc, short err )
178 {
179         Script *script = NULL;
180
181         if( !sc || !sc->script )
182                 return;
183
184         script = sc->script;
185
186         if( err ) {
187                 PyErr_Print(  );
188                 script->flags = 0;      /* mark script struct for deletion */
189                 SCRIPT_SET_NULL(script);
190                 script->scriptname[0] = '\0';
191                 script->scriptarg[0] = '\0';
192 // XXX 2.5              error_pyscript();
193 // XXX 2.5              scrarea_queue_redraw( sc->area );
194         }
195
196 #if 0 // XXX 2.5
197         BPy_Set_DrawButtonsList(sc->but_refs);
198         BPy_Free_DrawButtonsList(); /*clear all temp button references*/
199 #endif
200
201         sc->but_refs = NULL;
202         
203         Py_XDECREF( ( PyObject * ) script->py_draw );
204         Py_XDECREF( ( PyObject * ) script->py_event );
205         Py_XDECREF( ( PyObject * ) script->py_button );
206
207         script->py_draw = script->py_event = script->py_button = NULL;
208 }
209
210 static int bpy_run_script_init(bContext *C, SpaceScript * sc)
211 {
212         if (sc->script==NULL) 
213                 return 0;
214         
215         if (sc->script->py_draw==NULL && sc->script->scriptname[0] != '\0')
216                 BPY_run_python_script(C, sc->script->scriptname, NULL);
217                 
218         if (sc->script->py_draw==NULL)
219                 return 0;
220         
221         return 1;
222 }
223
224 int BPY_run_script_space_draw(bContext *C, SpaceScript * sc)
225 {
226         if (bpy_run_script_init(C, sc)) {
227                 PyGILState_STATE gilstate = PyGILState_Ensure();
228                 PyObject *result = PyObject_CallObject( sc->script->py_draw, NULL );
229                 
230                 if (result==NULL)
231                         exit_pydraw(sc, 1);
232                         
233                 PyGILState_Release(gilstate);
234         }
235         return 1;
236 }
237
238 // XXX - not used yet, listeners dont get a context
239 int BPY_run_script_space_listener(bContext *C, SpaceScript * sc)
240 {
241         if (bpy_run_script_init(C, sc)) {
242                 PyGILState_STATE gilstate = PyGILState_Ensure();
243                 
244                 PyObject *result = PyObject_CallObject( sc->script->py_draw, NULL );
245                 
246                 if (result==NULL)
247                         exit_pydraw(sc, 1);
248                         
249                 PyGILState_Release(gilstate);
250         }
251         return 1;
252 }
253
254 void BPY_DECREF(void *pyob_ptr)
255 {
256         Py_DECREF((PyObject *)pyob_ptr);
257 }
258
259 #if 0
260 /* called from the the scripts window, assume context is ok */
261 int BPY_run_python_script_space(const char *modulename, const char *func)
262 {
263         PyObject *py_dict, *py_result= NULL;
264         char pystring[512];
265         PyGILState_STATE gilstate;
266         
267         /* for calling the module function */
268         PyObject *py_func, 
269         
270         gilstate = PyGILState_Ensure();
271         
272         py_dict = CreateGlobalDictionary(C);
273         
274         PyObject *module = PyImport_ImportModule(scpt->script.filename);
275         if (module==NULL) {
276                 PyErr_SetFormat(PyExc_SystemError, "could not import '%s'", scpt->script.filename);
277         }
278         else {
279                 py_func = PyObject_GetAttrString(modulename, func);
280                 if (py_func==NULL) {
281                         PyErr_SetFormat(PyExc_SystemError, "module has no function '%s.%s'\n", scpt->script.filename, func);
282                 }
283                 else {
284                         Py_DECREF(py_func);
285                         if (!PyCallable_Check(py_func)) {
286                                 PyErr_SetFormat(PyExc_SystemError, "module item is not callable '%s.%s'\n", scpt->script.filename, func);
287                         }
288                         else {
289                                 py_result= PyObject_CallObject(py_func, NULL); // XXX will need args eventually
290                         }
291                 }
292         }
293         
294         if (!py_result)
295                 PyErr_Print();
296         else
297                 Py_DECREF( py_result );
298         
299         Py_XDECREF(module);
300         
301         
302         PyGILState_Release(gilstate);
303         return 1;
304 }
305 #endif
306
307 /* XXX this is temporary, need a proper script registration system for 2.5 */
308 void BPY_run_ui_scripts(bContext *C)
309 {
310         DIR *dir; 
311         struct dirent *de;
312         struct stat status;
313         char *file_extension;
314         char path[FILE_MAX];
315         char *dirname= BLI_gethome_folder("ui");
316
317         if(!dirname)
318                 return;
319         
320         dir = opendir(dirname);
321
322         if(!dir)
323                 return;
324
325         if (dir != NULL) {
326                 while((de = readdir(dir)) != NULL) {
327                         BLI_make_file_string("/", path, dirname, de->d_name);
328                         
329                         stat(path, &status);
330
331                         /* run if it is a .py file */
332                         if(S_ISREG(status.st_mode)) {
333                                 file_extension = strstr(de->d_name, ".py");
334
335                                 if(file_extension && *(file_extension + 3) == '\0')
336                                         BPY_run_python_script(C, path, NULL);
337                         }
338                 }
339
340                 closedir(dir);
341         }
342 }
343