Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BPY_interface.c « python « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b549b817587c99d587d192d92a2a92c645c851ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
/* 
 *
 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version. The Blender
 * Foundation also sells licenses for use in proprietary software under
 * the Blender License.  See http://www.blender.org/BL/ for information
 * about this.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA	02111-1307, USA.
 *
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 *
 * This is a new part of Blender.
 *
 * Contributor(s): Michel Selten, Willian P. Germano, Stephen Swaney,
 * Chris Keith
 *
 * ***** END GPL/BL DUAL LICENSE BLOCK *****
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <Python.h>
#include "compile.h" /* for the PyCodeObject */
#include "eval.h" /* for PyEval_EvalCode */

#include <stdio.h>

#include <MEM_guardedalloc.h>
#include <BLI_blenlib.h> /* for BLI_last_slash() */

#include <BIF_interface.h> /* for pupmenu */
#include <BIF_space.h>
#include <BIF_screen.h>
#include <BIF_toolbox.h>
#include <BKE_global.h>
#include <BKE_library.h>
#include <BKE_main.h>
#include <BKE_text.h>
#include <BKE_utildefines.h>
#include <BPI_script.h>

#include <DNA_camera_types.h>
#include <DNA_ID.h>
#include <DNA_lamp_types.h>
#include <DNA_material_types.h>
#include <DNA_object_types.h>
#include <DNA_scene_types.h>
#include <DNA_screen_types.h>
#include <DNA_scriptlink_types.h>
#include <DNA_space_types.h>
#include <DNA_text_types.h>
#include <DNA_world_types.h>
#include <DNA_userdef_types.h> /* for U.pythondir */

#include "BPY_extern.h"
#include "BPY_menus.h"
#include "api2_2x/EXPP_interface.h"
#include "api2_2x/constant.h"
#include "api2_2x/modules.h"

/* bpy_registryDict is declared in api2_2x/Registry.h and defined
 * here.	This Python dictionary will be used to store data that scripts
 * choose to preserve after they are executed, so user changes can be
 * restored next time the script is used.  Check the Blender.Registry module. */
extern PyObject *bpy_registryDict;

/*****************************************************************************/
/* Structure definitions																										 */
/*****************************************************************************/
#define FILENAME_LENGTH 24
typedef struct _ScriptError {
	char filename[FILENAME_LENGTH];
	int lineno;
} ScriptError;

/*****************************************************************************/
/* Global variables																													 */
/*****************************************************************************/
ScriptError g_script_error;

/*****************************************************************************/
/* Function prototypes																											 */
/*****************************************************************************/
PyObject *RunPython(Text *text, PyObject *globaldict);
char		 *GetName(Text *text);
PyObject *CreateGlobalDictionary (void);
void			ReleaseGlobalDictionary (PyObject * dict);
void			DoAllScriptsFromList (ListBase * list, short event);
PyObject *importText(char *name);
void init_ourImport(void);
PyObject *blender_import(PyObject *self, PyObject *args);

/*****************************************************************************/
/* Description: This function will initialise Python and all the implemented */
/*							api variations.																							 */
/* Notes:				Currently only the api for 2.2x will be initialised.				 */
/*****************************************************************************/
void BPY_start_python(void)
{
	bpy_registryDict = PyDict_New(); /* check comment at start of this file */

	if (!bpy_registryDict)
		printf("Error: Couldn't create the Registry Python Dictionary!");

/* TODO: Shouldn't "blender" be replaced by PACKAGE ?? (config.h) */
	Py_SetProgramName("blender");

	Py_Initialize ();

	init_ourImport ();

	initBlenderApi2_2x ();

	init_syspath();

	return;
}

/*****************************************************************************/
/* Description: This function will terminate the Python interpreter					 */
/*****************************************************************************/
void BPY_end_python(void)
{
	if (bpy_registryDict) {
		Py_DECREF (bpy_registryDict);
		bpy_registryDict = NULL;
	}

	Py_Finalize();

	BPyMenu_RemoveAllEntries(); /* freeing bpymenu mem */

	/* a script might've opened a .blend file but didn't close it, so: */
	EXPP_Library_Close();

	return;
}

