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