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"
37 /* external util modules */
38 #include "../generic/Mathutils.h"
39 #include "../generic/Geometry.h"
40 #include "../generic/BGL.h"
41 #include "../generic/IDProp.h"
43 static char bpy_home_paths_doc[] =
44 ".. function:: home_paths(subfolder)\n"
46 " return 3 paths to blender home directories.\n"
48 " :arg subfolder: The name of a subfolder to find within the blenders home directory.\n"
49 " :type subfolder: string\n"
50 " :return: (system, local, user) strings will be empty when not found.\n"
51 " :rtype: tuple of strigs\n";
53 PyObject *bpy_home_paths(PyObject *self, PyObject *args)
55 PyObject *ret= PyTuple_New(3);
59 if (!PyArg_ParseTuple(args, "|s:blender_homes", &subfolder))
62 path= BLI_gethome_folder(subfolder, BLI_GETHOME_SYSTEM);
63 PyTuple_SET_ITEM(ret, 0, PyUnicode_FromString(path?path:""));
65 path= BLI_gethome_folder(subfolder, BLI_GETHOME_LOCAL);
66 PyTuple_SET_ITEM(ret, 1, PyUnicode_FromString(path?path:""));
68 path= BLI_gethome_folder(subfolder, BLI_GETHOME_USER);
69 PyTuple_SET_ITEM(ret, 2, PyUnicode_FromString(path?path:""));
74 static PyMethodDef meth_bpy_home_paths[] = {{ "home_paths", (PyCFunction)bpy_home_paths, METH_VARARGS, bpy_home_paths_doc}};
76 static void bpy_import_test(char *modname)
78 PyObject *mod= PyImport_ImportModuleLevel(modname, NULL, NULL, NULL, 0);
88 /*****************************************************************************
89 * Description: Creates the bpy module and adds it to sys.modules for importing
90 *****************************************************************************/
91 void BPy_init_modules( void )
93 extern BPy_StructRNA *bpy_context_module;
96 /* Needs to be first since this dir is needed for future modules */
97 char *modpath= BLI_gethome_folder("scripts/modules", BLI_GETHOME_ALL);
99 PyObject *sys_path= PySys_GetObject("path"); /* borrow */
100 PyObject *py_modpath= PyUnicode_FromString(modpath);
101 PyList_Insert(sys_path, 0, py_modpath); /* add first */
102 Py_DECREF(py_modpath);
105 /* stand alone utility modules not related to blender directly */
112 mod = PyModule_New("_bpy");
114 /* add the module so we can import it */
115 PyDict_SetItemString(PySys_GetObject("modules"), "_bpy", mod);
118 /* run first, initializes rna types */
121 PyModule_AddObject( mod, "types", BPY_rna_types() ); /* needs to be first so bpy_types can run */
122 bpy_import_test("bpy_types");
123 PyModule_AddObject( mod, "data", BPY_rna_module() ); /* imports bpy_types by running this */
124 bpy_import_test("bpy_types");
125 PyModule_AddObject( mod, "props", BPY_rna_props() );
126 PyModule_AddObject( mod, "ops", BPY_operator_module() ); /* ops is now a python module that does the conversion from SOME_OT_foo -> some.foo */
127 PyModule_AddObject( mod, "app", BPY_app_struct() );
130 bpy_context_module= ( BPy_StructRNA * ) PyObject_NEW( BPy_StructRNA, &pyrna_struct_Type );
131 RNA_pointer_create(NULL, &RNA_Context, NULL, &bpy_context_module->ptr);
132 bpy_context_module->freeptr= 0;
133 PyModule_AddObject(mod, "context", (PyObject *)bpy_context_module);
135 /* utility func's that have nowhere else to go */
136 PyModule_AddObject(mod, meth_bpy_home_paths->ml_name, (PyObject *)PyCFunction_New(meth_bpy_home_paths, NULL));
138 /* add our own modules dir, this is a python package */
139 bpy_import_test("bpy");