Merged changes in the trunk up to revision 25863.
[blender.git] / source / blender / python / intern / bpy_interface.c
1 /**
2  * $Id:
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * Contributor(s): Michel Selten, Willian P. Germano, Stephen Swaney,
21  * Chris Keith, Chris Want, Ken Hughes, Campbell Barton
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25  
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/stat.h>
30
31
32 /* grr, python redefines */
33 #ifdef _POSIX_C_SOURCE
34 #undef _POSIX_C_SOURCE
35 #endif
36
37 #include <Python.h>
38 #include "compile.h"            /* for the PyCodeObject */
39 #include "eval.h"               /* for PyEval_EvalCode */
40
41 #include "bpy_rna.h"
42 #include "bpy_operator.h"
43 #include "bpy_ui.h"
44 #include "bpy_util.h"
45
46 #ifndef WIN32
47 #include <dirent.h>
48 #else
49 #include "BLI_winstuff.h"
50 #endif
51
52 #include "DNA_space_types.h"
53 #include "DNA_text_types.h"
54
55 #include "MEM_guardedalloc.h"
56
57 #include "BLI_path_util.h"
58 #include "BLI_storage.h"
59 #include "BLI_fileops.h"
60 #include "BLI_string.h"
61
62 #include "BKE_blender.h"
63 #include "BKE_context.h"
64 #include "BKE_text.h"
65 #include "BKE_context.h"
66 #include "BKE_global.h"
67 #include "BKE_main.h"
68
69 #include "BPY_extern.h"
70
71 #include "BPy_Freestyle.h"
72
73 #include "../generic/bpy_internal_import.h" // our own imports
74 /* external util modules */
75
76 #include "../generic/Mathutils.h"
77 #include "../generic/Geometry.h"
78 #include "../generic/BGL.h"
79 #include "../generic/IDProp.h"
80
81 /* for internal use, when starting and ending python scripts */
82
83 /* incase a python script triggers another python call, stop bpy_context_clear from invalidating */
84 static int py_call_level= 0;
85
86
87 // only for tests
88 #define TIME_PY_RUN
89
90 #ifdef TIME_PY_RUN
91 #include "PIL_time.h"
92 static int              bpy_timer_count = 0;
93 static double   bpy_timer; /* time since python starts */
94 static double   bpy_timer_run; /* time for each python script run */
95 static double   bpy_timer_run_tot; /* accumulate python runs */
96 #endif
97
98 void bpy_context_set(bContext *C, PyGILState_STATE *gilstate)
99 {
100         py_call_level++;
101
102         if(gilstate)
103                 *gilstate = PyGILState_Ensure();
104
105         if(py_call_level==1) {
106
107                 if(C) { // XXX - should always be true.
108                         BPy_SetContext(C);
109                         bpy_import_main_set(CTX_data_main(C));
110                 }
111                 else {
112                         fprintf(stderr, "ERROR: Python context called with a NULL Context. this should not happen!\n");
113                 }
114
115                 BPY_update_modules(); /* can give really bad results if this isnt here */
116
117 #ifdef TIME_PY_RUN
118                 if(bpy_timer_count==0) {
119                         /* record time from the beginning */
120                         bpy_timer= PIL_check_seconds_timer();
121                         bpy_timer_run = bpy_timer_run_tot = 0.0;
122                 }
123                 bpy_timer_run= PIL_check_seconds_timer();
124
125
126                 bpy_timer_count++;
127 #endif
128         }
129 }
130
131 void bpy_context_clear(bContext *C, PyGILState_STATE *gilstate)
132 {
133         py_call_level--;
134
135         if(gilstate)
136                 PyGILState_Release(*gilstate);
137
138         if(py_call_level < 0) {
139                 fprintf(stderr, "ERROR: Python context internal state bug. this should not happen!\n");
140         }
141         else if(py_call_level==0) {
142                 // XXX - Calling classes currently wont store the context :\, cant set NULL because of this. but this is very flakey still.
143                 //BPy_SetContext(NULL);
144                 //bpy_import_main_set(NULL);
145
146 #ifdef TIME_PY_RUN
147                 bpy_timer_run_tot += PIL_check_seconds_timer() - bpy_timer_run;
148                 bpy_timer_count++;
149 #endif
150
151         }
152 }
153
154 static void bpy_import_test(char *modname)
155 {
156         PyObject *mod= PyImport_ImportModuleLevel(modname, NULL, NULL, NULL, 0);
157         if(mod) {
158                 Py_DECREF(mod);
159         }
160         else {
161                 PyErr_Print();
162                 PyErr_Clear();
163         }       
164 }
165
166 void BPY_free_compiled_text( struct Text *text )
167 {
168         if( text->compiled ) {
169                 Py_DECREF( ( PyObject * ) text->compiled );
170                 text->compiled = NULL;
171         }
172 }
173
174 /*****************************************************************************
175 * Description: Creates the bpy module and adds it to sys.modules for importing
176 *****************************************************************************/
177 static BPy_StructRNA *bpy_context_module= NULL; /* for fast access */
178 static void bpy_init_modules( void )
179 {
180         PyObject *mod;
181
182         /* Needs to be first since this dir is needed for future modules */
183         char *modpath= BLI_gethome_folder("scripts/modules", BLI_GETHOME_ALL);
184         if(modpath) {
185                 PyObject *sys_path= PySys_GetObject("path"); /* borrow */
186                 PyObject *py_modpath= PyUnicode_FromString(modpath);
187                 PyList_Insert(sys_path, 0, py_modpath); /* add first */
188                 Py_DECREF(py_modpath);
189         }
190         
191         /* stand alone utility modules not related to blender directly */
192         Geometry_Init();
193         Mathutils_Init();
194         BGL_Init();
195         IDProp_Init_Types();
196         Freestyle_Init();
197
198
199         mod = PyModule_New("_bpy");
200
201         /* add the module so we can import it */
202         PyDict_SetItemString(PySys_GetObject("modules"), "_bpy", mod);
203         Py_DECREF(mod);
204
205         /* run first, initializes rna types */
206         BPY_rna_init();
207
208         PyModule_AddObject( mod, "types", BPY_rna_types() ); /* needs to be first so bpy_types can run */
209         bpy_import_test("bpy_types");
210         PyModule_AddObject( mod, "data", BPY_rna_module() ); /* imports bpy_types by running this */
211         bpy_import_test("bpy_types");
212         /* PyModule_AddObject( mod, "doc", BPY_rna_doc() ); */
213         PyModule_AddObject( mod, "props", BPY_rna_props() );
214         PyModule_AddObject( mod, "ops", BPY_operator_module() ); /* ops is now a python module that does the conversion from SOME_OT_foo -> some.foo */
215         PyModule_AddObject( mod, "ui", BPY_ui_module() ); // XXX very experimental, consider this a test, especially PyCObject is not meant to be permanent
216
217
218
219         /* bpy context */
220         {
221                 bpy_context_module= ( BPy_StructRNA * ) PyObject_NEW( BPy_StructRNA, &pyrna_struct_Type );
222                 RNA_pointer_create(NULL, &RNA_Context, NULL, &bpy_context_module->ptr);
223
224                 PyModule_AddObject(mod, "context", (PyObject *)bpy_context_module);
225         }
226
227         /* blender info that wont change at runtime, add into _bpy */
228         {
229                 extern char bprogname[]; /* argv[0] from creator.c */
230
231                 PyObject *mod_dict= PyModule_GetDict(mod);
232                 char tmpstr[256];
233                 PyModule_AddStringConstant(mod, "_HOME",  BLI_gethome());
234                 PyDict_SetItemString(mod_dict, "_VERSION", Py_BuildValue("(iii)", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION));
235                 sprintf(tmpstr, "%d.%02d (sub %d)", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
236                 PyModule_AddStringConstant(mod, "_VERSION_STR",  tmpstr);
237                 PyModule_AddStringConstant(mod, "_BINPATH",  bprogname);
238         }
239
240         /* add our own modules dir, this is a python package */
241         bpy_import_test("bpy");
242 }
243
244 void BPY_update_modules( void )
245 {
246 #if 0 // slow, this runs all the time poll, draw etc 100's of time a sec.
247         PyObject *mod= PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL, 0);
248         PyModule_AddObject( mod, "data", BPY_rna_module() );
249         PyModule_AddObject( mod, "types", BPY_rna_types() ); // atm this does not need updating
250 #endif
251
252         /* refreshes the main struct */
253         BPY_update_rna_module();
254         bpy_context_module->ptr.data= (void *)BPy_GetContext();
255 }
256
257 /*****************************************************************************
258 * Description: This function creates a new Python dictionary object.
259 *****************************************************************************/
260 static PyObject *CreateGlobalDictionary( bContext *C, const char *filename )
261 {
262         PyObject *mod;
263         PyObject *item;
264         PyObject *dict = PyDict_New(  );
265         PyDict_SetItemString( dict, "__builtins__", PyEval_GetBuiltins(  ) );
266
267         item = PyUnicode_FromString( "__main__" );
268         PyDict_SetItemString( dict, "__name__", item );
269         Py_DECREF(item);
270         
271         /* __file__ only for nice UI'ness */
272         if(filename) {
273                 PyObject *item = PyUnicode_FromString( filename );
274                 PyDict_SetItemString( dict, "__file__", item );
275                 Py_DECREF(item);
276         }
277
278         /* add bpy to global namespace */
279         mod= PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL, 0);
280         PyDict_SetItemString( dict, "bpy", mod );
281         Py_DECREF(mod);
282         
283         return dict;
284 }
285
286 /* must be called before Py_Initialize */
287 void BPY_start_python_path(void)
288 {
289         char *py_path_bundle= BLI_gethome_folder("python", BLI_GETHOME_ALL);
290
291         if(py_path_bundle==NULL)
292                 return;
293
294         /* set the environment path */
295         printf("found bundled python: %s\n", py_path_bundle);
296
297 #ifdef __APPLE__
298         /* OSX allow file/directory names to contain : character (represented as / in the Finder)
299          but current Python lib (release 3.1.1) doesn't handle these correctly */
300         if(strchr(py_path_bundle, ':'))
301                 printf("Warning : Blender application is located in a path containing : or / chars\
302                            \nThis may make python import function fail\n");
303 #endif
304         
305 #if 0
306         BLI_setenv("PYTHONHOME", py_path_bundle);
307         BLI_setenv("PYTHONPATH", py_path_bundle);
308 #endif
309
310         {
311                 static wchar_t py_path_bundle_wchar[FILE_MAXDIR];
312
313                 mbstowcs(py_path_bundle_wchar, py_path_bundle, FILE_MAXDIR);
314                 Py_SetPythonHome(py_path_bundle_wchar);
315         }
316 }
317
318
319
320 void BPY_set_context(bContext *C)
321 {
322         BPy_SetContext(C);
323 }
324
325 /* call BPY_set_context first */
326 void BPY_start_python( int argc, char **argv )
327 {
328         PyThreadState *py_tstate = NULL;
329         
330         BPY_start_python_path(); /* allow to use our own included python */
331
332         Py_Initialize(  );
333         
334         // PySys_SetArgv( argc, argv); // broken in py3, not a huge deal
335         /* sigh, why do python guys not have a char** version anymore? :( */
336         {
337                 int i;
338 #if 0
339                 PyObject *py_argv= PyList_New(argc);
340                 for (i=0; i<argc; i++)
341                         PyList_SET_ITEM(py_argv, i, PyUnicode_FromString(argv[i]));
342
343 #else   // should fix bug #20021 - utf path name problems
344                 PyObject *py_argv= PyList_New(0);
345                 for (i=0; i<argc; i++) {
346                         PyObject *item= PyUnicode_Decode(argv[i], strlen(argv[i]), Py_FileSystemDefaultEncoding, NULL);
347                         if(item==NULL) { // should never happen
348                                 PyErr_Print();
349                                 PyErr_Clear();
350                         }
351                         else {
352                                 PyList_Append(py_argv, item);
353                                 Py_DECREF(item);
354                         }
355                 }
356 #endif
357                 PySys_SetObject("argv", py_argv);
358                 Py_DECREF(py_argv);
359         }
360         
361         /* Initialize thread support (also acquires lock) */
362         PyEval_InitThreads();
363         
364         
365         /* bpy.* and lets us import it */
366         bpy_init_modules(); 
367
368         { /* our own import and reload functions */
369                 PyObject *item;
370                 //PyObject *m = PyImport_AddModule("__builtin__");
371                 //PyObject *d = PyModule_GetDict(m);
372                 PyObject *d = PyEval_GetBuiltins(  );
373                 PyDict_SetItemString(d, "reload",               item=PyCFunction_New(bpy_reload_meth, NULL));   Py_DECREF(item);
374                 PyDict_SetItemString(d, "__import__",   item=PyCFunction_New(bpy_import_meth, NULL));   Py_DECREF(item);
375         }
376         
377         pyrna_alloc_types();
378
379         py_tstate = PyGILState_GetThisThreadState();
380         PyEval_ReleaseThread(py_tstate);
381 }
382
383 void BPY_end_python( void )
384 {
385         // fprintf(stderr, "Ending Python!\n");
386
387         PyGILState_Ensure(); /* finalizing, no need to grab the state */
388         
389         // free other python data.
390         pyrna_free_types();
391
392         /* clear all python data from structs */
393         
394         Py_Finalize(  );
395         
396 #ifdef TIME_PY_RUN
397         // measure time since py started
398         bpy_timer = PIL_check_seconds_timer() - bpy_timer;
399
400         printf("*bpy stats* - ");
401         printf("tot exec: %d,  ", bpy_timer_count);
402         printf("tot run: %.4fsec,  ", bpy_timer_run_tot);
403         if(bpy_timer_count>0)
404                 printf("average run: %.6fsec,  ", (bpy_timer_run_tot/bpy_timer_count));
405
406         if(bpy_timer>0.0)
407                 printf("tot usage %.4f%%", (bpy_timer_run_tot/bpy_timer)*100.0);
408
409         printf("\n");
410
411         // fprintf(stderr, "Ending Python Done!\n");
412
413 #endif
414
415 }
416
417 /* Can run a file or text block */
418 int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struct ReportList *reports)
419 {
420         PyObject *py_dict, *py_result= NULL;
421         PyGILState_STATE gilstate;
422         
423         if (fn==NULL && text==NULL) {
424                 return 0;
425         }
426         
427         bpy_context_set(C, &gilstate);
428         
429         py_dict = CreateGlobalDictionary(C, text?text->id.name+2:fn);
430
431         if (text) {
432                 
433                 if( !text->compiled ) { /* if it wasn't already compiled, do it now */
434                         char *buf = txt_to_buf( text );
435
436                         text->compiled =
437                                 Py_CompileString( buf, text->id.name+2, Py_file_input );
438
439                         MEM_freeN( buf );
440
441                         if( PyErr_Occurred(  ) ) {
442                                 BPY_free_compiled_text( text );
443                         }
444                 }
445                 if(text->compiled)
446                         py_result =  PyEval_EvalCode( text->compiled, py_dict, py_dict );
447                 
448         } else {
449                 FILE *fp= fopen(fn, "r");               
450                 if(fp) {
451 #ifdef _WIN32
452                         /* Previously we used PyRun_File to run directly the code on a FILE 
453                          * object, but as written in the Python/C API Ref Manual, chapter 2,
454                          * 'FILE structs for different C libraries can be different and 
455                          * incompatible'.
456                          * So now we load the script file data to a buffer */
457                         char *pystring;
458
459                         fclose(fp);
460
461                         pystring= malloc(strlen(fn) + 32);
462                         pystring[0]= '\0';
463                         sprintf(pystring, "exec(open(r'%s').read())", fn);
464                         py_result = PyRun_String( pystring, Py_file_input, py_dict, py_dict );
465                         free(pystring);
466 #else
467                         py_result = PyRun_File(fp, fn, Py_file_input, py_dict, py_dict);
468                         fclose(fp);
469 #endif
470                 }
471                 else {
472                         PyErr_Format(PyExc_SystemError, "Python file \"%s\" could not be opened: %s", fn, strerror(errno));
473                         py_result= NULL;
474                 }
475         }
476         
477         if (!py_result) {
478                 BPy_errors_to_report(reports);
479         } else {
480                 Py_DECREF( py_result );
481         }
482         
483         Py_DECREF(py_dict);
484         
485         bpy_context_clear(C, &gilstate);
486
487         return py_result ? 1:0;
488 }
489
490
491 /* TODO - move into bpy_space.c ? */
492 /* GUI interface routines */
493
494 /* Copied from Draw.c */
495 static void exit_pydraw( SpaceScript * sc, short err )
496 {
497         Script *script = NULL;
498
499         if( !sc || !sc->script )
500                 return;
501
502         script = sc->script;
503
504         if( err ) {
505                 BPy_errors_to_report(NULL); // TODO, reports
506                 script->flags = 0;      /* mark script struct for deletion */
507                 SCRIPT_SET_NULL(script);
508                 script->scriptname[0] = '\0';
509                 script->scriptarg[0] = '\0';
510 // XXX 2.5              error_pyscript();
511 // XXX 2.5              scrarea_queue_redraw( sc->area );
512         }
513
514 #if 0 // XXX 2.5
515         BPy_Set_DrawButtonsList(sc->but_refs);
516         BPy_Free_DrawButtonsList(); /*clear all temp button references*/
517 #endif
518
519         sc->but_refs = NULL;
520         
521         Py_XDECREF( ( PyObject * ) script->py_draw );
522         Py_XDECREF( ( PyObject * ) script->py_event );
523         Py_XDECREF( ( PyObject * ) script->py_button );
524
525         script->py_draw = script->py_event = script->py_button = NULL;
526 }
527
528 static int bpy_run_script_init(bContext *C, SpaceScript * sc)
529 {
530         if (sc->script==NULL) 
531                 return 0;
532         
533         if (sc->script->py_draw==NULL && sc->script->scriptname[0] != '\0')
534                 BPY_run_python_script(C, sc->script->scriptname, NULL, NULL);
535                 
536         if (sc->script->py_draw==NULL)
537                 return 0;
538         
539         return 1;
540 }
541
542 int BPY_run_script_space_draw(const struct bContext *C, SpaceScript * sc)
543 {
544         if (bpy_run_script_init( (bContext *)C, sc)) {
545                 PyGILState_STATE gilstate = PyGILState_Ensure();
546                 PyObject *result = PyObject_CallObject( sc->script->py_draw, NULL );
547                 
548                 if (result==NULL)
549                         exit_pydraw(sc, 1);
550                         
551                 PyGILState_Release(gilstate);
552         }
553         return 1;
554 }
555
556 // XXX - not used yet, listeners dont get a context
557 int BPY_run_script_space_listener(bContext *C, SpaceScript * sc)
558 {
559         if (bpy_run_script_init(C, sc)) {
560                 PyGILState_STATE gilstate = PyGILState_Ensure();
561                 
562                 PyObject *result = PyObject_CallObject( sc->script->py_draw, NULL );
563                 
564                 if (result==NULL)
565                         exit_pydraw(sc, 1);
566                         
567                 PyGILState_Release(gilstate);
568         }
569         return 1;
570 }
571
572 void BPY_DECREF(void *pyob_ptr)
573 {
574         PyGILState_STATE gilstate = PyGILState_Ensure();
575         Py_DECREF((PyObject *)pyob_ptr);
576         PyGILState_Release(gilstate);
577 }
578
579 #if 0
580 /* called from the the scripts window, assume context is ok */
581 int BPY_run_python_script_space(const char *modulename, const char *func)
582 {
583         PyObject *py_dict, *py_result= NULL;
584         char pystring[512];
585         PyGILState_STATE gilstate;
586         
587         /* for calling the module function */
588         PyObject *py_func, 
589         
590         gilstate = PyGILState_Ensure();
591         
592         py_dict = CreateGlobalDictionary(C);
593         
594         PyObject *module = PyImport_ImportModule(scpt->script.filename);
595         if (module==NULL) {
596                 PyErr_SetFormat(PyExc_SystemError, "could not import '%s'", scpt->script.filename);
597         }
598         else {
599                 py_func = PyObject_GetAttrString(modulename, func);
600                 if (py_func==NULL) {
601                         PyErr_SetFormat(PyExc_SystemError, "module has no function '%s.%s'\n", scpt->script.filename, func);
602                 }
603                 else {
604                         Py_DECREF(py_func);
605                         if (!PyCallable_Check(py_func)) {
606                                 PyErr_SetFormat(PyExc_SystemError, "module item is not callable '%s.%s'\n", scpt->script.filename, func);
607                         }
608                         else {
609                                 py_result= PyObject_CallObject(py_func, NULL); // XXX will need args eventually
610                         }
611                 }
612         }
613         
614         if (!py_result) {
615                 BPy_errors_to_report(NULL); // TODO - reports
616         } else
617                 Py_DECREF( py_result );
618         
619         Py_XDECREF(module);
620         
621         Py_DECREF(py_dict);
622         
623         PyGILState_Release(gilstate);
624         return 1;
625 }
626 #endif
627
628 // #define TIME_REGISTRATION
629
630 #ifdef TIME_REGISTRATION
631 #include "PIL_time.h"
632 #endif
633
634
635 int BPY_button_eval(bContext *C, char *expr, double *value)
636 {
637         PyGILState_STATE gilstate;
638         PyObject *dict, *mod, *retval;
639         int error_ret = 0;
640         
641         if (!value || !expr || expr[0]=='\0') return -1;
642         
643         bpy_context_set(C, &gilstate);
644         
645         dict= CreateGlobalDictionary(C, NULL);
646         
647         /* import some modules: builtins,math*/
648         PyDict_SetItemString(dict, "__builtins__", PyEval_GetBuiltins());
649
650         mod = PyImport_ImportModule("math");
651         if (mod) {
652                 PyDict_Merge(dict, PyModule_GetDict(mod), 0); /* 0 - dont overwrite existing values */
653                 
654                 /* Only keep for backwards compat! - just import all math into root, they are standard */
655                 PyDict_SetItemString(dict, "math", mod);
656                 PyDict_SetItemString(dict, "m", mod);
657                 Py_DECREF(mod);
658         } 
659         
660         retval = PyRun_String(expr, Py_eval_input, dict, dict);
661         
662         if (retval == NULL) {
663                 error_ret= -1;
664         }
665         else {
666                 double val;
667
668                 if(PyTuple_Check(retval)) {
669                         /* Users my have typed in 10km, 2m
670                          * add up all values */
671                         int i;
672                         val= 0.0;
673
674                         for(i=0; i<PyTuple_GET_SIZE(retval); i++) {
675                                 val+= PyFloat_AsDouble(PyTuple_GET_ITEM(retval, i));
676                         }
677                 }
678                 else {
679                         val = PyFloat_AsDouble(retval);
680                 }
681                 Py_DECREF(retval);
682                 
683                 if(val==-1 && PyErr_Occurred()) {
684                         error_ret= -1;
685                 }
686                 else {
687                         *value= val;
688                 }
689         }
690         
691         if(error_ret) {
692                 BPy_errors_to_report(CTX_wm_reports(C));
693         }
694         
695         Py_DECREF(dict);
696         bpy_context_clear(C, &gilstate);
697         
698         return error_ret;
699 }
700
701 void BPY_load_user_modules(bContext *C)
702 {
703         PyGILState_STATE gilstate;
704         Text *text;
705
706         bpy_context_set(C, &gilstate);
707
708         for(text=CTX_data_main(C)->text.first; text; text= text->id.next) {
709                 if(text->flags & TXT_ISSCRIPT && BLI_testextensie(text->id.name+2, ".py")) {
710                         PyObject *module= bpy_text_import(text);
711
712                         if (module==NULL) {
713                                 PyErr_Print();
714                                 PyErr_Clear();
715                         }
716                         else {
717                                 Py_DECREF(module);
718                         }
719                 }
720         }
721         bpy_context_clear(C, &gilstate);
722 }
723
724 int BPY_context_get(bContext *C, const char *member, bContextDataResult *result)
725 {
726         PyObject *pyctx= (PyObject *)CTX_py_dict_get(C);
727         PyObject *item= PyDict_GetItemString(pyctx, member);
728         PointerRNA *ptr= NULL;
729         int done= 0;
730
731         if(item==NULL) {
732                 /* pass */
733         }
734         else if(item==Py_None) {
735                 /* pass */
736         }
737         else if(BPy_StructRNA_Check(item)) {
738                 ptr= &(((BPy_StructRNA *)item)->ptr);
739
740                 //result->ptr= ((BPy_StructRNA *)item)->ptr;
741                 CTX_data_pointer_set(result, ptr->id.data, ptr->type, ptr->data);
742                 done= 1;
743         }
744         else if (PySequence_Check(item)) {
745                 PyObject *seq_fast= PySequence_Fast(item, "bpy_context_get sequence conversion");
746                 if (seq_fast==NULL) {
747                         PyErr_Print();
748                         PyErr_Clear();
749                 }
750                 else {
751                         int len= PySequence_Fast_GET_SIZE(seq_fast);
752                         int i;
753                         for(i = 0; i < len; i++) {
754                                 PyObject *list_item= PySequence_Fast_GET_ITEM(seq_fast, i);
755
756                                 if(BPy_StructRNA_Check(list_item)) {
757                                         /*
758                                         CollectionPointerLink *link= MEM_callocN(sizeof(CollectionPointerLink), "bpy_context_get");
759                                         link->ptr= ((BPy_StructRNA *)item)->ptr;
760                                         BLI_addtail(&result->list, link);
761                                         */
762                                         ptr= &(((BPy_StructRNA *)list_item)->ptr);
763                                         CTX_data_list_add(result, ptr->id.data, ptr->type, ptr->data);
764                                 }
765                                 else {
766                                         printf("List item not a valid type\n");
767                                 }
768
769                         }
770                         Py_DECREF(seq_fast);
771
772                         done= 1;
773                 }
774         }
775
776         if(done==0) {
777                 if (item)       printf("Context '%s' not a valid type\n", member);
778                 else            printf("Context '%s' not found\n", member);
779         }
780         else if (G.f & G_DEBUG) {
781                 printf("Context '%s' found\n", member);
782         }
783
784         return done;
785 }
786