e90b58d9fac534dae228ee82eee9d879dc03c434
[blender-staging.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 #include "bpy_app.h"
26
27 #include "BLI_path_util.h"
28
29 #include "BKE_blender.h"
30 #include "BKE_global.h"
31 #include "structseq.h"
32
33 #ifdef BUILD_DATE
34 extern char build_date[];
35 extern char build_time[];
36 extern char build_rev[];
37 extern char build_platform[];
38 extern char build_type[];
39 #endif
40
41 static PyTypeObject BlenderAppType;
42
43 static PyStructSequence_Field app_info_fields[] = {
44         {"version", "The Blender version as a tuple of 3 numbers. eg. (2, 50, 11)"},
45         {"version_string", "The Blender version formatted as a string"},
46         {"home", "The blender home directory, normally matching $HOME"},
47         {"binary_path", "The location of blenders executable, useful for utilities that spawn new instances"},
48         {"debug", "Boolean, set when blender is running in debug mode (started with -d)"},
49
50         /* buildinfo */
51         {"build_date", "The date this blender instance was built"},
52         {"build_time", "The time this blender instance was built"},
53         {"build_revision", "The subversion revision this blender instance was built with"},
54         {"build_platform", "The platform this blender instance was built for"},
55         {"build_type", "The type of build (Release, Debug)"},
56         {0}
57 };
58
59 static PyStructSequence_Desc app_info_desc = {
60         "bpy.app",     /* name */
61         "This module contains application values that remain unchanged during runtime.",    /* doc */
62         app_info_fields,    /* fields */
63         10
64 };
65
66 static PyObject *make_app_info(void)
67 {
68         extern char bprogname[]; /* argv[0] from creator.c */
69
70         PyObject *app_info;
71         int pos = 0;
72
73         app_info = PyStructSequence_New(&BlenderAppType);
74         if (app_info == NULL) {
75                 return NULL;
76         }
77
78 #define SetIntItem(flag) \
79         PyStructSequence_SET_ITEM(app_info, pos++, PyLong_FromLong(flag))
80 #define SetStrItem(str) \
81         PyStructSequence_SET_ITEM(app_info, pos++, PyUnicode_FromString(str))
82 #define SetObjItem(obj) \
83         PyStructSequence_SET_ITEM(app_info, pos++, obj)
84
85         SetObjItem(Py_BuildValue("(iii)", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION));
86         SetObjItem(PyUnicode_FromFormat("%d.%02d (sub %d)", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION));
87         SetStrItem(BLI_gethome());
88         SetStrItem(bprogname);
89         SetObjItem(PyBool_FromLong(G.f & G_DEBUG));
90
91         /* build info */
92 #ifdef BUILD_DATE
93         SetStrItem(build_date);
94         SetStrItem(build_time);
95         SetStrItem(build_rev);
96         SetStrItem(build_platform);
97         SetStrItem(build_type);
98 #else
99         SetStrItem("Unknown");
100         SetStrItem("Unknown");
101         SetStrItem("Unknown");
102         SetStrItem("Unknown");
103         SetStrItem("Unknown");
104 #endif
105
106 #undef SetIntItem
107 #undef SetStrItem
108 #undef SetObjItem
109
110         if (PyErr_Occurred()) {
111                 Py_CLEAR(app_info);
112                 return NULL;
113         }
114         return app_info;
115 }
116
117 PyObject *BPY_app_struct(void)
118 {
119         PyObject *ret;
120
121         PyStructSequence_InitType(&BlenderAppType, &app_info_desc);
122
123         ret= make_app_info();
124
125         /* prevent user from creating new instances */
126         BlenderAppType.tp_init = NULL;
127         BlenderAppType.tp_new = NULL;
128         
129         return ret;
130 }