| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2010-03-21 01:14:04 +00:00
										 |  |  |  * $Id$ | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * ***** 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, | 
					
						
							| 
									
										
										
										
											2010-02-12 13:34:04 +00:00
										 |  |  |  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Contributor(s): Campbell Barton | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ***** END GPL LICENSE BLOCK ***** | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  |   | 
					
						
							|  |  |  | /* This file defines the '_bpy' module which is used by python's 'bpy' package.
 | 
					
						
							|  |  |  |  * a script writer should never directly access this module */ | 
					
						
							|  |  |  |   | 
					
						
							| 
									
										
										
										
											2010-03-03 13:59:57 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "bpy_util.h" 
 | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | #include "bpy_rna.h"
 | 
					
						
							|  |  |  | #include "bpy_app.h"
 | 
					
						
							|  |  |  | #include "bpy_props.h"
 | 
					
						
							|  |  |  | #include "bpy_operator.h"
 | 
					
						
							| 
									
										
										
										
											2010-05-11 07:08:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | #include "BLI_path_util.h"
 | 
					
						
							| 
									
										
										
										
											2010-05-11 07:08:32 +00:00
										 |  |  | #include "BLI_bpath.h"
 | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  |   | 
					
						
							|  |  |  |  /* external util modules */ | 
					
						
							| 
									
										
										
										
											2010-04-11 12:05:27 +00:00
										 |  |  | #include "../generic/geometry.h"
 | 
					
						
							| 
									
										
										
										
											2010-02-28 14:57:26 +00:00
										 |  |  | #include "../generic/bgl.h"
 | 
					
						
							| 
									
										
										
										
											2010-04-11 12:05:27 +00:00
										 |  |  | #include "../generic/blf_api.h"
 | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | #include "../generic/IDProp.h"
 | 
					
						
							| 
									
										
										
										
											2010-02-16 15:01:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-16 20:05:59 +00:00
										 |  |  | #include "AUD_PyInit.h"
 | 
					
						
							| 
									
										
										
										
											2010-07-09 12:35:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-15 20:02:53 +00:00
										 |  |  | static char bpy_script_paths_doc[] = | 
					
						
							|  |  |  | ".. function:: script_paths()\n" | 
					
						
							| 
									
										
										
										
											2010-02-16 15:01:34 +00:00
										 |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2010-07-15 20:02:53 +00:00
										 |  |  | "   Return 2 paths to blender scripts directories.\n" | 
					
						
							| 
									
										
										
										
											2010-02-16 15:01:34 +00:00
										 |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2010-07-15 20:02:53 +00:00
										 |  |  | "   :return: (system, user) strings will be empty when not found.\n" | 
					
						
							| 
									
										
										
										
											2010-02-16 15:01:34 +00:00
										 |  |  | "   :rtype: tuple of strigs\n"; | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-15 20:02:53 +00:00
										 |  |  | PyObject *bpy_script_paths(PyObject *self) | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-07-15 20:02:53 +00:00
										 |  |  | 	PyObject *ret= PyTuple_New(2); | 
					
						
							| 
									
										
										
										
											2010-03-22 09:30:00 +00:00
										 |  |  | 	char *path; | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  |      | 
					
						
							| 
									
										
										
										
											2010-07-15 20:02:53 +00:00
										 |  |  | 	path= BLI_get_folder(BLENDER_USER_SCRIPTS, NULL); | 
					
						
							| 
									
										
										
										
											2010-03-22 09:30:00 +00:00
										 |  |  | 	PyTuple_SET_ITEM(ret, 0, PyUnicode_FromString(path?path:"")); | 
					
						
							| 
									
										
										
										
											2010-07-15 20:02:53 +00:00
										 |  |  | 	path= BLI_get_folder(BLENDER_SYSTEM_SCRIPTS, NULL); | 
					
						
							| 
									
										
										
										
											2010-03-22 09:30:00 +00:00
										 |  |  | 	PyTuple_SET_ITEM(ret, 1, PyUnicode_FromString(path?path:"")); | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  |      | 
					
						
							| 
									
										
										
										
											2010-03-22 09:30:00 +00:00
										 |  |  | 	return ret; | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-11 07:08:32 +00:00
										 |  |  | static char bpy_blend_paths_doc[] = | 
					
						
							|  |  |  | ".. function:: blend_paths(absolute=False)\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2010-08-07 18:34:16 +00:00
										 |  |  | "   Returns a list of paths to external files referenced by the loaded .blend file.\n" | 
					
						
							| 
									
										
										
										
											2010-05-11 07:08:32 +00:00
										 |  |  | "\n" | 
					
						
							|  |  |  | "   :arg absolute: When true the paths returned are made absolute.\n" | 
					
						
							|  |  |  | "   :type absolute: boolean\n" | 
					
						
							|  |  |  | "   :return: path list.\n" | 
					
						
							|  |  |  | "   :rtype: list of strigs\n"; | 
					
						
							|  |  |  | static PyObject *bpy_blend_paths(PyObject * self, PyObject *args, PyObject *kw) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct BPathIterator bpi; | 
					
						
							|  |  |  | 	PyObject *list = PyList_New(0), *st; /* stupidly big string to be safe */ | 
					
						
							|  |  |  | 	/* be sure there is low chance of the path being too short */ | 
					
						
							|  |  |  | 	char filepath_expanded[1024]; | 
					
						
							|  |  |  | 	char *lib; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	int absolute = 0; | 
					
						
							|  |  |  | 	static char *kwlist[] = {"absolute", NULL}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!PyArg_ParseTupleAndKeywords(args, kw, "|i:blend_paths", kwlist, &absolute)) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for(BLI_bpathIterator_init(&bpi, NULL); !BLI_bpathIterator_isDone(&bpi); BLI_bpathIterator_step(&bpi)) { | 
					
						
							|  |  |  | 		/* build the list */ | 
					
						
							|  |  |  | 		if (absolute) { | 
					
						
							|  |  |  | 			BLI_bpathIterator_getPathExpanded(&bpi, filepath_expanded); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							|  |  |  | 			lib = BLI_bpathIterator_getLib(&bpi); | 
					
						
							|  |  |  | 			if (lib && (strcmp(lib, bpi.base_path))) { /* relative path to the library is NOT the same as our blendfile path, return an absolute path */ | 
					
						
							|  |  |  | 				BLI_bpathIterator_getPathExpanded(&bpi, filepath_expanded); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			else { | 
					
						
							|  |  |  | 				BLI_bpathIterator_getPath(&bpi, filepath_expanded); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		st = PyUnicode_FromString(filepath_expanded); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		PyList_Append(list, st); | 
					
						
							|  |  |  | 		Py_DECREF(st); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	BLI_bpathIterator_free(&bpi); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return list; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-15 20:02:53 +00:00
										 |  |  | static PyMethodDef meth_bpy_script_paths[] = {{ "script_paths", (PyCFunction)bpy_script_paths, METH_NOARGS, bpy_script_paths_doc}}; | 
					
						
							| 
									
										
										
										
											2010-05-11 07:08:32 +00:00
										 |  |  | static PyMethodDef meth_bpy_blend_paths[] = {{ "blend_paths", (PyCFunction)bpy_blend_paths, METH_VARARGS|METH_KEYWORDS, bpy_blend_paths_doc}}; | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +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(); | 
					
						
							|  |  |  | 	}	 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*****************************************************************************
 | 
					
						
							|  |  |  | * Description: Creates the bpy module and adds it to sys.modules for importing | 
					
						
							|  |  |  | *****************************************************************************/ | 
					
						
							|  |  |  | void BPy_init_modules( void ) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-03-03 08:56:48 +00:00
										 |  |  | 	extern BPy_StructRNA *bpy_context_module; | 
					
						
							| 
									
										
										
										
											2010-06-09 19:31:10 +00:00
										 |  |  | 	PointerRNA ctx_ptr; | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | 	PyObject *mod; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Needs to be first since this dir is needed for future modules */ | 
					
						
							| 
									
										
										
										
											2010-07-04 15:35:23 +00:00
										 |  |  | 	char *modpath= BLI_get_folder(BLENDER_SCRIPTS, "modules"); | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | 	if(modpath) { | 
					
						
							| 
									
										
										
										
											2010-04-18 14:47:45 +00:00
										 |  |  | 		// printf("bpy: found module path '%s'.\n", modpath);
 | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | 		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); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-04-18 14:47:45 +00:00
										 |  |  | 	else { | 
					
						
							|  |  |  | 		printf("bpy: couldnt find 'scripts/modules', blender probably wont start.\n"); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | 	/* stand alone utility modules not related to blender directly */ | 
					
						
							|  |  |  | 	Geometry_Init(); | 
					
						
							|  |  |  | 	Mathutils_Init(); | 
					
						
							| 
									
										
										
										
											2010-07-20 03:14:21 +00:00
										 |  |  | 	Noise_Init(); | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | 	BGL_Init(); | 
					
						
							| 
									
										
										
										
											2010-02-28 11:18:54 +00:00
										 |  |  | 	BLF_Init(); | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | 	IDProp_Init_Types(); | 
					
						
							| 
									
										
										
										
											2010-07-09 12:35:40 +00:00
										 |  |  | 	AUD_initPython(); | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	mod = PyModule_New("_bpy"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* add the module so we can import it */ | 
					
						
							| 
									
										
										
										
											2010-08-14 05:33:20 +00:00
										 |  |  | 	PyDict_SetItemString(PyImport_GetModuleDict(), "_bpy", mod); | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +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 */ | 
					
						
							| 
									
										
										
										
											2010-09-10 14:54:50 +00:00
										 |  |  | 	PyModule_AddObject(mod, "StructMetaIDProp", (PyObject *)&pyrna_struct_meta_idprop_Type); /* metaclass for idprop types, bpy_types.py needs access */ | 
					
						
							|  |  |  | 			 | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | 	bpy_import_test("bpy_types"); | 
					
						
							|  |  |  | 	PyModule_AddObject( mod, "data", BPY_rna_module() ); /* imports bpy_types by running this */ | 
					
						
							|  |  |  | 	bpy_import_test("bpy_types"); | 
					
						
							|  |  |  | 	PyModule_AddObject( mod, "props", BPY_rna_props() ); | 
					
						
							|  |  |  | 	PyModule_AddObject( mod, "ops", BPY_operator_module() ); /* ops is now a python module that does the conversion from SOME_OT_foo -> some.foo */ | 
					
						
							|  |  |  | 	PyModule_AddObject( mod, "app", BPY_app_struct() ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* bpy context */ | 
					
						
							| 
									
										
										
										
											2010-06-09 19:31:10 +00:00
										 |  |  | 	RNA_pointer_create(NULL, &RNA_Context, BPy_GetContext(), &ctx_ptr); | 
					
						
							|  |  |  | 	bpy_context_module= (BPy_StructRNA *)pyrna_struct_CreatePyObject(&ctx_ptr); | 
					
						
							|  |  |  | 	/* odd that this is needed, 1 ref on creation and another for the module
 | 
					
						
							|  |  |  | 	 * but without we get a crash on exit */ | 
					
						
							|  |  |  | 	Py_INCREF(bpy_context_module); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-03 08:56:48 +00:00
										 |  |  | 	PyModule_AddObject(mod, "context", (PyObject *)bpy_context_module); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | 	/* utility func's that have nowhere else to go */ | 
					
						
							| 
									
										
										
										
											2010-07-15 20:02:53 +00:00
										 |  |  | 	PyModule_AddObject(mod, meth_bpy_script_paths->ml_name, (PyObject *)PyCFunction_New(meth_bpy_script_paths, NULL)); | 
					
						
							| 
									
										
										
										
											2010-05-11 07:08:32 +00:00
										 |  |  | 	PyModule_AddObject(mod, meth_bpy_blend_paths->ml_name, (PyObject *)PyCFunction_New(meth_bpy_blend_paths, NULL)); | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* add our own modules dir, this is a python package */ | 
					
						
							|  |  |  | 	bpy_import_test("bpy"); | 
					
						
							|  |  |  | } |