void syspath_append(char *dirname)
{
	PyObject *mod_sys, *dict, *path, *dir;

	PyErr_Clear();

	dir = Py_BuildValue("s", dirname);

	mod_sys = PyImport_ImportModule("sys"); /* new ref */
	dict = PyModule_GetDict(mod_sys);				/* borrowed ref */
	path = PyDict_GetItemString(dict, "path"); /* borrowed ref */

	if (!PyList_Check(path)) return;

	PyList_Append(path, dir);

	if (PyErr_Occurred()) Py_FatalError("could not build sys.path");

	Py_DECREF(mod_sys);
}

void init_syspath(void)
{
	PyObject *path;
	PyObject *mod, *d;
	PyObject *p;
	char *c, *progname;
	char execdir[FILE_MAXDIR + FILE_MAXFILE];/*defines from DNA_space_types.h*/

	int n;

	path = Py_BuildValue("s", bprogname);

	mod = PyImport_ImportModule("Blender.sys");

	if (mod) {
		d = PyModule_GetDict(mod);
		PyDict_SetItemString(d, "progname", path);
		Py_DECREF(mod);
	}
	else
		printf("Warning: could not set Blender.sys.progname\n");

	progname = BLI_last_slash(bprogname); /* looks for the last dir separator */

	c = Py_GetPath(); /* get python system path */
	PySys_SetPath(c); /* initialize */

	n = progname - bprogname;
	if (n > 0) {
		strncpy(execdir, bprogname, n);
		if (execdir[n-1] == '.') n--; /*fix for when run as ./blender */
		execdir[n] = '\0';

		syspath_append(execdir);	/* append to module search path */

		/* set Blender.sys.progname */
	}
	else
		printf ("Warning: could not determine argv[0] path\n");

	/* 
	 * bring in the site module so we can add 
	 * site-package dirs to sys.path 
	 */

	mod = PyImport_ImportModule("site"); /* new ref */

	if (mod) {
		PyObject* item;
		int size = 0;
		int index;

		/* get the value of 'sitedirs' from the module */

		/* the ref man says GetDict() never fails!!! */
		d = PyModule_GetDict (mod); /* borrowed ref */
		p = PyDict_GetItemString (d, "sitedirs");  /* borrowed ref */

		if( p ) {  /* we got our string */
			/* append each item in sitedirs list to path */
			size = PyList_Size (p);

			for (index = 0; index < size; index++) {
	item	= PySequence_GetItem (p, index);	/* new ref */
	if( item )
		syspath_append (PyString_AsString(item));
			}
		}
		Py_DECREF(mod);
	}
	else {	/* import 'site' failed */
		PyErr_Clear();
		printf("sys_init:warning - no sitedirs added from site module.\n");
	}

	/* 
	 * initialize the sys module
	 * set sys.executable to the Blender exe 
	 * set argv[0] to the Blender exe
	 */

	mod = PyImport_ImportModule("sys"); /* new ref */

	if (mod) {
		d = PyModule_GetDict(mod); /* borrowed ref */
		PyDict_SetItemString(d, "executable", Py_BuildValue("s", bprogname));
		/* in the future this can be extended to have more argv's if needed: */
		PyDict_SetItemString(d, "argv", Py_BuildValue("[s]", bprogname));
		Py_DECREF(mod);
	}
}

/*****************************************************************************/
/* Description: This function finishes Python initialization in Blender.		 */
/*							Because U.pythondir (user defined dir for scripts) isn't		 */
/*							initialized when BPY_start_Python needs to be executed, we	 */
/*							postpone adding U.pythondir to sys.path and also BPyMenus		 */
/*							(mechanism to register scripts in Blender menus) for when		 */
/*							that dir info is available.																	 */
/*****************************************************************************/
void BPY_post_start_python(void)
{
	if (U.pythondir && U.pythondir[0] != '\0')
		syspath_append(U.pythondir);	/* append to module search path */

	BPyMenu_Init(0); /* get dynamic menus (registered scripts) data */
}

/*****************************************************************************/
/* Description: This function will return the linenumber on which an error	 */
/*							has occurred in the Python script.													 */
/*****************************************************************************/
int BPY_Err_getLinenumber(void)
{
	return g_script_error.lineno;
}

