| 
									
										
										
										
											2009-08-26 06:15:43 +00:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * $Id: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ***** BEGIN GPL 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. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Contributor(s): Michel Selten, Willian P. Germano, Stephen Swaney, | 
					
						
							|  |  |  |  * Chris Keith, Chris Want, Ken Hughes, Campbell Barton | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ***** END GPL LICENSE BLOCK ***** | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  |   | 
					
						
							| 
									
										
										
										
											2009-02-16 16:17:20 +00:00
										 |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							| 
									
										
										
										
											2009-04-11 02:18:24 +00:00
										 |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include <sys/stat.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-16 16:17:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-26 06:15:43 +00:00
										 |  |  | /* grr, python redefines */ | 
					
						
							|  |  |  | #ifdef _POSIX_C_SOURCE
 | 
					
						
							|  |  |  | #undef _POSIX_C_SOURCE
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-29 13:36:08 +00:00
										 |  |  | #include <Python.h>
 | 
					
						
							|  |  |  | #include "compile.h"		/* for the PyCodeObject */
 | 
					
						
							|  |  |  | #include "eval.h"		/* for PyEval_EvalCode */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "bpy_rna.h"
 | 
					
						
							| 
									
										
										
										
											2008-12-21 08:53:36 +00:00
										 |  |  | #include "bpy_operator.h"
 | 
					
						
							| 
									
										
										
										
											2009-02-28 13:27:45 +00:00
										 |  |  | #include "bpy_ui.h"
 | 
					
						
							| 
									
										
										
										
											2009-06-05 16:11:35 +00:00
										 |  |  | #include "bpy_util.h"
 | 
					
						
							| 
									
										
										
										
											2009-02-28 13:27:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-06 13:20:05 +00:00
										 |  |  | #ifndef WIN32
 | 
					
						
							|  |  |  | #include <dirent.h>
 | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | #include "BLI_winstuff.h"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 13:27:45 +00:00
										 |  |  | #include "DNA_space_types.h"
 | 
					
						
							| 
									
										
										
										
											2009-03-04 13:26:33 +00:00
										 |  |  | #include "DNA_text_types.h"
 | 
					
						
							| 
									
										
										
										
											2009-04-11 02:18:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-04 13:26:33 +00:00
										 |  |  | #include "MEM_guardedalloc.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-13 17:46:30 +00:00
										 |  |  | #include "BLI_path_util.h"
 | 
					
						
							| 
									
										
										
										
											2009-09-03 10:34:54 +00:00
										 |  |  | #include "BLI_storage.h"
 | 
					
						
							| 
									
										
										
										
											2009-07-19 17:45:14 +00:00
										 |  |  | #include "BLI_fileops.h"
 | 
					
						
							| 
									
										
										
										
											2009-04-11 16:17:39 +00:00
										 |  |  | #include "BLI_string.h"
 | 
					
						
							| 
									
										
										
										
											2009-04-11 02:18:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-28 10:00:04 +00:00
										 |  |  | #include "BKE_blender.h"
 | 
					
						
							| 
									
										
										
										
											2009-04-11 02:18:24 +00:00
										 |  |  | #include "BKE_context.h"
 | 
					
						
							|  |  |  | #include "BKE_text.h"
 | 
					
						
							| 
									
										
										
										
											2009-10-29 09:25:11 +00:00
										 |  |  | #include "BKE_context.h"
 | 
					
						
							| 
									
										
										
										
											2009-11-10 19:57:04 +00:00
										 |  |  | #include "BKE_global.h"
 | 
					
						
							| 
									
										
										
										
											2009-11-20 15:01:09 +00:00
										 |  |  | #include "BKE_main.h"
 | 
					
						
							| 
									
										
										
										
											2009-04-11 02:18:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
											
												2.5
More cleanup!
- removed old UI font completely, including from uiBeginBlock
- emboss hints for uiBlock only have three types now;
  Regular, Pulldown, or "Nothing" (only icon/text)
- removed old font path from Userdef
- removed all old button theme hinting
- removed old "auto block" to merge buttons in groups
  (was only in use for radiosity buttons)
And went over all warnings. One hooray for make giving clean output :)
Well, we need uniform definitions for warnings, so people at least fix
them... here's the real bad bugs I found:
- in mesh code, a call to editmesh mixed *em and *me
- in armature, ED_util.h was not included, so no warnings for wrong call
  to ED_undo_push()
- The extern Py api .h was not included in the bpy_interface.c, showing
  a several calls using different args.
