Merged from trunk
[blender-staging.git] / source / blender / python / 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  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * This is a new part of Blender.
24  *
25  * Contributor(s): Michel Selten, Willian P. Germano, Stephen Swaney,
26  * Chris Keith, Chris Want, Ken Hughes
27  *
28  * ***** END GPL LICENSE BLOCK *****
29 */
30
31 #include <Python.h>
32
33 #include "compile.h"            /* for the PyCodeObject */
34 #include "eval.h"               /* for PyEval_EvalCode */
35 #include "BLI_blenlib.h"        /* for BLI_last_slash() */
36 #include "BIF_interface.h"      /* for pupmenu() */
37 #include "BIF_space.h"
38 #include "BIF_screen.h"
39 #include "BIF_toolbox.h"
40 #include "BKE_action.h"         /* for get_pose_channel() */
41 #include "BKE_library.h"
42 #include "BKE_object.h"         /* during_scriptlink() */
43 #include "BKE_text.h"
44 #include "BKE_constraint.h" /* for bConstraintOb */
45 #include "BKE_idprop.h"
46
47 #include "DNA_curve_types.h" /* for struct IpoDriver */
48 #include "DNA_ID.h" /* ipo driver */
49 #include "DNA_object_types.h" /* ipo driver */
50 #include "DNA_constraint_types.h" /* for pyconstraint */
51
52 #include "DNA_screen_types.h"
53 #include "DNA_userdef_types.h"  /* for U.pythondir */
54 #include "MEM_guardedalloc.h"
55 #include "BPY_extern.h"
56 #include "BPY_menus.h"
57 #include "DNA_space_types.h"
58 #include "BKE_global.h"
59 #include "BKE_main.h"
60 #include "BKE_armature.h"
61 #include "BKE_depsgraph.h"
62 #include "api2_2x/EXPP_interface.h"
63 #include "api2_2x/constant.h"
64 #include "api2_2x/gen_utils.h"
65 #include "api2_2x/gen_library.h" /* GetPyObjectFromID */
66 #include "api2_2x/BGL.h" 
67 #include "api2_2x/Blender.h"
68 #include "api2_2x/Camera.h"
69 #include "api2_2x/Draw.h"
70 #include "api2_2x/Object.h"
71 #include "api2_2x/Registry.h"
72 #include "api2_2x/Pose.h"
73 #include "api2_2x/bpy.h" /* for the new "bpy" module */
74
75 /*these next two are for pyconstraints*/
76 #include "api2_2x/IDProp.h"
77 #include "api2_2x/matrix.h"
78
79 /* for scriptlinks */
80 #include "DNA_lamp_types.h"
81 #include "DNA_camera_types.h"
82 #include "DNA_world_types.h"
83 #include "DNA_scene_types.h"
84 #include "DNA_material_types.h"
85
86 /* bpy_registryDict is declared in api2_2x/Registry.h and defined
87  * in api2_2x/Registry.c
88  * This Python dictionary will be used to store data that scripts
89  * choose to preserve after they are executed, so user changes can be
90  * restored next time the script is used.  Check the Blender.Registry module. 
91  */
92 /*#include "api2_2x/Registry.h" */
93
94 /* for pydrivers (ipo drivers defined by one-line Python expressions) */
95 PyObject *bpy_pydriver_Dict = NULL;
96 PyObject *bpy_orig_syspath_List = NULL;
97
98 /*
99  * set up a weakref list for Armatures
100  *    creates list in __main__ module dict 
101  */
102   
103 int setup_armature_weakrefs()
104 {
105         PyObject *maindict;
106         PyObject *main_module;
107         char *list_name = ARM_WEAKREF_LIST_NAME;
108
109         main_module = PyImport_AddModule( "__main__");
110         if(main_module){
111                 PyObject *weakreflink;
112                 maindict= PyModule_GetDict(main_module);
113
114                 /* check if there is already a dict entry for the armature weakrefs,
115                  * and delete if so before making another one */
116
117                 weakreflink= PyDict_GetItemString(maindict,list_name);
118                 if( weakreflink != NULL ) {
119                         PyDict_DelItemString(maindict,list_name);
120                         Py_XDECREF( weakreflink );
121                 }
122
123                 if (PyDict_SetItemString(maindict, 
124                                                                  list_name, 
125                                                                  PyList_New(0)) == -1){
126                         printf("Oops - setup_armature_weakrefs()\n");
127                         
128                         return 0;
129                 }
130         }
131         return 1;
132 }
133
134 /* Declares the modules and their initialization functions
135  * These are TOP-LEVEL modules e.g. import `module` - there is no
136  * support for packages here e.g. import `package.module` */
137
138 static struct _inittab BPy_Inittab_Modules[] = {
139         {"Blender", M_Blender_Init},
140         {"bpy", m_bpy_init},
141         {NULL, NULL}
142 };
143
144 /*************************************************************************
145 * Structure definitions 
146 **************************************************************************/
147 #define FILENAME_LENGTH 24
148
149 typedef struct _ScriptError {
150         char filename[FILENAME_LENGTH];
151         int lineno;
152 } ScriptError;
153
154 /****************************************************************************
155 * Global variables 
156 ****************************************************************************/
157 ScriptError g_script_error;
158
159 /***************************************************************************
160 * Function prototypes 
161 ***************************************************************************/
162 PyObject *RunPython( Text * text, PyObject * globaldict );
163 char *GetName( Text * text );
164 PyObject *CreateGlobalDictionary( void );
165 void ReleaseGlobalDictionary( PyObject * dict );
166 void DoAllScriptsFromList( ListBase * list, short event );
167 PyObject *importText( char *name );
168 void init_ourImport( void );
169 void init_ourReload( void );
170 PyObject *blender_import( PyObject * self, PyObject * args );
171 PyObject *RunPython2( Text * text, PyObject * globaldict, PyObject *localdict );
172
173
174 void BPY_Err_Handle( char *script_name );
175 PyObject *traceback_getFilename( PyObject * tb );
176
177 /****************************************************************************
178 * Description: This function will start the interpreter and load all modules
179 * as well as search for a python installation.
180 ****************************************************************************/
181 void BPY_start_python( int argc, char **argv )
182 {
183         PyThreadState *py_tstate = NULL;
184         static int argc_copy = 0;
185         static char **argv_copy = NULL;
186         int first_time = argc;
187
188         /* we keep a copy of the values of argc and argv so that the game engine
189          * can call BPY_start_python(0, NULL) whenever a game ends, without having
190          * to know argc and argv there (in source/blender/src/space.c) */
191         if( first_time ) {
192                 argc_copy = argc;
193                 argv_copy = argv;
194         }
195
196         //stuff for Registry module
197         bpy_registryDict = PyDict_New(  );/* check comment at start of this file */
198         if( !bpy_registryDict )
199                 printf( "Error: Couldn't create the Registry Python Dictionary!" );
200         Py_SetProgramName( "blender" );
201
202         /* Py_Initialize() will attempt to import the site module and
203          * print an error if not found.  See init_syspath() for the
204          * rest of our init msgs.
205          */
206
207         /* print Python version
208          * Py_GetVersion() returns a ptr to a static string "9.9.9 (aaaa..." 
209          */
210         {
211                 int count = 3;  /* a nice default for major.minor.  example 2.5 */
212                 const char *version = Py_GetVersion();
213                 /* we know a blank is there somewhere! */
214                 char *blank_ptr = strchr( version, ' '); 
215                 if(blank_ptr)
216                         count = blank_ptr - version;
217                 
218                 printf( "Compiled with Python version %.*s.\n", count, version );
219         }
220
221
222         //Initialize the TOP-LEVEL modules
223         PyImport_ExtendInittab(BPy_Inittab_Modules);
224         
225         //Start the interpreter
226         Py_Initialize(  );
227         PySys_SetArgv( argc_copy, argv_copy );
228
229         /* Initialize thread support (also acquires lock) */
230         PyEval_InitThreads();
231
232         /* Don't allow the Python Interpreter to release the GIL on
233          * its own, to guarantee PyNodes work properly. For Blender this
234          * is currently the best default behavior.
235          * The following code in C is equivalent in Python to:
236          * "import sys; sys.setcheckinterval(sys.maxint)" */
237         _Py_CheckInterval = PyInt_GetMax();
238
239         //Overrides __import__
240         init_ourImport(  );
241         init_ourReload(  );
242
243         //init a global dictionary
244         g_blenderdict = NULL;
245
246         //Look for a python installation
247         init_syspath( first_time ); /* not first_time: some msgs are suppressed */
248
249         py_tstate = PyGILState_GetThisThreadState();
250         PyEval_ReleaseThread(py_tstate);
251
252         return;
253 }
254
255 /*****************************************************************************/
256 /* Description: This function will terminate the Python interpreter          */
257 /*****************************************************************************/
258 void BPY_end_python( void )
259 {
260         Script *script = NULL;
261         Script *next_script = NULL;
262
263         PyGILState_Ensure(); /* finalizing, no need to grab the state */
264
265         if( bpy_registryDict ) {
266                 Py_DECREF( bpy_registryDict );
267                 bpy_registryDict = NULL;
268         }
269
270         if( bpy_pydriver_Dict ) {
271                 Py_DECREF( bpy_pydriver_Dict );
272                 bpy_pydriver_Dict = NULL;
273         }
274         
275         if( bpy_orig_syspath_List ) {
276                 Py_DECREF( bpy_orig_syspath_List );
277                 bpy_orig_syspath_List = NULL;
278         }
279
280         /* Freeing all scripts here prevents problems with the order in which
281          * Python is finalized and G.main is freed in exit_usiblender() */
282         for (script = G.main->script.first; script; script = next_script) {
283                 next_script = script->id.next;
284                 free_libblock( &G.main->script, script );
285         }
286
287         Py_Finalize(  );
288
289         BPyMenu_RemoveAllEntries(  );   /* freeing bpymenu mem */
290
291         /* a script might've opened a .blend file but didn't close it, so: */
292         EXPP_Library_Close(  );
293
294         return;
295 }
296
297 void syspath_append( char *dirname )
298 {
299         PyObject *mod_sys= NULL, *dict= NULL, *path= NULL, *dir= NULL;
300         short ok=1;
301         PyErr_Clear(  );
302
303         dir = Py_BuildValue( "s", dirname );
304
305         mod_sys = PyImport_ImportModule( "sys" );       /* new ref */
306         
307         if (mod_sys) {
308                 dict = PyModule_GetDict( mod_sys );     /* borrowed ref */
309                 path = PyDict_GetItemString( dict, "path" );    /* borrowed ref */
310                 if ( !PyList_Check( path ) ) {
311                         ok = 0;
312                 }
313         } else {
314                 /* cant get the sys module */
315                 ok = 0;
316         }
317         
318         if (PySequence_Contains(path, dir)==0) { /* Only add if we need to */
319                 if (ok && PyList_Append( path, dir ) != 0)
320                         ok = 0; /* append failed */
321         
322                 if( (ok==0) || PyErr_Occurred(  ) )
323                         Py_FatalError( "could import or build sys.path, can't continue" );
324         }
325         Py_XDECREF( mod_sys );
326 }
327
328 void init_syspath( int first_time )
329 {
330         PyObject *path;
331         PyObject *mod, *d;
332         char *progname;
333         char execdir[FILE_MAXDIR];      /*defines from DNA_space_types.h */
334
335         int n;
336         
337         
338         path = Py_BuildValue( "s", bprogname );
339
340         mod = PyImport_ImportModule( "Blender.sys" );
341
342         if( mod ) {
343                 d = PyModule_GetDict( mod );
344                 EXPP_dict_set_item_str( d, "progname", path );
345                 Py_DECREF( mod );
346         } else
347                 printf( "Warning: could not set Blender.sys.progname\n" );
348
349         progname = BLI_last_slash( bprogname ); /* looks for the last dir separator */
350
351         n = progname - bprogname;
352         if( n > 0 ) {
353                 strncpy( execdir, bprogname, n );
354                 if( execdir[n - 1] == '.' )
355                         n--;    /*fix for when run as ./blender */
356                 execdir[n] = '\0';
357
358                 syspath_append( execdir );      /* append to module search path */
359         } else {
360                 printf( "Warning: could not determine argv[0] path\n" );
361         }
362         
363         /* 
364            attempt to import 'site' module as a check for valid
365            python install found.
366         */
367
368         printf("Checking for installed Python... "); /* appears after msg "Compiled with Python 2.x"  */
369         mod = PyImport_ImportModule( "site" );  /* new ref */
370
371         if( mod ) {
372                 printf("got it!\n");  
373                 Py_DECREF( mod );
374         } else {                /* import 'site' failed */
375                 PyErr_Clear(  );
376                 if( first_time ) {
377                         printf( "No installed Python found.\n" );
378                         printf( "Only built-in modules are available.  Some scripts may not run.\n" );
379                         printf( "Continuing happily.\n" );
380                 }
381         }
382
383
384         /* 
385          * initialize the sys module
386          * set sys.executable to the Blender exe 
387          */
388
389         mod = PyImport_ImportModule( "sys" );   /* new ref */
390
391         if( mod ) {
392                 d = PyModule_GetDict( mod );    /* borrowed ref */
393                 EXPP_dict_set_item_str( d, "executable", Py_BuildValue( "s", bprogname ) );
394                 
395                 if (bpy_orig_syspath_List == NULL) {
396                         /* backup the original sys.path to rebuild later */     
397                         PyObject *syspath = PyDict_GetItemString( d, "path" );  /* borrowed ref */
398                         if (bpy_orig_syspath_List) { /* This should never happen but just incase, be nice */
399                                 Py_DECREF(bpy_orig_syspath_List);
400                         }
401                         bpy_orig_syspath_List = PyList_GetSlice(syspath, 0, PyList_Size(syspath));
402                 }
403                 
404                 Py_DECREF( mod );
405         } else{
406                 printf("import of sys module failed\n");
407         }
408 }
409
410 void BPY_rebuild_syspath( void )
411 {
412         PyObject *mod, *dict, *syspath;
413         char dirpath[FILE_MAX];
414         char *sdir = NULL;
415         PyGILState_STATE gilstate = PyGILState_Ensure();
416
417         mod = PyImport_ImportModule( "sys" );   
418         if (!mod) {
419                 printf("error: could not import python sys module. some modules may not import.\n");
420                 return;
421         }
422         
423         if (!bpy_orig_syspath_List) { /* should never happen */
424                 printf("error refershing python path\n");
425                 Py_DECREF(mod);
426                 return;
427         }
428         
429         dict = PyModule_GetDict( mod ); /* borrowed ref */
430         
431         /* Reset sys.path */    
432         syspath = PyDict_GetItemString( dict, "path" ); /* borrowed ref */
433         PyList_SetSlice(syspath, 0, PyList_Size(syspath), bpy_orig_syspath_List);
434         
435         if(U.pythondir[0] != '\0' ) {
436                 char modpath[FILE_MAX];
437                 int upyslen = strlen(U.pythondir);
438                 BLI_strncpy(dirpath, U.pythondir, FILE_MAX);
439                 
440                 /* check if user pydir ends with a slash and, if so, remove the slash
441                  * (for eventual implementations of c library's stat function that might
442                  * not like it) */
443 #ifdef WIN32
444                 if (upyslen > 3) {
445 #else
446                 if (upyslen > 1) {
447 #endif
448                         if (dirpath[upyslen-1] == '\\' || dirpath[upyslen-1] == '/') {
449                                 dirpath[upyslen-1] = '\0';
450                         }
451                 }
452
453                 BLI_convertstringcode(dirpath, G.sce);
454                 syspath_append(dirpath);        /* append to module search path */
455                 BLI_join_dirfile( modpath, dirpath, "bpymodules" );
456                 if (BLI_exists(modpath)) syspath_append(modpath);
457         }
458         
459         sdir = bpy_gethome(1);
460         if (sdir) {
461                 syspath_append(sdir);
462                 BLI_make_file_string("/", dirpath, sdir, "bpymodules");
463                 if (BLI_exists(dirpath)) syspath_append(dirpath);
464         }
465         
466         Py_DECREF(mod);
467         PyGILState_Release(gilstate);
468 }
469
470 int BPY_path_update( void )
471 {
472         BPyMenu_RemoveAllEntries(); /* free old data */
473         BPY_rebuild_syspath();
474         if (BPyMenu_Init(1) == -1) { /* re-eval scripts registration in menus */
475                 return 0;
476         }
477         return 1;
478 }
479
480 /****************************************************************************
481 * Description: This function finishes Python initialization in Blender.  
482
483 Because U.pythondir (user defined dir for scripts) isn't         
484 initialized when BPY_start_Python needs to be executed, we       
485 postpone adding U.pythondir to sys.path and also BPyMenus         
486 (mechanism to register scripts in Blender menus) for when  
487 that dir info is available.   
488 ****************************************************************************/
489 void BPY_post_start_python( void )
490 {
491         PyGILState_STATE gilstate = PyGILState_Ensure();
492
493         BPY_rebuild_syspath();
494         BPyMenu_Init( 0 );      /* get dynamic menus (registered scripts) data */
495
496         PyGILState_Release(gilstate);
497 }
498
499 /****************************************************************************
500 * Description: This function will return the linenumber on which an error  
501 *               has occurred in the Python script.                      
502 ****************************************************************************/
503 int BPY_Err_getLinenumber( void )
504 {
505         return g_script_error.lineno;
506 }
507
508 /*****************************************************************************/
509 /* Description: This function will return the filename of the python script. */
510 /*****************************************************************************/
511 const char *BPY_Err_getFilename( void )
512 {
513         return g_script_error.filename;
514 }
515
516 /*****************************************************************************/
517 /* Description: Return PyString filename from a traceback object            */
518 /*****************************************************************************/
519 PyObject *traceback_getFilename( PyObject * tb )
520 {
521         PyObject *v = NULL;
522
523 /* co_filename is in f_code, which is in tb_frame, which is in tb */
524
525         v = PyObject_GetAttrString( tb, "tb_frame" );
526         if (v) {
527                 Py_DECREF( v );
528                 v = PyObject_GetAttrString( v, "f_code" );
529                 if (v) {
530                         Py_DECREF( v );
531                         v = PyObject_GetAttrString( v, "co_filename" );
532                 }
533         }
534
535         if (v) return v;
536         else return PyString_FromString("unknown");
537 }
538
539 /****************************************************************************
540 * Description: Blender Python error handler. This catches the error and 
541 * stores filename and line number in a global  
542 *****************************************************************************/
543 void BPY_Err_Handle( char *script_name )
544 {
545         PyObject *exception, *err, *tb, *v;
546
547         if( !script_name ) {
548                 printf( "Error: script has NULL name\n" );
549                 return;
550         }
551
552         PyErr_Fetch( &exception, &err, &tb );
553
554         if (!script_name) script_name = "untitled";
555         //if( !exception && !tb ) {
556         //      printf( "FATAL: spurious exception\n" );
557         //      return;
558         //}
559
560         strcpy( g_script_error.filename, script_name );
561
562         if( exception
563             && PyErr_GivenExceptionMatches( exception, PyExc_SyntaxError ) ) {
564                 /* no traceback available when SyntaxError */
565                 PyErr_Restore( exception, err, tb );    /* takes away reference! */
566                 PyErr_Print(  );
567                 v = PyObject_GetAttrString( err, "lineno" );
568                 if( v ) {
569                         g_script_error.lineno = PyInt_AsLong( v );
570                         Py_DECREF( v );
571                 } else {
572                         g_script_error.lineno = -1;
573                 }
574                 /* this avoids an abort in Python 2.3's garbage collecting: */
575                 PyErr_Clear(  );
576                 return;
577         } else {
578                 PyErr_NormalizeException( &exception, &err, &tb );
579                 PyErr_Restore( exception, err, tb );    /* takes away reference! */
580                 PyErr_Print(  );
581                 tb = PySys_GetObject( "last_traceback" );
582
583                 if( !tb ) {
584                         printf( "\nCan't get traceback\n" );
585                         return;
586                 }
587
588                 Py_INCREF( tb );
589
590 /* From old bpython BPY_main.c:
591  * 'check traceback objects and look for last traceback in the
592  *      same text file. This is used to jump to the line of where the
593  *      error occured. "If the error occured in another text file or module,
594  *      the last frame in the current file is adressed."' 
595  */
596
597                 for(;;) {
598                         v = PyObject_GetAttrString( tb, "tb_next" );
599
600                         if( !v || v == Py_None ||
601                                 strcmp(PyString_AsString(traceback_getFilename(v)), script_name)) {
602                                 break;
603                         }
604
605                         Py_DECREF( tb );
606                         tb = v;
607                 }
608
609                 v = PyObject_GetAttrString( tb, "tb_lineno" );
610                 if (v) {
611                         g_script_error.lineno = PyInt_AsLong(v);
612                         Py_DECREF(v);
613                 }
614                 v = traceback_getFilename( tb );
615                 if (v) {
616                         strncpy( g_script_error.filename, PyString_AsString( v ),
617                                 FILENAME_LENGTH );
618                         Py_DECREF(v);
619                 }
620                 Py_DECREF( tb );
621         }
622
623         return;
624 }
625
626 /****************************************************************************
627 * Description: This function executes the script passed by st.          
628 * Notes:        It is called by blender/src/drawtext.c when a Blender user  
629 *               presses ALT+PKEY in the script's text window. 
630 *****************************************************************************/
631 int BPY_txt_do_python_Text( struct Text *text )
632 {
633         PyObject *py_dict, *py_result;
634         BPy_constant *info;
635         char textname[24];
636         Script *script = G.main->script.first;
637         PyGILState_STATE gilstate;
638
639         if( !text )
640                 return 0;
641
642         /* check if this text is already running */
643         while( script ) {
644                 if( !strcmp( script->id.name + 2, text->id.name + 2 ) ) {
645                         /* if this text is already a running script, 
646                          * just move to it: */
647                         if (!G.background) {
648                                 SpaceScript *sc;
649                                 newspace( curarea, SPACE_SCRIPT );
650                                 sc = curarea->spacedata.first;
651                                 sc->script = script;
652                                 return 1;
653                         }
654                 }
655                 script = script->id.next;
656         }
657
658         /* Create a new script structure and initialize it: */
659         script = alloc_libblock( &G.main->script, ID_SCRIPT, GetName( text ) );
660
661         if( !script ) {
662                 printf( "couldn't allocate memory for Script struct!" );
663                 return 0;
664         }
665
666         /* if in the script Blender.Load(blendfile) is not the last command,
667          * an error after it will call BPY_Err_Handle below, but the text struct
668          * will have been deallocated already, so we need to copy its name here.
669          */
670         BLI_strncpy( textname, GetName( text ),
671                      strlen( GetName( text ) ) + 1 );
672
673         script->id.us = 1;
674         script->flags = SCRIPT_RUNNING;
675         script->py_draw = NULL;
676         script->py_event = NULL;
677         script->py_button = NULL;
678         script->py_browsercallback = NULL;
679         strncpy(script->scriptname, text->id.name+2, sizeof(script->scriptname));
680         gilstate = PyGILState_Ensure();
681
682         py_dict = CreateGlobalDictionary(  );
683
684         if( !setup_armature_weakrefs()){
685                 printf("Oops - weakref dict\n");
686                 PyGILState_Release(gilstate);
687                 return 0;
688         }
689
690         script->py_globaldict = py_dict;
691
692         info = ( BPy_constant * ) PyConstant_New(  );
693         if( info ) {
694                 PyConstant_Insert( info, "name",
695                                  PyString_FromString( script->id.name + 2 ) );
696                 Py_INCREF( Py_None );
697                 PyConstant_Insert( info, "arg", Py_None );
698                 EXPP_dict_set_item_str( py_dict, "__script__",
699                                       ( PyObject * ) info );
700         }
701
702         py_result = RunPython( text, py_dict ); /* Run the script */
703
704         if( !py_result ) {      /* Failed execution of the script */
705
706                 BPY_Err_Handle( textname );
707                 ReleaseGlobalDictionary( py_dict );
708                 script->py_globaldict = NULL;
709                 free_libblock( &G.main->script, script );
710                 PyGILState_Release(gilstate);
711                 return 0;
712         } else {
713                 Py_DECREF( py_result );
714                 script->flags &= ~SCRIPT_RUNNING;
715                 if( !script->flags ) {
716                         ReleaseGlobalDictionary( py_dict );
717                         script->py_globaldict = NULL;
718                         free_libblock( &G.main->script, script );
719                 }
720         }
721
722         PyGILState_Release(gilstate);
723
724         return 1;               /* normal return */
725 }
726
727 /****************************************************************************
728 * Description: Called from command line to run a Python script
729 * automatically. The script can be a file or a Blender Text in the current 
730 * .blend.
731 ****************************************************************************/
732 void BPY_run_python_script( char *fn )
733 {
734         Text *text = NULL;
735         int is_blender_text = 0;
736
737         if (!BLI_exists(fn)) {  /* if there's no such filename ... */
738                 text = G.main->text.first;      /* try an already existing Blender Text */
739
740                 while (text) {
741                         if (!strcmp(fn, text->id.name + 2)) break;
742                         text = text->id.next;
743                 }
744
745                 if (text == NULL) {
746                         printf("\nError: no such file or Blender text -- %s.\n", fn);
747                         return;
748                 }
749                 else is_blender_text = 1;       /* fn is already a Blender Text */
750         }
751
752         else {
753                 text = add_text(fn);
754
755                 if (text == NULL) {
756                         printf("\nError in BPY_run_python_script:\n"
757                                 "couldn't create Blender text from %s\n", fn);
758                 /* Chris: On Windows if I continue I just get a segmentation
759                  * violation.  To get a baseline file I exit here. */
760                 exit(2);
761                 /* return; */
762                 }
763         }
764
765         if (BPY_txt_do_python_Text(text) != 1) {
766                 printf("\nError executing Python script from command-line:\n"
767                         "%s (at line %d).\n", fn, BPY_Err_getLinenumber());
768         }
769
770         if (!is_blender_text) {
771                 /* We can't simply free the text, since the script might have called
772                  * Blender.Load() to load a new .blend, freeing previous data.
773                  * So we check if the pointer is still valid. */
774                 Text *txtptr = G.main->text.first;
775                 while (txtptr) {
776                         if (txtptr == text) {
777                                 free_libblock(&G.main->text, text);
778                                 break;
779                         }
780                         txtptr = txtptr->id.next;
781                 }
782         }
783 }
784
785 int BPY_run_script(Script *script)
786 {
787         PyObject *py_dict, *py_res, *pyarg;
788         Text *text = NULL;
789         BPy_constant *info;
790         int len;
791         
792         FILE *fp = NULL;
793         
794         PyGILState_STATE gilstate = PyGILState_Ensure();
795         
796         if (!BLI_exists(script->scriptname)) {
797                 /* The file dosnt exist, maybe this blend file was made on some other persons computer? */
798                 char fname[FILE_MAX];
799                 char fpath[FILE_MAX];
800                 char ftmp[FILE_MAX];
801                 char *bpyhome = bpy_gethome(1);
802                 
803                 if (bpyhome) {
804                         BLI_strncpy(ftmp, script->scriptname, sizeof(ftmp));
805                         BLI_split_dirfile_basic(ftmp, NULL, fname); /* get the filename only - fname */
806                         BLI_strncpy(fpath, bpy_gethome(1), sizeof(fpath));
807                         BLI_add_slash(fpath);
808                         strcat(fpath, fname);
809                 
810                         if (BLI_exists(fpath)) {
811                                 strncpy(script->scriptname, fpath, sizeof(script->scriptname));
812                         } else {
813                                 bpyhome = NULL; /* a bit dodgy, this is so the line below runs */
814                         }
815                 }
816                 
817                 if (bpyhome == NULL && U.pythondir[0]) {
818                         BLI_make_file_string("/", fpath, U.pythondir, fname);
819                         if (BLI_exists(fpath)) {
820                                 strncpy(script->scriptname, fpath, sizeof(script->scriptname));
821                         }
822                 }
823                 
824                 /* cant find the file?, fallback to text block */
825                 if (!BLI_exists(script->scriptname)) {
826                         for (text=G.main->text.first; text; text=text->id.next) {
827                                 if (strcmp(script->scriptname, text->id.name+2)==0) {
828                                         break;
829                                 }
830                         }
831                 }
832         }
833         if (text) {
834                 Py_INCREF( Py_None );
835                 pyarg = Py_None;
836         } else {
837                 if (BLI_exists(script->scriptname)) {
838                         fp = fopen( script->scriptname, "rb" );
839                 }
840                 
841                 if( !fp ) {
842                         printf( "Error loading script: couldn't open file %s\n", script->scriptname );
843                         free_libblock( &G.main->script, script );
844                         PyGILState_Release(gilstate);
845                         return 0;
846                 }
847                 
848                 if( script->scriptarg[0] == '\0' ) { /* no submenus */
849                         Py_INCREF( Py_None );
850                         pyarg = Py_None;
851                 } else {
852                         pyarg = PyString_FromString( script->scriptarg );
853                 }
854         }
855         
856         script->id.us = 1;
857         script->flags = SCRIPT_RUNNING;
858         script->py_draw = NULL;
859         script->py_event = NULL;
860         script->py_button = NULL;
861         script->py_browsercallback = NULL;
862         
863         py_dict = CreateGlobalDictionary(  );
864
865         script->py_globaldict = py_dict;
866
867         if( !setup_armature_weakrefs()){
868                 printf("Oops - weakref dict\n");
869                 free_libblock( &G.main->script, script );
870                 ReleaseGlobalDictionary( py_dict );
871                 PyGILState_Release(gilstate);
872                 return 0;
873         }
874         
875         info = ( BPy_constant * ) PyConstant_New(  );
876         if( info ) {
877                 PyConstant_Insert( info, "name",
878                                  PyString_FromString( script->id.name + 2 ) );
879                 PyConstant_Insert( info, "arg", pyarg );
880                 EXPP_dict_set_item_str( py_dict, "__script__",
881                                       ( PyObject * ) info );
882         }
883         
884         if (text) {
885                 py_res = RunPython( text, py_dict );
886         } else {
887                 /* Previously we used PyRun_File to run directly the code on a FILE 
888                 * object, but as written in the Python/C API Ref Manual, chapter 2,
889                 * 'FILE structs for different C libraries can be different and 
890                 * incompatible'.
891                 * So now we load the script file data to a buffer */
892                 char *buffer=NULL, *buffer_ofs=NULL, *b_to, *b_from;
893                 
894                 fseek( fp, 0L, SEEK_END );
895                 len = ftell( fp );
896                 fseek( fp, 0L, SEEK_SET );
897         
898                 buffer = buffer_ofs = MEM_mallocN( len + 2, "pyfilebuf" );      /* len+2 to add '\n\0' */
899                 len = fread( buffer, 1, len, fp );
900         
901                 buffer[len] = '\n';     /* fix syntax error in files w/o eol */
902                 buffer[len + 1] = '\0';
903                 
904                 
905                 /* fast clean-up of dos cr/lf line endings, remove convert '\r\n's to '\n' */
906                 if (*buffer_ofs == '\r' && *(buffer_ofs+1) == '\n') {
907                         buffer_ofs++;
908                 }
909                 b_from = b_to = buffer_ofs;
910                 
911                 while(*b_from != '\0') {
912                         if (*b_from == '\r' && *( b_from+1 ) == '\n') {
913                                 b_from++;
914                         }
915                         if (b_from != b_to) {
916                                 *b_to = *b_from;
917                         }
918                         b_to++;
919                         b_from++;
920                 }
921                 *b_to = '\0';
922                 /* done cleaning the string */
923                 
924                 fclose( fp );
925                 
926                 py_res = PyRun_String( buffer_ofs, Py_file_input, py_dict, py_dict );
927                 MEM_freeN( buffer );
928         }
929
930         if( !py_res ) {         /* Failed execution of the script */
931
932                 BPY_Err_Handle( script->id.name + 2 );
933                 ReleaseGlobalDictionary( py_dict );
934                 script->py_globaldict = NULL;
935                 free_libblock( &G.main->script, script );
936                 error_pyscript(  );
937
938                 PyGILState_Release(gilstate);
939                 return 0;
940         } else {
941                 Py_DECREF( py_res );
942                 script->flags &= ~SCRIPT_RUNNING;
943
944                 if( !script->flags ) {
945                         ReleaseGlobalDictionary( py_dict );
946                         script->py_globaldict = NULL;
947                         free_libblock( &G.main->script, script );
948
949                         /* special case: called from the menu in the Scripts window
950                          * we have to change sc->script pointer, since it'll be freed here.*/
951                         if (!G.background) {
952                                 if( curarea->spacetype == SPACE_SCRIPT ) {
953                                         SpaceScript *sc = curarea->spacedata.first;
954                                         sc->script = G.main->script.first;      /* can be null, which is ok ... */
955                                         /* ... meaning no other script is running right now. */
956                                 }
957                         }
958
959                 }
960         }
961         
962         PyGILState_Release(gilstate);
963         return 1;
964 }
965         
966 /****************************************************************************
967 * Description: This function executes the script chosen from a menu.
968 * Notes:        It is called by the ui code in src/header_???.c when a user  
969 *               clicks on a menu entry that refers to a script.
970 *               Scripts are searched in the BPyMenuTable, using the given
971 *               menutype and event values to know which one was chosen. 
972 *****************************************************************************/
973 int BPY_menu_do_python( short menutype, int event )
974 {
975         char *argstr = NULL;
976         BPyMenu *pym;
977         BPySubMenu *pysm;
978         char scriptname[21];
979         Script *script = NULL;
980         int ret, len;
981         PyGILState_STATE gilstate;
982         char filestr[FILE_MAX];
983
984         pym = BPyMenu_GetEntry( menutype, ( short ) event );
985
986         if( !pym )
987                 return 0;
988
989         gilstate = PyGILState_Ensure();
990
991         if( pym->version > G.version )
992                 notice( "Version mismatch: script was written for Blender %d. "
993                         "It may fail with yours: %d.", pym->version,
994                         G.version );
995
996 /* if there are submenus, let the user choose one from a pupmenu that we
997  * create here.*/
998         pysm = pym->submenus;
999         if( pysm ) {
1000                 char *pupstr;
1001                 int arg;
1002
1003                 pupstr = BPyMenu_CreatePupmenuStr( pym, menutype );
1004
1005                 if( pupstr ) {
1006                         arg = pupmenu( pupstr );
1007                         MEM_freeN( pupstr );
1008
1009                         if( arg >= 0 ) {
1010                                 while( arg-- )
1011                                         pysm = pysm->next;
1012                                 argstr = pysm->arg;
1013                         } else {
1014                                 PyGILState_Release(gilstate);
1015                                 return 0;
1016                         }
1017                 }
1018         }
1019
1020         if( pym->dir ) { /* script is in U.pythondir */
1021                 char upythondir[FILE_MAX];
1022
1023                 /* dirs in Blender can be "//", which has a special meaning */
1024                 BLI_strncpy(upythondir, U.pythondir, FILE_MAX);
1025                 BLI_convertstringcode(upythondir, G.sce); /* if so, this expands it */
1026                 BLI_make_file_string( "/", filestr, upythondir, pym->filename );
1027         }
1028         else { /* script is in default scripts dir */
1029                 char *scriptsdir = bpy_gethome(1);
1030
1031                 if (!scriptsdir) {
1032                         printf("Error loading script: can't find default scripts dir!");
1033                         PyGILState_Release(gilstate);
1034                         return 0;
1035                 }
1036
1037                 BLI_make_file_string( "/", filestr, scriptsdir, pym->filename );
1038         }
1039
1040         BLI_strncpy(scriptname, pym->name, 21);
1041         len = strlen(scriptname) - 1;
1042         /* by convention, scripts that open the file browser or have submenus
1043          * display '...'.  Here we remove them from the datablock name */
1044         while ((len > 0) && scriptname[len] == '.') {
1045                 scriptname[len] = '\0';
1046                 len--;
1047         }
1048         
1049         /* Create a new script structure and initialize it: */
1050         script = alloc_libblock( &G.main->script, ID_SCRIPT, scriptname );
1051
1052         if( !script ) {
1053                 printf( "couldn't allocate memory for Script struct!" );
1054                 PyGILState_Release(gilstate);
1055                 return 0;
1056         }
1057
1058         /* let's find a proper area for an eventual script gui:
1059          * (still experimenting here, need definition on which win
1060          * each group will be put to code this properly) */
1061         switch ( menutype ) {
1062
1063         case PYMENU_IMPORT:     /* first 4 were handled in header_info.c */
1064         case PYMENU_EXPORT:
1065         case PYMENU_HELP:
1066         case PYMENU_RENDER:
1067         case PYMENU_WIZARDS:
1068         case PYMENU_SCRIPTTEMPLATE:
1069         case PYMENU_MESHFACEKEY:
1070                 break;
1071
1072         default:
1073                 if( curarea->spacetype != SPACE_SCRIPT ) {
1074                         ScrArea *sa = NULL;
1075
1076                         sa = find_biggest_area_of_type( SPACE_BUTS );
1077                         if( sa ) {
1078                                 if( ( 1.5 * sa->winx ) < sa->winy )
1079                                         sa = NULL;      /* too narrow? */
1080                         }
1081
1082                         if( !sa )
1083                                 sa = find_biggest_area_of_type( SPACE_SCRIPT );
1084                         if( !sa )
1085                                 sa = find_biggest_area_of_type( SPACE_TEXT );
1086                         if( !sa )
1087                                 sa = find_biggest_area_of_type( SPACE_IMAGE );  /* group UV */
1088                         if( !sa )
1089                                 sa = find_biggest_area_of_type( SPACE_VIEW3D );
1090
1091                         if( !sa )
1092                                 sa = find_biggest_area(  );
1093
1094                         areawinset( sa->win );
1095                 }
1096                 break;
1097         }
1098         
1099         strncpy(script->scriptname, filestr, sizeof(script->scriptname));
1100         if (argstr!=NULL && argstr[0] != '\0')
1101                 strncpy(script->scriptarg, argstr, sizeof(script->scriptarg));
1102         
1103         ret = BPY_run_script(script);
1104
1105         return 1;               /* normal return */
1106 }
1107
1108 /*****************************************************************************
1109 * Description:  
1110 * Notes:
1111 *****************************************************************************/
1112 void BPY_free_compiled_text( struct Text *text )
1113 {
1114         if( !text->compiled )
1115                 return;
1116         Py_DECREF( ( PyObject * ) text->compiled );
1117         text->compiled = NULL;
1118
1119         return;
1120 }
1121
1122 /*****************************************************************************
1123 * Description: This function frees a finished (flags == 0) script.
1124 *****************************************************************************/
1125 void BPY_free_finished_script( Script * script )
1126 {
1127         PyGILState_STATE gilstate;
1128
1129         if( !script )
1130                 return;
1131
1132         gilstate = PyGILState_Ensure();
1133
1134         if( PyErr_Occurred(  ) ) {      /* if script ended after filesel */
1135                 PyErr_Print(  );        /* eventual errors are handled now */
1136                 error_pyscript(  );
1137         }
1138
1139         PyGILState_Release(gilstate);
1140
1141         free_libblock( &G.main->script, script );
1142         return;
1143 }
1144
1145 static void unlink_script( Script * script )
1146 {       /* copied from unlink_text in drawtext.c */
1147         bScreen *scr;
1148         ScrArea *area;
1149         SpaceLink *sl;
1150
1151         for( scr = G.main->screen.first; scr; scr = scr->id.next ) {
1152                 for( area = scr->areabase.first; area; area = area->next ) {
1153                         for( sl = area->spacedata.first; sl; sl = sl->next ) {
1154                                 if( sl->spacetype == SPACE_SCRIPT ) {
1155                                         SpaceScript *sc = ( SpaceScript * ) sl;
1156
1157                                         if( sc->script == script ) {
1158                                                 sc->script = NULL;
1159
1160                                                 if( sc ==
1161                                                     area->spacedata.first ) {
1162                                                         scrarea_queue_redraw
1163                                                                 ( area );
1164                                                 }
1165                                         }
1166                                 }
1167                         }
1168                 }
1169         }
1170 }
1171
1172 /* This is called from free_libblock( &G.main->script, script ); */
1173 void BPY_clear_script( Script * script )
1174 {
1175         PyObject *dict;
1176         PyGILState_STATE gilstate;
1177
1178         if( !script )
1179                 return;
1180
1181         gilstate = PyGILState_Ensure();
1182
1183         if (!Py_IsInitialized()) {
1184                 printf("\nError: trying to free script data after finalizing Python!");
1185                 printf("\nScript name: %s\n", script->id.name+2);
1186                 PyGILState_Release(gilstate);
1187                 return;
1188         }
1189
1190         Py_XDECREF( ( PyObject * ) script->py_draw );
1191         Py_XDECREF( ( PyObject * ) script->py_event );
1192         Py_XDECREF( ( PyObject * ) script->py_button );
1193         Py_XDECREF( ( PyObject * ) script->py_browsercallback );
1194         script->py_draw = NULL;
1195         script->py_event = NULL;
1196         script->py_button = NULL;
1197         script->py_browsercallback = NULL;
1198         script->scriptname[0] = '\0';
1199         script->scriptarg[0] = '\0';
1200         
1201         dict = script->py_globaldict;
1202
1203         if( dict ) {
1204                 PyDict_Clear( dict );
1205                 Py_DECREF( dict );      /* Release dictionary. */
1206                 script->py_globaldict = NULL;
1207         }
1208
1209         PyGILState_Release(gilstate);
1210
1211         unlink_script( script );
1212 }
1213
1214 /* PyDrivers */
1215
1216 /* PyDrivers are Ipo Drivers governed by expressions written in Python.
1217  * Expressions here are one-liners that evaluate to a float value. */
1218
1219 /* For faster execution we keep a special dictionary for pydrivers, with
1220  * the needed modules and aliases. */
1221 static int bpy_pydriver_create_dict(void)
1222 {
1223         PyObject *d, *mod;
1224
1225         if (bpy_pydriver_Dict) return -1;
1226
1227         d = PyDict_New();
1228         if (!d) return -1;
1229
1230         bpy_pydriver_Dict = d;
1231
1232         /* import some modules: builtins, Blender, math, Blender.noise */
1233
1234         PyDict_SetItemString(d, "__builtins__", PyEval_GetBuiltins());
1235
1236         mod = PyImport_ImportModule("Blender");
1237         if (mod) {
1238                 PyDict_SetItemString(d, "Blender", mod);
1239                 PyDict_SetItemString(d, "b", mod);
1240                 Py_DECREF(mod);
1241         }
1242
1243         mod = PyImport_ImportModule("math");
1244         if (mod) {
1245                 PyDict_Merge(d, PyModule_GetDict(mod), 0); /* 0 - dont overwrite existing values */
1246                 
1247                 /* Only keep for backwards compat! - just import all math into root, they are standard */
1248                 PyDict_SetItemString(d, "math", mod);
1249                 PyDict_SetItemString(d, "m", mod);
1250                 Py_DECREF(mod);
1251         } 
1252
1253         mod = PyImport_ImportModule("Blender.Noise");
1254         if (mod) {
1255                 PyDict_SetItemString(d, "noise", mod);
1256                 PyDict_SetItemString(d, "n", mod);
1257                 Py_DECREF(mod);
1258         }
1259
1260         /* If there's a Blender text called pydrivers.py, import it.
1261          * Users can add their own functions to this module. */
1262         if (G.f&G_DOSCRIPTLINKS) {
1263                 mod = importText("pydrivers"); /* can also use PyImport_Import() */
1264                 if (mod) {
1265                         PyDict_SetItemString(d, "pydrivers", mod);
1266                         PyDict_SetItemString(d, "p", mod);
1267                         Py_DECREF(mod);
1268                 } else {
1269                         PyErr_Clear();
1270                 }
1271         }
1272         /* short aliases for some Get() functions: */
1273
1274         /* ob(obname) == Blender.Object.Get(obname) */
1275         mod = PyImport_ImportModule("Blender.Object");
1276         if (mod) {
1277                 PyObject *fcn = PyObject_GetAttrString(mod, "Get");
1278                 Py_DECREF(mod);
1279                 if (fcn) {
1280                         PyDict_SetItemString(d, "ob", fcn);
1281                         Py_DECREF(fcn);
1282                 }
1283         }
1284         
1285         /* TODO - change these */
1286         /* me(meshname) == Blender.Mesh.Get(meshname) */
1287         mod = PyImport_ImportModule("Blender.Mesh");
1288         if (mod) {
1289                 PyObject *fcn = PyObject_GetAttrString(mod, "Get");
1290                 Py_DECREF(mod);
1291                 if (fcn) {
1292                         PyDict_SetItemString(d, "me", fcn);
1293                         Py_DECREF(fcn);
1294                 }
1295         }
1296
1297         /* ma(matname) == Blender.Material.Get(matname) */
1298         mod = PyImport_ImportModule("Blender.Material");
1299         if (mod) {
1300                 PyObject *fcn = PyObject_GetAttrString(mod, "Get");
1301                 Py_DECREF(mod);
1302                 if (fcn) {
1303                         PyDict_SetItemString(d, "ma", fcn);
1304                         Py_DECREF(fcn);
1305                 }
1306         }
1307
1308         return 0;
1309 }
1310
1311 /* error return function for BPY_eval_pydriver */
1312 static float pydriver_error(IpoDriver *driver) {
1313
1314         if (bpy_pydriver_oblist)
1315                 bpy_pydriver_freeList();
1316
1317         if (bpy_pydriver_Dict) { /* free the global dict used by pydrivers */
1318                 PyDict_Clear(bpy_pydriver_Dict);
1319                 Py_DECREF(bpy_pydriver_Dict);
1320                 bpy_pydriver_Dict = NULL;
1321         }
1322
1323         driver->flag |= IPO_DRIVER_FLAG_INVALID; /* py expression failed */
1324         
1325         if (driver->ob)
1326                 fprintf(stderr, "\nError in Ipo Driver: Object %s\nThis is the failed Python expression:\n'%s'\n\n", driver->ob->id.name+2, driver->name);
1327         else
1328                 fprintf(stderr, "\nError in Ipo Driver: No Object\nThis is the failed Python expression:\n'%s'\n\n", driver->name);
1329         
1330         PyErr_Print();
1331
1332         return 0.0f;
1333 }
1334
1335
1336 /********PyConstraints*********/
1337
1338 /* This function checks whether a text-buffer is a PyConstraint candidate.
1339  * It uses simple text parsing that could be easily confused!
1340  */
1341 int BPY_is_pyconstraint(Text *text)
1342 {
1343         TextLine *tline = text->lines.first;
1344
1345         if (tline && (tline->len > 10)) {
1346                 char *line = tline->line;
1347                 
1348                 /* Expected format: #BPYCONSTRAINT
1349                  * The actual checks are forgiving, so slight variations also work. */
1350                 if (line && line[0] == '#' && strstr(line, "BPYCONSTRAINT")) return 1;
1351         }
1352         return 0;
1353 }
1354
1355 /* This function frees links from pyconstraints to a given text-buffer.
1356  * Used when a text-buffer is unlinked!
1357  */
1358 void BPY_free_pyconstraint_links(Text *text)
1359 {
1360         Object *ob;
1361         bConstraint *con;
1362         short update;
1363         
1364         /*check all pyconstraints*/
1365         for (ob=G.main->object.first; ob; ob=ob->id.next) {
1366                 update = 0;
1367                 if(ob->type==OB_ARMATURE && ob->pose) {
1368                         bPoseChannel *pchan;
1369                         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1370                                 for (con = pchan->constraints.first; con; con=con->next) {
1371                                         if (con->type==CONSTRAINT_TYPE_PYTHON) {
1372                                                 bPythonConstraint *data = con->data;
1373                                                 if (data->text==text) data->text = NULL;
1374                                                 update = 1;
1375                                                 
1376                                         }
1377                                 }
1378                         }
1379                 }
1380                 for (con = ob->constraints.first; con; con=con->next) {
1381                         if (con->type==CONSTRAINT_TYPE_PYTHON) {
1382                                 bPythonConstraint *data = con->data;
1383                                 if (data->text==text) data->text = NULL;
1384                                 update = 1;
1385                         }
1386                 }
1387                 
1388                 if (update) {
1389                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1390                 }
1391         }
1392 }
1393
1394 /* This function is called to update PyConstraint data so that it is compatible with the script. 
1395  * Some of the allocating/freeing of memory for constraint targets occurs here, espcially
1396  * if the number of targets changes.
1397  */
1398 void BPY_pyconstraint_update(Object *owner, bConstraint *con)
1399 {
1400         bPythonConstraint *data= con->data;
1401         
1402         if (data->text) {       
1403                 /* script does exist. it is assumed that this is a valid pyconstraint script */
1404                 PyObject *globals;
1405                 PyObject *retval, *gval;
1406                 PyGILState_STATE gilstate;
1407                 int num, i;
1408                 
1409                 /* clear the relevant flags first */
1410                 data->flag = 0;
1411
1412                 gilstate = PyGILState_Ensure();
1413
1414                 /* populate globals dictionary */
1415                 globals = CreateGlobalDictionary();
1416                 retval = RunPython(data->text, globals);
1417                 
1418                 if (retval == NULL) {
1419                         BPY_Err_Handle(data->text->id.name);
1420                         ReleaseGlobalDictionary(globals);
1421                         data->flag |= PYCON_SCRIPTERROR;
1422                         PyGILState_Release(gilstate);
1423                         return;
1424                 }
1425                 
1426                 Py_XDECREF(retval);
1427                 retval = NULL;
1428                 
1429                 /* try to find NUM_TARGETS */
1430                 gval = PyDict_GetItemString(globals, "NUM_TARGETS");
1431                 if ( (gval) && (num= PyInt_AsLong(gval)) ) {
1432                         /* NUM_TARGETS is defined... and non-zero */
1433                         bConstraintTarget *ct;
1434                         
1435                         /* check if it is valid (just make sure it is not negative)
1436                          *      TODO: PyInt_AsLong may return -1 as sign of invalid input... 
1437                          */
1438                         num = abs(num);
1439                         data->flag |= PYCON_USETARGETS;
1440                         
1441                         /* check if the number of targets has changed */
1442                         if (num < data->tarnum) {
1443                                 /* free a few targets */
1444                                 num= data->tarnum - num;
1445                                 for (i = 0; i < num; i++, data->tarnum--) {
1446                                         ct= data->targets.last;
1447                                         BLI_freelinkN(&data->targets, ct);
1448                                 }
1449                         }
1450                         else if (num > data->tarnum) {
1451                                 /* add a few targets */
1452                                 num = num - data->tarnum;
1453                                 for (i = 0; i < num; i++, data->tarnum++) {
1454                                         ct= MEM_callocN(sizeof(bConstraintTarget), "PyConTarget");
1455                                         BLI_addtail(&data->targets, ct);
1456                                 }
1457                         }
1458                         
1459                         /* validate targets */
1460                         con->flag &= ~CONSTRAINT_DISABLE;
1461                         for (ct= data->targets.first; ct; ct= ct->next) {
1462                                 if (!exist_object(ct->tar)) {
1463                                         ct->tar = NULL;
1464                                         con->flag |= CONSTRAINT_DISABLE;
1465                                         break;
1466                                 }
1467                                 
1468                                 if ((ct->tar == owner) && (ct->subtarget[0] != 0)) {
1469                                         if (get_named_bone(get_armature(owner), ct->subtarget) == NULL) {
1470                                                 con->flag |= CONSTRAINT_DISABLE;
1471                                                 break;
1472                                         }
1473                                 }
1474                         }
1475                         
1476                         /* clear globals */
1477                         ReleaseGlobalDictionary(globals);
1478
1479                         PyGILState_Release(gilstate);
1480
1481                         return;
1482                 }
1483                 else {
1484                         /* NUM_TARGETS is not defined or equals 0 */
1485                         ReleaseGlobalDictionary(globals);
1486                         
1487                         /* free all targets */
1488                         BLI_freelistN(&data->targets);
1489                         data->tarnum = 0;
1490                         data->flag &= ~PYCON_USETARGETS;
1491                         
1492                         PyGILState_Release(gilstate);
1493
1494                         return;
1495                 }
1496         }
1497         else {
1498                 /* no script, so clear any settings/data now */
1499                 data->tarnum = 0;
1500                 data->flag = 0;
1501                 con->flag &= ~CONSTRAINT_DISABLE;
1502                 
1503                 BLI_freelistN(&data->targets);
1504                 
1505                 /* supposedly this should still leave the base struct... */
1506                 IDP_FreeProperty(data->prop);
1507         }
1508 }
1509
1510 /* PyConstraints Evaluation Function (only called from evaluate_constraint)
1511  * This function is responsible for modifying the ownermat that it is passed. 
1512  */
1513 void BPY_pyconstraint_eval(bPythonConstraint *con, bConstraintOb *cob, ListBase *targets)
1514 {
1515         PyObject *srcmat, *tarmat, *tarmats, *idprop;
1516         PyObject *globals;
1517         PyObject *gval;
1518         PyObject *pyargs, *retval;
1519         bConstraintTarget *ct;
1520         MatrixObject *retmat;
1521         int row, col, index;
1522         PyGILState_STATE gilstate;
1523
1524         if (!con->text) return;
1525         if (con->flag & PYCON_SCRIPTERROR) return;
1526
1527         gilstate = PyGILState_Ensure();
1528
1529         globals = CreateGlobalDictionary();
1530         
1531         /* wrap blender-data as PyObjects for evaluation 
1532          *      - we expose the owner's matrix as pymatrix
1533          *      - id-properties are wrapped using the id-properties pyapi
1534          *      - targets are presented as a list of matrices
1535          */
1536         srcmat = newMatrixObject((float *)cob->matrix, 4, 4, Py_NEW);
1537         idprop = BPy_Wrap_IDProperty(NULL, con->prop, NULL);
1538         
1539         tarmats= PyList_New(con->tarnum); 
1540         for (ct=targets->first, index=0; ct; ct=ct->next, index++) {
1541                 tarmat = newMatrixObject((float *)ct->matrix, 4, 4, Py_NEW);
1542                 PyList_SET_ITEM(tarmats, index, tarmat);
1543         }
1544         
1545         if (!setup_armature_weakrefs()) {
1546                 fprintf(stderr, "Oops - weakref dict setup\n");
1547                 PyGILState_Release(gilstate);
1548
1549                 return;
1550         }
1551         
1552         retval = RunPython(con->text, globals);
1553         
1554         if (retval == NULL) {
1555                 BPY_Err_Handle(con->text->id.name);
1556                 con->flag |= PYCON_SCRIPTERROR;
1557                 
1558                 /* free temp objects */
1559                 Py_XDECREF(idprop);
1560                 Py_XDECREF(srcmat);
1561                 Py_XDECREF(tarmats);
1562                 
1563                 ReleaseGlobalDictionary(globals);
1564
1565                 PyGILState_Release(gilstate);
1566
1567                 return;
1568         }
1569
1570         if (retval) {Py_XDECREF( retval );}
1571         retval = NULL;
1572         
1573         gval = PyDict_GetItemString(globals, "doConstraint");
1574         if (!gval) {
1575                 printf("ERROR: no doConstraint function in constraint!\n");
1576                 
1577                 /* free temp objects */
1578                 Py_XDECREF(idprop);
1579                 Py_XDECREF(srcmat);
1580                 Py_XDECREF(tarmats);
1581                 
1582                 ReleaseGlobalDictionary(globals);
1583
1584                 PyGILState_Release(gilstate);
1585
1586                 return;
1587         }
1588         
1589         /* Now for the fun part! Try and find the functions we need. */
1590         if (PyFunction_Check(gval)) {
1591                 pyargs = Py_BuildValue("OOO", srcmat, tarmats, idprop);
1592                 retval = PyObject_CallObject(gval, pyargs);
1593                 Py_XDECREF(pyargs);
1594         } 
1595         else {
1596                 printf("ERROR: doConstraint is supposed to be a function!\n");
1597                 con->flag |= PYCON_SCRIPTERROR;
1598                 
1599                 Py_XDECREF(idprop);
1600                 Py_XDECREF(srcmat);
1601                 Py_XDECREF(tarmats);
1602                 
1603                 ReleaseGlobalDictionary(globals);
1604
1605                 PyGILState_Release(gilstate);
1606
1607                 return;
1608         }
1609         
1610         if (!retval) {
1611                 BPY_Err_Handle(con->text->id.name);
1612                 con->flag |= PYCON_SCRIPTERROR;
1613                 
1614                 /* free temp objects */
1615                 Py_XDECREF(idprop);
1616                 Py_XDECREF(srcmat);
1617                 Py_XDECREF(tarmats);
1618                 
1619                 ReleaseGlobalDictionary(globals);
1620
1621                 PyGILState_Release(gilstate);
1622
1623                 return;
1624         }
1625         
1626         
1627         if (!PyObject_TypeCheck(retval, &matrix_Type)) {
1628                 printf("Error in PyConstraint - doConstraint: Function not returning a matrix!\n");
1629                 con->flag |= PYCON_SCRIPTERROR;
1630                 
1631                 Py_XDECREF(idprop);
1632                 Py_XDECREF(srcmat);
1633                 Py_XDECREF(tarmats);
1634                 Py_XDECREF(retval);
1635                 
1636                 ReleaseGlobalDictionary(globals);
1637
1638                 PyGILState_Release(gilstate);
1639
1640                 return;
1641         }
1642         
1643         retmat = (MatrixObject *)retval;
1644         if (retmat->rowSize != 4 || retmat->colSize != 4) {
1645                 printf("Error in PyConstraint - doConstraint: Matrix returned is the wrong size!\n");
1646                 con->flag |= PYCON_SCRIPTERROR;
1647                 
1648                 Py_XDECREF(idprop);
1649                 Py_XDECREF(srcmat);
1650                 Py_XDECREF(tarmats);
1651                 Py_XDECREF(retval);
1652                 
1653                 ReleaseGlobalDictionary(globals);
1654
1655                 PyGILState_Release(gilstate);
1656
1657                 return;
1658         }       
1659
1660         /* this is the reverse of code taken from newMatrix() */
1661         for(row = 0; row < 4; row++) {
1662                 for(col = 0; col < 4; col++) {
1663                         cob->matrix[row][col] = retmat->contigPtr[row*4+col];
1664                 }
1665         }
1666         
1667         /* free temp objects */
1668         Py_XDECREF(idprop);
1669         Py_XDECREF(srcmat);
1670         Py_XDECREF(tarmats);
1671         Py_XDECREF(retval);
1672         
1673         /* clear globals */
1674         ReleaseGlobalDictionary(globals);
1675
1676         PyGILState_Release(gilstate);
1677 }
1678
1679 /* This evaluates the target matrix for each target the PyConstraint uses.
1680  * NOTE: it only does one target at a time!
1681  */
1682 void BPY_pyconstraint_target(bPythonConstraint *con, bConstraintTarget *ct)
1683 {
1684         PyObject *tar, *subtar;
1685         PyObject *tarmat, *idprop;
1686         PyObject *globals;
1687         PyObject *gval;
1688         PyObject *pyargs, *retval;
1689         MatrixObject *retmat;
1690         int row, col;
1691         PyGILState_STATE gilstate;
1692
1693         if (!con->text) return;
1694         if (con->flag & PYCON_SCRIPTERROR) return;
1695         if (!ct) return;
1696         
1697         gilstate = PyGILState_Ensure();
1698
1699         globals = CreateGlobalDictionary();
1700         
1701         tar = Object_CreatePyObject(ct->tar);
1702         if ((ct->tar) && (ct->tar->type==OB_ARMATURE)) {
1703                 bPoseChannel *pchan;
1704                 pchan = get_pose_channel(ct->tar->pose, ct->subtarget);
1705                 subtar = PyPoseBone_FromPosechannel(pchan);
1706         }
1707         else
1708                 subtar = PyString_FromString(ct->subtarget);
1709         
1710         tarmat = newMatrixObject((float *)ct->matrix, 4, 4, Py_NEW);
1711         idprop = BPy_Wrap_IDProperty( NULL, con->prop, NULL);
1712         
1713         if (!setup_armature_weakrefs()) {
1714                 fprintf(stderr, "Oops - weakref dict setup\n");
1715                 PyGILState_Release(gilstate);
1716                 return;
1717         }
1718         
1719         retval = RunPython(con->text, globals);
1720
1721         if (retval == NULL) {
1722                 BPY_Err_Handle(con->text->id.name);
1723                 con->flag |= PYCON_SCRIPTERROR;
1724                 
1725                 /* free temp objects */
1726                 Py_XDECREF(tar);
1727                 Py_XDECREF(subtar);
1728                 Py_XDECREF(idprop);
1729                 Py_XDECREF(tarmat);
1730                 
1731                 ReleaseGlobalDictionary(globals);
1732
1733                 PyGILState_Release(gilstate);
1734
1735                 return;
1736         }
1737
1738         Py_XDECREF(retval);
1739         retval = NULL;
1740         
1741         /* try to find doTarget function to set the target matrix */
1742         gval = PyDict_GetItemString(globals, "doTarget");
1743         if (!gval) {
1744                 /* free temp objects */
1745                 Py_XDECREF(tar);
1746                 Py_XDECREF(subtar);
1747                 Py_XDECREF(idprop);
1748                 Py_XDECREF(tarmat);
1749                 
1750                 ReleaseGlobalDictionary(globals);
1751
1752                 PyGILState_Release(gilstate);
1753
1754                 return;
1755         }
1756         
1757         /* Now for the fun part! Try and find the functions we need.*/
1758         if (PyFunction_Check(gval)) {
1759                 pyargs = Py_BuildValue("OOOO", tar, subtar, tarmat, idprop);
1760                 retval = PyObject_CallObject(gval, pyargs);
1761                 Py_XDECREF(pyargs);
1762         } 
1763         else {
1764                 printf("ERROR: doTarget is supposed to be a function!\n");
1765                 con->flag |= PYCON_SCRIPTERROR;
1766                 
1767                 Py_XDECREF(tar);
1768                 Py_XDECREF(subtar);
1769                 Py_XDECREF(idprop);
1770                 Py_XDECREF(tarmat);
1771                 
1772                 ReleaseGlobalDictionary(globals);
1773
1774                 PyGILState_Release(gilstate);
1775
1776                 return;
1777         }
1778         
1779         if (!retval) {
1780                 BPY_Err_Handle(con->text->id.name);
1781                 con->flag |= PYCON_SCRIPTERROR;
1782                 
1783                 
1784                 /* free temp objects */
1785                 Py_XDECREF(tar);
1786                 Py_XDECREF(subtar);
1787                 Py_XDECREF(idprop);
1788                 Py_XDECREF(tarmat);
1789                 
1790                 ReleaseGlobalDictionary(globals);
1791
1792                 PyGILState_Release(gilstate);
1793
1794                 return;
1795         }
1796         
1797         if (!PyObject_TypeCheck(retval, &matrix_Type)) {
1798                 con->flag |= PYCON_SCRIPTERROR;
1799                 
1800                 Py_XDECREF(tar);
1801                 Py_XDECREF(subtar);
1802                 Py_XDECREF(idprop);
1803                 Py_XDECREF(tarmat);
1804                 Py_XDECREF(retval);
1805                 
1806                 ReleaseGlobalDictionary(globals);
1807
1808                 PyGILState_Release(gilstate);
1809
1810                 return;
1811         }
1812         
1813         retmat = (MatrixObject *)retval;
1814         if (retmat->rowSize != 4 || retmat->colSize != 4) {
1815                 printf("Error in PyConstraint - doTarget: Matrix returned is the wrong size!\n");
1816                 con->flag |= PYCON_SCRIPTERROR;
1817                 
1818                 Py_XDECREF(tar);
1819                 Py_XDECREF(subtar);
1820                 Py_XDECREF(idprop);
1821                 Py_XDECREF(tarmat);
1822                 Py_XDECREF(retval);
1823                 
1824                 ReleaseGlobalDictionary(globals);
1825
1826                 PyGILState_Release(gilstate);
1827
1828                 return;
1829         }       
1830
1831         /* this is the reverse of code taken from newMatrix() */
1832         for(row = 0; row < 4; row++) {
1833                 for(col = 0; col < 4; col++) {
1834                         ct->matrix[row][col] = retmat->contigPtr[row*4+col];
1835                 }
1836         }
1837         
1838         /* free temp objects */
1839         Py_XDECREF(tar);
1840         Py_XDECREF(subtar);
1841         Py_XDECREF(idprop);
1842         Py_XDECREF(tarmat);
1843         Py_XDECREF(retval);
1844         
1845         /* clear globals */
1846         ReleaseGlobalDictionary(globals);
1847
1848         PyGILState_Release(gilstate);
1849 }
1850
1851 /* This draws+handles the user-defined interface for editing pyconstraints idprops */
1852 void BPY_pyconstraint_settings(void *arg1, void *arg2)
1853 {
1854         bPythonConstraint *con= (bPythonConstraint *)arg1;
1855         PyObject *idprop;
1856         PyObject *globals;
1857         PyObject *gval;
1858         PyObject *retval;
1859         PyGILState_STATE gilstate;
1860         
1861         if (!con->text) return;
1862         if (con->flag & PYCON_SCRIPTERROR) return;
1863         
1864         gilstate = PyGILState_Ensure();
1865         
1866         globals = CreateGlobalDictionary();
1867         
1868         idprop = BPy_Wrap_IDProperty( NULL, con->prop, NULL);
1869         
1870         retval = RunPython(con->text, globals);
1871
1872         if (retval == NULL) {
1873                 BPY_Err_Handle(con->text->id.name);
1874                 ReleaseGlobalDictionary(globals);
1875                 con->flag |= PYCON_SCRIPTERROR;
1876                 
1877                 /* free temp objects */
1878                 Py_XDECREF(idprop);
1879                 
1880                 PyGILState_Release(gilstate);
1881                 
1882                 return;
1883         }
1884
1885         if (retval) {Py_XDECREF( retval );}
1886         retval = NULL;
1887         
1888         gval = PyDict_GetItemString(globals, "getSettings");
1889         if (!gval) {
1890                 printf("ERROR: no getSettings function in constraint!\n");
1891                 
1892                 /* free temp objects */
1893                 ReleaseGlobalDictionary( globals );
1894                 Py_XDECREF(idprop);
1895
1896                 PyGILState_Release(gilstate);
1897
1898                 return;
1899         }
1900         
1901         /* Now for the fun part! Try and find the functions we need. */
1902         if (PyFunction_Check(gval)) {
1903                 retval = PyObject_CallFunction(gval, "O", idprop);
1904         } 
1905         else {
1906                 printf("ERROR: getSettings is supposed to be a function!\n");
1907                 ReleaseGlobalDictionary( globals );
1908                 
1909                 Py_XDECREF(idprop);
1910                 
1911                 PyGILState_Release(gilstate);
1912                 
1913                 return;
1914         }
1915         
1916         if (!retval) {
1917                 BPY_Err_Handle(con->text->id.name);
1918                 con->flag |= PYCON_SCRIPTERROR;
1919                 
1920                 /* free temp objects */
1921                 ReleaseGlobalDictionary(globals);
1922                 Py_XDECREF(idprop);
1923                 
1924                 PyGILState_Release(gilstate);
1925                 
1926                 return;
1927         }
1928         else {
1929                 /* clear globals */
1930                 ReleaseGlobalDictionary(globals);
1931                 
1932                 /* free temp objects */
1933                 Py_XDECREF(idprop);
1934                 Py_DECREF(retval);
1935                 
1936                 PyGILState_Release(gilstate);
1937                 
1938                 return;
1939         }
1940 }
1941
1942 /* Update function, it gets rid of pydrivers global dictionary, forcing
1943  * BPY_pydriver_eval to recreate it. This function is used to force
1944  * reloading the Blender text module "pydrivers.py", if available, so
1945  * updates in it reach pydriver evaluation. */
1946 void BPY_pydriver_update(void)
1947 {
1948         PyGILState_STATE gilstate = PyGILState_Ensure();
1949
1950         if (bpy_pydriver_Dict) { /* free the global dict used by pydrivers */
1951                 PyDict_Clear(bpy_pydriver_Dict);
1952                 Py_DECREF(bpy_pydriver_Dict);
1953                 bpy_pydriver_Dict = NULL;
1954         }
1955
1956         PyGILState_Release(gilstate);
1957
1958         return;
1959 }
1960
1961 /* for depsgraph.c, runs py expr once to collect all refs. made
1962  * to objects (self refs. to the object that owns the py driver
1963  * are not allowed). */
1964 struct Object **BPY_pydriver_get_objects(IpoDriver *driver)
1965 {
1966         /*if (!driver || !driver->ob || driver->name[0] == '\0')
1967                 return NULL;*/
1968
1969         /*PyErr_Clear();*/
1970
1971         /* clear the flag that marks invalid python expressions */
1972         driver->flag &= ~IPO_DRIVER_FLAG_INVALID;
1973
1974         /* tell we're running a pydriver, so Get() functions know they need
1975          * to add the requested obj to our list */
1976         bpy_pydriver_running(1);
1977
1978         /* append driver owner object as the 1st ob in the list;
1979          * we put it there to make sure it is not itself referenced in
1980          * its pydriver expression */
1981         bpy_pydriver_appendToList(driver->ob);
1982
1983         /* this will append any other ob referenced in expr (driver->name)
1984          * or set the driver's error flag if driver's py expression fails */
1985         BPY_pydriver_eval(driver);
1986
1987         bpy_pydriver_running(0); /* ok, we're done */
1988
1989         return bpy_pydriver_obArrayFromList(); /* NULL if eval failed */
1990 }
1991
1992 /* This evals py driver expressions, 'expr' is a Python expression that
1993  * should evaluate to a float number, which is returned. */
1994 float BPY_pydriver_eval(IpoDriver *driver)
1995 {
1996         char *expr = NULL;
1997         PyObject *retval, *bpy_ob = NULL;
1998         float result = 0.0f; /* default return */
1999         int setitem_retval;
2000         PyGILState_STATE gilstate;
2001
2002         if (!driver ||  (G.f&G_DOSCRIPTLINKS)==0) return result;
2003
2004         expr = driver->name; /* the py expression to be evaluated */
2005         if (!expr || expr[0]=='\0') return result;
2006
2007         gilstate = PyGILState_Ensure();
2008
2009         if (!bpy_pydriver_Dict) {
2010                 if (bpy_pydriver_create_dict() != 0) {
2011                         fprintf(stderr, "Pydriver error: couldn't create Python dictionary");
2012                         PyGILState_Release(gilstate);
2013                         return result;
2014                 }
2015         }
2016
2017         if (driver->ob)
2018                 bpy_ob = Object_CreatePyObject(driver->ob);
2019
2020         if (!bpy_ob) {
2021                 Py_INCREF(Py_None);
2022                 bpy_ob = Py_None;
2023         }
2024
2025         setitem_retval = EXPP_dict_set_item_str(bpy_pydriver_Dict, "self", bpy_ob);
2026
2027         if( !setup_armature_weakrefs()){
2028                 fprintf( stderr, "Oops - weakref dict setup\n");
2029                 PyGILState_Release(gilstate);
2030                 return result;
2031         }
2032
2033         retval = PyRun_String(expr, Py_eval_input, bpy_pydriver_Dict,
2034                 bpy_pydriver_Dict);
2035
2036         if (retval == NULL) {
2037                 result = pydriver_error(driver);
2038                 PyGILState_Release(gilstate);
2039                 return result;
2040         }
2041
2042         result = ( float )PyFloat_AsDouble( retval );
2043         Py_DECREF(retval);
2044         
2045         if (result == -1 && PyErr_Occurred()) {
2046                 result = pydriver_error(driver);
2047                 PyGILState_Release(gilstate);
2048                 return result;
2049         }
2050
2051         /* remove 'self', since this dict is also used by py buttons */
2052         if (setitem_retval == 0) PyDict_DelItemString(bpy_pydriver_Dict, "self");
2053
2054         /* all fine, make sure the "invalid expression" flag is cleared */
2055         driver->flag &= ~IPO_DRIVER_FLAG_INVALID;
2056
2057         PyGILState_Release(gilstate);
2058
2059         return result;
2060 }
2061
2062 /* Button Python Evaluation */
2063
2064 /* Python evaluation for gui buttons:
2065  *      users can write any valid Python expression (that evals to an int or float)
2066  *      inside Blender's gui number buttons and have them evaluated to their
2067  *      actual int or float value.
2068  *
2069  *      The global dict used for pydrivers is also used here, so all imported
2070  *      modules for pydrivers (including the pydrivers.py Blender text) are
2071  *      available for button py eval, too. */
2072
2073 static int bpy_button_eval_error(char *expr) {
2074
2075         if (bpy_pydriver_oblist)
2076                 bpy_pydriver_freeList();
2077
2078         if (bpy_pydriver_Dict) { /* free the persistent global dict */
2079                 /* it's the same dict used by pydrivers */
2080                 PyDict_Clear(bpy_pydriver_Dict);
2081                 Py_DECREF(bpy_pydriver_Dict);
2082                 bpy_pydriver_Dict = NULL;
2083         }
2084
2085         fprintf(stderr, "\nError in button evaluation:\nThis is the failed Python expression:\n'%s'\n\n", expr);
2086
2087         PyErr_Print();
2088
2089         return -1;
2090 }
2091
2092 int BPY_button_eval(char *expr, double *value)
2093 {
2094         PyObject *retval, *floatval;
2095         PyGILState_STATE gilstate;
2096         int ret;
2097
2098         if (!value || !expr || expr[0]=='\0') return -1;
2099
2100         *value = 0.0; /* default value */
2101
2102         gilstate = PyGILState_Ensure();
2103
2104         if (!bpy_pydriver_Dict) {
2105                 if (bpy_pydriver_create_dict() != 0) {
2106                         fprintf(stderr,
2107                                 "Button Python Eval error: couldn't create Python dictionary \n");
2108                         PyGILState_Release(gilstate);
2109                         return -1;
2110                 }
2111         }
2112
2113
2114         if( !setup_armature_weakrefs()){
2115                 fprintf(stderr, "Oops - weakref dict\n");
2116                 PyGILState_Release(gilstate);
2117                 return -1;
2118         }
2119
2120         retval = PyRun_String(expr, Py_eval_input, bpy_pydriver_Dict,
2121                 bpy_pydriver_Dict);
2122
2123         if (retval == NULL) {
2124                 ret = bpy_button_eval_error(expr);
2125                 PyGILState_Release(gilstate);
2126                 return ret;
2127         }
2128         else {
2129                 floatval = PyNumber_Float(retval);
2130                 Py_DECREF(retval);
2131         }
2132
2133         if (floatval == NULL) {
2134                 ret = bpy_button_eval_error(expr);
2135                 PyGILState_Release(gilstate);
2136                 return ret;
2137         } else {
2138                 *value = (float)PyFloat_AsDouble(floatval);
2139                 Py_DECREF(floatval);
2140         }
2141
2142         PyGILState_Release(gilstate);
2143
2144         return 0; /* successful exit */
2145 }
2146
2147
2148 /*****************************************************************************/
2149 /* ScriptLinks                                                        */
2150 /*****************************************************************************/
2151
2152 /*****************************************************************************/
2153 /* Description:                                                          */
2154 /* Notes:                               Not implemented yet      */
2155 /*****************************************************************************/
2156 void BPY_clear_bad_scriptlinks( struct Text *byebye )
2157 {
2158 /*
2159         BPY_clear_bad_scriptlist(getObjectList(), byebye);
2160         BPY_clear_bad_scriptlist(getLampList(), byebye);
2161         BPY_clear_bad_scriptlist(getCameraList(), byebye);
2162         BPY_clear_bad_scriptlist(getMaterialList(), byebye);
2163         BPY_clear_bad_scriptlist(getWorldList(),        byebye);
2164         BPY_clear_bad_scriptlink(&scene_getCurrent()->id, byebye);
2165
2166         allqueue(REDRAWBUTSSCRIPT, 0);
2167 */
2168         return;
2169 }
2170
2171 /*****************************************************************************
2172 * Description: Loop through all scripts of a list of object types, and 
2173 *       execute these scripts.  
2174 *       For the scene, only the current active scene the scripts are 
2175 *       executed (if any).
2176 *****************************************************************************/
2177 void BPY_do_all_scripts( short event )
2178 {
2179         DoAllScriptsFromList( &( G.main->object ), event );
2180         DoAllScriptsFromList( &( G.main->lamp ), event );
2181         DoAllScriptsFromList( &( G.main->camera ), event );
2182         DoAllScriptsFromList( &( G.main->mat ), event );
2183         DoAllScriptsFromList( &( G.main->world ), event );
2184
2185         BPY_do_pyscript( &( G.scene->id ), event );
2186
2187         return;
2188 }
2189
2190 /*****************************************************************************
2191 * Description: Execute a Python script when an event occurs. The following  
2192 *               events are possible: frame changed, load script and redraw.  
2193 *               Only events happening to one of the following object types   
2194 *               are handled: Object, Lamp, Camera, Material, World and      
2195 *               Scene.                  
2196 *****************************************************************************/
2197
2198 static ScriptLink *ID_getScriptlink( ID * id )
2199 {
2200         switch ( MAKE_ID2( id->name[0], id->name[1] ) ) {
2201         case ID_OB:
2202                 return &( ( Object * ) id )->scriptlink;
2203         case ID_LA:
2204                 return &( ( Lamp * ) id )->scriptlink;
2205         case ID_CA:
2206                 return &( ( Camera * ) id )->scriptlink;
2207         case ID_MA:
2208                 return &( ( Material * ) id )->scriptlink;
2209         case ID_WO:
2210                 return &( ( World * ) id )->scriptlink;
2211         case ID_SCE:
2212                 return &( ( Scene * ) id )->scriptlink;
2213         default:
2214                 return NULL;
2215         }
2216 }
2217
2218 int BPY_has_onload_script( void )
2219 {
2220         ScriptLink *slink = &G.scene->scriptlink;
2221         int i;
2222
2223         if( !slink || !slink->totscript )
2224                 return 0;
2225
2226         for( i = 0; i < slink->totscript; i++ ) {
2227                 if( ( slink->flag[i] == SCRIPT_ONLOAD )
2228                     && ( slink->scripts[i] != NULL ) )
2229                         return 1;
2230         }
2231
2232         return 0;
2233 }
2234
2235 void BPY_do_pyscript( ID * id, short event )
2236 {
2237         ScriptLink *scriptlink;
2238
2239         if( !id ) return;
2240
2241         scriptlink = ID_getScriptlink( id );
2242
2243         if( scriptlink && scriptlink->totscript ) {
2244                 PyObject *value;
2245                 PyObject *dict;
2246                 PyObject *ret;
2247                 int index, during_slink = during_scriptlink(  );
2248                 PyGILState_STATE gilstate;
2249
2250                 /* invalid scriptlinks (new .blend was just loaded), return */
2251                 if( during_slink < 0 )
2252                         return;
2253
2254                 gilstate = PyGILState_Ensure();
2255
2256                 if( !setup_armature_weakrefs()){
2257                         printf("Oops - weakref dict, this is a bug\n");
2258                         PyGILState_Release(gilstate);
2259                         return;
2260                 }
2261                 
2262                 value = GetPyObjectFromID( id );
2263                 if( !value){
2264                         printf("Oops - could not get a valid python object for Blender.link, this is a bug\n");
2265                         PyGILState_Release(gilstate);
2266                         return;
2267                 }
2268                 
2269                 /* tell we're running a scriptlink.  The sum also tells if this script
2270                  * is running nested inside another.  Blender.Load needs this info to
2271                  * avoid trouble with invalid slink pointers. */
2272                 during_slink++;
2273                 disable_where_scriptlink( (short)during_slink );
2274
2275                 /* set globals in Blender module to identify scriptlink */
2276                 PyDict_SetItemString(   g_blenderdict, "bylink", Py_True);
2277                 EXPP_dict_set_item_str( g_blenderdict, "link", value );
2278                 EXPP_dict_set_item_str( g_blenderdict, "event",
2279                                       PyString_FromString( event_to_name
2280                                                            ( event ) ) );
2281                 if (event == SCRIPT_POSTRENDER) event = SCRIPT_RENDER;
2282
2283                 for( index = 0; index < scriptlink->totscript; index++ ) {
2284                         if( ( scriptlink->flag[index] == event ) &&
2285                             ( scriptlink->scripts[index] != NULL ) ) {
2286                                 dict = CreateGlobalDictionary(  );
2287                                 ret = RunPython( ( Text * ) scriptlink->
2288                                                  scripts[index], dict );
2289                                 ReleaseGlobalDictionary( dict );
2290
2291                                 if( !ret ) {
2292                                         /* Failed execution of the script */
2293                                         BPY_Err_Handle( scriptlink->
2294                                                         scripts[index]->name +
2295                                                         2 );
2296                                         //BPY_end_python ();
2297                                         //BPY_start_python ();
2298                                 } else {
2299                                         Py_DECREF( ret );
2300                                 }
2301                                 /* If a scriptlink has just loaded a new .blend file, the
2302                                  * scriptlink pointer became invalid (see api2_2x/Blender.c),
2303                                  * so we stop here. */
2304                                 if( during_scriptlink(  ) == -1 ) {
2305                                         during_slink = 1;
2306                                         break;
2307                                 }
2308                         }
2309                 }
2310
2311                 disable_where_scriptlink( (short)(during_slink - 1) );
2312
2313                 /* cleanup bylink flag and clear link so PyObject
2314                  * can be released 
2315                  */
2316                 PyDict_SetItemString(g_blenderdict, "bylink", Py_False);
2317                 PyDict_SetItemString( g_blenderdict, "link", Py_None );
2318                 EXPP_dict_set_item_str( g_blenderdict, "event", PyString_FromString( "" ) );
2319
2320                 PyGILState_Release(gilstate);
2321         }
2322 }
2323
2324
2325 /* SPACE HANDLERS */
2326
2327 /* These are special script links that can be assigned to ScrArea's to
2328  * (EVENT type) receive events sent to a given space (and use or ignore them) or
2329  * (DRAW type) be called after the space is drawn, to draw anything on top of
2330  * the space area. */
2331
2332 /* How to add space handlers to other spaces:
2333  * - add the space event defines to DNA_scriptlink_types.h, as done for
2334  *   3d view: SPACEHANDLER_VIEW3D_EVENT, for example;
2335  * - add the new defines to Blender.SpaceHandler dictionary in Blender.c;
2336  * - check space.c for how to call the event handlers;
2337  * - check drawview.c for how to call the draw handlers;
2338  * - check header_view3d.c for how to add the "Space Handler Scripts" menu.
2339  * Note: DRAW handlers should be called with 'event = 0', chech drawview.c */
2340
2341 int BPY_has_spacehandler(Text *text, ScrArea *sa)
2342 {
2343         ScriptLink *slink;
2344         int index;
2345
2346         if (!sa || !text) return 0;
2347
2348         slink = &sa->scriptlink;
2349
2350         for (index = 0; index < slink->totscript; index++) {
2351                 if (slink->scripts[index] && (slink->scripts[index] == (ID *)text))
2352                         return 1;
2353         }
2354
2355         return 0;       
2356 }
2357
2358 int BPY_is_spacehandler(Text *text, char spacetype)
2359 {
2360         TextLine *tline = text->lines.first;
2361         unsigned short type = 0;
2362
2363         if (tline && (tline->len > 10)) {
2364                 char *line = tline->line;
2365
2366                 /* Expected format: # SPACEHANDLER.SPACE.TYPE
2367                  * Ex: # SPACEHANDLER.VIEW3D.DRAW
2368                  * The actual checks are forgiving, so slight variations also work. */
2369                 if (line && line[0] == '#' && strstr(line, "HANDLER")) {
2370                         line++; /* skip '#' */
2371
2372                         /* only done for 3D View right now, trivial to add for others: */
2373                         switch (spacetype) {
2374                                 case SPACE_VIEW3D:
2375                                         if (strstr(line, "3D")) { /* VIEW3D, 3DVIEW */
2376                                                 if (strstr(line, "DRAW")) type = SPACEHANDLER_VIEW3D_DRAW;
2377                                                 else if (strstr(line, "EVENT")) type = SPACEHANDLER_VIEW3D_EVENT;
2378                                         }
2379                                         break;
2380                         }
2381                 }
2382         }
2383         return type; /* 0 if not a space handler */
2384 }
2385
2386 int BPY_del_spacehandler(Text *text, ScrArea *sa)
2387 {
2388         ScriptLink *slink;
2389         int i, j;
2390
2391         if (!sa || !text) return -1;
2392
2393         slink = &sa->scriptlink;
2394         if (slink->totscript < 1) return -1;
2395
2396         for (i = 0; i < slink->totscript; i++) {
2397                 if (text == (Text *)slink->scripts[i]) {
2398
2399                         for (j = i; j < slink->totscript - 1; j++) {
2400                                 slink->flag[j] = slink->flag[j+1];
2401                                 slink->scripts[j] = slink->scripts[j+1];
2402                         }
2403                         slink->totscript--;
2404                         /* like done in buttons_script.c we just free memory
2405                          * if all slinks have been removed -- less fragmentation,
2406                          * these should be quite small arrays */
2407                         if (slink->totscript == 0) {
2408                                 if (slink->scripts) MEM_freeN(slink->scripts);
2409                                 if (slink->flag) MEM_freeN(slink->flag);
2410                                 break;
2411                         }
2412                 }
2413         }
2414         return 0;
2415 }
2416
2417 int BPY_add_spacehandler(Text *text, ScrArea *sa, char spacetype)
2418 {
2419         unsigned short handlertype;
2420
2421         if (!sa || !text) return -1;
2422
2423         handlertype = (unsigned short)BPY_is_spacehandler(text, spacetype);
2424
2425         if (handlertype) {
2426                 ScriptLink *slink = &sa->scriptlink;
2427                 void *stmp, *ftmp;
2428                 unsigned short space_event = SPACEHANDLER_VIEW3D_EVENT;
2429
2430                 /* extend slink */
2431
2432                 stmp= slink->scripts;           
2433                 slink->scripts= MEM_mallocN(sizeof(ID*)*(slink->totscript+1),
2434                         "spacehandlerscripts");
2435         
2436                 ftmp= slink->flag;              
2437                 slink->flag= MEM_mallocN(sizeof(short*)*(slink->totscript+1),
2438                         "spacehandlerflags");
2439         
2440                 if (slink->totscript) {
2441                         memcpy(slink->scripts, stmp, sizeof(ID*)*(slink->totscript));
2442                         MEM_freeN(stmp);
2443
2444                         memcpy(slink->flag, ftmp, sizeof(short)*(slink->totscript));
2445                         MEM_freeN(ftmp);
2446                 }
2447
2448                 switch (spacetype) {
2449                         case SPACE_VIEW3D:
2450                                 if (handlertype == 1) space_event = SPACEHANDLER_VIEW3D_EVENT;
2451                                 else space_event = SPACEHANDLER_VIEW3D_DRAW;
2452                                 break;
2453                         default:
2454                                 break;
2455                 }
2456
2457                 slink->scripts[slink->totscript] = (ID *)text;
2458                 slink->flag[slink->totscript]= space_event;
2459
2460                 slink->totscript++;
2461                 slink->actscript = slink->totscript;
2462
2463         }
2464         return 0;
2465 }
2466
2467 int BPY_do_spacehandlers( ScrArea *sa, unsigned short event,
2468         unsigned short space_event )
2469 {
2470         ScriptLink *scriptlink;
2471         int retval = 0;
2472         PyGILState_STATE gilstate;
2473         
2474         if (!sa || !(G.f & G_DOSCRIPTLINKS)) return 0;
2475         
2476         scriptlink = &sa->scriptlink;
2477
2478         if (scriptlink->totscript > 0) {
2479                 PyObject *dict;
2480                 PyObject *ret;
2481                 int index, during_slink = during_scriptlink();
2482
2483                 /* invalid scriptlinks (new .blend was just loaded), return */
2484                 if (during_slink < 0) return 0;
2485
2486                 /* tell we're running a scriptlink.  The sum also tells if this script
2487                  * is running nested inside another.  Blender.Load needs this info to
2488                  * avoid trouble with invalid slink pointers.
2489                  * Update (test): allow EVENT space handlers to call file/image selectors,
2490                  * still disabled for DRAW space handlers: */
2491                 if (event == 0) { /* event = 0: DRAW space handler */
2492                         during_slink++;
2493                         disable_where_scriptlink( (short)during_slink );
2494                 }
2495                 
2496                 gilstate = PyGILState_Ensure();
2497                 
2498                 if( !setup_armature_weakrefs()){
2499                         printf("Oops - weakref dict, this is a bug\n");
2500                         PyGILState_Release(gilstate);
2501                         return 0;
2502                 }
2503                 
2504                 /* set globals in Blender module to identify space handler scriptlink */
2505                 PyDict_SetItemString(g_blenderdict, "bylink", Py_True);
2506                 /* unlike normal scriptlinks, here Blender.link is int (space event type) */
2507                 EXPP_dict_set_item_str(g_blenderdict, "link", PyInt_FromLong(space_event));
2508                 /* note: DRAW space_events set event to 0 */
2509                 EXPP_dict_set_item_str(g_blenderdict, "event", PyInt_FromLong(event));
2510                 /* now run all assigned space handlers for this space and space_event */
2511                 for( index = 0; index < scriptlink->totscript; index++ ) {
2512                         
2513                         /* for DRAW handlers: */
2514                         if (event == 0) {
2515                                 glPushAttrib(GL_ALL_ATTRIB_BITS);
2516                                 glMatrixMode(GL_PROJECTION);
2517                                 glPushMatrix();
2518                                 glMatrixMode(GL_MODELVIEW);
2519                                 glPushMatrix();
2520                         }
2521                         
2522                         if( ( scriptlink->flag[index] == space_event ) &&
2523                             ( scriptlink->scripts[index] != NULL ) ) {
2524                                 dict = CreateGlobalDictionary();
2525                                 ret = RunPython( ( Text * ) scriptlink->scripts[index], dict );
2526                                 ReleaseGlobalDictionary( dict );
2527
2528                                 if (!ret) { /* Failed execution of the script */
2529                                         BPY_Err_Handle( scriptlink->scripts[index]->name+2 );
2530                                 } else {
2531                                         Py_DECREF(ret);
2532
2533                                         /* an EVENT type (event != 0) script can either accept an event or
2534                                          * ignore it:
2535                                          * if the script sets Blender.event to None it accepted it;
2536                                          * otherwise the space's event handling callback that called us
2537                                          * can go on processing the event */
2538                                         if (event && (PyDict_GetItemString(g_blenderdict,"event") == Py_None))
2539                                                 retval = 1; /* event was swallowed */
2540                                 }
2541                                 
2542                                 /* If a scriptlink has just loaded a new .blend file, the
2543                                  * scriptlink pointer became invalid (see api2_2x/Blender.c),
2544                                  * so we stop here. */
2545                                 if( during_scriptlink(  ) == -1 ) {
2546                                         during_slink = 1;
2547                                         if (event == 0) glPopAttrib();
2548                                         break;
2549                                 }
2550                         }
2551                         
2552                         /* for DRAW handlers: */
2553                         if (event == 0) {
2554                                 glMatrixMode(GL_PROJECTION);
2555                                 glPopMatrix();
2556                                 glMatrixMode(GL_MODELVIEW);
2557                                 glPopMatrix();
2558                                 glPopAttrib();
2559                                 disable_where_scriptlink( (short)(during_slink - 1) );
2560                         }
2561                 
2562                 }
2563                 
2564                 PyDict_SetItemString(g_blenderdict, "bylink", Py_False);
2565                 PyDict_SetItemString(g_blenderdict, "link", Py_None );
2566                 EXPP_dict_set_item_str(g_blenderdict, "event", PyString_FromString(""));
2567                 
2568                 PyGILState_Release(gilstate);
2569         }
2570         
2571         /* retval:
2572          * space_event is of type EVENT:
2573          * 0 - event was returned,
2574          * 1 - event was processed;
2575          * space_event is of type DRAW:
2576          * 0 always */
2577
2578         return retval;
2579 }
2580
2581 /*****************************************************************************
2582 * Description:  
2583 * Notes:
2584 *****************************************************************************/
2585 void BPY_free_scriptlink( struct ScriptLink *slink )
2586 {
2587         if( slink->totscript ) {
2588                 if( slink->flag ) {
2589                         MEM_freeN( slink->flag );
2590                         slink->flag= NULL;
2591                 }
2592                 if( slink->scripts ) {
2593                         MEM_freeN( slink->scripts );
2594                         slink->scripts= NULL;
2595                 }
2596         }
2597
2598         return;
2599 }
2600
2601 static int CheckAllSpaceHandlers(Text *text)
2602 {
2603         bScreen *screen;
2604         ScrArea *sa;
2605         ScriptLink *slink;
2606         int fixed = 0;
2607
2608         for (screen = G.main->screen.first; screen; screen = screen->id.next) {
2609                 for (sa = screen->areabase.first; sa; sa = sa->next) {
2610                         slink = &sa->scriptlink;
2611                         if (!slink->totscript) continue;
2612                         if (BPY_del_spacehandler(text, sa) == 0) fixed++;
2613                 }
2614         }
2615         return fixed;
2616 }
2617
2618 static int CheckAllScriptsFromList( ListBase * list, Text * text )
2619 {
2620         ID *id;
2621         ScriptLink *scriptlink;
2622         int index;
2623         int fixed = 0;
2624
2625         id = list->first;
2626
2627         while( id != NULL ) {
2628                 scriptlink = ID_getScriptlink( id );
2629                 if( scriptlink && scriptlink->totscript ) {
2630                         for( index = 0; index < scriptlink->totscript; index++) {
2631                                 if ((Text *)scriptlink->scripts[index] == text) {
2632                                         scriptlink->scripts[index] = NULL;
2633                                         fixed++;
2634                                 }
2635                         }
2636                 }
2637                 id = id->next;
2638         }
2639
2640         return fixed;
2641 }
2642
2643 /* When a Text is deleted, we need to unlink it from eventual scriptlinks */
2644 int BPY_check_all_scriptlinks( Text * text )
2645 {
2646         int fixed = 0;
2647         fixed += CheckAllScriptsFromList( &( G.main->object ), text );
2648         fixed += CheckAllScriptsFromList( &( G.main->lamp ), text );
2649         fixed += CheckAllScriptsFromList( &( G.main->camera ), text );
2650         fixed += CheckAllScriptsFromList( &( G.main->mat ), text );
2651         fixed += CheckAllScriptsFromList( &( G.main->world ), text );
2652         fixed += CheckAllScriptsFromList( &( G.main->scene ), text );
2653         fixed += CheckAllSpaceHandlers(text);
2654
2655         return fixed;
2656 }
2657
2658 /*****************************************************************************
2659 * Description: 
2660 * Notes:
2661 *****************************************************************************/
2662 void BPY_copy_scriptlink( struct ScriptLink *scriptlink )
2663 {
2664         void *tmp;
2665
2666         if( scriptlink->totscript ) {
2667
2668                 tmp = scriptlink->scripts;
2669                 scriptlink->scripts =
2670                         MEM_mallocN( sizeof( ID * ) * scriptlink->totscript,
2671                                      "scriptlistL" );
2672                 memcpy( scriptlink->scripts, tmp,
2673                         sizeof( ID * ) * scriptlink->totscript );
2674
2675                 tmp = scriptlink->flag;
2676                 scriptlink->flag =
2677                         MEM_mallocN( sizeof( short ) * scriptlink->totscript,
2678                                      "scriptlistF" );
2679                 memcpy( scriptlink->flag, tmp,
2680                         sizeof( short ) * scriptlink->totscript );
2681         }
2682
2683         return;
2684 }
2685
2686 /****************************************************************************
2687 * Description:
2688 * Notes:                Not implemented yet
2689 *****************************************************************************/
2690 int BPY_call_importloader( char *name )
2691 {                       /* XXX Should this function go away from Blender? */
2692         printf( "In BPY_call_importloader(name=%s)\n", name );
2693         return ( 0 );
2694 }
2695
2696 /*****************************************************************************
2697 * Private functions
2698 *****************************************************************************/
2699
2700 /*****************************************************************************
2701 * Description: This function executes the python script passed by text. 
2702 *               The Python dictionary containing global variables needs to
2703 *               be passed in globaldict.
2704 *****************************************************************************/
2705 PyObject *RunPython( Text * text, PyObject * globaldict )
2706 {
2707         char *buf = NULL;
2708
2709 /* The script text is compiled to Python bytecode and saved at text->compiled
2710  * to speed-up execution if the user executes the script multiple times */
2711
2712         if( !text->compiled ) { /* if it wasn't already compiled, do it now */
2713                 buf = txt_to_buf( text );
2714
2715                 text->compiled =
2716                         Py_CompileString( buf, GetName( text ),
2717                                           Py_file_input );
2718
2719                 MEM_freeN( buf );
2720
2721                 if( PyErr_Occurred(  ) ) {
2722                         BPY_free_compiled_text( text );
2723                         return NULL;
2724                 }
2725
2726         }
2727
2728         return PyEval_EvalCode( text->compiled, globaldict, globaldict );
2729 }
2730
2731 /*****************************************************************************
2732 * Description: This function returns the value of the name field of the 
2733 *       given Text struct.
2734 *****************************************************************************/
2735 char *GetName( Text * text )
2736 {
2737         return ( text->id.name + 2 );
2738 }
2739
2740 /*****************************************************************************
2741 * Description: This function creates a new Python dictionary object.
2742 *****************************************************************************/
2743 PyObject *CreateGlobalDictionary( void )
2744 {
2745         PyObject *dict = PyDict_New(  );
2746
2747         PyDict_SetItemString( dict, "__builtins__", PyEval_GetBuiltins(  ) );
2748         EXPP_dict_set_item_str( dict, "__name__",
2749                               PyString_FromString( "__main__" ) );
2750
2751         return dict;
2752 }
2753
2754 /*****************************************************************************
2755 * Description: This function deletes a given Python dictionary object.
2756 *****************************************************************************/
2757 void ReleaseGlobalDictionary( PyObject * dict )
2758 {
2759         PyDict_Clear( dict );
2760         Py_DECREF( dict );      /* Release dictionary. */
2761
2762         return;
2763 }
2764
2765 /***************************************************************************
2766 * Description: This function runs all scripts (if any) present in the
2767 *               list argument. The event by which the function has been 
2768 *               called, is passed in the event argument.
2769 *****************************************************************************/
2770 void DoAllScriptsFromList( ListBase * list, short event )
2771 {
2772         ID *id;
2773
2774         id = list->first;
2775
2776         while( id != NULL ) {
2777                 BPY_do_pyscript( id, event );
2778                 id = id->next;
2779         }
2780
2781         return;
2782 }
2783
2784 PyObject *importText( char *name )
2785 {
2786         Text *text;
2787         char *txtname;
2788         char *buf = NULL;
2789         int namelen = strlen( name );
2790
2791         txtname = malloc( namelen + 3 + 1 );
2792         if( !txtname )
2793                 return NULL;
2794
2795         memcpy( txtname, name, namelen );
2796         memcpy( &txtname[namelen], ".py", 4 );
2797
2798         text = ( Text * ) & ( G.main->text.first );
2799
2800         while( text ) {
2801                 if( !strcmp( txtname, GetName( text ) ) )
2802                         break;
2803                 text = text->id.next;
2804         }
2805
2806         if( !text ) {
2807                 free( txtname );
2808                 return NULL;
2809         }
2810
2811         if( !text->compiled ) {
2812                 buf = txt_to_buf( text );
2813                 text->compiled =
2814                         Py_CompileString( buf, GetName( text ),
2815                                           Py_file_input );
2816                 MEM_freeN( buf );
2817
2818                 if( PyErr_Occurred(  ) ) {
2819                         PyErr_Print(  );
2820                         BPY_free_compiled_text( text );
2821                         free( txtname );
2822                         return NULL;
2823                 }
2824         }
2825
2826         free( txtname );
2827         return PyImport_ExecCodeModule( name, text->compiled );
2828 }
2829
2830 static PyMethodDef bimport[] = {
2831         {"blimport", blender_import, METH_VARARGS, "our own import"}
2832 };
2833
2834 PyObject *blender_import( PyObject * self, PyObject * args )
2835 {
2836         PyObject *exception, *err, *tb;
2837         char *name;
2838         PyObject *globals = NULL, *locals = NULL, *fromlist = NULL;
2839         PyObject *m;
2840
2841         if( !PyArg_ParseTuple( args, "s|OOO:bimport",
2842                                &name, &globals, &locals, &fromlist ) )
2843                 return NULL;
2844
2845         m = PyImport_ImportModuleEx( name, globals, locals, fromlist );
2846
2847         if( m )
2848                 return m;
2849         else
2850                 PyErr_Fetch( &exception, &err, &tb );   /*restore for probable later use */
2851
2852         m = importText( name );
2853         if( m ) {               /* found module, ignore above exception */
2854                 PyErr_Clear(  );
2855                 Py_XDECREF( exception );
2856                 Py_XDECREF( err );
2857                 Py_XDECREF( tb );
2858                 printf( "imported from text buffer...\n" );
2859         } else {
2860                 PyErr_Restore( exception, err, tb );
2861         }
2862         return m;
2863 }
2864
2865 void init_ourImport( void )
2866 {
2867         PyObject *m, *d;
2868         PyObject *import = PyCFunction_New( bimport, NULL );
2869
2870         m = PyImport_AddModule( "__builtin__" );
2871         d = PyModule_GetDict( m );
2872         
2873         EXPP_dict_set_item_str( d, "__import__", import );
2874 }
2875
2876 /*
2877  * find in-memory module and recompile
2878  */
2879
2880 static PyObject *reimportText( PyObject *module )
2881 {
2882         Text *text;
2883         char *txtname;
2884         char *name;
2885         char *buf = NULL;
2886
2887         /* get name, filename from the module itself */
2888
2889         txtname = PyModule_GetFilename( module );
2890         name = PyModule_GetName( module );
2891         if( !txtname || !name)
2892                 return NULL;
2893
2894         /* look up the text object */
2895         text = ( Text * ) & ( G.main->text.first );
2896         while( text ) {
2897                 if( !strcmp( txtname, GetName( text ) ) )
2898                         break;
2899                 text = text->id.next;
2900         }
2901
2902         /* uh-oh.... didn't find it */
2903         if( !text )
2904                 return NULL;
2905
2906         /* if previously compiled, free the object */
2907         /* (can't see how could be NULL, but check just in case) */ 
2908         if( text->compiled ){
2909                 Py_DECREF( (PyObject *)text->compiled );
2910         }
2911
2912         /* compile the buffer */
2913         buf = txt_to_buf( text );
2914         text->compiled = Py_CompileString( buf, GetName( text ),
2915                         Py_file_input );
2916         MEM_freeN( buf );
2917
2918         /* if compile failed.... return this error */
2919         if( PyErr_Occurred(  ) ) {
2920                 PyErr_Print(  );
2921                 BPY_free_compiled_text( text );
2922                 return NULL;
2923         }
2924
2925         /* make into a module */
2926         return PyImport_ExecCodeModule( name, text->compiled );
2927 }
2928
2929 /*
2930  * our reload() module, to handle reloading in-memory scripts
2931  */
2932
2933 static PyObject *blender_reload( PyObject * self, PyObject * args )
2934 {
2935         PyObject *exception, *err, *tb;
2936         PyObject *module = NULL;
2937         PyObject *newmodule = NULL;
2938
2939         /* check for a module arg */
2940         if( !PyArg_ParseTuple( args, "O:breload", &module ) )
2941                 return NULL;
2942
2943         /* try reimporting from file */
2944         newmodule = PyImport_ReloadModule( module );
2945         if( newmodule )
2946                 return newmodule;
2947
2948         /* no file, try importing from memory */
2949         PyErr_Fetch( &exception, &err, &tb );   /*restore for probable later use */
2950
2951         newmodule = reimportText( module );
2952         if( newmodule ) {               /* found module, ignore above exception */
2953                 PyErr_Clear(  );
2954                 Py_XDECREF( exception );
2955                 Py_XDECREF( err );
2956                 Py_XDECREF( tb );
2957         } else
2958                 PyErr_Restore( exception, err, tb );
2959
2960         return newmodule;
2961 }
2962
2963 static PyMethodDef breload[] = {
2964         {"blreload", blender_reload, METH_VARARGS, "our own reload"}
2965 };
2966
2967 void init_ourReload( void )
2968 {
2969         PyObject *m, *d;
2970         PyObject *reload = PyCFunction_New( breload, NULL );
2971
2972         m = PyImport_AddModule( "__builtin__" );
2973         d = PyModule_GetDict( m );
2974         EXPP_dict_set_item_str( d, "reload", reload );
2975 }
2976
2977
2978 void BPY_scripts_clear_pyobjects( void )
2979 {
2980         Script *script;
2981         for (script=G.main->script.first; script; script=script->id.next) {
2982                 Py_XDECREF((PyObject *)script->py_draw);
2983                 Py_XDECREF((PyObject *)script->py_event);
2984                 Py_XDECREF((PyObject *)script->py_button);
2985                 Py_XDECREF((PyObject *)script->py_browsercallback);
2986                 Py_XDECREF((PyObject *)script->py_globaldict); 
2987                 SCRIPT_SET_NULL(script)
2988         }
2989 }
2990 void error_pyscript( void )
2991 {
2992         error("Python script error: check console");
2993 }