Blender V5.3
bpy.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
12
13/* Future-proof, See https://docs.python.org/3/c-api/arg.html#strings-and-buffers */
14#define PY_SSIZE_T_CLEAN
15
16#include <Python.h>
17#include <optional>
18
19#include "BLI_string.hh"
20#include "BLI_string_utils.hh"
21#include "BLI_utildefines.hh"
22
23#include "BKE_appdir.hh"
24#include "BKE_blender_version.h"
25#include "BKE_bpath.hh"
26#include "BKE_global.hh" /* XXX, G_MAIN only */
27
28#include "RNA_access.hh"
29#include "RNA_enum_types.hh"
30#include "RNA_prototypes.hh"
31
32#include "GPU_state.hh"
33
34#include "WM_api.hh" /* For #WM_ghost_backend */
35
36#include "bpy.hh"
37#include "bpy_app.hh"
38#include "bpy_cli_command.hh"
39#include "bpy_driver.hh"
40#include "bpy_geometry_set.hh"
42#include "bpy_library.hh"
43#include "bpy_operator.hh"
44#include "bpy_props.hh"
45#include "bpy_rna.hh"
46#include "bpy_rna_data.hh"
47#include "bpy_rna_gizmo.hh"
49#include "bpy_rna_types_capi.hh"
50#include "bpy_utils_previews.hh"
51#include "bpy_utils_units.hh"
52
54#include "../generic/python_compat.hh" /* IWYU pragma: keep. */
56
57/* external util modules */
60#include "bpy_msgbus.hh"
61
62#ifdef WITH_FREESTYLE
63# include "BPy_Freestyle.h"
64#endif
65
66namespace blender {
67
68PyObject *bpy_package_py = nullptr;
69
71 /* Wrap. */
72 bpy_script_paths_doc,
73 ".. function:: script_paths()\n"
74 "\n"
75 " Return 2 paths to blender scripts directories.\n"
76 "\n"
77 " :return: (system, user) strings will be empty when not found.\n"
78 " :rtype: tuple[str, str]\n");
79static PyObject *bpy_script_paths(PyObject * /*self*/)
80{
81 PyObject *ret = PyTuple_New(2);
82 PyObject *item;
83
84 std::optional<std::string> path = BKE_appdir_folder_id(BLENDER_SYSTEM_SCRIPTS, nullptr);
85 item = PyC_UnicodeFromStdStr(path.value_or(""));
86 BLI_assert(item != nullptr);
87 PyTuple_SET_ITEM(ret, 0, item);
89 item = PyC_UnicodeFromStdStr(path.value_or(""));
90 BLI_assert(item != nullptr);
91 PyTuple_SET_ITEM(ret, 1, item);
92
93 return ret;
94}
95
97 char * /*path_dst*/,
98 size_t /*path_dst_maxncpy*/,
99 const char *path_src)
100{
101 PyObject *py_list = static_cast<PyObject *>(bpath_data->user_data);
102 PyList_APPEND(py_list, PyC_UnicodeFromBytes(path_src));
103 return false; /* Never edits the path. */
104}
105
107 /* Wrap. */
108 bpy_blend_paths_doc,
109 ".. function:: blend_paths(*, absolute=False, packed=False, local=False)\n"
110 "\n"
111 " Returns a list of paths to external files referenced by the loaded .blend file.\n"
112 "\n"
113 " :param absolute: When true the paths returned are made absolute.\n"
114 " :type absolute: bool\n"
115 " :param packed: When true include file paths for packed data.\n"
116 " :type packed: bool\n"
117 " :param local: When true skip linked library paths.\n"
118 " :type local: bool\n"
119 " :return: path list.\n"
120 " :rtype: list[str]\n");
121static PyObject *bpy_blend_paths(PyObject * /*self*/, PyObject *args, PyObject *kw)
122{
124 PyObject *list;
125
126 bool absolute = false;
127 bool packed = false;
128 bool local = false;
129
130 static const char *_keywords[] = {"absolute", "packed", "local", nullptr};
131 static _PyArg_Parser _parser = {
132 "|$" /* Optional, keyword only arguments. */
133 "O&" /* `absolute` */
134 "O&" /* `packed` */
135 "O&" /* `local` */
136 ":blend_paths",
137 _keywords,
138 nullptr,
139 };
140 if (!_PyArg_ParseTupleAndKeywordsFast(args,
141 kw,
142 &_parser,
144 &absolute,
146 &packed,
148 &local))
149 {
150 return nullptr;
151 }
152
153 if (absolute) {
155 }
156 if (!packed) {
158 }
159 if (local) {
161 }
162
163 list = PyList_New(0);
164
165 BPathForeachPathData path_data{};
166 path_data.bmain = G_MAIN;
168 path_data.flag = flag;
169 path_data.user_data = list;
170 BKE_bpath_foreach_path_main(&path_data);
171
172 return list;
173}
174
176 /* Wrap. */
177 bpy_flip_name_doc,
178 ".. function:: flip_name(name, *, strip_digits=False)\n"
179 "\n"
180 " Flip a name between left/right sides, useful for \n"
181 " mirroring bone names.\n"
182 "\n"
183 " :param name: Bone name to flip.\n"
184 " :type name: str\n"
185 " :param strip_digits: Whether to remove ``.###`` suffix.\n"
186 " :type strip_digits: bool\n"
187 " :return: The flipped name.\n"
188 " :rtype: str\n");
189static PyObject *bpy_flip_name(PyObject * /*self*/, PyObject *args, PyObject *kw)
190{
191 const char *name_src = nullptr;
192 Py_ssize_t name_src_len;
193 bool strip_digits = false;
194
195 static const char *_keywords[] = {"", "strip_digits", nullptr};
196 static _PyArg_Parser _parser = {
197 "s#" /* `name` */
198 "|$" /* Optional, keyword only arguments. */
199 "O&" /* `strip_digits` */
200 ":flip_name",
201 _keywords,
202 nullptr,
203 };
204 if (!_PyArg_ParseTupleAndKeywordsFast(
205 args, kw, &_parser, &name_src, &name_src_len, PyC_ParseBool, &strip_digits))
206 {
207 return nullptr;
208 }
209
210 /* Worst case we gain one extra byte (besides null-terminator) by changing
211 * "Left" to "Right", because only the first appearance of "Left" gets replaced. */
212 const size_t size = name_src_len + 2;
213 char *name_dst = static_cast<char *>(PyMem_MALLOC(size));
214 const size_t name_dst_len = BLI_string_flip_side_name(name_dst, name_src, strip_digits, size);
215
216 PyObject *result = PyUnicode_FromStringAndSize(name_dst, name_dst_len);
217
218 PyMem_FREE(name_dst);
219
220 return result;
221}
222
223/* `bpy_user_resource_doc`, Now in `bpy/utils/__init__.py`. */
224static PyObject *bpy_user_resource(PyObject * /*self*/, PyObject *args, PyObject *kw)
225{
226 const PyC_StringEnumItems type_items[] = {
227 {BLENDER_USER_DATAFILES, "DATAFILES"},
228 {BLENDER_USER_CONFIG, "CONFIG"},
229 {BLENDER_USER_SCRIPTS, "SCRIPTS"},
230 {BLENDER_USER_EXTENSIONS, "EXTENSIONS"},
231 {0, nullptr},
232 };
233 PyC_StringEnum type = {type_items};
234 PyC_UnicodeAsBytesAndSize_Data subdir_data = {nullptr};
235
236 static const char *_keywords[] = {"type", "path", nullptr};
237 static _PyArg_Parser _parser = {
238 "O&" /* `type` */
239 "|$" /* Optional, keyword only arguments. */
240 "O&" /* `path` */
241 ":user_resource",
242 _keywords,
243 nullptr,
244 };
245 if (!_PyArg_ParseTupleAndKeywordsFast(args,
246 kw,
247 &_parser,
249 &type,
251 &subdir_data))
252 {
253 return nullptr;
254 }
255
256 /* same logic as BKE_appdir_folder_id_create(),
257 * but best leave it up to the script author to create */
258 const std::optional<std::string> path = BKE_appdir_folder_id_user_notest(type.value_found,
259 subdir_data.value);
260 Py_XDECREF(subdir_data.value_coerce);
261
262 return PyC_UnicodeFromStdStr(path.value_or(""));
263}
264
266 /* Wrap. */
267 bpy_system_resource_doc,
268 ".. function:: system_resource(type, *, path=\"\")\n"
269 "\n"
270 " Return a system resource path.\n"
271 "\n"
272 " :param type: string in ['DATAFILES', 'SCRIPTS', 'EXTENSIONS', 'PYTHON'].\n"
273 " :type type: str\n"
274 " :param path: Optional subdirectory.\n"
275 " :type path: str | bytes\n");
276static PyObject *bpy_system_resource(PyObject * /*self*/, PyObject *args, PyObject *kw)
277{
278 const PyC_StringEnumItems type_items[] = {
279 {BLENDER_SYSTEM_DATAFILES, "DATAFILES"},
280 {BLENDER_SYSTEM_SCRIPTS, "SCRIPTS"},
281 {BLENDER_SYSTEM_EXTENSIONS, "EXTENSIONS"},
282 {BLENDER_SYSTEM_PYTHON, "PYTHON"},
283 {0, nullptr},
284 };
285 PyC_StringEnum type = {type_items};
286
287 PyC_UnicodeAsBytesAndSize_Data subdir_data = {nullptr};
288
289 static const char *_keywords[] = {"type", "path", nullptr};
290 static _PyArg_Parser _parser = {
291 "O&" /* `type` */
292 "|$" /* Optional, keyword only arguments. */
293 "O&" /* `path` */
294 ":system_resource",
295 _keywords,
296 nullptr,
297 };
298 if (!_PyArg_ParseTupleAndKeywordsFast(args,
299 kw,
300 &_parser,
302 &type,
304 &subdir_data))
305 {
306 return nullptr;
307 }
308
309 std::optional<std::string> path = BKE_appdir_folder_id(type.value_found, subdir_data.value);
310 Py_XDECREF(subdir_data.value_coerce);
311
312 return PyC_UnicodeFromStdStr(path.value_or(""));
313}
314
316 /* Wrap. */
317 bpy_resource_path_doc,
318 ".. function:: resource_path(type, *, major=None, minor=None)\n"
319 "\n"
320 " Return the base path for storing system files.\n"
321 "\n"
322 " :param type: The resource type.\n"
323 " :type type: Literal['USER', 'LOCAL', 'SYSTEM', 'SYSTEM_LIBS']\n"
324 " :param major: Major version. None (the default) uses ``bpy.app.version[0]``.\n"
325 " :type major: int | None\n"
326 " :param minor: Minor version. None (the default) uses ``bpy.app.version[1]``.\n"
327 " :type minor: int | None\n"
328 " :return: the resource path (not necessarily existing).\n"
329 " :rtype: str\n"
330 "\n"
331 " .. note::\n"
332 "\n"
333 " ``SYSTEM_LIBS`` mirrors ``SYSTEM`` "
334 "but resolves to the directory for architecture-dependent libraries "
335 "(typically under ``/usr/lib/...`` rather than ``/usr/share/...``). "
336 "It is an empty string on builds without a separate library directory, "
337 "such as portable builds and the Python module.\n");
338static PyObject *bpy_resource_path(PyObject * /*self*/, PyObject *args, PyObject *kw)
339{
340 const PyC_StringEnumItems type_items[] = {
344 {BLENDER_RESOURCE_PATH_SYSTEM_LIBS, "SYSTEM_LIBS"},
345 {0, nullptr},
346 };
347 PyC_StringEnum type = {type_items};
348
349 std::optional<int> major;
350 std::optional<int> minor;
351
352 static const char *_keywords[] = {"type", "major", "minor", nullptr};
353 static _PyArg_Parser _parser = {
354 "O&" /* `type` */
355 "|$" /* Optional, keyword only arguments. */
356 "O&" /* `major` */
357 "O&" /* `minor` */
358 ":resource_path",
359 _keywords,
360 nullptr,
361 };
362 if (!_PyArg_ParseTupleAndKeywordsFast(args,
363 kw,
364 &_parser,
366 &type,
368 &major,
370 &minor))
371 {
372 return nullptr;
373 }
374
375 /* `None` (or missing) selects the current Blender version. */
376 const int version = major.value_or(BLENDER_VERSION / 100) * 100 +
377 minor.value_or(BLENDER_VERSION % 100);
378
379 const std::optional<std::string> path = BKE_appdir_resource_path_id_with_version(
380 type.value_found, false, version);
381
382 return PyC_UnicodeFromStdStr(path.value_or(""));
383}
384
385/* This is only exposed for tests, see: `tests/python/bl_pyapi_bpy_driver_secure_eval.py`. */
387 /* Wrap. */
388 bpy_driver_secure_code_test_doc,
389 ".. function:: _driver_secure_code_test(code, *, namespace=None, verbose=False)\n"
390 "\n"
391 " Test if the script should be considered trusted.\n"
392 "\n"
393 " :param code: The code to test.\n"
394 " :type code: code\n"
395 " :param namespace: The namespace of values which are allowed.\n"
396 " :type namespace: dict[str, Any] | None\n"
397 " :param verbose: Print the reason for considering insecure to the ``stderr``.\n"
398 " :type verbose: bool\n"
399 " :return: True when the script is considered trusted.\n"
400 " :rtype: bool\n");
401static PyObject *bpy_driver_secure_code_test(PyObject * /*self*/, PyObject *args, PyObject *kw)
402{
403 PyObject *py_code;
404 PyObject *py_namespace = nullptr;
405 PyC_TypeOrNone py_namespace_or_none = {&PyDict_Type, &py_namespace};
406 bool verbose = false;
407 static const char *_keywords[] = {"code", "namespace", "verbose", nullptr};
408 static _PyArg_Parser _parser = {
409 "O!" /* `code` */
410 "|$" /* Optional, keyword only arguments. */
411 "O&" /* `namespace` */
412 "O&" /* `verbose` */
413 ":driver_secure_code_test",
414 _keywords,
415 nullptr,
416 };
417 if (!_PyArg_ParseTupleAndKeywordsFast(args,
418 kw,
419 &_parser,
420 &PyCode_Type,
421 &py_code,
423 &py_namespace_or_none,
425 &verbose))
426 {
427 return nullptr;
428 }
429 return PyBool_FromLong(BPY_driver_secure_bytecode_test(py_code, py_namespace, verbose));
430}
431
433 /* Wrap. */
434 bpy_escape_identifier_doc,
435 ".. function:: escape_identifier(string)\n"
436 "\n"
437 " Simple string escaping function used for animation paths.\n"
438 "\n"
439 " :param string: text\n"
440 " :type string: str\n"
441 " :return: The escaped string.\n"
442 " :rtype: str\n");
443static PyObject *bpy_escape_identifier(PyObject * /*self*/, PyObject *value)
444{
445 Py_ssize_t value_str_len;
446 const char *value_str = PyUnicode_AsUTF8AndSize(value, &value_str_len);
447
448 if (value_str == nullptr) {
449 PyErr_SetString(PyExc_TypeError, "expected a string");
450 return nullptr;
451 }
452
453 const size_t size = (value_str_len * 2) + 1;
454 char *value_escape_str = static_cast<char *>(PyMem_MALLOC(size));
455 const Py_ssize_t value_escape_str_len = BLI_str_escape(value_escape_str, value_str, size);
456
457 PyObject *value_escape;
458 if (value_escape_str_len == value_str_len) {
459 Py_INCREF(value);
460 value_escape = value;
461 }
462 else {
463 value_escape = PyUnicode_FromStringAndSize(value_escape_str, value_escape_str_len);
464 }
465
466 PyMem_FREE(value_escape_str);
467
468 return value_escape;
469}
470
472 /* Wrap. */
473 bpy_unescape_identifier_doc,
474 ".. function:: unescape_identifier(string)\n"
475 "\n"
476 " Simple string un-escape function used for animation paths.\n"
477 " This performs the reverse of :func:`escape_identifier`.\n"
478 "\n"
479 " :param string: text\n"
480 " :type string: str\n"
481 " :return: The un-escaped string.\n"
482 " :rtype: str\n");
483static PyObject *bpy_unescape_identifier(PyObject * /*self*/, PyObject *value)
484{
485 Py_ssize_t value_str_len;
486 const char *value_str = PyUnicode_AsUTF8AndSize(value, &value_str_len);
487
488 if (value_str == nullptr) {
489 PyErr_SetString(PyExc_TypeError, "expected a string");
490 return nullptr;
491 }
492
493 const size_t size = value_str_len + 1;
494 char *value_unescape_str = static_cast<char *>(PyMem_MALLOC(size));
495 const Py_ssize_t value_unescape_str_len = BLI_str_unescape(
496 value_unescape_str, value_str, value_str_len);
497
498 PyObject *value_unescape;
499 if (value_unescape_str_len == value_str_len) {
500 Py_INCREF(value);
501 value_unescape = value;
502 }
503 else {
504 value_unescape = PyUnicode_FromStringAndSize(value_unescape_str, value_unescape_str_len);
505 }
506
507 PyMem_FREE(value_unescape_str);
508
509 return value_unescape;
510}
511
512extern "C" const char *buttons_context_dir[];
513extern "C" const char *clip_context_dir[];
514extern "C" const char *file_context_dir[];
515extern "C" const char *image_context_dir[];
516extern "C" const char *node_context_dir[];
517extern "C" const char *screen_context_dir[];
518extern "C" const char *sequencer_context_dir[];
519extern "C" const char *text_context_dir[];
520extern "C" const char *view3d_context_dir[];
521
526 /* Wrap. */
527 bpy_context_members_doc,
528 ".. function:: context_members()\n"
529 "\n"
530 " :return: A dict where the key is the context and the value is a tuple of it's members.\n"
531 " :rtype: dict[str, tuple[str]]\n");
532static PyObject *bpy_context_members(PyObject * /*self*/)
533{
534
535 struct {
536 const char *name;
537 const char **dir;
538 } context_members_all[] = {
539 {"buttons", buttons_context_dir},
540 {"clip", clip_context_dir},
541 {"file", file_context_dir},
542 {"image", image_context_dir},
543 {"node", node_context_dir},
544 {"screen", screen_context_dir},
545 {"sequencer", sequencer_context_dir},
546 {"text", text_context_dir},
547 {"view3d", view3d_context_dir},
548 };
549
550 PyObject *result = _PyDict_NewPresized(ARRAY_SIZE(context_members_all));
551 for (int context_index = 0; context_index < ARRAY_SIZE(context_members_all); context_index++) {
552 const char *name = context_members_all[context_index].name;
553 const char **dir = context_members_all[context_index].dir;
554 int i;
555 for (i = 0; dir[i]; i++) {
556 /* Pass. */
557 }
558 PyObject *members = PyTuple_New(i);
559 for (i = 0; dir[i]; i++) {
560 PyTuple_SET_ITEM(members, i, PyUnicode_FromString(dir[i]));
561 }
562 PyDict_SetItemString(result, name, members);
563 Py_DECREF(members);
564 }
565 BLI_assert(PyDict_GET_SIZE(result) == ARRAY_SIZE(context_members_all));
566
567 return result;
568}
569
574 /* Wrap. */
575 bpy_rna_enum_items_static_doc,
576 ".. function:: rna_enum_items_static()\n"
577 "\n"
578 " :return: A dict where the key the name of the enum, the value is a tuple of enum items.\n"
579 " :rtype: dict[str, tuple[:class:`bpy.types.EnumPropertyItem`]]\n");
580static PyObject *bpy_rna_enum_items_static(PyObject * /*self*/)
581{
582#define DEF_ENUM(id) {STRINGIFY(id), id},
583 struct {
584 const char *id;
585 const EnumPropertyItem *items;
586 } enum_info[] = {
587#include "RNA_enum_items.hh"
588 };
589 PyObject *result = _PyDict_NewPresized(ARRAY_SIZE(enum_info));
590 for (int i = 0; i < ARRAY_SIZE(enum_info); i++) {
591 /* Include all items (including headings & separators), can be shown in documentation. */
592 const EnumPropertyItem *items = enum_info[i].items;
593 const int items_count = RNA_enum_items_count(items);
594 PyObject *value = PyTuple_New(items_count);
595 for (int item_index = 0; item_index < items_count; item_index++) {
597 nullptr, RNA_EnumPropertyItem, (void *)&items[item_index]);
598 PyTuple_SET_ITEM(value, item_index, pyrna_struct_CreatePyObject(&ptr));
599 }
600 PyDict_SetItemString(result, enum_info[i].id, value);
601 Py_DECREF(value);
602 }
603 return result;
604}
605
606/* This is only exposed for (Unix/Linux), see: #GHOST_ISystem::getSystemBackend for details. */
608 /* Wrap. */
609 bpy_ghost_backend_doc,
610 ".. function:: _ghost_backend()\n"
611 "\n"
612 " :return: An identifier for the GHOST back-end.\n"
613 " :rtype: str\n");
614static PyObject *bpy_ghost_backend(PyObject * /*self*/)
615{
616 return PyUnicode_FromString(WM_ghost_backend());
617}
618
619/* NOTE(@ideasman42): This is a private function because the keys in the returned dictionary,
620 * are not considered stable. Sometimes a function is temporarily only supported by one platform.
621 * Once all platforms support the functionality there is no need for the flag
622 * and it can be removed. This is at odds with a public API that has values which are
623 * intended to be kept between releases.
624 * If this were to be made public we would have to document that this is subject to change. */
625
627 /* Wrap. */
628 bpy_wm_capabilities_doc,
629 ".. function:: _wm_capabilities()\n"
630 "\n"
631 " :return: A dictionary of capabilities (string keys, boolean values).\n"
632 " :rtype: dict[str, bool]\n");
633static PyObject *bpy_wm_capabilities(PyObject *self)
634{
635 PyObject *py_id_capabilities = PyUnicode_FromString("_wm_capabilities_");
636 PyObject *result = nullptr;
637 switch (PyObject_GetOptionalAttr(self, py_id_capabilities, &result)) {
638 case 1: {
639 BLI_assert(result != nullptr);
640 break;
641 }
642 case 0: {
643 result = PyDict_New();
644
646
647#define SetFlagItem(x) \
648 PyDict_SetItemString(result, STRINGIFY(x), ((WM_CAPABILITY_##x) & flag) ? Py_True : Py_False);
649
650 /* Only exposed flags which are used, by Blender's built-in scripts
651 * since this is a private API. */
652
653 SetFlagItem(TRACKPAD_PHYSICAL_DIRECTION);
654 SetFlagItem(KEYBOARD_HYPER_KEY);
655
656#undef SetFlagItem
657 PyObject_SetAttr(self, py_id_capabilities, result);
658 break;
659 }
660 default:
661 /* Unlikely, but there may be an error, forward it. */
662 BLI_assert(result == nullptr);
663 break;
664 }
665
666 Py_DECREF(py_id_capabilities);
667 return result;
668}
669
670#ifdef __GNUC__
671# ifdef __clang__
672# pragma clang diagnostic push
673# pragma clang diagnostic ignored "-Wcast-function-type"
674# else
675# pragma GCC diagnostic push
676# pragma GCC diagnostic ignored "-Wcast-function-type"
677# endif
678#endif
679
680static PyMethodDef bpy_methods[] = {
681 {"script_paths",
682 reinterpret_cast<PyCFunction>(bpy_script_paths),
683 METH_NOARGS,
684 bpy_script_paths_doc},
685 {"blend_paths",
686 reinterpret_cast<PyCFunction>(bpy_blend_paths),
687 METH_VARARGS | METH_KEYWORDS,
688 bpy_blend_paths_doc},
689 {"flip_name",
690 reinterpret_cast<PyCFunction>(bpy_flip_name),
691 METH_VARARGS | METH_KEYWORDS,
692 bpy_flip_name_doc},
693 {"user_resource",
694 reinterpret_cast<PyCFunction>(bpy_user_resource),
695 METH_VARARGS | METH_KEYWORDS,
696 nullptr},
697 {"system_resource",
698 reinterpret_cast<PyCFunction>(bpy_system_resource),
699 METH_VARARGS | METH_KEYWORDS,
700 bpy_system_resource_doc},
701 {"resource_path",
702 reinterpret_cast<PyCFunction>(bpy_resource_path),
703 METH_VARARGS | METH_KEYWORDS,
704 bpy_resource_path_doc},
705 {"escape_identifier",
706 static_cast<PyCFunction>(bpy_escape_identifier),
707 METH_O,
708 bpy_escape_identifier_doc},
709 {"unescape_identifier",
710 static_cast<PyCFunction>(bpy_unescape_identifier),
711 METH_O,
712 bpy_unescape_identifier_doc},
713 {"context_members",
714 reinterpret_cast<PyCFunction>(bpy_context_members),
715 METH_NOARGS,
716 bpy_context_members_doc},
717 {"rna_enum_items_static",
718 reinterpret_cast<PyCFunction>(bpy_rna_enum_items_static),
719 METH_NOARGS,
720 bpy_rna_enum_items_static_doc},
721
722 /* Private functions (not part of the public API and may be removed at any time). */
723 {"_driver_secure_code_test",
724 reinterpret_cast<PyCFunction>(bpy_driver_secure_code_test),
725 METH_VARARGS | METH_KEYWORDS,
726 bpy_driver_secure_code_test_doc},
727 {"_ghost_backend",
728 reinterpret_cast<PyCFunction>(bpy_ghost_backend),
729 METH_NOARGS,
730 bpy_ghost_backend_doc},
731 {"_wm_capabilities",
732 reinterpret_cast<PyCFunction>(bpy_wm_capabilities),
733 METH_NOARGS,
734 bpy_wm_capabilities_doc},
735
736 {nullptr, nullptr, 0, nullptr},
737};
738
739#ifdef __GNUC__
740# ifdef __clang__
741# pragma clang diagnostic pop
742# else
743# pragma GCC diagnostic pop
744# endif
745#endif
746
747static PyObject *bpy_import_test(const char *modname)
748{
749 PyObject *mod = PyImport_ImportModuleLevel(modname, nullptr, nullptr, nullptr, 0);
750
751 if (mod) {
752 Py_DECREF(mod);
753 }
754 else {
755 PyErr_Print();
756 }
757
758 return mod;
759}
760
762{
763 PyObject *mod;
764
765 /* Needs to be first since this dir is needed for future modules */
766 const std::optional<std::string> modpath = BKE_appdir_folder_id(BLENDER_SYSTEM_SCRIPTS,
767 "modules");
768 if (modpath.has_value()) {
769 // printf("bpy: found module path '%s'.\n", modpath);
770 PyObject *sys_path = PySys_GetObject("path"); /* borrow */
771 PyObject *py_modpath = PyC_UnicodeFromStdStr(modpath.value());
772 PyList_Insert(sys_path, 0, py_modpath); /* add first */
773 Py_DECREF(py_modpath);
774 }
775 else {
776 printf("bpy: couldn't find 'scripts/modules', blender probably won't start.\n");
777 }
778 /* stand alone utility modules not related to blender directly */
779 IDProp_Init_Types(); /* not actually a submodule, just types */
781#ifdef WITH_FREESTYLE
783#endif
784
785 mod = PyModule_New("_bpy");
786
787 /* add the module so we can import it */
788 PyDict_SetItemString(PyImport_GetModuleDict(), "_bpy", mod);
789 Py_DECREF(mod);
790
791 /* Needs to be first so `_bpy_types` can run. */
792 PyObject *bpy_types = BPY_rna_types();
793 PyModule_AddObject(bpy_types, "GeometrySet", BPyInit_geometry_set_type());
794 PyModule_AddObject(bpy_types, "InlineShaderNodes", BPyInit_inline_shader_nodes_type());
795 PyModule_AddObject(bpy_types, "BlendDataPathMeta", BPyInit_blend_data_path_meta_type());
796 PyModule_AddObject(mod, "types", bpy_types);
797
798 /* Needs to be first so `_bpy_types` can run. */
800
802
804
805 /* Important to internalizes `_bpy_types` before creating RNA instances. */
806 {
807 /* Set a dummy module so the `_bpy_types.py` can access `bpy.types.ID`
808 * without a null pointer dereference when instancing types. */
809 PyObject *bpy_types_dict_dummy = PyDict_New();
810 BPY_rna_types_dict_set(bpy_types_dict_dummy);
811 PyObject *bpy_types_module_py = bpy_import_test("_bpy_types");
812 /* Something has gone wrong if this is ever populated. */
813 BLI_assert(PyDict_GET_SIZE(bpy_types_dict_dummy) == 0);
814 Py_DECREF(bpy_types_dict_dummy);
815
816 PyObject *bpy_types_module_py_dict = PyModule_GetDict(bpy_types_module_py);
817 BPY_rna_types_dict_set(bpy_types_module_py_dict);
818 }
819 PyModule_AddObject(mod, "data", BPY_rna_module());
821
822 PyModule_AddObject(mod, "props", BPY_rna_props());
823 PyModule_AddObject(mod, "ops", BPY_operator_module());
824 PyModule_AddObject(mod, "app", BPY_app_struct());
825 PyModule_AddObject(mod, "_utils_units", BPY_utils_units());
826 PyModule_AddObject(mod, "_utils_previews", BPY_utils_previews_module());
827 PyModule_AddObject(mod, "msgbus", BPY_msgbus_module());
828
829 PointerRNA ctx_ptr = RNA_pointer_create_discrete(nullptr, RNA_Context, C);
830 bpy_context_module = reinterpret_cast<BPy_StructRNA *>(pyrna_struct_CreatePyObject(&ctx_ptr));
831 PyModule_AddObject(mod, "context", reinterpret_cast<PyObject *>(bpy_context_module));
832
833 /* Register methods and property get/set for RNA types. */
834 BPY_rna_types_extend_capi(bpy_types);
835
836#define PYMODULE_ADD_METHOD(mod, meth) \
837 PyModule_AddObject(mod, (meth)->ml_name, (PyObject *)PyCFunction_New(meth, mod))
838
839 for (int i = 0; bpy_methods[i].ml_name; i++) {
840 PyMethodDef *m = &bpy_methods[i];
841 /* Currently there is no need to support these. */
842 BLI_assert((m->ml_flags & (METH_CLASS | METH_STATIC)) == 0);
844 }
845
846 /* Register functions (`bpy_rna.cc`). */
849
852
853 /* Register command functions. */
856
857#undef PYMODULE_ADD_METHOD
858
859 /* add our own modules dir, this is a python package */
861}
862
863} // namespace blender
#define BLENDER_VERSION
#define G_MAIN
PyObject * Freestyle_Init()
#define C
Definition RandGen.cpp:29
#define PYMODULE_ADD_METHOD(mod, meth)
#define SetFlagItem(x)
btMatrix3x3 absolute() const
Return the matrix with all values non negative.
Definition btVector3.h:364
VecBase< float, D > constexpr mod(VecOp< float, D >, VecOp< float, D >) RET
BMesh Iterators.
static PyObject * bpy_resource_path(PyObject *, PyObject *args, PyObject *kw)
Definition bpy.cc:338
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
const char * file_context_dir[]
const char * node_context_dir[]
static PyObject * bpy_wm_capabilities(PyObject *self)
Definition bpy.cc:633
const char * WM_ghost_backend()
void IDProp_Init_Types()
int PyC_ParseBool(PyObject *o, void *p)
static bool bpy_blend_foreach_path_cb(BPathForeachPathData *bpath_data, char *, size_t, const char *path_src)
Definition bpy.cc:96
PyObject * BPY_rna_module()
Definition bpy_rna.cc:9081
void IDPropertyUIData_Init_Types()
bool BPY_rna_gizmo_module(PyObject *mod_par)
BPy_StructRNA * bpy_context_module
Definition bpy_rna.cc:100
PyMethodDef meth_bpy_owner_id_set
Definition bpy_rna.cc:11036
const char * image_context_dir[]
void BKE_bpath_foreach_path_main(BPathForeachPathData *bpath_data)
Definition bpath.cc:121
PyDoc_STRVAR(bpy_bm_new_doc, ".. function:: new(*, use_operators=True)\n" "\n" " :param use_operators: Support calling operators in :mod:`bmesh.ops` (uses some " "extra memory per vert/edge/face).\n" " :type use_operators: bool\n" " :return: Return a new, empty BMesh.\n" " :rtype: :class:`bmesh.types.BMesh`\n")
static PyObject * bpy_driver_secure_code_test(PyObject *, PyObject *args, PyObject *kw)
Definition bpy.cc:401
const char * buttons_context_dir[]
PyObject * bpy_package_py
Definition bpy.cc:68
PyObject * BPY_operator_module()
static PyObject * bpy_system_resource(PyObject *, PyObject *args, PyObject *kw)
Definition bpy.cc:276
static PyObject * bpy_context_members(PyObject *)
Definition bpy.cc:532
uint RNA_enum_items_count(const EnumPropertyItem *item)
void BPY_rna_types_dict_set(PyObject *dict)
Definition bpy_rna.cc:8615
PyObject * pyrna_struct_CreatePyObject(PointerRNA *ptr)
Definition bpy_rna.cc:8874
@ BLENDER_RESOURCE_PATH_SYSTEM_LIBS
@ BLENDER_RESOURCE_PATH_SYSTEM
@ BLENDER_RESOURCE_PATH_USER
@ BLENDER_RESOURCE_PATH_LOCAL
PyMethodDef meth_bpy_unregister_class
Definition bpy_rna.cc:10808
PyObject * PyC_UnicodeFromBytes(const char *str)
PyObject * BPyInit_blend_data_path_meta_type()
PyObject * BPyInit_geometry_set_type()
void BPY_rna_types_finalize_external_types(PyObject *submodule)
Definition bpy_rna.cc:9277
bool BPY_driver_secure_bytecode_test(PyObject *expr_code, PyObject *py_namespace, const bool verbose)
std::optional< std::string > BKE_appdir_folder_id(int folder_id, const char *subfolder) ATTR_WARN_UNUSED_RESULT
Definition appdir.cc:762
static PyObject * bpy_ghost_backend(PyObject *)
Definition bpy.cc:614
PyObject * BPY_utils_units()
PyObject * BPY_utils_previews_module()
PyMethodDef meth_bpy_register_class
Definition bpy_rna.cc:10547
int BPY_library_load_type_ready()
PyObject * BPyInit_inline_shader_nodes_type()
char char size_t BLI_str_escape(char *__restrict dst, StringRef src, size_t dst_maxncpy) ATTR_NONNULL(1)
PyObject * BPY_msgbus_module()
PyMethodDef BPY_cli_command_register_def
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
PyObject * BPY_rna_types()
Definition bpy_rna.cc:9246
int PyC_ParseOptionalInt(PyObject *o, void *p)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
int PyC_ParseUnicodeAsBytesAndSize(PyObject *o, void *p)
const char * clip_context_dir[]
static PyObject * bpy_escape_identifier(PyObject *, PyObject *value)
Definition bpy.cc:443
const char * view3d_context_dir[]
const char * text_context_dir[]
static PyObject * bpy_blend_paths(PyObject *, PyObject *args, PyObject *kw)
Definition bpy.cc:121
std::optional< std::string > BKE_appdir_folder_id_user_notest(int folder_id, const char *subfolder) ATTR_WARN_UNUSED_RESULT
Definition appdir.cc:771
static PyObject * bpy_import_test(const char *modname)
Definition bpy.cc:747
static PyMethodDef bpy_methods[]
Definition bpy.cc:680
eWM_CapabilitiesFlag
Definition WM_api.hh:206
eBPathForeachFlag
Definition BKE_bpath.hh:39
@ BKE_BPATH_FOREACH_PATH_SKIP_PACKED
Definition BKE_bpath.hh:49
@ BKE_BPATH_FOREACH_PATH_ABSOLUTE
Definition BKE_bpath.hh:45
@ BKE_BPATH_FOREACH_PATH_SKIP_LINKED
Definition BKE_bpath.hh:47
void BPy_init_modules(bContext *C)
Definition bpy.cc:761
eWM_CapabilitiesFlag WM_capabilities_flag()
std::optional< std::string > BKE_appdir_resource_path_id_with_version(int folder_id, bool check_is_dir, int version)
Definition appdir.cc:847
size_t size_t BLI_str_unescape(char *__restrict dst, const char *__restrict src, size_t src_len) ATTR_NONNULL(1
PyObject * PyC_UnicodeFromStdStr(const std::string &str)
const char * screen_context_dir[]
static PyObject * bpy_unescape_identifier(PyObject *, PyObject *value)
Definition bpy.cc:483
int PyC_ParseStringEnum(PyObject *o, void *p)
static PyObject * bpy_flip_name(PyObject *, PyObject *args, PyObject *kw)
Definition bpy.cc:189
@ BLENDER_SYSTEM_EXTENSIONS
@ BLENDER_USER_EXTENSIONS
@ BLENDER_USER_CONFIG
@ BLENDER_SYSTEM_PYTHON
@ BLENDER_SYSTEM_DATAFILES
@ BLENDER_USER_DATAFILES
@ BLENDER_SYSTEM_SCRIPTS
@ BLENDER_USER_SCRIPTS
void BPY_rna_types_extend_capi(PyObject *bpy_types)
PyMethodDef BPY_cli_command_unregister_def
int PyC_ParseTypeOrNone(PyObject *o, void *p)
PyMethodDef meth_bpy_owner_id_get
Definition bpy_rna.cc:11030
static PyObject * bpy_user_resource(PyObject *, PyObject *args, PyObject *kw)
Definition bpy.cc:224
static PyObject * bpy_script_paths(PyObject *)
Definition bpy.cc:79
int BPY_rna_data_context_type_ready()
const char * sequencer_context_dir[]
Definition bpy.cc:518
PyObject * BPY_rna_props()
PyObject * BPY_app_struct()
Definition bpy_app.cc:891
size_t BLI_string_flip_side_name(char *name_dst, const char *name_src, bool strip_number, size_t name_dst_maxncpy) ATTR_NONNULL(1
header-only compatibility defines.
header-only utilities
BPathForeachPathFunctionCallback callback_function
Definition BKE_bpath.hh:124