Further just added the missing includes, and removed unused vars.
											
										 
											2009-04-14 15:59:52 +00:00
										 |  |  | #include "BPY_extern.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | #include "../generic/bpy_internal_import.h" // our own imports
 | 
					
						
							| 
									
										
										
										
											2009-07-02 12:11:20 +00:00
										 |  |  | /* external util modules */ | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "../generic/Mathutils.h"
 | 
					
						
							|  |  |  | #include "../generic/Geometry.h"
 | 
					
						
							|  |  |  | #include "../generic/BGL.h"
 | 
					
						
							| 
									
										
										
										
											2009-11-16 19:18:04 +00:00
										 |  |  | #include "../generic/IDProp.h"
 | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-07 16:20:19 +00:00
										 |  |  | /* for internal use, when starting and ending python scripts */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* incase a python script triggers another python call, stop bpy_context_clear from invalidating */ | 
					
						
							|  |  |  | static int py_call_level= 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-09 13:20:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // only for tests
 | 
					
						
							|  |  |  | #define TIME_PY_RUN
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef TIME_PY_RUN
 | 
					
						
							|  |  |  | #include "PIL_time.h"
 | 
					
						
							|  |  |  | static int		bpy_timer_count = 0; | 
					
						
							|  |  |  | static double	bpy_timer; /* time since python starts */ | 
					
						
							|  |  |  | static double	bpy_timer_run; /* time for each python script run */ | 
					
						
							|  |  |  | static double	bpy_timer_run_tot; /* accumulate python runs */ | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-07 16:20:19 +00:00
										 |  |  | void bpy_context_set(bContext *C, PyGILState_STATE *gilstate) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	py_call_level++; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(gilstate) | 
					
						
							|  |  |  | 		*gilstate = PyGILState_Ensure(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(py_call_level==1) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if(C) { // XXX - should always be true.
 | 
					
						
							|  |  |  | 			BPy_SetContext(C); | 
					
						
							|  |  |  | 			bpy_import_main_set(CTX_data_main(C)); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							|  |  |  | 			fprintf(stderr, "ERROR: Python context called with a NULL Context. this should not happen!\n"); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-08-09 13:20:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-05 11:17:09 +00:00
										 |  |  | 		BPY_update_modules(); /* can give really bad results if this isnt here */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-09 13:20:12 +00:00
										 |  |  | #ifdef TIME_PY_RUN
 | 
					
						
							|  |  |  | 		if(bpy_timer_count==0) { | 
					
						
							|  |  |  | 			/* record time from the beginning */ | 
					
						
							|  |  |  | 			bpy_timer= PIL_check_seconds_timer(); | 
					
						
							|  |  |  | 			bpy_timer_run = bpy_timer_run_tot = 0.0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		bpy_timer_run= PIL_check_seconds_timer(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		bpy_timer_count++; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2009-08-07 16:20:19 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void bpy_context_clear(bContext *C, PyGILState_STATE *gilstate) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	py_call_level--; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(gilstate) | 
					
						
							|  |  |  | 		PyGILState_Release(*gilstate); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(py_call_level < 0) { | 
					
						
							|  |  |  | 		fprintf(stderr, "ERROR: Python context internal state bug. this should not happen!\n"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else if(py_call_level==0) { | 
					
						
							|  |  |  | 		// XXX - Calling classes currently wont store the context :\, cant set NULL because of this. but this is very flakey still.
 | 
					
						
							|  |  |  | 		//BPy_SetContext(NULL);
 | 
					
						
							|  |  |  | 		//bpy_import_main_set(NULL);
 | 
					
						
							| 
									
										
										
										
											2009-08-09 13:20:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef TIME_PY_RUN
 | 
					
						
							|  |  |  | 		bpy_timer_run_tot += PIL_check_seconds_timer() - bpy_timer_run; | 
					
						
							|  |  |  | 		bpy_timer_count++; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-07 16:20:19 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-28 05:02:09 +00:00
										 |  |  | static void bpy_import_test(char *modname) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	PyObject *mod= PyImport_ImportModuleLevel(modname, NULL, NULL, NULL, 0); | 
					
						
							|  |  |  | 	if(mod) { | 
					
						
							|  |  |  | 		Py_DECREF(mod); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		PyErr_Print(); | 
					
						
							|  |  |  | 		PyErr_Clear(); | 
					
						
							|  |  |  | 	}	 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-08-07 16:20:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-04 13:26:33 +00:00
										 |  |  | void BPY_free_compiled_text( struct Text *text ) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if( text->compiled ) { | 
					
						
							|  |  |  | 		Py_DECREF( ( PyObject * ) text->compiled ); | 
					
						
							|  |  |  | 		text->compiled = NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2008-11-29 13:36:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*****************************************************************************
 | 
					
						
							| 
									
										
										
										
											2009-04-11 16:17:39 +00:00
										 |  |  | * Description: Creates the bpy module and adds it to sys.modules for importing | 
					
						
							| 
									
										
										
										
											2008-11-29 13:36:08 +00:00
										 |  |  | *****************************************************************************/ | 
					
						
							| 
									
										
										
										
											2009-11-05 11:17:09 +00:00
										 |  |  | static BPy_StructRNA *bpy_context_module= NULL; /* for fast access */ | 
					
						
							| 
									
										
										
										
											2009-04-11 16:17:39 +00:00
										 |  |  | static void bpy_init_modules( void ) | 
					
						
							| 
									
										
										
										
											2008-11-29 13:36:08 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-03-13 07:50:07 +00:00
										 |  |  | 	PyObject *mod; | 
					
						
							| 
									
										
										
										
											2009-11-16 19:03:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-08 01:13:19 +00:00
										 |  |  | 	/* Needs to be first since this dir is needed for future modules */ | 
					
						
							|  |  |  | 	char *modpath= BLI_gethome_folder("scripts/modules", BLI_GETHOME_ALL); | 
					
						
							|  |  |  | 	if(modpath) { | 
					
						
							|  |  |  | 		PyObject *sys_path= PySys_GetObject("path"); /* borrow */ | 
					
						
							|  |  |  | 		PyObject *py_modpath= PyUnicode_FromString(modpath); | 
					
						
							|  |  |  | 		PyList_Insert(sys_path, 0, py_modpath); /* add first */ | 
					
						
							|  |  |  | 		Py_DECREF(py_modpath); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-02-16 16:17:20 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-11-16 19:18:04 +00:00
										 |  |  | 	/* stand alone utility modules not related to blender directly */ | 
					
						
							|  |  |  | 	Geometry_Init(); | 
					
						
							|  |  |  | 	Mathutils_Init(); | 
					
						
							|  |  |  | 	BGL_Init(); | 
					
						
							|  |  |  | 	IDProp_Init_Types(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-13 09:28:05 +00:00
										 |  |  | 	mod = PyModule_New("_bpy"); | 
					
						
							| 
									
										
										
										
											2009-11-08 01:13:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* add the module so we can import it */ | 
					
						
							| 
									
										
										
										
											2009-11-13 09:28:05 +00:00
										 |  |  | 	PyDict_SetItemString(PySys_GetObject("modules"), "_bpy", mod); | 
					
						
							| 
									
										
										
										
											2009-11-08 01:13:19 +00:00
										 |  |  | 	Py_DECREF(mod); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* run first, initializes rna types */ | 
					
						
							|  |  |  | 	BPY_rna_init(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	PyModule_AddObject( mod, "types", BPY_rna_types() ); /* needs to be first so bpy_types can run */ | 
					
						
							|  |  |  | 	bpy_import_test("bpy_types"); | 
					
						
							|  |  |  | 	PyModule_AddObject( mod, "data", BPY_rna_module() ); /* imports bpy_types by running this */ | 
					
						
							|  |  |  | 	bpy_import_test("bpy_types"); | 
					
						
							| 
									
										
										
										
											2009-03-13 07:50:07 +00:00
										 |  |  | 	/* PyModule_AddObject( mod, "doc", BPY_rna_doc() ); */ | 
					
						
							| 
									
										
										
										
											2009-06-18 19:51:22 +00:00
										 |  |  | 	PyModule_AddObject( mod, "props", BPY_rna_props() ); | 
					
						
							| 
									
										
										
										
											2009-11-13 09:28:05 +00:00
										 |  |  | 	PyModule_AddObject( mod, "ops", BPY_operator_module() ); /* ops is now a python module that does the conversion from SOME_OT_foo -> some.foo */ | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | 	PyModule_AddObject( mod, "ui", BPY_ui_module() ); // XXX very experimental, consider this a test, especially PyCObject is not meant to be permanent
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-08 01:13:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-05 11:17:09 +00:00
										 |  |  | 	/* bpy context */ | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		bpy_context_module= ( BPy_StructRNA * ) PyObject_NEW( BPy_StructRNA, &pyrna_struct_Type ); | 
					
						
							|  |  |  | 		RNA_pointer_create(NULL, &RNA_Context, NULL, &bpy_context_module->ptr); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		PyModule_AddObject(mod, "context", (PyObject *)bpy_context_module); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-28 10:00:04 +00:00
										 |  |  | 	/* blender info that wont change at runtime, add into _bpy */ | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2010-01-02 23:14:01 +00:00
										 |  |  | 		extern char bprogname[]; /* argv[0] from creator.c */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-28 10:00:04 +00:00
										 |  |  | 		PyObject *mod_dict= PyModule_GetDict(mod); | 
					
						
							|  |  |  | 		char tmpstr[256]; | 
					
						
							|  |  |  | 		PyModule_AddStringConstant(mod, "_HOME",  BLI_gethome()); | 
					
						
							|  |  |  | 		PyDict_SetItemString(mod_dict, "_VERSION", Py_BuildValue("(iii)", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION)); | 
					
						
							|  |  |  | 		sprintf(tmpstr, "%d.%02d (sub %d)", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION); | 
					
						
							|  |  |  | 		PyModule_AddStringConstant(mod, "_VERSION_STR",  tmpstr); | 
					
						
							| 
									
										
										
										
											2010-01-02 23:14:01 +00:00
										 |  |  | 		PyModule_AddStringConstant(mod, "_BINPATH",  bprogname); | 
					
						
							| 
									
										
										
										
											2009-12-28 10:00:04 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-13 09:28:05 +00:00
										 |  |  | 	/* add our own modules dir, this is a python package */ | 
					
						
							|  |  |  | 	bpy_import_test("bpy"); | 
					
						
							| 
									
										
										
										
											2009-04-11 16:17:39 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void BPY_update_modules( void ) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-08-15 09:53:38 +00:00
										 |  |  | #if 0 // slow, this runs all the time poll, draw etc 100's of time a sec.
 | 
					
						
							| 
									
										
										
										
											2009-04-11 16:17:39 +00:00
										 |  |  | 	PyObject *mod= PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL, 0); | 
					
						
							|  |  |  | 	PyModule_AddObject( mod, "data", BPY_rna_module() ); | 
					
						
							| 
									
										
										
										
											2009-08-15 09:53:38 +00:00
										 |  |  | 	PyModule_AddObject( mod, "types", BPY_rna_types() ); // atm this does not need updating
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* refreshes the main struct */ | 
					
						
							|  |  |  | 	BPY_update_rna_module(); | 
					
						
							| 
									
										
										
										
											2009-11-05 11:17:09 +00:00
										 |  |  | 	bpy_context_module->ptr.data= (void *)BPy_GetContext(); | 
					
						
							| 
									
										
										
										
											2009-04-11 16:17:39 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*****************************************************************************
 | 
					
						
							|  |  |  | * Description: This function creates a new Python dictionary object. | 
					
						
							|  |  |  | *****************************************************************************/ | 
					
						
							| 
									
										
										
										
											2009-11-20 16:07:10 +00:00
										 |  |  | static PyObject *CreateGlobalDictionary( bContext *C, const char *filename ) | 
					
						
							| 
									
										
										
										
											2009-04-11 16:17:39 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	PyObject *mod; | 
					
						
							| 
									
										
										
										
											2009-11-20 16:07:10 +00:00
										 |  |  | 	PyObject *item; | 
					
						
							| 
									
										
										
										
											2009-04-11 16:17:39 +00:00
										 |  |  | 	PyObject *dict = PyDict_New(  ); | 
					
						
							|  |  |  | 	PyDict_SetItemString( dict, "__builtins__", PyEval_GetBuiltins(  ) ); | 
					
						
							| 
									
										
										
										
											2009-11-20 16:07:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	item = PyUnicode_FromString( "__main__" ); | 
					
						
							| 
									
										
										
										
											2009-04-11 16:17:39 +00:00
										 |  |  | 	PyDict_SetItemString( dict, "__name__", item ); | 
					
						
							|  |  |  | 	Py_DECREF(item); | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-11-20 16:07:10 +00:00
										 |  |  | 	/* __file__ only for nice UI'ness */ | 
					
						
							|  |  |  | 	if(filename) { | 
					
						
							|  |  |  | 		PyObject *item = PyUnicode_FromString( filename ); | 
					
						
							|  |  |  | 		PyDict_SetItemString( dict, "__file__", item ); | 
					
						
							|  |  |  | 		Py_DECREF(item); | 
					
						
							| 
									
										
										
										
											2009-03-16 15:54:43 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-11-20 16:07:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-11 16:17:39 +00:00
										 |  |  | 	/* add bpy to global namespace */ | 
					
						
							|  |  |  | 	mod= PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL, 0); | 
					
						
							|  |  |  | 	PyDict_SetItemString( dict, "bpy", mod ); | 
					
						
							|  |  |  | 	Py_DECREF(mod); | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2008-11-29 13:36:08 +00:00
										 |  |  | 	return dict; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-03 04:38:55 +00:00
										 |  |  | /* must be called before Py_Initialize */ | 
					
						
							|  |  |  | void BPY_start_python_path(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-09-21 03:16:26 +00:00
										 |  |  | 	char *py_path_bundle= BLI_gethome_folder("python", BLI_GETHOME_ALL); | 
					
						
							| 
									
										
										
										
											2009-07-03 04:38:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if(py_path_bundle==NULL) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* set the environment path */ | 
					
						
							|  |  |  | 	printf("found bundled python: %s\n", py_path_bundle); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-08 12:16:34 +00:00
										 |  |  | #ifdef __APPLE__
 | 
					
						
							|  |  |  | 	/* OSX allow file/directory names to contain : character (represented as / in the Finder)
 | 
					
						
							|  |  |  | 	 but current Python lib (release 3.1.1) doesn't handle these correctly */ | 
					
						
							|  |  |  | 	if(strchr(py_path_bundle, ':')) | 
					
						
							|  |  |  | 		printf("Warning : Blender application is located in a path containing : or / chars\
 | 
					
						
							|  |  |  | 			   \nThis may make python import function fail\n"); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-09-13 18:09:13 +00:00
										 |  |  | #if 0
 | 
					
						
							| 
									
										
										
										
											2009-07-21 09:26:28 +00:00
										 |  |  | 	BLI_setenv("PYTHONHOME", py_path_bundle); | 
					
						
							|  |  |  | 	BLI_setenv("PYTHONPATH", py_path_bundle); | 
					
						
							| 
									
										
										
										
											2009-09-13 18:09:13 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		static wchar_t py_path_bundle_wchar[FILE_MAXDIR]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		mbstowcs(py_path_bundle_wchar, py_path_bundle, FILE_MAXDIR); | 
					
						
							|  |  |  | 		Py_SetPythonHome(py_path_bundle_wchar); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-07-03 04:38:55 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-13 09:28:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | void BPY_set_context(bContext *C) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	BPy_SetContext(C); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* call BPY_set_context first */ | 
					
						
							| 
									
										
										
											
												2.5
More cleanup!
- removed old UI font completely, including from uiBeginBlock
- emboss hints for uiBlock only have three types now;
  Regular, Pulldown, or "Nothing" (only icon/text)
- removed old font path from Userdef
- removed all old button theme hinting
- removed old "auto block" to merge buttons in groups
  (was only in use for radiosity buttons)
And went over all warnings. One hooray for make giving clean output :)
Well, we need uniform definitions for warnings, so people at least fix
them... here's the real bad bugs I found:
- in mesh code, a call to editmesh mixed *em and *me
- in armature, ED_util.h was not included, so no warnings for wrong call
  to ED_undo_push()
- The extern Py api .h was not included in the bpy_interface.c, showing
  a several calls using different args.
Further just added the missing includes, and removed unused vars.
											
										 
											2009-04-14 15:59:52 +00:00
										 |  |  | void BPY_start_python( int argc, char **argv ) | 
					
						
							| 
									
										
										
										
											2008-11-29 13:36:08 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	PyThreadState *py_tstate = NULL; | 
					
						
							| 
									
										
										
										
											2009-04-11 16:17:39 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-07-03 04:38:55 +00:00
										 |  |  | 	BPY_start_python_path(); /* allow to use our own included python */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-29 13:36:08 +00:00
										 |  |  | 	Py_Initialize(  ); | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-08-10 00:07:34 +00:00
										 |  |  | 	// PySys_SetArgv( argc, argv); // broken in py3, not a huge deal
 | 
					
						
							| 
									
										
										
										
											2009-07-08 09:23:49 +00:00
										 |  |  | 	/* sigh, why do python guys not have a char** version anymore? :( */ | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		int i; | 
					
						
							| 
									
										
										
										
											2009-12-07 10:41:16 +00:00
										 |  |  | #if 0
 | 
					
						
							| 
									
										
										
										
											2009-07-08 09:23:49 +00:00
										 |  |  | 		PyObject *py_argv= PyList_New(argc); | 
					
						
							|  |  |  | 		for (i=0; i<argc; i++) | 
					
						
							|  |  |  | 			PyList_SET_ITEM(py_argv, i, PyUnicode_FromString(argv[i])); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-07 10:41:16 +00:00
										 |  |  | #else	// should fix bug #20021 - utf path name problems
 | 
					
						
							|  |  |  | 		PyObject *py_argv= PyList_New(0); | 
					
						
							|  |  |  | 		for (i=0; i<argc; i++) { | 
					
						
							|  |  |  | 			PyObject *item= PyUnicode_Decode(argv[i], strlen(argv[i]), Py_FileSystemDefaultEncoding, NULL); | 
					
						
							|  |  |  | 			if(item==NULL) { // should never happen
 | 
					
						
							|  |  |  | 				PyErr_Print(); | 
					
						
							|  |  |  | 				PyErr_Clear(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			else { | 
					
						
							|  |  |  | 				PyList_Append(py_argv, item); | 
					
						
							|  |  |  | 				Py_DECREF(item); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2009-07-08 09:23:49 +00:00
										 |  |  | 		PySys_SetObject("argv", py_argv); | 
					
						
							|  |  |  | 		Py_DECREF(py_argv); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-11-29 13:36:08 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	/* Initialize thread support (also acquires lock) */ | 
					
						
							|  |  |  | 	PyEval_InitThreads(); | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-04-11 16:17:39 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	/* bpy.* and lets us import it */ | 
					
						
							|  |  |  | 	bpy_init_modules();  | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	{ /* our own import and reload functions */ | 
					
						
							|  |  |  | 		PyObject *item; | 
					
						
							|  |  |  | 		//PyObject *m = PyImport_AddModule("__builtin__");
 | 
					
						
							|  |  |  | 		//PyObject *d = PyModule_GetDict(m);
 | 
					
						
							|  |  |  | 		PyObject *d = PyEval_GetBuiltins(  ); | 
					
						
							|  |  |  | 		PyDict_SetItemString(d, "reload",		item=PyCFunction_New(bpy_reload_meth, NULL));	Py_DECREF(item); | 
					
						
							|  |  |  | 		PyDict_SetItemString(d, "__import__",	item=PyCFunction_New(bpy_import_meth, NULL));	Py_DECREF(item); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-11-29 13:36:08 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-08-15 09:53:38 +00:00
										 |  |  | 	pyrna_alloc_types(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-29 13:36:08 +00:00
										 |  |  | 	py_tstate = PyGILState_GetThisThreadState(); | 
					
						
							|  |  |  | 	PyEval_ReleaseThread(py_tstate); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-27 04:55:45 +00:00
										 |  |  | void BPY_end_python( void ) | 
					
						
							| 
									
										
										
										
											2008-11-29 13:36:08 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-08-14 13:13:36 +00:00
										 |  |  | 	// fprintf(stderr, "Ending Python!\n");
 | 
					
						
							| 
									
										
										
										
											2009-08-14 12:29:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-29 13:36:08 +00:00
										 |  |  | 	PyGILState_Ensure(); /* finalizing, no need to grab the state */ | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	// free other python data.
 | 
					
						
							| 
									
										
										
										
											2009-08-14 12:29:55 +00:00
										 |  |  | 	pyrna_free_types(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* clear all python data from structs */ | 
					
						
							| 
									
										
										
										
											2008-11-29 13:36:08 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	Py_Finalize(  ); | 
					
						
							| 
									
										
										
										
											2009-03-11 17:28:37 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-08-09 13:20:12 +00:00
										 |  |  | #ifdef TIME_PY_RUN
 | 
					
						
							|  |  |  | 	// measure time since py started
 | 
					
						
							|  |  |  | 	bpy_timer = PIL_check_seconds_timer() - bpy_timer; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	printf("*bpy stats* - "); | 
					
						
							|  |  |  | 	printf("tot exec: %d,  ", bpy_timer_count); | 
					
						
							|  |  |  | 	printf("tot run: %.4fsec,  ", bpy_timer_run_tot); | 
					
						
							|  |  |  | 	if(bpy_timer_count>0) | 
					
						
							|  |  |  | 		printf("average run: %.6fsec,  ", (bpy_timer_run_tot/bpy_timer_count)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(bpy_timer>0.0) | 
					
						
							|  |  |  | 		printf("tot usage %.4f%%", (bpy_timer_run_tot/bpy_timer)*100.0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	printf("\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-14 13:13:36 +00:00
										 |  |  | 	// fprintf(stderr, "Ending Python Done!\n");
 | 
					
						
							| 
									
										
										
										
											2009-08-14 12:29:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-09 13:20:12 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-29 13:36:08 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-04 13:26:33 +00:00
										 |  |  | /* Can run a file or text block */ | 
					
						
							| 
									
										
										
										
											2009-06-14 12:53:47 +00:00
										 |  |  | int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struct ReportList *reports) | 
					
						
							| 
									
										
										
										
											2008-11-29 13:36:08 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-08-07 16:20:19 +00:00
										 |  |  | 	PyObject *py_dict, *py_result= NULL; | 
					
						
							| 
									
										
										
										
											2008-11-29 13:36:08 +00:00
										 |  |  | 	PyGILState_STATE gilstate; | 
					
						
							| 
									
										
										
										
											2009-03-04 13:26:33 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	if (fn==NULL && text==NULL) { | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-11-29 13:36:08 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-08-07 16:20:19 +00:00
										 |  |  | 	bpy_context_set(C, &gilstate); | 
					
						
							| 
									
										
										
										
											2009-04-16 01:42:53 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-11-20 16:07:10 +00:00
										 |  |  | 	py_dict = CreateGlobalDictionary(C, text?text->id.name+2:fn); | 
					
						
							| 
									
										
										
										
											2009-03-04 13:26:33 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (text) { | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		if( !text->compiled ) {	/* if it wasn't already compiled, do it now */ | 
					
						
							|  |  |  | 			char *buf = txt_to_buf( text ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			text->compiled = | 
					
						
							|  |  |  | 				Py_CompileString( buf, text->id.name+2, Py_file_input ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			MEM_freeN( buf ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if( PyErr_Occurred(  ) ) { | 
					
						
							|  |  |  | 				BPY_free_compiled_text( text ); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-08-07 16:20:19 +00:00
										 |  |  | 		if(text->compiled) | 
					
						
							|  |  |  | 			py_result =  PyEval_EvalCode( text->compiled, py_dict, py_dict ); | 
					
						
							| 
									
										
										
										
											2009-03-04 13:26:33 +00:00
										 |  |  | 		 | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2009-07-26 18:18:14 +00:00
										 |  |  | 		FILE *fp= fopen(fn, "r");		 | 
					
						
							|  |  |  | 		if(fp) { | 
					
						
							| 
									
										
										
										
											2009-11-11 13:27:54 +00:00
										 |  |  | #ifdef _WIN32
 | 
					
						
							|  |  |  | 			/* 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 */ | 
					
						
							|  |  |  | 			char *pystring; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			fclose(fp); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			pystring= malloc(strlen(fn) + 32); | 
					
						
							|  |  |  | 			pystring[0]= '\0'; | 
					
						
							|  |  |  | 			sprintf(pystring, "exec(open(r'%s').read())", fn); | 
					
						
							|  |  |  | 			py_result = PyRun_String( pystring, Py_file_input, py_dict, py_dict ); | 
					
						
							|  |  |  | 			free(pystring); | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2009-07-26 18:18:14 +00:00
										 |  |  | 			py_result = PyRun_File(fp, fn, Py_file_input, py_dict, py_dict); | 
					
						
							|  |  |  | 			fclose(fp); | 
					
						
							| 
									
										
										
										
											2009-11-11 13:27:54 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2009-07-26 18:18:14 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							|  |  |  | 			PyErr_Format(PyExc_SystemError, "Python file \"%s\" could not be opened: %s", fn, strerror(errno)); | 
					
						
							|  |  |  | 			py_result= NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-03-04 13:26:33 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-11-29 13:36:08 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-03-04 13:26:33 +00:00
										 |  |  | 	if (!py_result) { | 
					
						
							| 
									
										
										
										
											2009-06-14 12:53:47 +00:00
										 |  |  | 		BPy_errors_to_report(reports); | 
					
						
							| 
									
										
										
										
											2009-03-04 13:26:33 +00:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2008-11-29 13:36:08 +00:00
										 |  |  | 		Py_DECREF( py_result ); | 
					
						
							| 
									
										
										
										
											2009-03-04 13:26:33 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-04-23 09:15:42 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	Py_DECREF(py_dict); | 
					
						
							| 
									
										
										
										
											2008-11-29 13:36:08 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-08-07 16:20:19 +00:00
										 |  |  | 	bpy_context_clear(C, &gilstate); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-04 13:26:33 +00:00
										 |  |  | 	return py_result ? 1:0; | 
					
						
							| 
									
										
										
										
											2008-11-29 13:36:08 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-02-28 13:27:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* TODO - move into bpy_space.c ? */ | 
					
						
							|  |  |  | /* GUI interface routines */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Copied from Draw.c */ | 
					
						
							|  |  |  | static void exit_pydraw( SpaceScript * sc, short err ) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	Script *script = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if( !sc || !sc->script ) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	script = sc->script; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if( err ) { | 
					
						
							| 
									
										
										
										
											2009-06-14 12:53:47 +00:00
										 |  |  | 		BPy_errors_to_report(NULL); // TODO, reports
 | 
					
						
							| 
									
										
										
										
											2009-02-28 13:27:45 +00:00
										 |  |  | 		script->flags = 0;	/* mark script struct for deletion */ | 
					
						
							|  |  |  | 		SCRIPT_SET_NULL(script); | 
					
						
							|  |  |  | 		script->scriptname[0] = '\0'; | 
					
						
							|  |  |  | 		script->scriptarg[0] = '\0'; | 
					
						
							|  |  |  | // XXX 2.5		error_pyscript();
 | 
					
						
							|  |  |  | // XXX 2.5		scrarea_queue_redraw( sc->area );
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if 0 // XXX 2.5
 | 
					
						
							|  |  |  | 	BPy_Set_DrawButtonsList(sc->but_refs); | 
					
						
							|  |  |  | 	BPy_Free_DrawButtonsList(); /*clear all temp button references*/ | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	sc->but_refs = NULL; | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	Py_XDECREF( ( PyObject * ) script->py_draw ); | 
					
						
							|  |  |  | 	Py_XDECREF( ( PyObject * ) script->py_event ); | 
					
						
							|  |  |  | 	Py_XDECREF( ( PyObject * ) script->py_button ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	script->py_draw = script->py_event = script->py_button = NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int bpy_run_script_init(bContext *C, SpaceScript * sc) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (sc->script==NULL)  | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	if (sc->script->py_draw==NULL && sc->script->scriptname[0] != '\0') | 
					
						
							| 
									
										
										
										
											2009-06-14 12:53:47 +00:00
										 |  |  | 		BPY_run_python_script(C, sc->script->scriptname, NULL, NULL); | 
					
						
							| 
									
										
										
										
											2009-02-28 13:27:45 +00:00
										 |  |  | 		 | 
					
						
							|  |  |  | 	if (sc->script->py_draw==NULL) | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-09 15:40:04 +00:00
										 |  |  | int BPY_run_script_space_draw(const struct bContext *C, SpaceScript * sc) | 
					
						
							| 
									
										
										
										
											2009-02-28 13:27:45 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-07-09 15:40:04 +00:00
										 |  |  | 	if (bpy_run_script_init( (bContext *)C, sc)) { | 
					
						
							| 
									
										
										
										
											2009-02-28 13:27:45 +00:00
										 |  |  | 		PyGILState_STATE gilstate = PyGILState_Ensure(); | 
					
						
							|  |  |  | 		PyObject *result = PyObject_CallObject( sc->script->py_draw, NULL ); | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		if (result==NULL) | 
					
						
							|  |  |  | 			exit_pydraw(sc, 1); | 
					
						
							| 
									
										
										
										
											2009-03-02 20:18:29 +00:00
										 |  |  | 			 | 
					
						
							|  |  |  | 		PyGILState_Release(gilstate); | 
					
						
							| 
									
										
										
										
											2009-02-28 13:27:45 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // XXX - not used yet, listeners dont get a context
 | 
					
						
							|  |  |  | int BPY_run_script_space_listener(bContext *C, SpaceScript * sc) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (bpy_run_script_init(C, sc)) { | 
					
						
							|  |  |  | 		PyGILState_STATE gilstate = PyGILState_Ensure(); | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		PyObject *result = PyObject_CallObject( sc->script->py_draw, NULL ); | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		if (result==NULL) | 
					
						
							|  |  |  | 			exit_pydraw(sc, 1); | 
					
						
							| 
									
										
										
										
											2009-03-02 20:18:29 +00:00
										 |  |  | 			 | 
					
						
							|  |  |  | 		PyGILState_Release(gilstate); | 
					
						
							| 
									
										
										
										
											2009-02-28 13:27:45 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-01 12:43:07 +00:00
										 |  |  | void BPY_DECREF(void *pyob_ptr) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-12-08 10:36:46 +00:00
										 |  |  | 	PyGILState_STATE gilstate = PyGILState_Ensure(); | 
					
						
							| 
									
										
										
										
											2009-04-01 12:43:07 +00:00
										 |  |  | 	Py_DECREF((PyObject *)pyob_ptr); | 
					
						
							| 
									
										
										
										
											2009-12-08 10:36:46 +00:00
										 |  |  | 	PyGILState_Release(gilstate); | 
					
						
							| 
									
										
										
										
											2009-04-01 12:43:07 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 13:27:45 +00:00
										 |  |  | #if 0
 | 
					
						
							|  |  |  | /* called from the the scripts window, assume context is ok */ | 
					
						
							|  |  |  | int BPY_run_python_script_space(const char *modulename, const char *func) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	PyObject *py_dict, *py_result= NULL; | 
					
						
							|  |  |  | 	char pystring[512]; | 
					
						
							|  |  |  | 	PyGILState_STATE gilstate; | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	/* for calling the module function */ | 
					
						
							|  |  |  | 	PyObject *py_func,  | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	gilstate = PyGILState_Ensure(); | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	py_dict = CreateGlobalDictionary(C); | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	PyObject *module = PyImport_ImportModule(scpt->script.filename); | 
					
						
							|  |  |  | 	if (module==NULL) { | 
					
						
							|  |  |  | 		PyErr_SetFormat(PyExc_SystemError, "could not import '%s'", scpt->script.filename); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		py_func = PyObject_GetAttrString(modulename, func); | 
					
						
							|  |  |  | 		if (py_func==NULL) { | 
					
						
							|  |  |  | 			PyErr_SetFormat(PyExc_SystemError, "module has no function '%s.%s'\n", scpt->script.filename, func); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							| 
									
										
										
										
											2009-03-18 22:22:58 +00:00
										 |  |  | 			Py_DECREF(py_func); | 
					
						
							| 
									
										
										
										
											2009-02-28 13:27:45 +00:00
										 |  |  | 			if (!PyCallable_Check(py_func)) { | 
					
						
							|  |  |  | 				PyErr_SetFormat(PyExc_SystemError, "module item is not callable '%s.%s'\n", scpt->script.filename, func); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			else { | 
					
						
							|  |  |  | 				py_result= PyObject_CallObject(py_func, NULL); // XXX will need args eventually
 | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-04-23 09:15:42 +00:00
										 |  |  | 	if (!py_result) { | 
					
						
							| 
									
										
										
										
											2009-06-14 12:53:47 +00:00
										 |  |  | 		BPy_errors_to_report(NULL); // TODO - reports
 | 
					
						
							| 
									
										
										
										
											2009-04-23 09:15:42 +00:00
										 |  |  | 	} else | 
					
						
							| 
									
										
										
										
											2009-02-28 13:27:45 +00:00
										 |  |  | 		Py_DECREF( py_result ); | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	Py_XDECREF(module); | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-04-23 09:15:42 +00:00
										 |  |  | 	Py_DECREF(py_dict); | 
					
						
							| 
									
										
										
										
											2009-02-28 13:27:45 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	PyGILState_Release(gilstate); | 
					
						
							|  |  |  | 	return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2009-04-11 02:18:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-11 16:17:39 +00:00
										 |  |  | // #define TIME_REGISTRATION
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef TIME_REGISTRATION
 | 
					
						
							|  |  |  | #include "PIL_time.h"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-10 11:58:53 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | int BPY_button_eval(bContext *C, char *expr, double *value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	PyGILState_STATE gilstate; | 
					
						
							| 
									
										
										
										
											2009-10-22 23:23:09 +00:00
										 |  |  | 	PyObject *dict, *mod, *retval; | 
					
						
							| 
									
										
										
										
											2009-08-10 11:58:53 +00:00
										 |  |  | 	int error_ret = 0; | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	if (!value || !expr || expr[0]=='\0') return -1; | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	bpy_context_set(C, &gilstate); | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-11-20 16:07:10 +00:00
										 |  |  | 	dict= CreateGlobalDictionary(C, NULL); | 
					
						
							| 
									
										
										
										
											2009-10-22 23:23:09 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	/* import some modules: builtins,math*/ | 
					
						
							|  |  |  | 	PyDict_SetItemString(dict, "__builtins__", PyEval_GetBuiltins()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	mod = PyImport_ImportModule("math"); | 
					
						
							|  |  |  | 	if (mod) { | 
					
						
							|  |  |  | 		PyDict_Merge(dict, PyModule_GetDict(mod), 0); /* 0 - dont overwrite existing values */ | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		/* Only keep for backwards compat! - just import all math into root, they are standard */ | 
					
						
							|  |  |  | 		PyDict_SetItemString(dict, "math", mod); | 
					
						
							|  |  |  | 		PyDict_SetItemString(dict, "m", mod); | 
					
						
							|  |  |  | 		Py_DECREF(mod); | 
					
						
							|  |  |  | 	}  | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-08-10 11:58:53 +00:00
										 |  |  | 	retval = PyRun_String(expr, Py_eval_input, dict, dict); | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	if (retval == NULL) { | 
					
						
							|  |  |  | 		error_ret= -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							| 
									
										
										
											
												user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
											
										 
											2009-08-11 18:53:01 +00:00
										 |  |  | 		double val; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if(PyTuple_Check(retval)) { | 
					
						
							|  |  |  | 			/* Users my have typed in 10km, 2m
 | 
					
						
							|  |  |  | 			 * add up all values */ | 
					
						
							|  |  |  | 			int i; | 
					
						
							|  |  |  | 			val= 0.0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			for(i=0; i<PyTuple_GET_SIZE(retval); i++) { | 
					
						
							|  |  |  | 				val+= PyFloat_AsDouble(PyTuple_GET_ITEM(retval, i)); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							|  |  |  | 			val = PyFloat_AsDouble(retval); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-08-10 11:58:53 +00:00
										 |  |  | 		Py_DECREF(retval); | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		if(val==-1 && PyErr_Occurred()) { | 
					
						
							|  |  |  | 			error_ret= -1; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							|  |  |  | 			*value= val; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	if(error_ret) { | 
					
						
							|  |  |  | 		BPy_errors_to_report(CTX_wm_reports(C)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	Py_DECREF(dict); | 
					
						
							|  |  |  | 	bpy_context_clear(C, &gilstate); | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	return error_ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-20 15:01:09 +00:00
										 |  |  | void BPY_load_user_modules(bContext *C) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	PyGILState_STATE gilstate; | 
					
						
							|  |  |  | 	Text *text; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bpy_context_set(C, &gilstate); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for(text=CTX_data_main(C)->text.first; text; text= text->id.next) { | 
					
						
							|  |  |  | 		if(text->flags & TXT_ISSCRIPT && BLI_testextensie(text->id.name+2, ".py")) { | 
					
						
							|  |  |  | 			PyObject *module= bpy_text_import(text); | 
					
						
							| 
									
										
										
										
											2009-10-29 09:25:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-20 15:01:09 +00:00
										 |  |  | 			if (module==NULL) { | 
					
						
							|  |  |  | 				PyErr_Print(); | 
					
						
							|  |  |  | 				PyErr_Clear(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			else { | 
					
						
							|  |  |  | 				Py_DECREF(module); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	bpy_context_clear(C, &gilstate); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-10-29 09:25:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-10 16:18:54 +00:00
										 |  |  | int BPY_context_get(bContext *C, const char *member, bContextDataResult *result) | 
					
						
							| 
									
										
										
										
											2009-10-29 09:25:11 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	PyObject *pyctx= (PyObject *)CTX_py_dict_get(C); | 
					
						
							|  |  |  | 	PyObject *item= PyDict_GetItemString(pyctx, member); | 
					
						
							|  |  |  | 	PointerRNA *ptr= NULL; | 
					
						
							|  |  |  | 	int done= 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(item==NULL) { | 
					
						
							|  |  |  | 		/* pass */ | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else if(item==Py_None) { | 
					
						
							|  |  |  | 		/* pass */ | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else if(BPy_StructRNA_Check(item)) { | 
					
						
							|  |  |  | 		ptr= &(((BPy_StructRNA *)item)->ptr); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		//result->ptr= ((BPy_StructRNA *)item)->ptr;
 | 
					
						
							|  |  |  | 		CTX_data_pointer_set(result, ptr->id.data, ptr->type, ptr->data); | 
					
						
							|  |  |  | 		done= 1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-10-29 10:03:34 +00:00
										 |  |  | 	else if (PySequence_Check(item)) { | 
					
						
							|  |  |  | 		PyObject *seq_fast= PySequence_Fast(item, "bpy_context_get sequence conversion"); | 
					
						
							|  |  |  | 		if (seq_fast==NULL) { | 
					
						
							|  |  |  | 			PyErr_Print(); | 
					
						
							|  |  |  | 			PyErr_Clear(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							|  |  |  | 			int len= PySequence_Fast_GET_SIZE(seq_fast); | 
					
						
							|  |  |  | 			int i; | 
					
						
							|  |  |  | 			for(i = 0; i < len; i++) { | 
					
						
							|  |  |  | 				PyObject *list_item= PySequence_Fast_GET_ITEM(seq_fast, i); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if(BPy_StructRNA_Check(list_item)) { | 
					
						
							|  |  |  | 					/*
 | 
					
						
							|  |  |  | 					CollectionPointerLink *link= MEM_callocN(sizeof(CollectionPointerLink), "bpy_context_get"); | 
					
						
							|  |  |  | 					link->ptr= ((BPy_StructRNA *)item)->ptr; | 
					
						
							|  |  |  | 					BLI_addtail(&result->list, link); | 
					
						
							|  |  |  | 					*/ | 
					
						
							|  |  |  | 					ptr= &(((BPy_StructRNA *)list_item)->ptr); | 
					
						
							|  |  |  | 					CTX_data_list_add(result, ptr->id.data, ptr->type, ptr->data); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				else { | 
					
						
							|  |  |  | 					printf("List item not a valid type\n"); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-29 09:25:11 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2009-10-29 10:03:34 +00:00
										 |  |  | 			Py_DECREF(seq_fast); | 
					
						
							| 
									
										
										
										
											2009-10-29 09:25:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-29 10:03:34 +00:00
										 |  |  | 			done= 1; | 
					
						
							| 
									
										
										
										
											2009-10-29 09:25:11 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(done==0) { | 
					
						
							| 
									
										
										
										
											2009-11-10 19:57:04 +00:00
										 |  |  | 		if (item)	printf("Context '%s' not a valid type\n", member); | 
					
						
							|  |  |  | 		else		printf("Context '%s' not found\n", member); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else if (G.f & G_DEBUG) { | 
					
						
							|  |  |  | 		printf("Context '%s' found\n", member); | 
					
						
							| 
									
										
										
										
											2009-10-29 09:25:11 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return done; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 |