/*****************************************************************************/
/* Description: This function will return the filename of the python script. */
/*****************************************************************************/
const char *BPY_Err_getFilename(void)
{
	return g_script_error.filename;
}

/*****************************************************************************/
/* Description: Return PyString filename from a traceback object						 */
/*****************************************************************************/
PyObject *traceback_getFilename(PyObject *tb)
{
	PyObject *v;

/* co_filename is in f_code, which is in tb_frame, which is in tb */

	v = PyObject_GetAttrString(tb, "tb_frame"); Py_XDECREF(v);
	v = PyObject_GetAttrString(v, "f_code"); Py_XDECREF(v);
	v = PyObject_GetAttrString(v, "co_filename");

	return v;
}

/*****************************************************************************/
/* Description: Blender Python error handler. This catches the error and		 */
/* stores filename and line number in a global															 */
/*****************************************************************************/
void BPY_Err_Handle(char *script_name)
{
	PyObject *exception, *err, *tb, *v;

	if (!script_name) {
		printf("Error: script has NULL name\n");
		return;
	}

	PyErr_Fetch(&exception, &err, &tb);

	if (!exception && !tb) {
		printf("FATAL: spurious exception\n");
		return;
	}

	strcpy(g_script_error.filename, script_name);

	if (exception && PyErr_GivenExceptionMatches(exception, PyExc_SyntaxError)) {
		/* no traceback available when SyntaxError */
		PyErr_Restore(exception, err, tb); /* takes away reference! */
		PyErr_Print();
		v = PyObject_GetAttrString(err, "lineno");
		if (v) {
			g_script_error.lineno = PyInt_AsLong(v);
			Py_DECREF(v);
		} else {
			g_script_error.lineno = -1;
		}
		/* this avoids an abort in Python 2.3's garbage collecting: */
		PyErr_Clear(); 
		return;
	} else {
		PyErr_NormalizeException(&exception, &err, &tb);
		PyErr_Restore(exception, err, tb); // takes away reference!
		PyErr_Print();
		tb = PySys_GetObject("last_traceback");

		if (!tb) {
			printf("\nCan't get traceback\n");
			return;
		}

		Py_INCREF(tb);

/* From old bpython BPY_main.c:
 * 'check traceback objects and look for last traceback in the
 *	same text file. This is used to jump to the line of where the
 *	error occured. "If the error occured in another text file or module,
 *	the last frame in the current file is adressed."' */

		while (1) { 
			v = PyObject_GetAttrString(tb, "tb_next");

			if (v == Py_None || strcmp(PyString_AsString(traceback_getFilename(v)),
															script_name)) {
				break;
			}

			Py_DECREF(tb);
			tb = v;
		}

		v = PyObject_GetAttrString(tb, "tb_lineno");
		g_script_error.lineno = PyInt_AsLong(v);
		Py_XDECREF(v);
		v = traceback_getFilename(tb);
		strncpy(g_script_error.filename, PyString_AsString(v), FILENAME_LENGTH);
		Py_XDECREF(v);
		Py_DECREF(tb);
	}

	return;
}

