cc3b88722b6f564d024afc3a23a2e6bebb2553f6
[blender.git] / source / blender / python / intern / bpy_app.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Campbell Barton
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/python/intern/bpy_app.c
24  *  \ingroup pythonintern
25  */
26
27
28 #include <Python.h>
29
30 #include "bpy_app.h"
31 #include "bpy_app_handlers.h"
32 #include "bpy_driver.h"
33
34 #include "BLI_path_util.h"
35 #include "BLI_utildefines.h"
36
37
38 #include "BKE_blender.h"
39 #include "BKE_global.h"
40 #include "structseq.h"
41
42 #include "../generic/py_capi_utils.h"
43
44 #ifdef BUILD_DATE
45 extern char build_date[];
46 extern char build_time[];
47 extern char build_rev[];
48 extern char build_platform[];
49 extern char build_type[];
50 extern char build_cflags[];
51 extern char build_cxxflags[];
52 extern char build_linkflags[];
53 extern char build_system[];
54 #endif
55
56 static PyTypeObject BlenderAppType;
57
58 static PyStructSequence_Field app_info_fields[]= {
59         {(char *)"version", (char *)"The Blender version as a tuple of 3 numbers. eg. (2, 50, 11)"},
60         {(char *)"version_string", (char *)"The Blender version formatted as a string"},
61         {(char *)"version_char", (char *)"The Blender version character (for minor releases)"},
62         {(char *)"version_cycle", (char *)"The release status of this build alpha/beta/rc/release"},
63         {(char *)"binary_path", (char *)"The location of blenders executable, useful for utilities that spawn new instances"},
64         {(char *)"background", (char *)"Boolean, True when blender is running without a user interface (started with -b)"},
65
66         /* buildinfo */
67         {(char *)"build_date", (char *)"The date this blender instance was built"},
68         {(char *)"build_time", (char *)"The time this blender instance was built"},
69         {(char *)"build_revision", (char *)"The subversion revision this blender instance was built with"},
70         {(char *)"build_platform", (char *)"The platform this blender instance was built for"},
71         {(char *)"build_type", (char *)"The type of build (Release, Debug)"},
72         {(char *)"build_cflags", (char *)"C compiler flags"},
73         {(char *)"build_cxxflags", (char *)"C++ compiler flags"},
74         {(char *)"build_linkflags", (char *)"Binary linking flags"},
75         {(char *)"build_system", (char *)"Build system used"},
76
77         /* submodules */
78         {(char *)"handlers", (char *)"Application handler callbacks"},
79         {NULL}
80 };
81
82 static PyStructSequence_Desc app_info_desc= {
83         (char *)"bpy.app",     /* name */
84         (char *)"This module contains application values that remain unchanged during runtime.",    /* doc */
85         app_info_fields,    /* fields */
86         (sizeof(app_info_fields)/sizeof(PyStructSequence_Field)) - 1
87 };
88
89 #define DO_EXPAND(VAL)  VAL ## 1
90 #define EXPAND(VAL)     DO_EXPAND(VAL)
91
92 static PyObject *make_app_info(void)
93 {
94         PyObject *app_info;
95         int pos= 0;
96
97         app_info= PyStructSequence_New(&BlenderAppType);
98         if (app_info == NULL) {
99                 return NULL;
100         }
101
102 #define SetIntItem(flag) \
103         PyStructSequence_SET_ITEM(app_info, pos++, PyLong_FromLong(flag))
104 #define SetStrItem(str) \
105         PyStructSequence_SET_ITEM(app_info, pos++, PyUnicode_FromString(str))
106 #define SetObjItem(obj) \
107         PyStructSequence_SET_ITEM(app_info, pos++, obj)
108
109         SetObjItem(Py_BuildValue("(iii)", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION));
110         SetObjItem(PyUnicode_FromFormat("%d.%02d (sub %d)", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION));
111 #if defined(BLENDER_VERSION_CHAR) && EXPAND(BLENDER_VERSION_CHAR) != 1
112         SetStrItem(STRINGIFY(BLENDER_VERSION_CHAR));
113 #else
114         SetStrItem("");
115 #endif
116         SetStrItem(STRINGIFY(BLENDER_VERSION_CYCLE));
117         SetStrItem(BLI_program_path());
118         SetObjItem(PyBool_FromLong(G.background));
119
120         /* build info */
121 #ifdef BUILD_DATE
122         SetStrItem(build_date);
123         SetStrItem(build_time);
124         SetStrItem(build_rev);
125         SetStrItem(build_platform);
126         SetStrItem(build_type);
127         SetStrItem(build_cflags);
128         SetStrItem(build_cxxflags);
129         SetStrItem(build_linkflags);
130         SetStrItem(build_system);
131 #else
132         SetStrItem("Unknown");
133         SetStrItem("Unknown");
134         SetStrItem("Unknown");
135         SetStrItem("Unknown");
136         SetStrItem("Unknown");
137         SetStrItem("Unknown");
138         SetStrItem("Unknown");
139         SetStrItem("Unknown");
140         SetStrItem("Unknown");
141 #endif
142
143         SetObjItem(BPY_app_handlers_struct());
144
145 #undef SetIntItem
146 #undef SetStrItem
147 #undef SetObjItem
148
149         if (PyErr_Occurred()) {
150                 Py_CLEAR(app_info);
151                 return NULL;
152         }
153         return app_info;
154 }
155
156 /* a few getsets because it makes sense for them to be in bpy.app even though
157  * they are not static */
158 static PyObject *bpy_app_debug_get(PyObject *UNUSED(self), void *UNUSED(closure))
159 {
160         return PyBool_FromLong(G.f & G_DEBUG);
161 }
162
163 static int bpy_app_debug_set(PyObject *UNUSED(self), PyObject *value, void *UNUSED(closure))
164 {
165         int param= PyObject_IsTrue(value);
166
167         if (param < 0) {
168                 PyErr_SetString(PyExc_TypeError, "bpy.app.debug can only be True/False");
169                 return -1;
170         }
171         
172         if (param)  G.f |=  G_DEBUG;
173         else        G.f &= ~G_DEBUG;
174         
175         return 0;
176 }
177
178 static PyObject *bpy_app_debug_value_get(PyObject *UNUSED(self), void *UNUSED(closure))
179 {
180         return PyLong_FromSsize_t(G.rt);
181 }
182
183 static int bpy_app_debug_value_set(PyObject *UNUSED(self), PyObject *value, void *UNUSED(closure))
184 {
185         int param= PyLong_AsSsize_t(value);
186
187         if (param == -1 && PyErr_Occurred()) {
188                 PyErr_SetString(PyExc_TypeError, "bpy.app.debug_value can only be set to a whole number");
189                 return -1;
190         }
191         
192         G.rt= param;
193
194         return 0;
195 }
196
197 static PyObject *bpy_app_tempdir_get(PyObject *UNUSED(self), void *UNUSED(closure))
198 {
199         return PyC_UnicodeFromByte(BLI_temporary_dir());
200 }
201
202 static PyObject *bpy_app_driver_dict_get(PyObject *UNUSED(self), void *UNUSED(closure))
203 {
204         if (bpy_pydriver_Dict == NULL)
205                 if (bpy_pydriver_create_dict() != 0) {
206                         PyErr_SetString(PyExc_RuntimeError, "bpy.app.driver_namespace failed to create dictionary");
207                         return NULL;
208         }
209
210         Py_INCREF(bpy_pydriver_Dict);
211         return bpy_pydriver_Dict;
212 }
213
214
215 static PyGetSetDef bpy_app_getsets[]= {
216         {(char *)"debug", bpy_app_debug_get, bpy_app_debug_set, (char *)"Boolean, set when blender is running in debug mode (started with -d)", NULL},
217         {(char *)"debug_value", bpy_app_debug_value_get, bpy_app_debug_value_set, (char *)"Int, number which can be set to non-zero values for testing purposes.", NULL},
218         {(char *)"tempdir", bpy_app_tempdir_get, NULL, (char *)"String, the temp directory used by blender (read-only)", NULL},
219         {(char *)"driver_namespace", bpy_app_driver_dict_get, NULL, (char *)"Dictionary for drivers namespace, editable in-place, reset on file load (read-only)", NULL},
220         {NULL, NULL, NULL, NULL, NULL}
221 };
222
223 static void py_struct_seq_getset_init(void)
224 {
225         /* tricky dynamic members, not to py-spec! */
226         PyGetSetDef *getset;
227
228         for (getset= bpy_app_getsets; getset->name; getset++) {
229                 PyDict_SetItemString(BlenderAppType.tp_dict, getset->name, PyDescr_NewGetSet(&BlenderAppType, getset));
230         }
231 }
232 /* end dynamic bpy.app */
233
234
235 PyObject *BPY_app_struct(void)
236 {
237         PyObject *ret;
238         
239         PyStructSequence_InitType(&BlenderAppType, &app_info_desc);
240
241         ret= make_app_info();
242
243         /* prevent user from creating new instances */
244         BlenderAppType.tp_init= NULL;
245         BlenderAppType.tp_new= NULL;
246
247         /* kindof a hack ontop of PyStructSequence */
248         py_struct_seq_getset_init();
249
250         return ret;
251 }
252