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