/*****************************************************************************/
/* Description: This function executes the script passed by st.							 */
/* Notes:				It is called by blender/src/drawtext.c when a Blender user	 */
/*							presses ALT+PKEY in the script's text window.								 */
/*****************************************************************************/
int BPY_txt_do_python_Text(struct Text* text)
{
	PyObject *py_dict, *py_result;
	BPy_constant *info;
	Script *script = G.main->script.first;

	if (!text) return 0;

	/* check if this text is already running */
	while (script) {
		if (!strcmp(script->id.name+2, text->id.name+2)) {
			/* if this text is already a running script, just move to it: */	
			SpaceScript *sc;
			newspace(curarea, SPACE_SCRIPT);
			sc = curarea->spacedata.first;
			sc->script = script;
			return 1;
		}
		script = script->id.next;
	}

	/* Create a new script structure and initialize it: */
	script = alloc_libblock(&G.main->script, ID_SCRIPT, GetName(text));

	if (!script) {
		printf("couldn't allocate memory for Script struct!");
		return 0;
	}

	script->id.us = 1;
	script->flags = SCRIPT_RUNNING;
	script->py_draw = NULL;
	script->py_event = NULL;
	script->py_button = NULL;

	py_dict = CreateGlobalDictionary();

	script->py_globaldict = py_dict;

	info = (BPy_constant *)M_constant_New();
	if (info) {
		constant_insert(info, "name", PyString_FromString(script->id.name+2));
		Py_INCREF (Py_None);
		constant_insert(info, "arg", Py_None);
		PyDict_SetItemString(py_dict, "__script__", (PyObject *)info);
	}

	py_result = RunPython (text, py_dict); /* Run the script */

	if (!py_result) { /* Failed execution of the script */

		BPY_Err_Handle(GetName(text));
		ReleaseGlobalDictionary(py_dict);
		script->py_globaldict = NULL;
		free_libblock(&G.main->script, script);
		//BPY_end_python();
		//BPY_start_python();

		return 0;
	}
	else {
		Py_DECREF (py_result);
		script->flags &=~SCRIPT_RUNNING;
		if (!script->flags) {
			ReleaseGlobalDictionary(py_dict);
			script->py_globaldict = NULL;
			free_libblock(&G.main->script, script);
		}
	}

	return 1; /* normal return */
}

/*****************************************************************************/
/* Description: The original function of this name has been refactored			 */
/* into BPY_txt_do_python_Text.  That version is needed for the command			 */
/* line support for Python.  This is here to keep the interface the					 */
/* same and reduce code changes elsewhere.																	 */
/*****************************************************************************/
int BPY_txt_do_python(struct SpaceText* st)
{
	return BPY_txt_do_python_Text(st->text);
}

/*****************************************************************************/
/* Description: Called from command line to run a Python script
* automatically. */
/*****************************************************************************/
void BPY_run_python_script(char *fn)
{
	Text	*text;

	if ( !BLI_exists(fn) ) {
		printf("\nError: no such file -- %s.\n", fn);
		return;
	}

	text = add_text(fn);
	if (text == NULL) {
		printf("Error in BPY_run_python_script: couldn't create Blender text "
			"from %s\n", fn);
		// On Windows if I continue I just get a segmentation
		// violation.  To get a baseline file I exit here.
		exit(2);
	}

	if (BPY_txt_do_python_Text(text) != 1) {
		printf( "\nError executing Python script:\n"
			"%s (at line %d)\n", fn, BPY_Err_getLinenumber());
	}

	free_libblock(&G.main->text, text);
}

