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