4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * Contributor(s): Campbell Barton
22 * ***** END GPL LICENSE BLOCK *****
25 /** \file blender/python/intern/bpy.c
26 * \ingroup pythonintern
30 /* This file defines the '_bpy' module which is used by python's 'bpy' package.
31 * a script writer should never directly access this module */
33 #define WITH_PYTHON /* for AUD_PyInit.h, possibly others */
41 #include "bpy_props.h"
42 #include "bpy_operator.h"
44 #include "BLI_path_util.h"
45 #include "BLI_string.h"
46 #include "BLI_bpath.h"
47 #include "BLI_utildefines.h"
50 #include "BKE_global.h" /* XXX, G.main only */
51 #include "BKE_blender.h"
53 #include "RNA_access.h"
55 #include "MEM_guardedalloc.h"
57 /* external util modules */
58 #include "../generic/mathutils.h"
59 #include "../generic/bgl.h"
60 #include "../generic/blf_py_api.h"
61 #include "../generic/IDProp.h"
63 #include "AUD_PyInit.h"
65 PyObject *bpy_package_py= NULL;
67 PyDoc_STRVAR(bpy_script_paths_doc,
68 ".. function:: script_paths()\n"
70 " Return 2 paths to blender scripts directories.\n"
72 " :return: (system, user) strings will be empty when not found.\n"
73 " :rtype: tuple of strings\n"
75 static PyObject *bpy_script_paths(PyObject *UNUSED(self))
77 PyObject *ret= PyTuple_New(2);
80 path= BLI_get_folder(BLENDER_SYSTEM_SCRIPTS, NULL);
81 PyTuple_SET_ITEM(ret, 0, PyUnicode_FromString(path?path:""));
82 path= BLI_get_folder(BLENDER_USER_SCRIPTS, NULL);
83 PyTuple_SET_ITEM(ret, 1, PyUnicode_FromString(path?path:""));
88 PyDoc_STRVAR(bpy_blend_paths_doc,
89 ".. function:: blend_paths(absolute=False)\n"
91 " Returns a list of paths to external files referenced by the loaded .blend file.\n"
93 " :arg absolute: When true the paths returned are made absolute.\n"
94 " :type absolute: boolean\n"
95 " :return: path list.\n"
96 " :rtype: list of strings\n"
98 static PyObject *bpy_blend_paths(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
100 struct BPathIterator *bpi;
101 PyObject *list, *st; /* stupidly big string to be safe */
102 /* be sure there is low chance of the path being too short */
103 char filepath_expanded[1024];
107 static const char *kwlist[]= {"absolute", NULL};
109 if (!PyArg_ParseTupleAndKeywords(args, kw, "|i:blend_paths", (char **)kwlist, &absolute))
114 for(BLI_bpathIterator_init(&bpi, G.main, G.main->name, 0); !BLI_bpathIterator_isDone(bpi); BLI_bpathIterator_step(bpi)) {
117 BLI_bpathIterator_getPathExpanded(bpi, filepath_expanded);
120 lib= BLI_bpathIterator_getLib(bpi);
121 if (lib && (BLI_path_cmp(lib, BLI_bpathIterator_getBasePath(bpi)))) { /* relative path to the library is NOT the same as our blendfile path, return an absolute path */
122 BLI_bpathIterator_getPathExpanded(bpi, filepath_expanded);
125 BLI_bpathIterator_getPath(bpi, filepath_expanded);
128 st= PyUnicode_DecodeFSDefault(filepath_expanded);
130 PyList_Append(list, st);
134 BLI_bpathIterator_free(bpi);
140 // PyDoc_STRVAR(bpy_user_resource_doc[]= // now in bpy/utils.py
141 static PyObject *bpy_user_resource(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
146 static const char *kwlist[]= {"type", "subdir", NULL};
150 if (!PyArg_ParseTupleAndKeywords(args, kw, "s|s:user_resource", (char **)kwlist, &type, &subdir))
153 /* stupid string compare */
154 if (!strcmp(type, "DATAFILES")) folder_id= BLENDER_USER_DATAFILES;
155 else if(!strcmp(type, "CONFIG")) folder_id= BLENDER_USER_CONFIG;
156 else if(!strcmp(type, "SCRIPTS")) folder_id= BLENDER_USER_SCRIPTS;
157 else if(!strcmp(type, "AUTOSAVE")) folder_id= BLENDER_USER_AUTOSAVE;
159 PyErr_SetString(PyExc_ValueError, "invalid resource argument");
163 /* same logic as BLI_get_folder_create(), but best leave it up to the script author to create */
164 path= BLI_get_folder(folder_id, subdir);
167 path= BLI_get_user_folder_notest(folder_id, subdir);
169 return PyUnicode_DecodeFSDefault(path ? path : "");
172 PyDoc_STRVAR(bpy_resource_path_doc,
173 ".. function:: resource_path(type, major=2, minor=57)\n"
175 " Return the base path for storing system files.\n"
177 " :arg type: string in ['USER', 'LOCAL', 'SYSTEM'].\n"
178 " :type type: string\n"
179 " :arg major: major version, defaults to current.\n"
180 " :type major: int\n"
181 " :arg minor: minor version, defaults to current.\n"
182 " :type minor: string\n"
183 " :return: the resource path (not necessarily existing).\n"
186 static PyObject *bpy_resource_path(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
189 int major= BLENDER_VERSION/100, minor= BLENDER_VERSION%100;
190 static const char *kwlist[]= {"type", "major", "minor", NULL};
194 if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ii:resource_path", (char **)kwlist, &type, &major, &minor))
197 /* stupid string compare */
198 if (!strcmp(type, "USER")) folder_id= BLENDER_RESOURCE_PATH_USER;
199 else if(!strcmp(type, "LOCAL")) folder_id= BLENDER_RESOURCE_PATH_LOCAL;
200 else if(!strcmp(type, "SYSTEM")) folder_id= BLENDER_RESOURCE_PATH_SYSTEM;
202 PyErr_SetString(PyExc_ValueError, "invalid resource argument");
206 path= BLI_get_folder_version(folder_id, (major * 100) + minor, FALSE);
208 return PyUnicode_DecodeFSDefault(path);
211 static PyMethodDef meth_bpy_script_paths= {"script_paths", (PyCFunction)bpy_script_paths, METH_NOARGS, bpy_script_paths_doc};
212 static PyMethodDef meth_bpy_blend_paths= {"blend_paths", (PyCFunction)bpy_blend_paths, METH_VARARGS|METH_KEYWORDS, bpy_blend_paths_doc};
213 static PyMethodDef meth_bpy_user_resource= {"user_resource", (PyCFunction)bpy_user_resource, METH_VARARGS|METH_KEYWORDS, NULL};
214 static PyMethodDef meth_bpy_resource_path= {"resource_path", (PyCFunction)bpy_resource_path, METH_VARARGS|METH_KEYWORDS, bpy_resource_path_doc};
217 static PyObject *bpy_import_test(const char *modname)
219 PyObject *mod= PyImport_ImportModuleLevel((char *)modname, NULL, NULL, NULL, 0);
231 /*****************************************************************************
232 * Description: Creates the bpy module and adds it to sys.modules for importing
233 *****************************************************************************/
234 void BPy_init_modules( void )
236 extern BPy_StructRNA *bpy_context_module;
237 extern int bpy_lib_init(PyObject *);
241 /* Needs to be first since this dir is needed for future modules */
242 char *modpath= BLI_get_folder(BLENDER_SCRIPTS, "modules");
244 // printf("bpy: found module path '%s'.\n", modpath);
245 PyObject *sys_path= PySys_GetObject("path"); /* borrow */
246 PyObject *py_modpath= PyUnicode_FromString(modpath);
247 PyList_Insert(sys_path, 0, py_modpath); /* add first */
248 Py_DECREF(py_modpath);
251 printf("bpy: couldnt find 'scripts/modules', blender probably wont start.\n");
253 /* stand alone utility modules not related to blender directly */
254 IDProp_Init_Types(); /* not actually a submodule, just types */
256 mod= PyModule_New("_bpy");
258 /* add the module so we can import it */
259 PyDict_SetItemString(PyImport_GetModuleDict(), "_bpy", mod);
262 /* run first, initializes rna types */
265 PyModule_AddObject( mod, "types", BPY_rna_types() ); /* needs to be first so bpy_types can run */
266 PyModule_AddObject(mod, "StructMetaPropGroup", (PyObject *)&pyrna_struct_meta_idprop_Type); /* metaclass for idprop types, bpy_types.py needs access */
268 bpy_lib_init(mod); /* adds '_bpy._library_load', must be called before 'bpy_types' which uses it */
270 bpy_import_test("bpy_types");
271 PyModule_AddObject( mod, "data", BPY_rna_module() ); /* imports bpy_types by running this */
272 bpy_import_test("bpy_types");
273 PyModule_AddObject( mod, "props", BPY_rna_props() );
274 PyModule_AddObject( mod, "ops", BPY_operator_module() ); /* ops is now a python module that does the conversion from SOME_OT_foo -> some.foo */
275 PyModule_AddObject( mod, "app", BPY_app_struct() );
278 RNA_pointer_create(NULL, &RNA_Context, (void *)BPy_GetContext(), &ctx_ptr);
279 bpy_context_module= (BPy_StructRNA *)pyrna_struct_CreatePyObject(&ctx_ptr);
280 /* odd that this is needed, 1 ref on creation and another for the module
281 * but without we get a crash on exit */
282 Py_INCREF(bpy_context_module);
284 PyModule_AddObject(mod, "context", (PyObject *)bpy_context_module);
286 /* utility func's that have nowhere else to go */
287 PyModule_AddObject(mod, meth_bpy_script_paths.ml_name, (PyObject *)PyCFunction_New(&meth_bpy_script_paths, NULL));
288 PyModule_AddObject(mod, meth_bpy_blend_paths.ml_name, (PyObject *)PyCFunction_New(&meth_bpy_blend_paths, NULL));
289 PyModule_AddObject(mod, meth_bpy_user_resource.ml_name, (PyObject *)PyCFunction_New(&meth_bpy_user_resource, NULL));
290 PyModule_AddObject(mod, meth_bpy_resource_path.ml_name, (PyObject *)PyCFunction_New(&meth_bpy_resource_path, NULL));
292 /* register funcs (bpy_rna.c) */
293 PyModule_AddObject(mod, meth_bpy_register_class.ml_name, (PyObject *)PyCFunction_New(&meth_bpy_register_class, NULL));
294 PyModule_AddObject(mod, meth_bpy_unregister_class.ml_name, (PyObject *)PyCFunction_New(&meth_bpy_unregister_class, NULL));
296 /* add our own modules dir, this is a python package */
297 bpy_package_py= bpy_import_test("bpy");