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 /* This file defines the '_bpy' module which is used by python's 'bpy' package.
26 * a script writer should never directly access this module */
32 #include "bpy_props.h"
33 #include "bpy_operator.h"
35 #include "BLI_path_util.h"
36 #include "BLI_bpath.h"
38 /* external util modules */
39 #include "../generic/geometry.h"
40 #include "../generic/bgl.h"
41 #include "../generic/blf_api.h"
42 #include "../generic/IDProp.h"
44 static char bpy_home_paths_doc[] =
45 ".. function:: home_paths(subfolder)\n"
47 " Return 3 paths to blender home directories.\n"
49 " :arg subfolder: The name of a subfolder to find within the blenders home directory.\n"
50 " :type subfolder: string\n"
51 " :return: (system, local, user) strings will be empty when not found.\n"
52 " :rtype: tuple of strigs\n";
54 PyObject *bpy_home_paths(PyObject *self, PyObject *args)
56 PyObject *ret= PyTuple_New(3);
60 if (!PyArg_ParseTuple(args, "|s:blender_homes", &subfolder))
63 path= BLI_gethome_folder(subfolder, BLI_GETHOME_SYSTEM);
64 PyTuple_SET_ITEM(ret, 0, PyUnicode_FromString(path?path:""));
66 path= BLI_gethome_folder(subfolder, BLI_GETHOME_LOCAL);
67 PyTuple_SET_ITEM(ret, 1, PyUnicode_FromString(path?path:""));
69 path= BLI_gethome_folder(subfolder, BLI_GETHOME_USER);
70 PyTuple_SET_ITEM(ret, 2, PyUnicode_FromString(path?path:""));
75 static char bpy_blend_paths_doc[] =
76 ".. function:: blend_paths(absolute=False)\n"
78 " Returns a list of paths associated with this blend file.\n"
80 " :arg absolute: When true the paths returned are made absolute.\n"
81 " :type absolute: boolean\n"
82 " :return: path list.\n"
83 " :rtype: list of strigs\n";
84 static PyObject *bpy_blend_paths(PyObject * self, PyObject *args, PyObject *kw)
86 struct BPathIterator bpi;
87 PyObject *list = PyList_New(0), *st; /* stupidly big string to be safe */
88 /* be sure there is low chance of the path being too short */
89 char filepath_expanded[1024];
93 static char *kwlist[] = {"absolute", NULL};
95 if (!PyArg_ParseTupleAndKeywords(args, kw, "|i:blend_paths", kwlist, &absolute))
98 for(BLI_bpathIterator_init(&bpi, NULL); !BLI_bpathIterator_isDone(&bpi); BLI_bpathIterator_step(&bpi)) {
101 BLI_bpathIterator_getPathExpanded(&bpi, filepath_expanded);
104 lib = BLI_bpathIterator_getLib(&bpi);
105 if (lib && (strcmp(lib, bpi.base_path))) { /* relative path to the library is NOT the same as our blendfile path, return an absolute path */
106 BLI_bpathIterator_getPathExpanded(&bpi, filepath_expanded);
109 BLI_bpathIterator_getPath(&bpi, filepath_expanded);
112 st = PyUnicode_FromString(filepath_expanded);
114 PyList_Append(list, st);
118 BLI_bpathIterator_free(&bpi);
123 static PyMethodDef meth_bpy_home_paths[] = {{ "home_paths", (PyCFunction)bpy_home_paths, METH_VARARGS, bpy_home_paths_doc}};
124 static PyMethodDef meth_bpy_blend_paths[] = {{ "blend_paths", (PyCFunction)bpy_blend_paths, METH_VARARGS|METH_KEYWORDS, bpy_blend_paths_doc}};
126 static void bpy_import_test(char *modname)
128 PyObject *mod= PyImport_ImportModuleLevel(modname, NULL, NULL, NULL, 0);
138 /*****************************************************************************
139 * Description: Creates the bpy module and adds it to sys.modules for importing
140 *****************************************************************************/
141 void BPy_init_modules( void )
143 extern BPy_StructRNA *bpy_context_module;
147 /* Needs to be first since this dir is needed for future modules */
148 char *modpath= BLI_get_folder(BLENDER_SCRIPTS, "modules");
150 // printf("bpy: found module path '%s'.\n", modpath);
151 PyObject *sys_path= PySys_GetObject("path"); /* borrow */
152 PyObject *py_modpath= PyUnicode_FromString(modpath);
153 PyList_Insert(sys_path, 0, py_modpath); /* add first */
154 Py_DECREF(py_modpath);
157 printf("bpy: couldnt find 'scripts/modules', blender probably wont start.\n");
159 /* stand alone utility modules not related to blender directly */
167 mod = PyModule_New("_bpy");
169 /* add the module so we can import it */
170 PyDict_SetItemString(PySys_GetObject("modules"), "_bpy", mod);
173 /* run first, initializes rna types */
176 PyModule_AddObject( mod, "types", BPY_rna_types() ); /* needs to be first so bpy_types can run */
177 bpy_import_test("bpy_types");
178 PyModule_AddObject( mod, "data", BPY_rna_module() ); /* imports bpy_types by running this */
179 bpy_import_test("bpy_types");
180 PyModule_AddObject( mod, "props", BPY_rna_props() );
181 PyModule_AddObject( mod, "ops", BPY_operator_module() ); /* ops is now a python module that does the conversion from SOME_OT_foo -> some.foo */
182 PyModule_AddObject( mod, "app", BPY_app_struct() );
185 RNA_pointer_create(NULL, &RNA_Context, BPy_GetContext(), &ctx_ptr);
186 bpy_context_module= (BPy_StructRNA *)pyrna_struct_CreatePyObject(&ctx_ptr);
187 /* odd that this is needed, 1 ref on creation and another for the module
188 * but without we get a crash on exit */
189 Py_INCREF(bpy_context_module);
191 PyModule_AddObject(mod, "context", (PyObject *)bpy_context_module);
193 /* utility func's that have nowhere else to go */
194 PyModule_AddObject(mod, meth_bpy_home_paths->ml_name, (PyObject *)PyCFunction_New(meth_bpy_home_paths, NULL));
195 PyModule_AddObject(mod, meth_bpy_blend_paths->ml_name, (PyObject *)PyCFunction_New(meth_bpy_blend_paths, NULL));
197 /* add our own modules dir, this is a python package */
198 bpy_import_test("bpy");