| 
									
										
										
										
											2011-02-23 10:52:22 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											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 ***** | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2011-02-27 20:10:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** \file blender/python/intern/bpy.c
 | 
					
						
							|  |  |  |  *  \ingroup pythonintern | 
					
						
							| 
									
										
										
										
											2011-11-05 08:21:12 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This file defines the '_bpy' module which is used by python's 'bpy' package | 
					
						
							|  |  |  |  * to access C defined builtin functions. | 
					
						
							|  |  |  |  * A script writer should never directly access this module. | 
					
						
							| 
									
										
										
										
											2011-02-27 20:10:08 +00:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2010-03-03 13:59:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-14 04:15:25 +00:00
										 |  |  | #include <Python.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-04 18:36:37 +00:00
										 |  |  | #include "BLI_utildefines.h"
 | 
					
						
							| 
									
										
										
										
											2013-01-07 05:26:12 +00:00
										 |  |  | #include "BLI_string.h"
 | 
					
						
							| 
									
										
										
										
											2013-03-07 02:44:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-23 14:37:13 +01:00
										 |  |  | #include "BKE_appdir.h"
 | 
					
						
							| 
									
										
										
										
											2013-03-07 02:44:55 +00:00
										 |  |  | #include "BKE_main.h"
 | 
					
						
							|  |  |  | #include "BKE_global.h" /* XXX, G.main only */
 | 
					
						
							|  |  |  | #include "BKE_blender.h"
 | 
					
						
							| 
									
										
										
										
											2013-01-07 05:26:12 +00:00
										 |  |  | #include "BKE_bpath.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-07 02:44:55 +00:00
										 |  |  | #include "RNA_types.h"
 | 
					
						
							|  |  |  | #include "RNA_access.h"
 | 
					
						
							| 
									
										
										
										
											2013-01-07 05:26:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "bpy.h"
 | 
					
						
							|  |  |  | #include "bpy_util.h"
 | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | #include "bpy_rna.h"
 | 
					
						
							|  |  |  | #include "bpy_app.h"
 | 
					
						
							|  |  |  | #include "bpy_props.h"
 | 
					
						
							| 
									
										
										
										
											2012-09-15 01:52:28 +00:00
										 |  |  | #include "bpy_library.h"
 | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | #include "bpy_operator.h"
 | 
					
						
							| 
									
										
										
										
											2014-06-17 16:03:40 +02:00
										 |  |  | #include "bpy_utils_units.h"
 | 
					
						
							| 
									
										
										
										
											2010-05-11 07:08:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-11 17:53:42 +10:00
										 |  |  | #include "../generic/py_capi_utils.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-05 23:14:48 +00:00
										 |  |  | #include "MEM_guardedalloc.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-26 20:41:54 +00:00
										 |  |  | /* external util modules */ | 
					
						
							| 
									
										
										
										
											2011-11-15 09:28:15 +00:00
										 |  |  | #include "../generic/idprop_py_api.h"
 | 
					
						
							| 
									
										
										
										
											2010-02-28 14:57:26 +00:00
										 |  |  | #include "../generic/bgl.h"
 | 
					
						
							| 
									
										
										
										
											2010-12-09 17:31:42 +00:00
										 |  |  | #include "../generic/blf_py_api.h"
 | 
					
						
							| 
									
										
										
										
											2011-07-15 04:01:47 +00:00
										 |  |  | #include "../mathutils/mathutils.h"
 | 
					
						
							| 
									
										
										
										
											2010-02-13 01:13:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-20 07:57:26 +00:00
										 |  |  | #ifdef WITH_FREESTYLE
 | 
					
						
							|  |  |  | #  include "BPy_Freestyle.h"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2010-02-16 22:34:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | PyObject *bpy_package_py = NULL; | 
					
						
							| 
									
										
										
										
											2011-02-20 23:39:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | PyDoc_STRVAR(bpy_script_paths_doc, | 
					
						
							| 
									
										
										
										
											2010-07-15 20:02:53 +00:00
										 |  |  | ".. 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" | 
					
						
							| 
									
										
										
										
											2011-04-11 13:56:58 +00:00
										 |  |  | "   :rtype: tuple of strings\n" | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2011-02-13 10:52:18 +00:00
										 |  |  | static PyObject *bpy_script_paths(PyObject *UNUSED(self)) | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 	PyObject *ret = PyTuple_New(2); | 
					
						
							| 
									
										
										
										
											2012-03-21 22:29:49 +00:00
										 |  |  | 	PyObject *item; | 
					
						
							| 
									
										
										
										
											2013-03-05 03:17:46 +00:00
										 |  |  | 	const char *path; | 
					
						
							| 
									
										
										
										
											2011-04-21 13:11:51 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-23 15:54:29 +01:00
										 |  |  | 	path = BKE_appdir_folder_id(BLENDER_SYSTEM_SCRIPTS, NULL); | 
					
						
							| 
									
										
										
										
											2014-08-11 17:53:42 +10:00
										 |  |  | 	item = PyC_UnicodeFromByte(path ? path : ""); | 
					
						
							| 
									
										
										
										
											2012-03-21 22:29:49 +00:00
										 |  |  | 	BLI_assert(item != NULL); | 
					
						
							|  |  |  | 	PyTuple_SET_ITEM(ret, 0, item); | 
					
						
							| 
									
										
										
										
											2014-11-23 15:54:29 +01:00
										 |  |  | 	path = BKE_appdir_folder_id(BLENDER_USER_SCRIPTS, NULL); | 
					
						
							| 
									
										
										
										
											2014-08-11 17:53:42 +10:00
										 |  |  | 	item = PyC_UnicodeFromByte(path ? path : ""); | 
					
						
							| 
									
										
										
										
											2012-03-21 22:29:49 +00:00
										 |  |  | 	BLI_assert(item != NULL); | 
					
						
							|  |  |  | 	PyTuple_SET_ITEM(ret, 1, item); | 
					
						
							| 
									
										
										
										
											2011-04-21 13:11:51 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-22 09:30:00 +00:00
										 |  |  | 	return ret; | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-24 03:06:32 +00:00
										 |  |  | static bool bpy_blend_paths_visit_cb(void *userdata, char *UNUSED(path_dst), const char *path_src) | 
					
						
							| 
									
										
										
										
											2011-10-27 01:25:07 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 	PyObject *list = (PyObject *)userdata; | 
					
						
							| 
									
										
										
										
											2014-08-11 17:53:42 +10:00
										 |  |  | 	PyObject *item = PyC_UnicodeFromByte(path_src); | 
					
						
							| 
									
										
										
										
											2011-10-27 01:25:07 +00:00
										 |  |  | 	PyList_Append(list, item); | 
					
						
							|  |  |  | 	Py_DECREF(item); | 
					
						
							| 
									
										
										
										
											2013-01-07 05:26:12 +00:00
										 |  |  | 	return false; /* never edits the path */ | 
					
						
							| 
									
										
										
										
											2011-10-27 01:25:07 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | PyDoc_STRVAR(bpy_blend_paths_doc, | 
					
						
							| 
									
										
										
										
											2011-10-27 03:40:12 +00:00
										 |  |  | ".. function:: blend_paths(absolute=False, packed=False, local=False)\n" | 
					
						
							| 
									
										
										
										
											2010-05-11 07:08:32 +00:00
										 |  |  | "\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" | 
					
						
							| 
									
										
										
										
											2011-10-27 03:40:12 +00:00
										 |  |  | "   :arg packed: When true skip file paths for packed data.\n" | 
					
						
							|  |  |  | "   :type packed: boolean\n" | 
					
						
							|  |  |  | "   :arg local: When true skip linked library paths.\n" | 
					
						
							|  |  |  | "   :type local: boolean\n" | 
					
						
							| 
									
										
										
										
											2010-05-11 07:08:32 +00:00
										 |  |  | "   :return: path list.\n" | 
					
						
							| 
									
										
										
										
											2011-04-11 13:56:58 +00:00
										 |  |  | "   :rtype: list of strings\n" | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2010-10-13 23:25:08 +00:00
										 |  |  | static PyObject *bpy_blend_paths(PyObject *UNUSED(self), PyObject *args, PyObject *kw) | 
					
						
							| 
									
										
										
										
											2010-05-11 07:08:32 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 	int flag = 0; | 
					
						
							| 
									
										
										
										
											2011-10-27 01:25:07 +00:00
										 |  |  | 	PyObject *list; | 
					
						
							| 
									
										
										
										
											2010-05-11 07:08:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-07 05:26:12 +00:00
										 |  |  | 	int absolute = false; | 
					
						
							|  |  |  | 	int packed   = false; | 
					
						
							|  |  |  | 	int local    = false; | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 	static const char *kwlist[] = {"absolute", "packed", "local", NULL}; | 
					
						
							| 
									
										
										
										
											2010-05-11 07:08:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-20 10:14:11 +00:00
										 |  |  | 	if (!PyArg_ParseTupleAndKeywords(args, kw, "|iii:blend_paths", | 
					
						
							|  |  |  | 	                                 (char **)kwlist, &absolute, &packed, &local)) | 
					
						
							| 
									
										
										
										
											2011-10-27 03:40:12 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2010-05-11 07:08:32 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-15 15:31:50 +00:00
										 |  |  | 	if (absolute) flag |= BKE_BPATH_TRAVERSE_ABS; | 
					
						
							|  |  |  | 	if (!packed)  flag |= BKE_BPATH_TRAVERSE_SKIP_PACKED; | 
					
						
							|  |  |  | 	if (local)    flag |= BKE_BPATH_TRAVERSE_SKIP_LIBRARY; | 
					
						
							| 
									
										
										
										
											2010-12-05 23:14:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 	list = PyList_New(0); | 
					
						
							| 
									
										
										
										
											2010-05-11 07:08:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-15 15:31:50 +00:00
										 |  |  | 	BKE_bpath_traverse_main(G.main, bpy_blend_paths_visit_cb, flag, (void *)list); | 
					
						
							| 
									
										
										
										
											2010-05-11 07:08:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return list; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-03 20:00:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | // PyDoc_STRVAR(bpy_user_resource_doc[] = // now in bpy/utils.py
 | 
					
						
							| 
									
										
										
										
											2010-10-13 23:25:08 +00:00
										 |  |  | static PyObject *bpy_user_resource(PyObject *UNUSED(self), PyObject *args, PyObject *kw) | 
					
						
							| 
									
										
										
										
											2010-10-03 20:00:22 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-04-27 00:21:23 +10:00
										 |  |  | 	const char *type; | 
					
						
							|  |  |  | 	const char *subdir = NULL; | 
					
						
							| 
									
										
										
										
											2010-10-03 20:00:22 +00:00
										 |  |  | 	int folder_id; | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 	static const char *kwlist[] = {"type", "subdir", NULL}; | 
					
						
							| 
									
										
										
										
											2010-10-03 20:00:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-05 03:17:46 +00:00
										 |  |  | 	const char *path; | 
					
						
							| 
									
										
										
										
											2010-10-03 20:00:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-03 17:05:21 +00:00
										 |  |  | 	if (!PyArg_ParseTupleAndKeywords(args, kw, "s|s:user_resource", (char **)kwlist, &type, &subdir)) | 
					
						
							| 
									
										
										
										
											2010-10-03 20:00:22 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	/* stupid string compare */ | 
					
						
							| 
									
										
										
										
											2013-03-10 06:18:03 +00:00
										 |  |  | 	if      (STREQ(type, "DATAFILES")) folder_id = BLENDER_USER_DATAFILES; | 
					
						
							|  |  |  | 	else if (STREQ(type, "CONFIG"))    folder_id = BLENDER_USER_CONFIG; | 
					
						
							|  |  |  | 	else if (STREQ(type, "SCRIPTS"))   folder_id = BLENDER_USER_SCRIPTS; | 
					
						
							|  |  |  | 	else if (STREQ(type, "AUTOSAVE"))  folder_id = BLENDER_USER_AUTOSAVE; | 
					
						
							| 
									
										
										
										
											2010-10-03 20:00:22 +00:00
										 |  |  | 	else { | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_ValueError, "invalid resource argument"); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2014-11-23 15:54:29 +01:00
										 |  |  | 	/* same logic as BKE_appdir_folder_id_create(), but best leave it up to the script author to create */ | 
					
						
							|  |  |  | 	path = BKE_appdir_folder_id(folder_id, subdir); | 
					
						
							| 
									
										
										
										
											2010-10-03 20:00:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!path) | 
					
						
							| 
									
										
										
										
											2014-11-23 15:54:29 +01:00
										 |  |  | 		path = BKE_appdir_folder_id_user_notest(folder_id, subdir); | 
					
						
							| 
									
										
										
										
											2010-10-03 20:00:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-11 17:53:42 +10:00
										 |  |  | 	return PyC_UnicodeFromByte(path ? path : ""); | 
					
						
							| 
									
										
										
										
											2010-10-03 20:00:22 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | PyDoc_STRVAR(bpy_resource_path_doc, | 
					
						
							| 
									
										
										
										
											2012-09-29 11:51:18 +00:00
										 |  |  | ".. function:: resource_path(type, major=bpy.app.version[0], minor=bpy.app.version[1])\n" | 
					
						
							| 
									
										
										
										
											2011-04-11 13:56:58 +00:00
										 |  |  | "\n" | 
					
						
							|  |  |  | "   Return the base path for storing system files.\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "   :arg type: string in ['USER', 'LOCAL', 'SYSTEM'].\n" | 
					
						
							|  |  |  | "   :type type: string\n" | 
					
						
							|  |  |  | "   :arg major: major version, defaults to current.\n" | 
					
						
							|  |  |  | "   :type major: int\n" | 
					
						
							|  |  |  | "   :arg minor: minor version, defaults to current.\n" | 
					
						
							|  |  |  | "   :type minor: string\n" | 
					
						
							|  |  |  | "   :return: the resource path (not necessarily existing).\n" | 
					
						
							|  |  |  | "   :rtype: string\n" | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2011-04-11 13:56:58 +00:00
										 |  |  | static PyObject *bpy_resource_path(PyObject *UNUSED(self), PyObject *args, PyObject *kw) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-04-27 00:21:23 +10:00
										 |  |  | 	const char *type; | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 	int major = BLENDER_VERSION / 100, minor = BLENDER_VERSION % 100; | 
					
						
							|  |  |  | 	static const char *kwlist[] = {"type", "major", "minor", NULL}; | 
					
						
							| 
									
										
										
										
											2011-04-11 13:56:58 +00:00
										 |  |  | 	int folder_id; | 
					
						
							| 
									
										
										
										
											2013-03-05 03:17:46 +00:00
										 |  |  | 	const char *path; | 
					
						
							| 
									
										
										
										
											2011-04-11 13:56:58 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ii:resource_path", (char **)kwlist, &type, &major, &minor)) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* stupid string compare */ | 
					
						
							| 
									
										
										
										
											2013-03-10 06:18:03 +00:00
										 |  |  | 	if      (STREQ(type, "USER"))    folder_id = BLENDER_RESOURCE_PATH_USER; | 
					
						
							|  |  |  | 	else if (STREQ(type, "LOCAL"))   folder_id = BLENDER_RESOURCE_PATH_LOCAL; | 
					
						
							|  |  |  | 	else if (STREQ(type, "SYSTEM"))  folder_id = BLENDER_RESOURCE_PATH_SYSTEM; | 
					
						
							| 
									
										
										
										
											2011-04-11 13:56:58 +00:00
										 |  |  | 	else { | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_ValueError, "invalid resource argument"); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-23 15:54:29 +01:00
										 |  |  | 	path = BKE_appdir_folder_id_version(folder_id, (major * 100) + minor, false); | 
					
						
							| 
									
										
										
										
											2011-04-11 13:56:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-11 17:53:42 +10:00
										 |  |  | 	return PyC_UnicodeFromByte(path ? path : ""); | 
					
						
							| 
									
										
										
										
											2011-04-11 13:56:58 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-25 16:18:10 +11:00
										 |  |  | PyDoc_STRVAR(bpy_escape_identifier_doc, | 
					
						
							|  |  |  | ".. function:: escape_identifier(string)\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "   Simple string escaping function used for animation paths.\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "   :arg string: text\n" | 
					
						
							|  |  |  | "   :type string: string\n" | 
					
						
							|  |  |  | "   :return: The escaped string.\n" | 
					
						
							|  |  |  | "   :rtype: string\n" | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | static PyObject *bpy_escape_identifier(PyObject *UNUSED(self), PyObject *value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	const char *value_str; | 
					
						
							|  |  |  | 	Py_ssize_t  value_str_len; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	char       *value_escape_str; | 
					
						
							|  |  |  | 	Py_ssize_t  value_escape_str_len; | 
					
						
							|  |  |  | 	PyObject   *value_escape; | 
					
						
							|  |  |  | 	size_t size; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	value_str = _PyUnicode_AsStringAndSize(value, &value_str_len); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (value_str == NULL) { | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_TypeError, "expected a string"); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	size = (value_str_len * 2) + 1; | 
					
						
							|  |  |  | 	value_escape_str = PyMem_MALLOC(size); | 
					
						
							|  |  |  | 	value_escape_str_len = BLI_strescape(value_escape_str, value_str, size); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (value_escape_str_len == value_str_len) { | 
					
						
							|  |  |  | 		Py_INCREF(value); | 
					
						
							|  |  |  | 		value_escape = value; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		value_escape = PyUnicode_FromStringAndSize(value_escape_str, value_escape_str_len); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	PyMem_FREE(value_escape_str); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return value_escape; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | static PyMethodDef meth_bpy_script_paths = | 
					
						
							| 
									
										
										
										
											2011-11-26 15:18:30 +00:00
										 |  |  | 	{"script_paths", (PyCFunction)bpy_script_paths, METH_NOARGS, bpy_script_paths_doc}; | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | static PyMethodDef meth_bpy_blend_paths = | 
					
						
							| 
									
										
										
										
											2012-03-26 20:41:54 +00:00
										 |  |  | 	{"blend_paths", (PyCFunction)bpy_blend_paths, METH_VARARGS | METH_KEYWORDS, bpy_blend_paths_doc}; | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | static PyMethodDef meth_bpy_user_resource = | 
					
						
							| 
									
										
										
										
											2012-03-26 20:41:54 +00:00
										 |  |  | 	{"user_resource", (PyCFunction)bpy_user_resource, METH_VARARGS | METH_KEYWORDS, NULL}; | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | static PyMethodDef meth_bpy_resource_path = | 
					
						
							| 
									
										
										
										
											2012-03-26 20:41:54 +00:00
										 |  |  | 	{"resource_path", (PyCFunction)bpy_resource_path, METH_VARARGS | METH_KEYWORDS, bpy_resource_path_doc}; | 
					
						
							| 
									
										
										
										
											2014-02-25 16:18:10 +11:00
										 |  |  | static PyMethodDef meth_bpy_escape_identifier = | 
					
						
							|  |  |  | 	{"escape_identifier", (PyCFunction)bpy_escape_identifier, METH_O, bpy_escape_identifier_doc}; | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-20 23:39:29 +00:00
										 |  |  | static PyObject *bpy_import_test(const char *modname) | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-03-17 18:30:31 +00:00
										 |  |  | 	PyObject *mod = PyImport_ImportModuleLevel(modname, NULL, NULL, NULL, 0); | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (mod) { | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | 		Py_DECREF(mod); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		PyErr_Print(); | 
					
						
							|  |  |  | 		PyErr_Clear(); | 
					
						
							| 
									
										
										
										
											2012-12-11 22:00:22 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-02-20 23:39:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return mod; | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-20 13:29:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-03 20:36:09 +00:00
										 |  |  | /******************************************************************************
 | 
					
						
							|  |  |  |  * Description: Creates the bpy module and adds it to sys.modules for importing | 
					
						
							|  |  |  |  ******************************************************************************/ | 
					
						
							| 
									
										
										
										
											2011-06-02 08:29:16 +00:00
										 |  |  | void BPy_init_modules(void) | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											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 */ | 
					
						
							| 
									
										
										
										
											2014-11-23 15:54:29 +01:00
										 |  |  | 	const char * const modpath = BKE_appdir_folder_id(BLENDER_SYSTEM_SCRIPTS, "modules"); | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (modpath) { | 
					
						
							| 
									
										
										
										
											2010-04-18 14:47:45 +00:00
										 |  |  | 		// printf("bpy: found module path '%s'.\n", modpath);
 | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 		PyObject *sys_path = PySys_GetObject("path"); /* borrow */ | 
					
						
							|  |  |  | 		PyObject *py_modpath = PyUnicode_FromString(modpath); | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | 		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 */ | 
					
						
							| 
									
										
										
										
											2010-10-29 22:59:39 +00:00
										 |  |  | 	IDProp_Init_Types(); /* not actually a submodule, just types */ | 
					
						
							| 
									
										
										
										
											2012-12-20 07:57:26 +00:00
										 |  |  | #ifdef WITH_FREESTYLE
 | 
					
						
							| 
									
										
										
										
											2010-02-13 01:13:16 +00:00
										 |  |  | 	Freestyle_Init(); | 
					
						
							| 
									
										
										
										
											2012-12-20 07:57:26 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 	mod = PyModule_New("_bpy"); | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* 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(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-26 15:18:30 +00:00
										 |  |  | 	/* needs to be first so bpy_types can run */ | 
					
						
							|  |  |  | 	PyModule_AddObject(mod, "types", BPY_rna_types()); | 
					
						
							| 
									
										
										
											
												library loading api.
this is not well suited to RNA so this is a native python api.
This uses:
  bpy.data.libraries.load(filepath, link=False, relative=False)
however the return value needs to use pythons context manager, this means the library loading is confined to a block of code and python cant leave a half loaded library state.
eg, load a single scene we know the name of:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = ["Scene"]
eg, load all scenes:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = data_from.scenes
eg, load all objects starting with 'A'
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.objects = [name for name in data_from.objects if name.startswith("A")]
As you can see gives 2 objects like 'bpy.data', but containing lists of strings which can be moved from one into another.
											
										 
											2011-03-12 16:06:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-26 15:18:30 +00:00
										 |  |  | 	/* metaclass for idprop types, bpy_types.py needs access */ | 
					
						
							|  |  |  | 	PyModule_AddObject(mod, "StructMetaPropGroup", (PyObject *)&pyrna_struct_meta_idprop_Type); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* needs to be first so bpy_types can run */ | 
					
						
							| 
									
										
										
										
											2012-09-15 01:52:28 +00:00
										 |  |  | 	BPY_library_module(mod); | 
					
						
							| 
									
										
										
											
												library loading api.
this is not well suited to RNA so this is a native python api.
This uses:
  bpy.data.libraries.load(filepath, link=False, relative=False)
however the return value needs to use pythons context manager, this means the library loading is confined to a block of code and python cant leave a half loaded library state.
eg, load a single scene we know the name of:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = ["Scene"]
eg, load all scenes:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = data_from.scenes
eg, load all objects starting with 'A'
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.objects = [name for name in data_from.objects if name.startswith("A")]
As you can see gives 2 objects like 'bpy.data', but containing lists of strings which can be moved from one into another.
											
										 
											2011-03-12 16:06:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | 	bpy_import_test("bpy_types"); | 
					
						
							| 
									
										
										
										
											2011-06-02 08:29:16 +00:00
										 |  |  | 	PyModule_AddObject(mod, "data", BPY_rna_module()); /* imports bpy_types by running this */ | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | 	bpy_import_test("bpy_types"); | 
					
						
							| 
									
										
										
										
											2012-10-21 05:46:41 +00:00
										 |  |  | 	PyModule_AddObject(mod, "props", BPY_rna_props()); | 
					
						
							| 
									
										
										
										
											2012-03-26 20:41:54 +00:00
										 |  |  | 	/* ops is now a python module that does the conversion from SOME_OT_foo -> some.foo */ | 
					
						
							| 
									
										
										
										
											2011-11-26 15:18:30 +00:00
										 |  |  | 	PyModule_AddObject(mod, "ops", BPY_operator_module()); | 
					
						
							| 
									
										
										
										
											2011-06-02 08:29:16 +00:00
										 |  |  | 	PyModule_AddObject(mod, "app", BPY_app_struct()); | 
					
						
							| 
									
										
										
										
											2014-06-17 16:03:40 +02:00
										 |  |  | 	PyModule_AddObject(mod, "_utils_units", BPY_utils_units()); | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* bpy context */ | 
					
						
							| 
									
										
										
										
											2010-12-07 04:12:15 +00:00
										 |  |  | 	RNA_pointer_create(NULL, &RNA_Context, (void *)BPy_GetContext(), &ctx_ptr); | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 	bpy_context_module = (BPy_StructRNA *)pyrna_struct_CreatePyObject(&ctx_ptr); | 
					
						
							| 
									
										
										
										
											2010-06-09 19:31:10 +00:00
										 |  |  | 	/* 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); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-20 13:29:58 +00:00
										 |  |  | 	/* register bpy/rna classmethod callbacks */ | 
					
						
							|  |  |  | 	BPY_rna_register_cb(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | 	/* utility func's that have nowhere else to go */ | 
					
						
							| 
									
										
										
										
											2010-10-03 20:00:22 +00:00
										 |  |  | 	PyModule_AddObject(mod, meth_bpy_script_paths.ml_name, (PyObject *)PyCFunction_New(&meth_bpy_script_paths, NULL)); | 
					
						
							|  |  |  | 	PyModule_AddObject(mod, meth_bpy_blend_paths.ml_name, (PyObject *)PyCFunction_New(&meth_bpy_blend_paths, NULL)); | 
					
						
							|  |  |  | 	PyModule_AddObject(mod, meth_bpy_user_resource.ml_name, (PyObject *)PyCFunction_New(&meth_bpy_user_resource, NULL)); | 
					
						
							| 
									
										
										
										
											2011-04-11 13:56:58 +00:00
										 |  |  | 	PyModule_AddObject(mod, meth_bpy_resource_path.ml_name, (PyObject *)PyCFunction_New(&meth_bpy_resource_path, NULL)); | 
					
						
							| 
									
										
										
										
											2014-02-25 16:18:10 +11:00
										 |  |  | 	PyModule_AddObject(mod, meth_bpy_escape_identifier.ml_name, (PyObject *)PyCFunction_New(&meth_bpy_escape_identifier, NULL)); | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-11 00:11:17 +00:00
										 |  |  | 	/* register funcs (bpy_rna.c) */ | 
					
						
							|  |  |  | 	PyModule_AddObject(mod, meth_bpy_register_class.ml_name, (PyObject *)PyCFunction_New(&meth_bpy_register_class, NULL)); | 
					
						
							|  |  |  | 	PyModule_AddObject(mod, meth_bpy_unregister_class.ml_name, (PyObject *)PyCFunction_New(&meth_bpy_unregister_class, NULL)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | 	/* add our own modules dir, this is a python package */ | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 	bpy_package_py = bpy_import_test("bpy"); | 
					
						
							| 
									
										
										
										
											2010-02-11 14:08:22 +00:00
										 |  |  | } |