/*****************************************************************************/
/* Description: This function executes the script chosen from a menu.				 */
/* Notes:				It is called by the ui code in src/header_???.c when a user  */
/*							clicks on a menu entry that refers to a script.							 */
/*							Scripts are searched in the BPyMenuTable, using the given		 */
/*							menutype and event values to know which one was chosen.			 */
/*****************************************************************************/
int BPY_menu_do_python(short menutype, int event)
{
	PyObject *py_dict, *py_res, *pyarg = NULL;
	BPy_constant *info;
	BPyMenu *pym;
	BPySubMenu *pysm;
	FILE *fp = NULL;
	char *buffer, *s;
	char filestr[FILE_MAXDIR+FILE_MAXFILE];
	char dirname[FILE_MAXDIR];
	Script *script = G.main->script.first;
	int len;

	pym = BPyMenu_GetEntry(menutype, (short)event);

	if (!pym) return 0;

	if (pym->version > G.version)
		notice ("Version mismatch: script was written for Blender %d. "
						"It may fail with yours: %d.", pym->version, G.version);

/* if there are submenus, let the user choose one from a pupmenu that we
 * create here.*/
	pysm = pym->submenus;
	if (pysm) {
		char *pupstr; 
		int arg;

		pupstr = BPyMenu_CreatePupmenuStr(pym, menutype);

		if (pupstr) {
			arg = pupmenu(pupstr);
			MEM_freeN(pupstr);

			if (arg >= 0) {
				while (arg--) pysm = pysm->next;
				pyarg = PyString_FromString(pysm->arg);
			}
			else return 0;
		}
	}

	if (!pyarg) {/* no submenus */
		Py_INCREF (Py_None);
		pyarg = Py_None;
	}

	if (pym->dir) /* script is in U.pythondir */
		BLI_make_file_string("/", filestr, U.pythondir, pym->filename);
	else { /* script is in ~/.blender/scripts/ */
		BLI_make_file_string("/", dirname, bpymenu_gethome(), "scripts");
		BLI_make_file_string("/", filestr, dirname,	pym->filename);
	}

	fp = fopen(filestr, "rb");
	if (!fp) {
		printf("Error loading script: couldn't open file %s\n", filestr);
		return 0;
	}

	/* Create a new script structure and initialize it: */
	script = alloc_libblock(&G.main->script, ID_SCRIPT, pym->name);

	if (!script) {
		printf("couldn't allocate memory for Script struct!");
		fclose(fp);
		return 0;
	}

	script->id.us = 1;
	script->flags = SCRIPT_RUNNING;
	script->py_draw = NULL;
	script->py_event = NULL;
	script->py_button = NULL;

	py_dict = CreateGlobalDictionary();

	script->py_globaldict = py_dict;

	info = (BPy_constant *)M_constant_New();
	if (info) {
		constant_insert(info, "name", PyString_FromString(script->id.name+2));
		constant_insert(info, "arg", pyarg);
		PyDict_SetItemString(py_dict, "__script__", (PyObject *)info);
	}

	/* Previously we used PyRun_File to run directly the code on a FILE object,
	 * but as written in the Python/C API Ref Manual, chapter 2,
	 * 'FILE structs for different C libraries can be different and incompatible'
	 * 
	 * So now we load the script file data to a buffer */

	fseek(fp, 0L, SEEK_END);
	len = ftell(fp);
	fseek(fp, 0L, SEEK_SET);

	buffer = MEM_mallocN(len+2, "pyfilebuf"); /* len+2 to add '\n\0' */
	len = fread(buffer, 1, len, fp);

	buffer[len] = '\n'; /* fix syntax error in files w/o eol*/
	buffer[len+1] = '\0';

	/* fast clean-up of dos cr/lf line endings: change '\r' to space */

	/* we also have to check for line splitters: '\\' */
	/* to avoid possible syntax errors on dos files on win */
	/**/
	/* but first make sure we won't disturb memory below &buffer[0]: */
	if (*buffer == '\r') *buffer = ' ';

	/* now handle the whole buffer */
	for (s = buffer + 1; *s != '\0'; s++)	{
		if (*s == '\r') {
			if (*(s-1) == '\\') { /* special case: long lines split with '\': */
				*(s-1) = ' '; /* we write ' \', because '\ ' is a syntax error */
				*s = '\\';
			}
			else *s = ' '; /* not a split line, just replace '\r' with ' ' */
		}
	}

	fclose(fp);

	/* run the string buffer */

	py_res = PyRun_String(buffer, Py_file_input, py_dict, py_dict);

	MEM_freeN(buffer);

	if (!py_res) { /* Failed execution of the script */

		BPY_Err_Handle(script->id.name+2);
		PyErr_Print();
		ReleaseGlobalDictionary(py_dict);
		free_libblock(&G.main->script, script);
	//	BPY_end_python();
	//	BPY_start_python();
		error ("Python script error: check console");

		return 0;
	}
	else {
		Py_DECREF (py_res);
		script->flags &=~SCRIPT_RUNNING;

		if (!script->flags) {
			ReleaseGlobalDictionary(py_dict);
			script->py_globaldict = NULL;
			free_libblock(&G.main->script, script);

			/* special case: called from the menu in the Scripts window
			 * we have to change sc->script pointer, since it'll be freed here.*/
			if (curarea->spacetype == SPACE_SCRIPT) {
				SpaceScript *sc = curarea->spacedata.first;
				sc->script = G.main->script.first; /* can be null, which is ok ... */
				/* ... meaning no other script is running right now. */
			}
	
		}
	}

	return 1; /* normal return */
}

