Blender V5.3
bpy_app_icons.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
10
11#include <Python.h>
12
13#include "MEM_guardedalloc.h"
14
15#include "BKE_icons.hh"
16
18#include "../generic/python_compat.hh" /* IWYU pragma: keep. */
19
20#include "bpy_app_icons.hh"
21
22namespace blender {
23
24/* We may want to load direct from file. */
26 /* Wrap. */
27 bpy_app_icons_new_triangles_doc,
28 ".. function:: new_triangles(range, coords, colors)\n"
29 "\n"
30 " Create a new icon from triangle geometry.\n"
31 "\n"
32 " :param range: Pair of ints.\n"
33 " :type range: tuple[int, int]\n"
34 " :param coords: Sequence of bytes (6 floats for one triangle) for (X, Y) coordinates.\n"
35 " :type coords: bytes\n"
36 " :param colors: Sequence of bytes (12 for one triangle) for RGBA.\n"
37 " :type colors: bytes\n"
38 " :return: Unique icon value (pass to interface ``icon_value`` argument).\n"
39 " :rtype: int\n");
40static PyObject *bpy_app_icons_new_triangles(PyObject * /*self*/, PyObject *args, PyObject *kw)
41{
42 /* bytes */
43 uchar coords_range[2];
44 PyObject *py_coords, *py_colors;
45
46 static const char *_keywords[] = {"range", "coords", "colors", nullptr};
47 static _PyArg_Parser _parser = {
48 "(BB)" /* `range` */
49 "S" /* `coords` */
50 "S" /* `colors` */
51 ":new_triangles",
52 _keywords,
53 nullptr,
54 };
55 if (!_PyArg_ParseTupleAndKeywordsFast(
56 args, kw, &_parser, &coords_range[0], &coords_range[1], &py_coords, &py_colors))
57 {
58 return nullptr;
59 }
60
61 const int coords_len = PyBytes_GET_SIZE(py_coords);
62 const int tris_len = coords_len / 6;
63 if (tris_len * 6 != coords_len) {
64 PyErr_SetString(PyExc_ValueError, "coords must be multiple of 6");
65 return nullptr;
66 }
67 if (PyBytes_GET_SIZE(py_colors) != 2 * coords_len) {
68 PyErr_SetString(PyExc_ValueError, "colors must be twice size of coords");
69 return nullptr;
70 }
71
72 const size_t items_num = size_t(tris_len) * 3;
73 uchar(*coords)[2] = MEM_new_array_uninitialized<uchar[2]>(items_num, __func__);
74 uchar(*colors)[4] = MEM_new_array_uninitialized<uchar[4]>(items_num, __func__);
75
76 memcpy(coords, PyBytes_AS_STRING(py_coords), sizeof(*coords) * items_num);
77 memcpy(colors, PyBytes_AS_STRING(py_colors), sizeof(*colors) * items_num);
78
80 geom->coords_len = tris_len;
81 geom->coords_range[0] = coords_range[0];
82 geom->coords_range[1] = coords_range[1];
83 geom->coords = coords;
84 geom->colors = colors;
85 geom->icon_id = 0;
86 geom->mem = nullptr;
87 const int icon_id = BKE_icon_geom_ensure(geom);
88 return PyLong_FromLong(icon_id);
89}
90
92 /* Wrap. */
93 bpy_app_icons_new_triangles_from_file_doc,
94 ".. function:: new_triangles_from_file(filepath)\n"
95 "\n"
96 " Create a new icon from triangle geometry.\n"
97 "\n"
98 " :param filepath: File path.\n"
99 " :type filepath: str | bytes\n"
100 " :return: Unique icon value (pass to interface ``icon_value`` argument).\n"
101 " :rtype: int\n");
102static PyObject *bpy_app_icons_new_triangles_from_file(PyObject * /*self*/,
103 PyObject *args,
104 PyObject *kw)
105{
106 PyC_UnicodeAsBytesAndSize_Data filepath_data = {nullptr};
107
108 static const char *_keywords[] = {"filepath", nullptr};
109 static _PyArg_Parser _parser = {
110 "O&" /* `filepath` */
111 ":new_triangles_from_file",
112 _keywords,
113 nullptr,
114 };
115 if (!_PyArg_ParseTupleAndKeywordsFast(
116 args, kw, &_parser, PyC_ParseUnicodeAsBytesAndSize, &filepath_data))
117 {
118 return nullptr;
119 }
120
121 Icon_Geom *geom = BKE_icon_geom_from_file(filepath_data.value);
122 Py_XDECREF(filepath_data.value_coerce);
123
124 if (geom == nullptr) {
125 PyErr_SetString(PyExc_ValueError, "Unable to load from file");
126 return nullptr;
127 }
128 const int icon_id = BKE_icon_geom_ensure(geom);
129 return PyLong_FromLong(icon_id);
130}
131
133 /* Wrap. */
134 bpy_app_icons_release_doc,
135 ".. function:: release(icon_id)\n"
136 "\n"
137 " Release the icon.\n"
138 "\n"
139 " :param icon_id: The icon id to release.\n"
140 " :type icon_id: int\n");
141static PyObject *bpy_app_icons_release(PyObject * /*self*/, PyObject *args, PyObject *kw)
142{
143 int icon_id;
144 static const char *_keywords[] = {"icon_id", nullptr};
145 static _PyArg_Parser _parser = {
146 "i" /* `icon_id` */
147 ":release",
148 _keywords,
149 nullptr,
150 };
151 if (!_PyArg_ParseTupleAndKeywordsFast(args, kw, &_parser, &icon_id)) {
152 return nullptr;
153 }
154
155 if (!BKE_icon_delete_unmanaged(icon_id)) {
156 PyErr_SetString(PyExc_ValueError, "invalid icon_id");
157 return nullptr;
158 }
159 Py_RETURN_NONE;
160}
161
162#ifdef __GNUC__
163# ifdef __clang__
164# pragma clang diagnostic push
165# pragma clang diagnostic ignored "-Wcast-function-type"
166# else
167# pragma GCC diagnostic push
168# pragma GCC diagnostic ignored "-Wcast-function-type"
169# endif
170#endif
171
172static PyMethodDef M_AppIcons_methods[] = {
173 {"new_triangles",
174 reinterpret_cast<PyCFunction>(bpy_app_icons_new_triangles),
175 METH_VARARGS | METH_KEYWORDS,
176 bpy_app_icons_new_triangles_doc},
177 {"new_triangles_from_file",
178 reinterpret_cast<PyCFunction>(bpy_app_icons_new_triangles_from_file),
179 METH_VARARGS | METH_KEYWORDS,
180 bpy_app_icons_new_triangles_from_file_doc},
181 {"release",
182 reinterpret_cast<PyCFunction>(bpy_app_icons_release),
183 METH_VARARGS | METH_KEYWORDS,
184 bpy_app_icons_release_doc},
185 {nullptr, nullptr, 0, nullptr},
186};
187
188#ifdef __GNUC__
189# ifdef __clang__
190# pragma clang diagnostic pop
191# else
192# pragma GCC diagnostic pop
193# endif
194#endif
195
196static PyModuleDef M_AppIcons_module_def = {
197 /*m_base*/ PyModuleDef_HEAD_INIT,
198 /*m_name*/ "bpy.app.icons",
199 /*m_doc*/ nullptr,
200 /*m_size*/ 0,
201 /*m_methods*/ M_AppIcons_methods,
202 /*m_slots*/ nullptr,
203 /*m_traverse*/ nullptr,
204 /*m_clear*/ nullptr,
205 /*m_free*/ nullptr,
206};
207
209{
210 PyObject *sys_modules = PyImport_GetModuleDict();
211
212 PyObject *mod = PyModule_Create(&M_AppIcons_module_def);
213
214 PyC_Module_AddToSysModules(sys_modules, mod);
215
216 return mod;
217}
218
219} // namespace blender
Read Blender memory allocation and freeing.
VecBase< float, D > constexpr mod(VecOp< float, D >, VecOp< float, D >) RET
BMesh Iterators.
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
bool BKE_icon_delete_unmanaged(int icon_id)
Definition icons.cc:563
static PyMethodDef M_AppIcons_methods[]
Icon_Geom * BKE_icon_geom_from_file(const char *filename)
Definition icons.cc:647
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
static PyModuleDef M_AppIcons_module_def
PyObject * BPY_app_icons_module()
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_app_icons_new_triangles(PyObject *, PyObject *args, PyObject *kw)
static PyObject * bpy_app_icons_release(PyObject *, PyObject *args, PyObject *kw)
unsigned char uchar
Definition BKE_text.h:58
int PyC_ParseUnicodeAsBytesAndSize(PyObject *o, void *p)
int PyC_Module_AddToSysModules(PyObject *sys_modules, PyObject *module)
int BKE_icon_geom_ensure(Icon_Geom *geom)
Definition icons.cc:592
static PyObject * bpy_app_icons_new_triangles_from_file(PyObject *, PyObject *args, PyObject *kw)
header-only compatibility defines.
const unsigned char * mem
Definition BKE_icons.hh:72
unsigned char(* colors)[4]
Definition BKE_icons.hh:70
unsigned char(* coords)[2]
Definition BKE_icons.hh:69