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 #include "BPy_Freestyle.h"
45 static char bpy_home_paths_doc[] =
46 ".. function:: home_paths(subfolder)\n"
48 " return 3 paths to blender home directories.\n"
50 " :arg subfolder: The name of a subfolder to find within the blenders home directory.\n"
51 " :type subfolder: string\n"
52 " :return: (system, local, user) strings will be empty when not found.\n"
53 " :rtype: tuple of strigs\n";
55 PyObject *bpy_home_paths(PyObject *self, PyObject *args)
57 PyObject *ret= PyTuple_New(3);
61 if (!PyArg_ParseTuple(args, "|s:blender_homes", &subfolder))
64 path= BLI_gethome_folder(subfolder, BLI_GETHOME_SYSTEM);
65 PyTuple_SET_ITEM(ret, 0, PyUnicode_FromString(path?path:""));
67 path= BLI_gethome_folder(subfolder, BLI_GETHOME_LOCAL);
68 PyTuple_SET_ITEM(ret, 1, PyUnicode_FromString(path?path:""));
70 path= BLI_gethome_folder(subfolder, BLI_GETHOME_USER);
71 PyTuple_SET_ITEM(ret, 2, PyUnicode_FromString(path?path:""));
76 static PyMethodDef meth_bpy_home_paths[] = {{ "home_paths", (PyCFunction)bpy_home_paths, METH_VARARGS, bpy_home_paths_doc}};
78 static void bpy_import_test(char *modname)
80 PyObject *mod= PyImport_ImportModuleLevel(modname, NULL, NULL, NULL, 0);
90 /*****************************************************************************
91 * Description: Creates the bpy module and adds it to sys.modules for importing
92 *****************************************************************************/
93 void BPy_init_modules( void )
95 extern BPy_StructRNA *bpy_context_module;
98 /* Needs to be first since this dir is needed for future modules */
99 char *modpath= BLI_gethome_folder("scripts/modules", BLI_GETHOME_ALL);
101 PyObject *sys_path= PySys_GetObject("path"); /* borrow */
102 PyObject *py_modpath= PyUnicode_FromString(modpath);
103 PyList_Insert(sys_path, 0, py_modpath); /* add first */
104 Py_DECREF(py_modpath);
107 /* stand alone utility modules not related to blender directly */
115 mod = PyModule_New("_bpy");
117 /* add the module so we can import it */
118 PyDict_SetItemString(PySys_GetObject("modules"), "_bpy", mod);
121 /* run first, initializes rna types */
124 PyModule_AddObject( mod, "types", BPY_rna_types() ); /* needs to be first so bpy_types can run */
125 bpy_import_test("bpy_types");
126 PyModule_AddObject( mod, "data", BPY_rna_module() ); /* imports bpy_types by running this */
127 bpy_import_test("bpy_types");
128 PyModule_AddObject( mod, "props", BPY_rna_props() );
129 PyModule_AddObject( mod, "ops", BPY_operator_module() ); /* ops is now a python module that does the conversion from SOME_OT_foo -> some.foo */
130 PyModule_AddObject( mod, "app", BPY_app_struct() );
133 bpy_context_module= ( BPy_StructRNA * ) PyObject_NEW( BPy_StructRNA, &pyrna_struct_Type );
134 RNA_pointer_create(NULL, &RNA_Context, NULL, &bpy_context_module->ptr);
135 bpy_context_module->freeptr= 0;
136 PyModule_AddObject(mod, "context", (PyObject *)bpy_context_module);
138 /* utility func's that have nowhere else to go */
139 PyModule_AddObject(mod, meth_bpy_home_paths->ml_name, (PyObject *)PyCFunction_New(meth_bpy_home_paths, NULL));
141 /* add our own modules dir, this is a python package */
142 bpy_import_test("bpy");