/*****************************************************************************/
/* Description:																															 */
/* Notes:																																		 */
/*****************************************************************************/
void BPY_free_compiled_text(struct Text* text)
{
	if (!text->compiled) return;
	Py_DECREF((PyObject*) text->compiled);
	text->compiled = NULL;

	return;
}

/*****************************************************************************/
/* Description: This function frees a finished (flags == 0) script.					 */
/*****************************************************************************/
void BPY_free_finished_script(Script *script)
{
	if (!script) return;

	if (PyErr_Occurred()) { /* if script ended after filesel */
		PyErr_Print(); /* eventual errors are handled now */
		error ("Python script error: check console");
	}

	free_libblock(&G.main->script, script);
	return;
}

void unlink_script(Script *script)
{ /* copied from unlink_text in drawtext.c */
	bScreen *scr;
	ScrArea *area;
	SpaceLink *sl;

	for (scr= G.main->screen.first; scr; scr= scr->id.next) {
		for (area= scr->areabase.first; area; area= area->next) {
			for (sl= area->spacedata.first; sl; sl= sl->next) {
				if (sl->spacetype==SPACE_SCRIPT) {
					SpaceScript *sc= (SpaceScript*) sl;

					if (sc->script==script) {
						sc->script= NULL;

						if (sc==area->spacedata.first) {
							scrarea_queue_redraw(area);
						}
					}
				}
			}
		}
	}
}

void BPY_clear_script (Script *script)
{
	PyObject *dict;

	if (!script) return;

	Py_XDECREF((PyObject *)script->py_draw);
	Py_XDECREF((PyObject *)script->py_event);
	Py_XDECREF((PyObject *)script->py_button);

	dict = script->py_globaldict;

	if (dict) {
		PyDict_Clear (dict);
		Py_DECREF (dict);		/* Release dictionary. */
		script->py_globaldict = NULL;
	}

	unlink_script (script);
}

/*****************************************************************************/
/* ScriptLinks																															 */
/*****************************************************************************/

/*****************************************************************************/
/* Description:																															 */
/* Notes:				Not implemented yet																					 */
/*****************************************************************************/
void BPY_clear_bad_scriptlinks(struct Text *byebye)
{
/*
	BPY_clear_bad_scriptlist(getObjectList(), byebye);
	BPY_clear_bad_scriptlist(getLampList(), byebye);
	BPY_clear_bad_scriptlist(getCameraList(), byebye);
	BPY_clear_bad_scriptlist(getMaterialList(), byebye);
	BPY_clear_bad_scriptlist(getWorldList(),	byebye);
	BPY_clear_bad_scriptlink(&scene_getCurrent()->id, byebye);

	allqueue(REDRAWBUTSSCRIPT, 0);
*/
	return;
}

/*****************************************************************************/
/* Description: Loop through all scripts of a list of object types, and			 */
/*							execute these scripts.																			 */
/*							For the scene, only the current active scene the scripts are */
/*							executed (if any).																					 */
/*****************************************************************************/
void BPY_do_all_scripts(short event)
{
	DoAllScriptsFromList (&(G.main->object), event);
	DoAllScriptsFromList (&(G.main->lamp), event);
	DoAllScriptsFromList (&(G.main->camera), event);
	DoAllScriptsFromList (&(G.main->mat), event);
	DoAllScriptsFromList (&(G.main->world), event);

	BPY_do_pyscript (&(G.scene->id), event);

	return;
}

/*****************************************************************************/
/* Description: Execute a Python script when an event occurs. The following  */
/*							events are possible: frame changed, load script and redraw.  */
/*							Only events happening to one of the following object types	 */
/*							are handled: Object, Lamp, Camera, Material, World and			 */
/*							Scene.																											 */
/*****************************************************************************/

static ScriptLink *ID_getScriptlink(ID *id)
{
	switch (MAKE_ID2 (id->name[0], id->name[1])) {
	case ID_OB:
		return &((Object*)id)->scriptlink;
	case ID_LA:
		return &((Lamp*)id)->scriptlink;
	case ID_CA:
		return &((Camera*)id)->scriptlink;
	case ID_MA:
		return &((Material*)id)->scriptlink;
	case ID_WO:
		return &((World*)id)->scriptlink;
	case ID_SCE:
		return &((Scene*)id)->scriptlink;
	default:
		return NULL;
	}
}

