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