41de1171aaa270147117cd6ec1e2bd0dedaf27c8
[blender.git] / source / blender / python / intern / bpy_app.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
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.
10  *
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.
15  *
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.
19  *
20  * Contributor(s): Campbell Barton
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 /** \file blender/python/intern/bpy_app.c
26  *  \ingroup pythonintern
27  */
28
29
30 #include <Python.h>
31
32 #include "bpy_app.h"
33 #include "bpy_driver.h"
34
35 #include "BLI_path_util.h"
36 #include "BLI_utildefines.h"
37
38
39 #include "BKE_blender.h"
40 #include "BKE_global.h"
41 #include "structseq.h"
42
43 #include "../generic/py_capi_utils.h"
44
45 #ifdef BUILD_DATE
46 extern char build_date[];
47 extern char build_time[];
48 extern char build_rev[];
49 extern char build_platform[];
50 extern char build_type[];
51 extern char build_cflags[];
52 extern char build_cxxflags[];
53 extern char build_linkflags[];
54 extern char build_system[];
55 #endif
56
57 static PyTypeObject BlenderAppType;
58
59 static PyStructSequence_Field app_info_fields[]= {
60         {(char *)"version", (char *)"The Blender version as a tuple of 3 numbers. eg. (2, 50, 11)"},
61         {(char *)"version_string", (char *)"The Blender version formatted as a string"},
62         {(char *)"version_char", (char *)"The Blender version character (for minor releases)"},
63         {(char *)"version_cycle", (char *)"The release status of this build alpha/beta/rc/release"},
64         {(char *)"binary_path", (char *)"The location of blenders executable, useful for utilities that spawn new instances"},
65         {(char *)"background", (char *)"Boolean, True when blender is running without a user interface (started with -b)"},
66
67         /* buildinfo */
68         {(char *)"build_date", (char *)"The date this blender instance was built"},
69         {(char *)"build_time", (char *)"The time this blender instance was built"},
70         {(char *)"build_revision", (char *)"The subversion revision this blender instance was built with"},
71         {(char *)"build_platform", (char *)"The platform this blender instance was built for"},
72         {(char *)"build_type", (char *)"The type of build (Release, Debug)"},
73         {(char *)"build_cflags", (char *)"C compiler flags"},
74         {(char *)"build_cxxflags", (char *)"C++ compiler flags"},
75         {(char *)"build_linkflags", (char *)"Binary linking flags"},
76         {(char *)"build_system", (char *)"Build system used"},
77         {NULL}
78 };
79
80 static PyStructSequence_Desc app_info_desc= {
81         (char *)"bpy.app",     /* name */
82         (char *)"This module contains application values that remain unchanged during runtime.",    /* doc */
83         app_info_fields,    /* fields */
84         (sizeof(app_info_fields)/sizeof(PyStructSequence_Field)) - 1
85 };
86
87 #define DO_EXPAND(VAL)  VAL ## 1
88 #define EXPAND(VAL)     DO_EXPAND(VAL)
89
90 static PyObject *make_app_info(void)
91 {
92         extern char bprogname[]; /* argv[0] from creator.c */
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(bprogname);
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 #undef SetIntItem
144 #undef SetStrItem
145 #undef SetObjItem
146
147         if (PyErr_Occurred()) {
148                 Py_CLEAR(app_info);
149                 return NULL;
150         }
151         return app_info;
152 }
153
154 /* a few getsets because it makes sense for them to be in bpy.app even though
155  * they are not static */
156 static PyObject *bpy_app_debug_get(PyObject *UNUSED(self), void *UNUSED(closure))
157 {
158         return PyBool_FromLong(G.f & G_DEBUG);
159 }
160
161 static int bpy_app_debug_set(PyObject *UNUSED(self), PyObject *value, void *UNUSED(closure))
162 {
163         int param= PyObject_IsTrue(value);
164
165         if(param < 0) {
166                 PyErr_SetString(PyExc_TypeError, "bpy.app.debug can only be True/False");
167                 return -1;
168         }
169         
170         if(param)       G.f |=  G_DEBUG;
171         else            G.f &= ~G_DEBUG;
172         
173         return 0;
174 }
175
176 static PyObject *bpy_app_debug_value_get(PyObject *UNUSED(self), void *UNUSED(closure))
177 {
178         return PyLong_FromSsize_t(G.rt);
179 }
180
181 static int bpy_app_debug_value_set(PyObject *UNUSED(self), PyObject *value, void *UNUSED(closure))
182 {
183         int param= PyLong_AsSsize_t(value);
184
185         if (param == -1 && PyErr_Occurred()) {
186                 PyErr_SetString(PyExc_TypeError, "bpy.app.debug_value can only be set to a whole number");
187                 return -1;
188         }
189         
190         G.rt= param;
191
192         return 0;
193 }
194
195 static PyObject *bpy_app_tempdir_get(PyObject *UNUSED(self), void *UNUSED(closure))
196 {
197         extern char btempdir[];
198         return PyC_UnicodeFromByte(btempdir);
199 }
200
201 static PyObject *bpy_app_driver_dict_get(PyObject *UNUSED(self), void *UNUSED(closure))
202 {
203         if (bpy_pydriver_Dict == NULL)
204                 if (bpy_pydriver_create_dict() != 0) {
205                         PyErr_SetString(PyExc_RuntimeError, "bpy.app.driver_namespace failed to create dictionary");
206                         return NULL;
207         }
208
209         Py_INCREF(bpy_pydriver_Dict);
210         return bpy_pydriver_Dict;
211 }
212
213
214 static PyGetSetDef bpy_app_getsets[]= {
215         {(char *)"debug", bpy_app_debug_get, bpy_app_debug_set, (char *)"Boolean, set when blender is running in debug mode (started with -d)", NULL},
216         {(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},
217         {(char *)"tempdir", bpy_app_tempdir_get, NULL, (char *)"String, the temp directory used by blender (read-only)", NULL},
218         {(char *)"driver_namespace", bpy_app_driver_dict_get, NULL, (char *)"Dictionary for drivers namespace, editable in-place, reset on file load (read-only)", NULL},
219         {NULL, NULL, NULL, NULL, NULL}
220 };
221
222 static void py_struct_seq_getset_init(void)
223 {
224         /* tricky dynamic members, not to py-spec! */
225         PyGetSetDef *getset;
226
227         for(getset= bpy_app_getsets; getset->name; getset++) {
228                 PyDict_SetItemString(BlenderAppType.tp_dict, getset->name, PyDescr_NewGetSet(&BlenderAppType, getset));
229         }
230 }
231 /* end dynamic bpy.app */
232
233
234 PyObject *BPY_app_struct(void)
235 {
236         PyObject *ret;
237         
238         PyStructSequence_InitType(&BlenderAppType, &app_info_desc);
239
240         ret= make_app_info();
241
242         /* prevent user from creating new instances */
243         BlenderAppType.tp_init= NULL;
244         BlenderAppType.tp_new= NULL;
245
246         /* kindof a hack ontop of PyStructSequence */
247         py_struct_seq_getset_init();
248
249         return ret;
250 }
251