static PyObject *ID_asPyObject(ID *id)
{
	switch (MAKE_ID2 (id->name[0], id->name[1])) {
	case ID_OB:
		return Object_CreatePyObject((Object*) id);
	case ID_LA:
		return Lamp_CreatePyObject((Lamp*) id);
	case ID_CA:
		return Camera_CreatePyObject((Camera*) id);
	case ID_MA:
		return Material_CreatePyObject((Material*) id);
	case ID_WO:
		return World_CreatePyObject((World*) id);
	case ID_SCE:
		return Scene_CreatePyObject((Scene*) id);
	default:
		return NULL;
	}
}

void BPY_do_pyscript(ID *id, short event)
{
	ScriptLink *scriptlink = ID_getScriptlink(id);

	if (scriptlink && scriptlink->totscript) {
		PyObject *dict;
		PyObject *ret;
		int index;

			// set globals in Blender module to identify scriptlink
		Py_INCREF(Py_True);
		PyDict_SetItemString(g_blenderdict, "bylink", Py_True);
		PyDict_SetItemString(g_blenderdict, "link", ID_asPyObject(id));
		PyDict_SetItemString(g_blenderdict, "event", 
			PyString_FromString(event_to_name(event)));

		for (index = 0; index < scriptlink->totscript; index++) {
			if ((scriptlink->flag[index] == event) &&
					(scriptlink->scripts[index] != NULL)) {
				dict = CreateGlobalDictionary();
				ret = RunPython ((Text*) scriptlink->scripts[index], dict);
				ReleaseGlobalDictionary (dict);
				if (!ret) {
					/* Failed execution of the script */
					BPY_Err_Handle (scriptlink->scripts[index]->name+2);
					BPY_end_python ();
					BPY_start_python ();
				} else {
					Py_DECREF (ret);
				}
			}
		}

			// cleanup bylink flag and clear link so PyObject can be released
		Py_INCREF(Py_False);
		PyDict_SetItemString(g_blenderdict, "bylink", Py_False);
		Py_INCREF(Py_None);
		PyDict_SetItemString(g_blenderdict, "link", Py_None);
	}
}

/*****************************************************************************/
/* Description:																															 */
/* Notes:																																		 */
/*****************************************************************************/
void BPY_free_scriptlink(struct ScriptLink *slink)
{
	if (slink->totscript) {
		if(slink->flag) MEM_freeN(slink->flag);
		if(slink->scripts) MEM_freeN(slink->scripts); 
	}

	return;
}

/*****************************************************************************/
/* Description:																															 */
/* Notes:																																		 */
/*****************************************************************************/
void BPY_copy_scriptlink(struct ScriptLink *scriptlink)
{
	void *tmp;

	if (scriptlink->totscript) {

		tmp = scriptlink->scripts;
		scriptlink->scripts =
			MEM_mallocN(sizeof(ID*)*scriptlink->totscript, "scriptlistL");
		memcpy(scriptlink->scripts, tmp, sizeof(ID*)*scriptlink->totscript);

		tmp = scriptlink->flag;
		scriptlink->flag =
			MEM_mallocN(sizeof(short)*scriptlink->totscript, "scriptlistF");
		memcpy(scriptlink->flag, tmp, sizeof(short)*scriptlink->totscript);
	}

	return;
}

/*****************************************************************************/
/* Description:																															 */
/* Notes:				Not implemented yet																					 */
/*****************************************************************************/
int BPY_call_importloader(char *name)
{ /* XXX Should this function go away from Blender? */
	printf ("In BPY_call_importloader(name=%s)\n",name);
	return (0);
}

/*****************************************************************************/
/* Private functions																												 */
/*****************************************************************************/

