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