Blender V2.61 - r43446

bpy_app.c

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * Contributor(s): Campbell Barton
00019  *
00020  * ***** END GPL LICENSE BLOCK *****
00021  */
00022 
00032 #include <Python.h>
00033 
00034 #include "bpy_app.h"
00035 
00036 #include "bpy_app_ffmpeg.h"
00037 
00038 #include "bpy_app_handlers.h"
00039 #include "bpy_driver.h"
00040 
00041 #include "BLI_path_util.h"
00042 #include "BLI_utildefines.h"
00043 
00044 
00045 #include "BKE_blender.h"
00046 #include "BKE_global.h"
00047 #include "structseq.h"
00048 
00049 #include "../generic/py_capi_utils.h"
00050 
00051 #ifdef BUILD_DATE
00052 extern char build_date[];
00053 extern char build_time[];
00054 extern char build_rev[];
00055 extern char build_platform[];
00056 extern char build_type[];
00057 extern char build_cflags[];
00058 extern char build_cxxflags[];
00059 extern char build_linkflags[];
00060 extern char build_system[];
00061 #endif
00062 
00063 static PyTypeObject BlenderAppType;
00064 
00065 static PyStructSequence_Field app_info_fields[] = {
00066     {(char *)"version", (char *)"The Blender version as a tuple of 3 numbers. eg. (2, 50, 11)"},
00067     {(char *)"version_string", (char *)"The Blender version formatted as a string"},
00068     {(char *)"version_char", (char *)"The Blender version character (for minor releases)"},
00069     {(char *)"version_cycle", (char *)"The release status of this build alpha/beta/rc/release"},
00070     {(char *)"binary_path", (char *)"The location of blenders executable, useful for utilities that spawn new instances"},
00071     {(char *)"background", (char *)"Boolean, True when blender is running without a user interface (started with -b)"},
00072 
00073     /* buildinfo */
00074     {(char *)"build_date", (char *)"The date this blender instance was built"},
00075     {(char *)"build_time", (char *)"The time this blender instance was built"},
00076     {(char *)"build_revision", (char *)"The subversion revision this blender instance was built with"},
00077     {(char *)"build_platform", (char *)"The platform this blender instance was built for"},
00078     {(char *)"build_type", (char *)"The type of build (Release, Debug)"},
00079     {(char *)"build_cflags", (char *)"C compiler flags"},
00080     {(char *)"build_cxxflags", (char *)"C++ compiler flags"},
00081     {(char *)"build_linkflags", (char *)"Binary linking flags"},
00082     {(char *)"build_system", (char *)"Build system used"},
00083 
00084     /* submodules */
00085     {(char *)"ffmpeg", (char *)"FFmpeg library information backend"},
00086     {(char *)"handlers", (char *)"Application handler callbacks"},
00087     {NULL}
00088 };
00089 
00090 static PyStructSequence_Desc app_info_desc = {
00091     (char *)"bpy.app",     /* name */
00092     (char *)"This module contains application values that remain unchanged during runtime.",    /* doc */
00093     app_info_fields,    /* fields */
00094     (sizeof(app_info_fields) / sizeof(PyStructSequence_Field)) - 1
00095 };
00096 
00097 #define DO_EXPAND(VAL)  VAL ## 1
00098 #define EXPAND(VAL)     DO_EXPAND(VAL)
00099 
00100 static PyObject *make_app_info(void)
00101 {
00102     PyObject *app_info;
00103     int pos = 0;
00104 
00105     app_info = PyStructSequence_New(&BlenderAppType);
00106     if (app_info == NULL) {
00107         return NULL;
00108     }
00109 
00110 #define SetIntItem(flag) \
00111     PyStructSequence_SET_ITEM(app_info, pos++, PyLong_FromLong(flag))
00112 #define SetStrItem(str) \
00113     PyStructSequence_SET_ITEM(app_info, pos++, PyUnicode_FromString(str))
00114 #define SetObjItem(obj) \
00115     PyStructSequence_SET_ITEM(app_info, pos++, obj)
00116 
00117     SetObjItem(Py_BuildValue("(iii)",
00118                              BLENDER_VERSION / 100, BLENDER_VERSION % 100, BLENDER_SUBVERSION));
00119     SetObjItem(PyUnicode_FromFormat("%d.%02d (sub %d)",
00120                                     BLENDER_VERSION / 100, BLENDER_VERSION % 100, BLENDER_SUBVERSION));
00121 
00122 #if defined(BLENDER_VERSION_CHAR) && EXPAND(BLENDER_VERSION_CHAR) != 1
00123     SetStrItem(STRINGIFY(BLENDER_VERSION_CHAR));
00124 #else
00125     SetStrItem("");
00126 #endif
00127     SetStrItem(STRINGIFY(BLENDER_VERSION_CYCLE));
00128     SetStrItem(BLI_program_path());
00129     SetObjItem(PyBool_FromLong(G.background));
00130 
00131     /* build info */
00132 #ifdef BUILD_DATE
00133     SetStrItem(build_date);
00134     SetStrItem(build_time);
00135     SetStrItem(build_rev);
00136     SetStrItem(build_platform);
00137     SetStrItem(build_type);
00138     SetStrItem(build_cflags);
00139     SetStrItem(build_cxxflags);
00140     SetStrItem(build_linkflags);
00141     SetStrItem(build_system);
00142 #else
00143     SetStrItem("Unknown");
00144     SetStrItem("Unknown");
00145     SetStrItem("Unknown");
00146     SetStrItem("Unknown");
00147     SetStrItem("Unknown");
00148     SetStrItem("Unknown");
00149     SetStrItem("Unknown");
00150     SetStrItem("Unknown");
00151     SetStrItem("Unknown");
00152 #endif
00153 
00154     SetObjItem(BPY_app_ffmpeg_struct());
00155     SetObjItem(BPY_app_handlers_struct());
00156 
00157 #undef SetIntItem
00158 #undef SetStrItem
00159 #undef SetObjItem
00160 
00161     if (PyErr_Occurred()) {
00162         Py_CLEAR(app_info);
00163         return NULL;
00164     }
00165     return app_info;
00166 }
00167 
00168 /* a few getsets because it makes sense for them to be in bpy.app even though
00169  * they are not static */
00170 
00171 PyDoc_STRVAR(bpy_app_debug_doc,
00172 "Boolean, set when blender is running in debug mode (started with --debug)"
00173 );
00174 static PyObject *bpy_app_debug_get(PyObject *UNUSED(self), void *UNUSED(closure))
00175 {
00176     return PyBool_FromLong(G.f & G_DEBUG);
00177 }
00178 
00179 static int bpy_app_debug_set(PyObject *UNUSED(self), PyObject *value, void *UNUSED(closure))
00180 {
00181     int param = PyObject_IsTrue(value);
00182 
00183     if (param < 0) {
00184         PyErr_SetString(PyExc_TypeError, "bpy.app.debug can only be True/False");
00185         return -1;
00186     }
00187     
00188     if (param)  G.f |=  G_DEBUG;
00189     else        G.f &= ~G_DEBUG;
00190     
00191     return 0;
00192 }
00193 
00194 PyDoc_STRVAR(bpy_app_debug_value_doc,
00195 "Int, number which can be set to non-zero values for testing purposes"
00196 );
00197 static PyObject *bpy_app_debug_value_get(PyObject *UNUSED(self), void *UNUSED(closure))
00198 {
00199     return PyLong_FromSsize_t(G.rt);
00200 }
00201 
00202 static int bpy_app_debug_value_set(PyObject *UNUSED(self), PyObject *value, void *UNUSED(closure))
00203 {
00204     int param = PyLong_AsSsize_t(value);
00205 
00206     if (param == -1 && PyErr_Occurred()) {
00207         PyErr_SetString(PyExc_TypeError, "bpy.app.debug_value can only be set to a whole number");
00208         return -1;
00209     }
00210     
00211     G.rt = param;
00212 
00213     return 0;
00214 }
00215 
00216 PyDoc_STRVAR(bpy_app_tempdir_doc,
00217 "String, the temp directory used by blender (read-only)"
00218 );
00219 static PyObject *bpy_app_tempdir_get(PyObject *UNUSED(self), void *UNUSED(closure))
00220 {
00221     return PyC_UnicodeFromByte(BLI_temporary_dir());
00222 }
00223 
00224 PyDoc_STRVAR(bpy_app_driver_dict_doc,
00225 "Dictionary for drivers namespace, editable in-place, reset on file load (read-only)"
00226 );
00227 static PyObject *bpy_app_driver_dict_get(PyObject *UNUSED(self), void *UNUSED(closure))
00228 {
00229     if (bpy_pydriver_Dict == NULL)
00230         if (bpy_pydriver_create_dict() != 0) {
00231             PyErr_SetString(PyExc_RuntimeError, "bpy.app.driver_namespace failed to create dictionary");
00232             return NULL;
00233     }
00234 
00235     Py_INCREF(bpy_pydriver_Dict);
00236     return bpy_pydriver_Dict;
00237 }
00238 
00239 
00240 static PyGetSetDef bpy_app_getsets[] = {
00241     {(char *)"debug", bpy_app_debug_get, bpy_app_debug_set, (char *)bpy_app_debug_doc, NULL},
00242     {(char *)"debug_value", bpy_app_debug_value_get, bpy_app_debug_value_set, (char *)bpy_app_debug_value_doc, NULL},
00243     {(char *)"tempdir", bpy_app_tempdir_get, NULL, (char *)bpy_app_tempdir_doc, NULL},
00244     {(char *)"driver_namespace", bpy_app_driver_dict_get, NULL, (char *)bpy_app_driver_dict_doc, NULL},
00245     {NULL, NULL, NULL, NULL, NULL}
00246 };
00247 
00248 static void py_struct_seq_getset_init(void)
00249 {
00250     /* tricky dynamic members, not to py-spec! */
00251     PyGetSetDef *getset;
00252 
00253     for (getset = bpy_app_getsets; getset->name; getset++) {
00254         PyDict_SetItemString(BlenderAppType.tp_dict, getset->name, PyDescr_NewGetSet(&BlenderAppType, getset));
00255     }
00256 }
00257 /* end dynamic bpy.app */
00258 
00259 
00260 PyObject *BPY_app_struct(void)
00261 {
00262     PyObject *ret;
00263     
00264     PyStructSequence_InitType(&BlenderAppType, &app_info_desc);
00265 
00266     ret = make_app_info();
00267 
00268     /* prevent user from creating new instances */
00269     BlenderAppType.tp_init = NULL;
00270     BlenderAppType.tp_new = NULL;
00271     BlenderAppType.tp_hash = (hashfunc)_Py_HashPointer; /* without this we can't do set(sys.modules) [#29635] */
00272 
00273     /* kindof a hack ontop of PyStructSequence */
00274     py_struct_seq_getset_init();
00275 
00276     return ret;
00277 }