2c615a52860c3a29aeb0753eae124909ec71139e
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 /* grr, python redefines */
27 #ifdef _POSIX_C_SOURCE
28 #undef _POSIX_C_SOURCE
29 #endif
30
31 #include <Python.h>
32
33 #include "bpy.h"
34 #include "bpy_rna.h"
35 #include "bpy_util.h"
36
37 #include "DNA_space_types.h"
38 #include "DNA_text_types.h"
39
40 #include "MEM_guardedalloc.h"
41 #include "BLI_path_util.h"
42 #include "BLI_math_base.h"
43 #include "BLI_string.h"
44
45 #include "BKE_utildefines.h"
46 #include "BKE_context.h"
47 #include "BKE_text.h"
48 #include "BKE_font.h" /* only for utf8towchar */
49 #include "BKE_main.h"
50 #include "BKE_global.h" /* only for script checking */
51
52 #include "BPY_extern.h"
53
54 #include "../generic/bpy_internal_import.h" // our own imports
55 #include "../generic/py_capi_utils.h"
56
57 /* for internal use, when starting and ending python scripts */
58
59 /* incase a python script triggers another python call, stop bpy_context_clear from invalidating */
60 static int py_call_level= 0;
61 BPy_StructRNA *bpy_context_module= NULL; /* for fast access */
62
63 // only for tests
64 #define TIME_PY_RUN
65
66 #ifdef TIME_PY_RUN
67 #include "PIL_time.h"
68 static int              bpy_timer_count = 0;
69 static double   bpy_timer; /* time since python starts */
70 static double   bpy_timer_run; /* time for each python script run */
71 static double   bpy_timer_run_tot; /* accumulate python runs */
72 #endif
73
74 void bpy_context_set(bContext *C, PyGILState_STATE *gilstate)
75 {
76         py_call_level++;
77
78         if(gilstate)
79                 *gilstate = PyGILState_Ensure();
80
81         if(py_call_level==1) {
82
83                 if(C) { // XXX - should always be true.
84                         BPy_SetContext(C);
85                         bpy_import_main_set(CTX_data_main(C));
86                 }
87                 else {
88                         fprintf(stderr, "ERROR: Python context called with a NULL Context. this should not happen!\n");
89                 }
90
91                 BPY_update_modules(C); /* can give really bad results if this isnt here */
92
93 #ifdef TIME_PY_RUN
94                 if(bpy_timer_count==0) {
95                         /* record time from the beginning */
96                         bpy_timer= PIL_check_seconds_timer();
97                         bpy_timer_run = bpy_timer_run_tot = 0.0;
98                 }
99                 bpy_timer_run= PIL_check_seconds_timer();
100
101
102                 bpy_timer_count++;
103 #endif
104         }
105 }
106
107 /* context should be used but not now because it causes some bugs */
108 void bpy_context_clear(bContext *UNUSED(C), PyGILState_STATE *gilstate)
109 {
110         py_call_level--;
111
112         if(gilstate)
113                 PyGILState_Release(*gilstate);
114
115         if(py_call_level < 0) {
116                 fprintf(stderr, "ERROR: Python context internal state bug. this should not happen!\n");
117         }
118         else if(py_call_level==0) {
119                 // XXX - Calling classes currently wont store the context :\, cant set NULL because of this. but this is very flakey still.
120                 //BPy_SetContext(NULL);
121                 //bpy_import_main_set(NULL);
122
123 #ifdef TIME_PY_RUN
124                 bpy_timer_run_tot += PIL_check_seconds_timer() - bpy_timer_run;
125                 bpy_timer_count++;
126 #endif
127
128         }
129 }
130
131 void BPY_free_compiled_text( struct Text *text )
132 {
133         if( text->compiled ) {
134                 Py_DECREF( ( PyObject * ) text->compiled );
135                 text->compiled = NULL;
136         }
137 }
138
139 void BPY_update_modules(bContext *C)
140 {
141 #if 0 // slow, this runs all the time poll, draw etc 100's of time a sec.
142         PyObject *mod= PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL, 0);
143         PyModule_AddObject( mod, "data", BPY_rna_module() );
144         PyModule_AddObject( mod, "types", BPY_rna_types() ); // atm this does not need updating
145 #endif
146
147         /* refreshes the main struct */
148         BPY_update_rna_module();
149         bpy_context_module->ptr.data= (void *)C;
150 }
151
152 /* must be called before Py_Initialize */
153 void BPY_start_python_path(void)
154 {
155         char *py_path_bundle= BLI_get_folder(BLENDER_PYTHON, NULL);
156
157         if(py_path_bundle==NULL)
158                 return;
159
160         /* set the environment path */
161         printf("found bundled python: %s\n", py_path_bundle);
162
163 #ifdef __APPLE__
164         /* OSX allow file/directory names to contain : character (represented as / in the Finder)
165          but current Python lib (release 3.1.1) doesn't handle these correctly */
166         if(strchr(py_path_bundle, ':'))
167                 printf("Warning : Blender application is located in a path containing : or / chars\
168                            \nThis may make python import function fail\n");
169 #endif
170         
171 #ifdef _WIN32
172         /* cmake/MSVC debug build crashes without this, why only
173            in this case is unknown.. */
174         {
175                 BLI_setenv("PYTHONPATH", py_path_bundle);       
176         }
177 #endif
178
179         {
180                 static wchar_t py_path_bundle_wchar[FILE_MAX];
181
182                 /* cant use this, on linux gives bug: #23018, TODO: try LANG="en_US.UTF-8" /usr/bin/blender, suggested 22008 */
183                 /* mbstowcs(py_path_bundle_wchar, py_path_bundle, FILE_MAXDIR); */
184
185                 utf8towchar(py_path_bundle_wchar, py_path_bundle);
186
187                 Py_SetPythonHome(py_path_bundle_wchar);
188                 // printf("found python (wchar_t) '%ls'\n", py_path_bundle_wchar);
189         }
190 }
191
192
193
194 void BPY_set_context(bContext *C)
195 {
196         BPy_SetContext(C);
197 }
198
199 /* init-tab */
200 extern PyObject *BPyInit_noise(void);
201 extern PyObject *BPyInit_mathutils(void);
202 // extern PyObject *BPyInit_mathutils_geometry(void); // BPyInit_mathutils calls, py doesnt work with thos :S
203 extern PyObject *BPyInit_bgl(void);
204 extern PyObject *BPyInit_blf(void);
205 extern PyObject *AUD_initPython(void);
206
207 static struct _inittab bpy_internal_modules[]= {
208         {(char *)"noise", BPyInit_noise},
209         {(char *)"mathutils", BPyInit_mathutils},
210 //      {(char *)"mathutils.geometry", BPyInit_mathutils_geometry},
211         {(char *)"bgl", BPyInit_bgl},
212         {(char *)"blf", BPyInit_blf},
213         {(char *)"aud", AUD_initPython},
214         {NULL, NULL}
215 };
216
217 /* call BPY_set_context first */
218 void BPY_start_python( int argc, char **argv )
219 {
220         PyThreadState *py_tstate = NULL;
221         
222         /* not essential but nice to set our name */
223         static wchar_t bprogname_wchar[FILE_MAXDIR+FILE_MAXFILE]; /* python holds a reference */
224         utf8towchar(bprogname_wchar, bprogname);
225         Py_SetProgramName(bprogname_wchar);
226
227         /* builtin modules */
228         PyImport_ExtendInittab(bpy_internal_modules);
229
230         BPY_start_python_path(); /* allow to use our own included python */
231
232         Py_Initialize(  );
233         
234         // PySys_SetArgv( argc, argv); // broken in py3, not a huge deal
235         /* sigh, why do python guys not have a char** version anymore? :( */
236         {
237                 int i;
238                 PyObject *py_argv= PyList_New(argc);
239                 for (i=0; i<argc; i++)
240                         PyList_SET_ITEM(py_argv, i, PyC_UnicodeFromByte(argv[i])); /* should fix bug #20021 - utf path name problems, by replacing PyUnicode_FromString */
241
242                 PySys_SetObject("argv", py_argv);
243                 Py_DECREF(py_argv);
244         }
245         
246         /* Initialize thread support (also acquires lock) */
247         PyEval_InitThreads();
248         
249         
250         /* bpy.* and lets us import it */
251         BPy_init_modules();
252
253         { /* our own import and reload functions */
254                 PyObject *item;
255                 PyObject *mod;
256                 //PyObject *m = PyImport_AddModule("__builtin__");
257                 //PyObject *d = PyModule_GetDict(m);
258                 PyObject *d = PyEval_GetBuiltins(  );
259 //              PyDict_SetItemString(d, "reload",               item=PyCFunction_New(&bpy_reload_meth, NULL));  Py_DECREF(item);
260                 PyDict_SetItemString(d, "__import__",   item=PyCFunction_New(&bpy_import_meth, NULL));  Py_DECREF(item);
261
262                 /* move reload here
263                  * XXX, use import hooks */
264                 mod= PyImport_ImportModuleLevel((char *)"imp", NULL, NULL, NULL, 0);
265                 if(mod) {
266                         PyDict_SetItemString(PyModule_GetDict(mod), "reload",           item=PyCFunction_New(&bpy_reload_meth, NULL));  Py_DECREF(item);
267                         Py_DECREF(mod);
268                 }
269                 else {
270                         BKE_assert(!"unable to load 'imp' module.");
271                 }
272
273         }
274         
275         pyrna_alloc_types();
276
277         py_tstate = PyGILState_GetThisThreadState();
278         PyEval_ReleaseThread(py_tstate);
279 }
280
281 void BPY_end_python( void )
282 {
283         // fprintf(stderr, "Ending Python!\n");
284
285         PyGILState_Ensure(); /* finalizing, no need to grab the state */
286         
287         // free other python data.
288         pyrna_free_types();
289
290         /* clear all python data from structs */
291         
292         Py_Finalize(  );
293         
294 #ifdef TIME_PY_RUN
295         // measure time since py started
296         bpy_timer = PIL_check_seconds_timer() - bpy_timer;
297
298         printf("*bpy stats* - ");
299         printf("tot exec: %d,  ", bpy_timer_count);
300         printf("tot run: %.4fsec,  ", bpy_timer_run_tot);
301         if(bpy_timer_count>0)
302                 printf("average run: %.6fsec,  ", (bpy_timer_run_tot/bpy_timer_count));
303
304         if(bpy_timer>0.0)
305                 printf("tot usage %.4f%%", (bpy_timer_run_tot/bpy_timer)*100.0);
306
307         printf("\n");
308
309         // fprintf(stderr, "Ending Python Done!\n");
310
311 #endif
312
313 }
314
315 /* Can run a file or text block */
316 int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struct ReportList *reports)
317 {
318         PyObject *py_dict= NULL, *py_result= NULL;
319         PyGILState_STATE gilstate;
320         
321         if (fn==NULL && text==NULL) {
322                 return 0;
323         }
324
325         bpy_context_set(C, &gilstate);
326
327         if (text) {
328                 char fn_dummy[FILE_MAXDIR];
329                 bpy_text_filename_get(fn_dummy, text);
330                 
331                 if( !text->compiled ) { /* if it wasn't already compiled, do it now */
332                         char *buf = txt_to_buf( text );
333
334                         text->compiled =
335                                 Py_CompileString( buf, fn_dummy, Py_file_input );
336
337                         MEM_freeN( buf );
338
339                         if( PyErr_Occurred(  ) ) {
340                                 BPY_free_compiled_text( text );
341                         }
342                 }
343
344                 if(text->compiled) {
345                         py_dict = PyC_DefaultNameSpace(fn_dummy);
346                         py_result =  PyEval_EvalCode(text->compiled, py_dict, py_dict);
347                 }
348                 
349         }
350         else {
351                 FILE *fp= fopen(fn, "r");
352
353                 if(fp) {
354                         py_dict = PyC_DefaultNameSpace(fn);
355
356 #ifdef _WIN32
357                         /* Previously we used PyRun_File to run directly the code on a FILE 
358                          * object, but as written in the Python/C API Ref Manual, chapter 2,
359                          * 'FILE structs for different C libraries can be different and 
360                          * incompatible'.
361                          * So now we load the script file data to a buffer */
362                         {
363                                 char *pystring;
364
365                                 fclose(fp);
366
367                                 pystring= MEM_mallocN(strlen(fn) + 32, "pystring");
368                                 pystring[0]= '\0';
369                                 sprintf(pystring, "exec(open(r'%s').read())", fn);
370                                 py_result = PyRun_String( pystring, Py_file_input, py_dict, py_dict );
371                                 MEM_freeN(pystring);
372                         }
373 #else
374                         py_result = PyRun_File(fp, fn, Py_file_input, py_dict, py_dict);
375                         fclose(fp);
376 #endif
377                 }
378                 else {
379                         PyErr_Format(PyExc_SystemError, "Python file \"%s\" could not be opened: %s", fn, strerror(errno));
380                         py_result= NULL;
381                 }
382         }
383         
384         if (!py_result) {
385                 BPy_errors_to_report(reports);
386         } else {
387                 Py_DECREF( py_result );
388         }
389
390 /* super annoying, undo _PyModule_Clear() */    
391 #define PYMODULE_CLEAR_WORKAROUND
392
393         if(py_dict) {
394 #ifdef PYMODULE_CLEAR_WORKAROUND
395                 PyObject *py_dict_back= PyDict_Copy(py_dict);
396                 Py_INCREF(py_dict);
397 #endif
398                 /* normal */
399                 PyDict_SetItemString(PyThreadState_GET()->interp->modules, "__main__", Py_None);
400 #ifdef PYMODULE_CLEAR_WORKAROUND
401                 PyDict_Clear(py_dict);
402                 PyDict_Update(py_dict, py_dict_back);
403                 Py_DECREF(py_dict);
404                 Py_DECREF(py_dict_back);
405 #endif
406 #undef PYMODULE_CLEAR_WORKAROUND
407         }
408         
409         bpy_context_clear(C, &gilstate);
410
411         return py_result ? 1:0;
412 }
413
414
415 /* TODO - move into bpy_space.c ? */
416 /* GUI interface routines */
417
418 /* Copied from Draw.c */
419 static void exit_pydraw( SpaceScript * sc, short err )
420 {
421         Script *script = NULL;
422
423         if( !sc || !sc->script )
424                 return;
425
426         script = sc->script;
427
428         if( err ) {
429                 BPy_errors_to_report(NULL); // TODO, reports
430                 script->flags = 0;      /* mark script struct for deletion */
431                 SCRIPT_SET_NULL(script);
432                 script->scriptname[0] = '\0';
433                 script->scriptarg[0] = '\0';
434 // XXX 2.5              error_pyscript();
435 // XXX 2.5              scrarea_queue_redraw( sc->area );
436         }
437
438 #if 0 // XXX 2.5
439         BPy_Set_DrawButtonsList(sc->but_refs);
440         BPy_Free_DrawButtonsList(); /*clear all temp button references*/
441 #endif
442
443         sc->but_refs = NULL;
444         
445         Py_XDECREF( ( PyObject * ) script->py_draw );
446         Py_XDECREF( ( PyObject * ) script->py_event );
447         Py_XDECREF( ( PyObject * ) script->py_button );
448
449         script->py_draw = script->py_event = script->py_button = NULL;
450 }
451
452 static int bpy_run_script_init(bContext *C, SpaceScript * sc)
453 {
454         if (sc->script==NULL) 
455                 return 0;
456         
457         if (sc->script->py_draw==NULL && sc->script->scriptname[0] != '\0')
458                 BPY_run_python_script(C, sc->script->scriptname, NULL, NULL);
459                 
460         if (sc->script->py_draw==NULL)
461                 return 0;
462         
463         return 1;
464 }
465
466 int BPY_run_script_space_draw(const struct bContext *C, SpaceScript * sc)
467 {
468         if (bpy_run_script_init( (bContext *)C, sc)) {
469                 PyGILState_STATE gilstate = PyGILState_Ensure();
470                 PyObject *result = PyObject_CallObject( sc->script->py_draw, NULL );
471                 
472                 if (result==NULL)
473                         exit_pydraw(sc, 1);
474                         
475                 PyGILState_Release(gilstate);
476         }
477         return 1;
478 }
479
480 // XXX - not used yet, listeners dont get a context
481 int BPY_run_script_space_listener(bContext *C, SpaceScript * sc)
482 {
483         if (bpy_run_script_init(C, sc)) {
484                 PyGILState_STATE gilstate = PyGILState_Ensure();
485                 
486                 PyObject *result = PyObject_CallObject( sc->script->py_draw, NULL );
487                 
488                 if (result==NULL)
489                         exit_pydraw(sc, 1);
490                         
491                 PyGILState_Release(gilstate);
492         }
493         return 1;
494 }
495
496 void BPY_DECREF(void *pyob_ptr)
497 {
498         PyGILState_STATE gilstate = PyGILState_Ensure();
499         Py_DECREF((PyObject *)pyob_ptr);
500         PyGILState_Release(gilstate);
501 }
502
503 #if 0
504 /* called from the the scripts window, assume context is ok */
505 int BPY_run_python_script_space(const char *modulename, const char *func)
506 {
507         PyObject *py_dict, *py_result= NULL;
508         char pystring[512];
509         PyGILState_STATE gilstate;
510         
511         /* for calling the module function */
512         PyObject *py_func, 
513         
514         gilstate = PyGILState_Ensure();
515         
516         py_dict = PyC_DefaultNameSpace("<dummy>");
517         
518         PyObject *module = PyImport_ImportModule(scpt->script.filename);
519         if (module==NULL) {
520                 PyErr_SetFormat(PyExc_SystemError, "could not import '%s'", scpt->script.filename);
521         }
522         else {
523                 py_func = PyObject_GetAttrString(modulename, func);
524                 if (py_func==NULL) {
525                         PyErr_SetFormat(PyExc_SystemError, "module has no function '%s.%s'\n", scpt->script.filename, func);
526                 }
527                 else {
528                         Py_DECREF(py_func);
529                         if (!PyCallable_Check(py_func)) {
530                                 PyErr_SetFormat(PyExc_SystemError, "module item is not callable '%s.%s'\n", scpt->script.filename, func);
531                         }
532                         else {
533                                 py_result= PyObject_CallObject(py_func, NULL); // XXX will need args eventually
534                         }
535                 }
536         }
537         
538         if (!py_result) {
539                 BPy_errors_to_report(NULL); // TODO - reports
540         } else
541                 Py_DECREF( py_result );
542         
543         Py_XDECREF(module);
544         
545         PyDict_SetItemString(PyThreadState_GET()->interp->modules, "__main__", Py_None);
546         
547         PyGILState_Release(gilstate);
548         return 1;
549 }
550 #endif
551
552
553 int BPY_eval_button(bContext *C, const char *expr, double *value)
554 {
555         PyGILState_STATE gilstate;
556         PyObject *py_dict, *mod, *retval;
557         int error_ret = 0;
558         
559         if (!value || !expr) return -1;
560
561         if(expr[0]=='\0') {
562                 *value= 0.0;
563                 return error_ret;
564         }
565
566         bpy_context_set(C, &gilstate);
567         
568         py_dict= PyC_DefaultNameSpace("<blender button>");
569
570         mod = PyImport_ImportModule("math");
571         if (mod) {
572                 PyDict_Merge(py_dict, PyModule_GetDict(mod), 0); /* 0 - dont overwrite existing values */
573                 Py_DECREF(mod);
574         }
575         else { /* highly unlikely but possibly */
576                 PyErr_Print();
577                 PyErr_Clear();
578         }
579         
580         retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict);
581         
582         if (retval == NULL) {
583                 error_ret= -1;
584         }
585         else {
586                 double val;
587
588                 if(PyTuple_Check(retval)) {
589                         /* Users my have typed in 10km, 2m
590                          * add up all values */
591                         int i;
592                         val= 0.0;
593
594                         for(i=0; i<PyTuple_GET_SIZE(retval); i++) {
595                                 val+= PyFloat_AsDouble(PyTuple_GET_ITEM(retval, i));
596                         }
597                 }
598                 else {
599                         val = PyFloat_AsDouble(retval);
600                 }
601                 Py_DECREF(retval);
602                 
603                 if(val==-1 && PyErr_Occurred()) {
604                         error_ret= -1;
605                 }
606                 else if (!finite(val)) {
607                         *value= 0.0;
608                 }
609                 else {
610                         *value= val;
611                 }
612         }
613         
614         if(error_ret) {
615                 BPy_errors_to_report(CTX_wm_reports(C));
616         }
617
618         PyDict_SetItemString(PyThreadState_GET()->interp->modules, "__main__", Py_None);
619         
620         bpy_context_clear(C, &gilstate);
621         
622         return error_ret;
623 }
624
625 int BPY_eval_string(bContext *C, const char *expr)
626 {
627         PyGILState_STATE gilstate;
628         PyObject *py_dict, *retval;
629         int error_ret = 0;
630
631         if (!expr) return -1;
632
633         if(expr[0]=='\0') {
634                 return error_ret;
635         }
636
637         bpy_context_set(C, &gilstate);
638
639         py_dict= PyC_DefaultNameSpace("<blender string>");
640
641         retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict);
642
643         if (retval == NULL) {
644                 error_ret= -1;
645
646                 BPy_errors_to_report(CTX_wm_reports(C));
647         }
648         else {
649                 Py_DECREF(retval);
650         }
651
652         PyDict_SetItemString(PyThreadState_GET()->interp->modules, "__main__", Py_None);
653         
654         bpy_context_clear(C, &gilstate);
655         
656         return error_ret;
657 }
658
659
660 void BPY_load_user_modules(bContext *C)
661 {
662         PyGILState_STATE gilstate;
663         Main *bmain= CTX_data_main(C);
664         Text *text;
665
666         /* can happen on file load */
667         if(bmain==NULL)
668                 return;
669
670         bpy_context_set(C, &gilstate);
671
672         for(text=CTX_data_main(C)->text.first; text; text= text->id.next) {
673                 if(text->flags & TXT_ISSCRIPT && BLI_testextensie(text->id.name+2, ".py")) {
674                         if(!(G.f & G_SCRIPT_AUTOEXEC)) {
675                                 printf("scripts disabled for \"%s\", skipping '%s'\n", bmain->name, text->id.name+2);
676                         }
677                         else {
678                                 PyObject *module= bpy_text_import(text);
679
680                                 if (module==NULL) {
681                                         PyErr_Print();
682                                         PyErr_Clear();
683                                 }
684                                 else {
685                                         Py_DECREF(module);
686                                 }
687                         }
688                 }
689         }
690         bpy_context_clear(C, &gilstate);
691 }
692
693 int BPY_context_get(bContext *C, const char *member, bContextDataResult *result)
694 {
695         PyObject *pyctx= (PyObject *)CTX_py_dict_get(C);
696         PyObject *item= PyDict_GetItemString(pyctx, member);
697         PointerRNA *ptr= NULL;
698         int done= 0;
699
700         if(item==NULL) {
701                 /* pass */
702         }
703         else if(item==Py_None) {
704                 /* pass */
705         }
706         else if(BPy_StructRNA_Check(item)) {
707                 ptr= &(((BPy_StructRNA *)item)->ptr);
708
709                 //result->ptr= ((BPy_StructRNA *)item)->ptr;
710                 CTX_data_pointer_set(result, ptr->id.data, ptr->type, ptr->data);
711                 done= 1;
712         }
713         else if (PySequence_Check(item)) {
714                 PyObject *seq_fast= PySequence_Fast(item, "bpy_context_get sequence conversion");
715                 if (seq_fast==NULL) {
716                         PyErr_Print();
717                         PyErr_Clear();
718                 }
719                 else {
720                         int len= PySequence_Fast_GET_SIZE(seq_fast);
721                         int i;
722                         for(i = 0; i < len; i++) {
723                                 PyObject *list_item= PySequence_Fast_GET_ITEM(seq_fast, i);
724
725                                 if(BPy_StructRNA_Check(list_item)) {
726                                         /*
727                                         CollectionPointerLink *link= MEM_callocN(sizeof(CollectionPointerLink), "bpy_context_get");
728                                         link->ptr= ((BPy_StructRNA *)item)->ptr;
729                                         BLI_addtail(&result->list, link);
730                                         */
731                                         ptr= &(((BPy_StructRNA *)list_item)->ptr);
732                                         CTX_data_list_add(result, ptr->id.data, ptr->type, ptr->data);
733                                 }
734                                 else {
735                                         printf("List item not a valid type\n");
736                                 }
737
738                         }
739                         Py_DECREF(seq_fast);
740
741                         done= 1;
742                 }
743         }
744
745         if(done==0) {
746                 if (item)       printf("Context '%s' not a valid type\n", member);
747                 else            printf("Context '%s' not found\n", member);
748         }
749         else {
750                 printf("Context '%s' found\n", member);
751         }
752
753         return done;
754 }
755