/*****************************************************************************/
/* Description: This function executes the python script passed by text.		 */
/*							The Python dictionary containing global variables needs to	 */
/*							be passed in globaldict.																		 */
/*****************************************************************************/
PyObject * RunPython(Text *text, PyObject *globaldict)
{
	char *buf = NULL;

/* The script text is compiled to Python bytecode and saved at text->compiled
 * to speed-up execution if the user executes the script multiple times */

	if (!text->compiled) { /* if it wasn't already compiled, do it now */
		buf = txt_to_buf(text);

		text->compiled = Py_CompileString(buf, GetName(text), Py_file_input);

		MEM_freeN(buf);

		if (PyErr_Occurred()) {
			BPY_free_compiled_text(text);
			return NULL;
		}

	}

	return PyEval_EvalCode(text->compiled, globaldict, globaldict);
}

/*****************************************************************************/
/* Description: This function returns the value of the name field of the		 */
/*							given Text struct.																					 */
/*****************************************************************************/
char * GetName(Text *text)
{
	return (text->id.name+2);
}

/*****************************************************************************/
/* Description: This function creates a new Python dictionary object.				 */
/*****************************************************************************/
PyObject * CreateGlobalDictionary (void)
{
	PyObject *dict = PyDict_New();

	PyDict_SetItemString (dict, "__builtins__", PyEval_GetBuiltins());
	PyDict_SetItemString (dict, "__name__", PyString_FromString("__main__"));

	return dict;
}

/*****************************************************************************/
/* Description: This function deletes a given Python dictionary object.			 */
/*****************************************************************************/
void ReleaseGlobalDictionary (PyObject * dict)
{
	PyDict_Clear (dict);
	Py_DECREF (dict);		/* Release dictionary. */

	return;
}

/*****************************************************************************/
/* Description: This function runs all scripts (if any) present in the			 */
/*							list argument. The event by which the function has been			 */
/*							called, is passed in the event argument.										 */
/*****************************************************************************/
void DoAllScriptsFromList (ListBase *list, short event)
{
	ID *id;

	id = list->first;

	while (id != NULL) {
		BPY_do_pyscript (id, event);
		id = id->next;
	}

	return;
}

PyObject *importText(char *name)
{
	Text *text;
	char *txtname;
	char *buf = NULL;
	int namelen = strlen(name);

	txtname = malloc(namelen+3+1);
	if (!txtname) return NULL;

	memcpy(txtname, name, namelen);
	memcpy(&txtname[namelen], ".py", 4);

	text = (Text*) &(G.main->text.first);

	while(text) {
		if (!strcmp (txtname, GetName(text)))
			break;
		text = text->id.next;
	}

	if (!text) {
		free(txtname);
		return NULL;
	}

	if (!text->compiled) {
		buf = txt_to_buf(text);
		text->compiled = Py_CompileString(buf, GetName(text), Py_file_input);
		MEM_freeN(buf);

		if (PyErr_Occurred()) {
			PyErr_Print();
			BPY_free_compiled_text(text);
			free(txtname);
			return NULL;
		}
	}

	free(txtname);
	return PyImport_ExecCodeModule(name, text->compiled);
}

static PyMethodDef bimport[] = {
	{ "blimport", blender_import, METH_VARARGS, "our own import"}
};

PyObject *blender_import(PyObject *self, PyObject *args)
{
	PyObject *exception, *err, *tb;
	char *name;
	PyObject *globals = NULL, *locals = NULL, *fromlist = NULL;
	PyObject *m;

	if (!PyArg_ParseTuple(args, "s|OOO:bimport",
					&name, &globals, &locals, &fromlist))
			return NULL;

	m = PyImport_ImportModuleEx(name, globals, locals, fromlist);

	if (m) 
		return m;
	else
		PyErr_Fetch(&exception, &err, &tb); /*restore for probable later use*/
	
	m = importText(name);
	if (m) { /* found module, ignore above exception*/
		PyErr_Clear();
		Py_XDECREF(exception); Py_XDECREF(err); Py_XDECREF(tb);
		printf("imported from text buffer...\n");
	} else {
		PyErr_Restore(exception, err, tb);
	}
	return m;
}

void init_ourImport(void)
{
	PyObject *m, *d;
	PyObject *import = PyCFunction_New(bimport, NULL);

	m = PyImport_AddModule("__builtin__");
	d = PyModule_GetDict(m);
	PyDict_SetItemString(d, "__import__", import);
}