Blender  V2.93
bpy_app.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
25 #include <Python.h>
26 
27 #include "bpy_app.h"
28 
29 #include "bpy_app_alembic.h"
30 #include "bpy_app_build_options.h"
31 #include "bpy_app_ffmpeg.h"
32 #include "bpy_app_ocio.h"
33 #include "bpy_app_oiio.h"
34 #include "bpy_app_opensubdiv.h"
35 #include "bpy_app_openvdb.h"
36 #include "bpy_app_sdl.h"
37 #include "bpy_app_usd.h"
38 
39 #include "bpy_app_translations.h"
40 
41 #include "bpy_app_handlers.h"
42 #include "bpy_driver.h"
43 
44 /* modules */
45 #include "bpy_app_icons.h"
46 #include "bpy_app_timers.h"
47 
48 #include "BLI_utildefines.h"
49 
50 #include "BKE_appdir.h"
51 #include "BKE_blender_version.h"
52 #include "BKE_global.h"
53 
54 #include "DNA_ID.h"
55 
56 #include "UI_interface_icons.h"
57 
58 /* for notifiers */
59 #include "WM_api.h"
60 #include "WM_types.h"
61 
62 #include "../generic/py_capi_utils.h"
63 #include "../generic/python_utildefines.h"
64 
65 #ifdef BUILD_DATE
66 extern char build_date[];
67 extern char build_time[];
69 extern char build_commit_date[];
70 extern char build_commit_time[];
71 extern char build_hash[];
72 extern char build_branch[];
73 extern char build_platform[];
74 extern char build_type[];
75 extern char build_cflags[];
76 extern char build_cxxflags[];
77 extern char build_linkflags[];
78 extern char build_system[];
79 #endif
80 
81 static PyTypeObject BlenderAppType;
82 
83 static PyStructSequence_Field app_info_fields[] = {
84  {"version", "The Blender version as a tuple of 3 numbers. eg. (2, 83, 1)"},
85  {"version_file", "The blend file version, compatible with ``bpy.data.version``"},
86  {"version_string", "The Blender version formatted as a string"},
87  {"version_cycle", "The release status of this build alpha/beta/rc/release"},
88  {"version_char", "Deprecated, always an empty string"},
89  {"binary_path",
90  "The location of Blender's executable, useful for utilities that open new instances"},
91  {"background",
92  "Boolean, True when blender is running without a user interface (started with -b)"},
93  {"factory_startup", "Boolean, True when blender is running with --factory-startup)"},
94 
95  /* buildinfo */
96  {"build_date", "The date this blender instance was built"},
97  {"build_time", "The time this blender instance was built"},
98  {"build_commit_timestamp", "The unix timestamp of commit this blender instance was built"},
99  {"build_commit_date", "The date of commit this blender instance was built"},
100  {"build_commit_time", "The time of commit this blender instance was built"},
101  {"build_hash", "The commit hash this blender instance was built with"},
102  {"build_branch", "The branch this blender instance was built from"},
103  {"build_platform", "The platform this blender instance was built for"},
104  {"build_type", "The type of build (Release, Debug)"},
105  {"build_cflags", "C compiler flags"},
106  {"build_cxxflags", "C++ compiler flags"},
107  {"build_linkflags", "Binary linking flags"},
108  {"build_system", "Build system used"},
109 
110  /* submodules */
111  {"alembic", "Alembic library information backend"},
112  {"usd", "USD library information backend"},
113  {"ffmpeg", "FFmpeg library information backend"},
114  {"ocio", "OpenColorIO library information backend"},
115  {"oiio", "OpenImageIO library information backend"},
116  {"opensubdiv", "OpenSubdiv library information backend"},
117  {"openvdb", "OpenVDB library information backend"},
118  {"sdl", "SDL library information backend"},
119  {"build_options", "A set containing most important enabled optional build features"},
120  {"handlers", "Application handler callbacks"},
121  {"translations", "Application and addons internationalization API"},
122 
123  /* Modules (not struct sequence). */
124  {"icons", "Manage custom icons"},
125  {"timers", "Manage timers"},
126  {NULL},
127 };
128 
129 PyDoc_STRVAR(bpy_app_doc,
130  "This module contains application values that remain unchanged during runtime.");
131 
132 static PyStructSequence_Desc app_info_desc = {
133  "bpy.app", /* name */
134  bpy_app_doc, /* doc */
135  app_info_fields, /* fields */
137 };
138 
139 static PyObject *make_app_info(void)
140 {
141  PyObject *app_info;
142  int pos = 0;
143 
144  app_info = PyStructSequence_New(&BlenderAppType);
145  if (app_info == NULL) {
146  return NULL;
147  }
148 #define SetIntItem(flag) PyStructSequence_SET_ITEM(app_info, pos++, PyLong_FromLong(flag))
149 #define SetStrItem(str) PyStructSequence_SET_ITEM(app_info, pos++, PyUnicode_FromString(str))
150 #define SetBytesItem(str) PyStructSequence_SET_ITEM(app_info, pos++, PyBytes_FromString(str))
151 #define SetObjItem(obj) PyStructSequence_SET_ITEM(app_info, pos++, obj)
152 
153  SetObjItem(
158 
160  SetStrItem("");
162  SetObjItem(PyBool_FromLong(G.background));
163  SetObjItem(PyBool_FromLong(G.factory_startup));
164 
165  /* build info, use bytes since we can't assume _any_ encoding:
166  * see patch T30154 for issue */
167 #ifdef BUILD_DATE
181 #else
182  SetBytesItem("Unknown");
183  SetBytesItem("Unknown");
184  SetIntItem(0);
185  SetBytesItem("Unknown");
186  SetBytesItem("Unknown");
187  SetBytesItem("Unknown");
188  SetBytesItem("Unknown");
189  SetBytesItem("Unknown");
190  SetBytesItem("Unknown");
191  SetBytesItem("Unknown");
192  SetBytesItem("Unknown");
193  SetBytesItem("Unknown");
194  SetBytesItem("Unknown");
195 #endif
196 
208 
209  /* modules */
212 
213 #undef SetIntItem
214 #undef SetStrItem
215 #undef SetBytesItem
216 #undef SetObjItem
217 
218  if (PyErr_Occurred()) {
219  Py_DECREF(app_info);
220  return NULL;
221  }
222  return app_info;
223 }
224 
225 /* a few getsets because it makes sense for them to be in bpy.app even though
226  * they are not static */
227 
229  bpy_app_debug_doc,
230  "Boolean, for debug info (started with --debug / --debug_* matching this attribute name)");
231 static PyObject *bpy_app_debug_get(PyObject *UNUSED(self), void *closure)
232 {
233  const int flag = POINTER_AS_INT(closure);
234  return PyBool_FromLong(G.debug & flag);
235 }
236 
237 static int bpy_app_debug_set(PyObject *UNUSED(self), PyObject *value, void *closure)
238 {
239  const int flag = POINTER_AS_INT(closure);
240  const int param = PyObject_IsTrue(value);
241 
242  if (param == -1) {
243  PyErr_SetString(PyExc_TypeError, "bpy.app.debug can only be True/False");
244  return -1;
245  }
246 
247  if (param) {
248  G.debug |= flag;
249  }
250  else {
251  G.debug &= ~flag;
252  }
253 
254  return 0;
255 }
256 
258  bpy_app_global_flag_doc,
259  "Boolean, for application behavior (started with --enable-* matching this attribute name)");
260 static PyObject *bpy_app_global_flag_get(PyObject *UNUSED(self), void *closure)
261 {
262  const int flag = POINTER_AS_INT(closure);
263  return PyBool_FromLong(G.f & flag);
264 }
265 
266 static int bpy_app_global_flag_set(PyObject *UNUSED(self), PyObject *value, void *closure)
267 {
268  const int flag = POINTER_AS_INT(closure);
269  const int param = PyObject_IsTrue(value);
270 
271  if (param == -1) {
272  PyErr_SetString(PyExc_TypeError, "bpy.app.use_* can only be True/False");
273  return -1;
274  }
275 
276  if (param) {
277  G.f |= flag;
278  }
279  else {
280  G.f &= ~flag;
281  }
282 
283  return 0;
284 }
285 
287  PyObject *value,
288  void *closure)
289 {
290  const int param = PyObject_IsTrue(value);
291  if (param == 1) {
292  PyErr_SetString(PyExc_ValueError, "This bpy.app.use_* option can only be disabled");
293  return -1;
294  }
295  return bpy_app_global_flag_set(NULL, value, closure);
296 }
297 
298 PyDoc_STRVAR(bpy_app_binary_path_python_doc,
299  "String, the path to the python executable (read-only). "
300  "Deprecated! Use ``sys.executable`` instead.");
301 static PyObject *bpy_app_binary_path_python_get(PyObject *UNUSED(self), void *UNUSED(closure))
302 {
303  PyErr_Warn(PyExc_RuntimeWarning, "Use 'sys.executable' instead of 'binary_path_python'!");
304  return Py_INCREF_RET(PySys_GetObject("executable"));
305 }
306 
307 PyDoc_STRVAR(bpy_app_debug_value_doc,
308  "Short, number which can be set to non-zero values for testing purposes");
309 static PyObject *bpy_app_debug_value_get(PyObject *UNUSED(self), void *UNUSED(closure))
310 {
311  return PyLong_FromLong(G.debug_value);
312 }
313 
314 static int bpy_app_debug_value_set(PyObject *UNUSED(self), PyObject *value, void *UNUSED(closure))
315 {
316  const short param = PyC_Long_AsI16(value);
317 
318  if (param == -1 && PyErr_Occurred()) {
319  PyC_Err_SetString_Prefix(PyExc_TypeError,
320  "bpy.app.debug_value can only be set to a whole number");
321  return -1;
322  }
323 
324  G.debug_value = param;
325 
327 
328  return 0;
329 }
330 
331 PyDoc_STRVAR(bpy_app_tempdir_doc, "String, the temp directory used by blender (read-only)");
332 static PyObject *bpy_app_tempdir_get(PyObject *UNUSED(self), void *UNUSED(closure))
333 {
335 }
336 
338  bpy_app_driver_dict_doc,
339  "Dictionary for drivers namespace, editable in-place, reset on file load (read-only)");
340 static PyObject *bpy_app_driver_dict_get(PyObject *UNUSED(self), void *UNUSED(closure))
341 {
342  if (bpy_pydriver_Dict == NULL) {
343  if (bpy_pydriver_create_dict() != 0) {
344  PyErr_SetString(PyExc_RuntimeError, "bpy.app.driver_namespace failed to create dictionary");
345  return NULL;
346  }
347  }
348 
349  return Py_INCREF_RET(bpy_pydriver_Dict);
350 }
351 
352 PyDoc_STRVAR(bpy_app_preview_render_size_doc,
353  "Reference size for icon/preview renders (read-only)");
354 static PyObject *bpy_app_preview_render_size_get(PyObject *UNUSED(self), void *closure)
355 {
356  return PyLong_FromLong((long)UI_icon_preview_to_render_size(POINTER_AS_INT(closure)));
357 }
358 
359 static PyObject *bpy_app_autoexec_fail_message_get(PyObject *UNUSED(self), void *UNUSED(closure))
360 {
361  return PyC_UnicodeFromByte(G.autoexec_fail);
362 }
363 
364 static PyGetSetDef bpy_app_getsets[] = {
365  {"debug", bpy_app_debug_get, bpy_app_debug_set, bpy_app_debug_doc, (void *)G_DEBUG},
366  {"debug_ffmpeg",
369  bpy_app_debug_doc,
370  (void *)G_DEBUG_FFMPEG},
371  {"debug_freestyle",
374  bpy_app_debug_doc,
375  (void *)G_DEBUG_FREESTYLE},
376  {"debug_python",
379  bpy_app_debug_doc,
380  (void *)G_DEBUG_PYTHON},
381  {"debug_events",
384  bpy_app_debug_doc,
385  (void *)G_DEBUG_EVENTS},
386  {"debug_handlers",
389  bpy_app_debug_doc,
390  (void *)G_DEBUG_HANDLERS},
391  {"debug_wm", bpy_app_debug_get, bpy_app_debug_set, bpy_app_debug_doc, (void *)G_DEBUG_WM},
392  {"debug_depsgraph",
395  bpy_app_debug_doc,
396  (void *)G_DEBUG_DEPSGRAPH},
397  {"debug_depsgraph_build",
400  bpy_app_debug_doc,
401  (void *)G_DEBUG_DEPSGRAPH_BUILD},
402  {"debug_depsgraph_eval",
405  bpy_app_debug_doc,
406  (void *)G_DEBUG_DEPSGRAPH_EVAL},
407  {"debug_depsgraph_tag",
410  bpy_app_debug_doc,
411  (void *)G_DEBUG_DEPSGRAPH_TAG},
412  {"debug_depsgraph_time",
415  bpy_app_debug_doc,
416  (void *)G_DEBUG_DEPSGRAPH_TIME},
417  {"debug_depsgraph_pretty",
420  bpy_app_debug_doc,
421  (void *)G_DEBUG_DEPSGRAPH_PRETTY},
422  {"debug_simdata",
425  bpy_app_debug_doc,
426  (void *)G_DEBUG_SIMDATA},
427  {"debug_io", bpy_app_debug_get, bpy_app_debug_set, bpy_app_debug_doc, (void *)G_DEBUG_IO},
428 
429  {"use_event_simulate",
432  bpy_app_global_flag_doc,
433  (void *)G_FLAG_EVENT_SIMULATE},
434 
435  {"use_userpref_skip_save_on_exit",
438  bpy_app_global_flag_doc,
440 
441  {"binary_path_python",
443  NULL,
444  bpy_app_binary_path_python_doc,
445  NULL},
446 
447  {"debug_value",
450  bpy_app_debug_value_doc,
451  NULL},
452  {"tempdir", bpy_app_tempdir_get, NULL, bpy_app_tempdir_doc, NULL},
453  {"driver_namespace", bpy_app_driver_dict_get, NULL, bpy_app_driver_dict_doc, NULL},
454 
455  {"render_icon_size",
457  NULL,
458  bpy_app_preview_render_size_doc,
459  (void *)ICON_SIZE_ICON},
460  {"render_preview_size",
462  NULL,
463  bpy_app_preview_render_size_doc,
464  (void *)ICON_SIZE_PREVIEW},
465 
466  /* security */
467  {"autoexec_fail", bpy_app_global_flag_get, NULL, NULL, (void *)G_FLAG_SCRIPT_AUTOEXEC_FAIL},
468  {"autoexec_fail_quiet",
470  NULL,
471  NULL,
473  {"autoexec_fail_message", bpy_app_autoexec_fail_message_get, NULL, NULL, NULL},
474  {NULL, NULL, NULL, NULL, NULL},
475 };
476 
477 static void py_struct_seq_getset_init(void)
478 {
479  /* tricky dynamic members, not to py-spec! */
480  for (PyGetSetDef *getset = bpy_app_getsets; getset->name; getset++) {
481  PyObject *item = PyDescr_NewGetSet(&BlenderAppType, getset);
482  PyDict_SetItem(BlenderAppType.tp_dict, PyDescr_NAME(item), item);
483  Py_DECREF(item);
484  }
485 }
486 /* end dynamic bpy.app */
487 
488 PyObject *BPY_app_struct(void)
489 {
490  PyObject *ret;
491 
492  PyStructSequence_InitType(&BlenderAppType, &app_info_desc);
493 
494  ret = make_app_info();
495 
496  /* prevent user from creating new instances */
497  BlenderAppType.tp_init = NULL;
498  BlenderAppType.tp_new = NULL;
499  BlenderAppType.tp_hash = (hashfunc)
500  _Py_HashPointer; /* without this we can't do set(sys.modules) T29635. */
501 
502  /* kindof a hack ontop of PyStructSequence */
504 
505  return ret;
506 }
const char * BKE_appdir_program_path(void)
Definition: appdir.c:882
#define BLENDER_VERSION_PATCH
#define BLENDER_VERSION_CYCLE
#define BLENDER_FILE_SUBVERSION
#define BLENDER_VERSION
#define BLENDER_FILE_VERSION
const char * BKE_blender_version_string(void)
Definition: blender.c:142
@ G_DEBUG
Definition: BKE_global.h:133
@ G_DEBUG_HANDLERS
Definition: BKE_global.h:137
@ G_DEBUG_FREESTYLE
Definition: BKE_global.h:140
@ G_DEBUG_IO
Definition: BKE_global.h:152
@ G_DEBUG_SIMDATA
Definition: BKE_global.h:150
@ G_DEBUG_FFMPEG
Definition: BKE_global.h:134
@ G_DEBUG_DEPSGRAPH_PRETTY
Definition: BKE_global.h:146
@ G_DEBUG_DEPSGRAPH_TIME
Definition: BKE_global.h:144
@ G_DEBUG_DEPSGRAPH
Definition: BKE_global.h:148
@ G_DEBUG_DEPSGRAPH_EVAL
Definition: BKE_global.h:142
@ G_DEBUG_DEPSGRAPH_TAG
Definition: BKE_global.h:143
@ G_DEBUG_WM
Definition: BKE_global.h:138
@ G_DEBUG_EVENTS
Definition: BKE_global.h:136
@ G_DEBUG_PYTHON
Definition: BKE_global.h:135
@ G_DEBUG_DEPSGRAPH_BUILD
Definition: BKE_global.h:141
@ G_FLAG_EVENT_SIMULATE
Definition: BKE_global.h:113
@ G_FLAG_SCRIPT_AUTOEXEC_FAIL_QUIET
Definition: BKE_global.h:120
@ G_FLAG_USERPREF_NO_SAVE_ON_EXIT
Definition: BKE_global.h:114
@ G_FLAG_SCRIPT_AUTOEXEC_FAIL
Definition: BKE_global.h:119
unsigned long ulong
Definition: BLI_sys_types.h:85
#define ARRAY_SIZE(arr)
#define STRINGIFY(x)
#define UNUSED(x)
#define POINTER_AS_INT(i)
ID and Library types, which are fundamental for sdna.
@ ICON_SIZE_PREVIEW
Definition: DNA_ID_enums.h:30
@ ICON_SIZE_ICON
Definition: DNA_ID_enums.h:29
int UI_icon_preview_to_render_size(enum eIconSizes size)
#define NC_WINDOW
Definition: WM_types.h:277
static void py_struct_seq_getset_init(void)
Definition: bpy_app.c:477
char build_type[]
Definition: buildinfo.c:54
char build_cflags[]
Definition: buildinfo.c:62
char build_hash[]
Definition: buildinfo.c:47
static PyStructSequence_Field app_info_fields[]
Definition: bpy_app.c:83
static PyObject * make_app_info(void)
Definition: bpy_app.c:139
static PyObject * bpy_app_global_flag_get(PyObject *UNUSED(self), void *closure)
Definition: bpy_app.c:260
char build_commit_date[]
Definition: buildinfo.c:49
static PyObject * bpy_app_driver_dict_get(PyObject *UNUSED(self), void *UNUSED(closure))
Definition: bpy_app.c:340
static PyObject * bpy_app_tempdir_get(PyObject *UNUSED(self), void *UNUSED(closure))
Definition: bpy_app.c:332
#define SetBytesItem(str)
static PyGetSetDef bpy_app_getsets[]
Definition: bpy_app.c:364
static PyObject * bpy_app_debug_value_get(PyObject *UNUSED(self), void *UNUSED(closure))
Definition: bpy_app.c:309
static int bpy_app_debug_set(PyObject *UNUSED(self), PyObject *value, void *closure)
Definition: bpy_app.c:237
static int bpy_app_global_flag_set__only_disable(PyObject *UNUSED(self), PyObject *value, void *closure)
Definition: bpy_app.c:286
char build_commit_time[]
Definition: buildinfo.c:50
char build_linkflags[]
Definition: buildinfo.c:64
char build_system[]
Definition: buildinfo.c:65
char build_branch[]
Definition: buildinfo.c:51
static PyObject * bpy_app_preview_render_size_get(PyObject *UNUSED(self), void *closure)
Definition: bpy_app.c:354
#define SetIntItem(flag)
static PyObject * bpy_app_debug_get(PyObject *UNUSED(self), void *closure)
Definition: bpy_app.c:231
static int bpy_app_debug_value_set(PyObject *UNUSED(self), PyObject *value, void *UNUSED(closure))
Definition: bpy_app.c:314
char build_date[]
Definition: buildinfo.c:45
PyDoc_STRVAR(bpy_app_doc, "This module contains application values that remain unchanged during runtime.")
PyObject * BPY_app_struct(void)
Definition: bpy_app.c:488
char build_cxxflags[]
Definition: buildinfo.c:63
static PyObject * bpy_app_binary_path_python_get(PyObject *UNUSED(self), void *UNUSED(closure))
Definition: bpy_app.c:301
#define SetStrItem(str)
ulong build_commit_timestamp
Definition: buildinfo.c:48
static PyStructSequence_Desc app_info_desc
Definition: bpy_app.c:132
static int bpy_app_global_flag_set(PyObject *UNUSED(self), PyObject *value, void *closure)
Definition: bpy_app.c:266
char build_time[]
Definition: buildinfo.c:46
static PyTypeObject BlenderAppType
Definition: bpy_app.c:81
char build_platform[]
Definition: buildinfo.c:53
static PyObject * bpy_app_autoexec_fail_message_get(PyObject *UNUSED(self), void *UNUSED(closure))
Definition: bpy_app.c:359
#define SetObjItem(obj)
PyObject * BPY_app_alembic_struct(void)
PyObject * BPY_app_build_options_struct(void)
PyObject * BPY_app_ffmpeg_struct(void)
PyObject * BPY_app_handlers_struct(void)
PyObject * BPY_app_icons_module(void)
PyObject * BPY_app_ocio_struct(void)
Definition: bpy_app_ocio.c:95
PyObject * BPY_app_oiio_struct(void)
Definition: bpy_app_oiio.c:91
PyObject * BPY_app_opensubdiv_struct(void)
PyObject * BPY_app_openvdb_struct(void)
PyObject * BPY_app_sdl_struct(void)
Definition: bpy_app_sdl.c:128
PyObject * BPY_app_timers_module(void)
PyObject * BPY_app_translations_struct(void)
PyObject * BPY_app_usd_struct(void)
Definition: bpy_app_usd.c:94
PyObject * bpy_pydriver_Dict
Definition: bpy_driver.c:64
int bpy_pydriver_create_dict(void)
Definition: bpy_driver.c:73
uint pos
void * BKE_tempdir_session
int16_t PyC_Long_AsI16(PyObject *value)
PyObject * PyC_Err_SetString_Prefix(PyObject *exception_type_prefix, const char *str)
PyObject * PyC_UnicodeFromByte(const char *str)
#define PyC_Tuple_Pack_I32(...)
Definition: py_capi_utils.h:67
return ret
#define G(x, y, z)
void WM_main_add_notifier(unsigned int type, void *reference)