Blender  V2.93
bpy_app_icons.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 
23 #include <Python.h>
24 
25 #include "MEM_guardedalloc.h"
26 
27 #include "BLI_utildefines.h"
28 
29 #include "BKE_icons.h"
30 
31 #include "../generic/py_capi_utils.h"
32 
33 #include "bpy_app_icons.h"
34 
35 /* We may want to load direct from file. */
37  bpy_app_icons_new_triangles_doc,
38  ".. function:: new_triangles(range, coords, colors)"
39  "\n"
40  " Create a new icon from triangle geometry.\n"
41  "\n"
42  " :arg range: Pair of ints.\n"
43  " :type range: tuple.\n"
44  " :arg coords: Sequence of bytes (6 floats for one triangle) for (X, Y) coordinates.\n"
45  " :type coords: byte sequence.\n"
46  " :arg colors: Sequence of ints (12 for one triangles) for RGBA.\n"
47  " :type colors: byte sequence.\n"
48  " :return: Unique icon value (pass to interface ``icon_value`` argument).\n"
49  " :rtype: int\n");
50 static PyObject *bpy_app_icons_new_triangles(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
51 {
52  /* bytes */
53  uchar coords_range[2];
54  PyObject *py_coords, *py_colors;
55 
56  static const char *_keywords[] = {"range", "coords", "colors", NULL};
57  static _PyArg_Parser _parser = {"(BB)SS:new_triangles", _keywords, 0};
58  if (!_PyArg_ParseTupleAndKeywordsFast(
59  args, kw, &_parser, &coords_range[0], &coords_range[1], &py_coords, &py_colors)) {
60  return NULL;
61  }
62 
63  const int coords_len = PyBytes_GET_SIZE(py_coords);
64  const int tris_len = coords_len / 6;
65  if (tris_len * 6 != coords_len) {
66  PyErr_SetString(PyExc_ValueError, "coords must be multiple of 6");
67  return NULL;
68  }
69  if (PyBytes_GET_SIZE(py_colors) != 2 * coords_len) {
70  PyErr_SetString(PyExc_ValueError, "colors must be twice size of coords");
71  return NULL;
72  }
73 
74  const int coords_size = sizeof(uchar[2]) * tris_len * 3;
75  const int colors_size = sizeof(uchar[4]) * tris_len * 3;
76  uchar(*coords)[2] = MEM_mallocN(coords_size, __func__);
77  uchar(*colors)[4] = MEM_mallocN(colors_size, __func__);
78 
79  memcpy(coords, PyBytes_AS_STRING(py_coords), coords_size);
80  memcpy(colors, PyBytes_AS_STRING(py_colors), colors_size);
81 
82  struct Icon_Geom *geom = MEM_mallocN(sizeof(*geom), __func__);
83  geom->coords_len = tris_len;
84  geom->coords_range[0] = coords_range[0];
85  geom->coords_range[1] = coords_range[1];
86  geom->coords = coords;
87  geom->colors = colors;
88  geom->icon_id = 0;
89  const int icon_id = BKE_icon_geom_ensure(geom);
90  return PyLong_FromLong(icon_id);
91 }
92 
93 PyDoc_STRVAR(bpy_app_icons_new_triangles_from_file_doc,
94  ".. function:: new_triangles_from_file(filename)"
95  "\n"
96  " Create a new icon from triangle geometry.\n"
97  "\n"
98  " :arg filename: File path.\n"
99  " :type filename: string.\n"
100  " :return: Unique icon value (pass to interface ``icon_value`` argument).\n"
101  " :rtype: int\n");
102 static PyObject *bpy_app_icons_new_triangles_from_file(PyObject *UNUSED(self),
103  PyObject *args,
104  PyObject *kw)
105 {
106  /* bytes */
107  char *filename;
108 
109  static const char *_keywords[] = {"filename", NULL};
110  static _PyArg_Parser _parser = {"s:new_triangles_from_file", _keywords, 0};
111  if (!_PyArg_ParseTupleAndKeywordsFast(args, kw, &_parser, &filename)) {
112  return NULL;
113  }
114 
115  struct Icon_Geom *geom = BKE_icon_geom_from_file(filename);
116  if (geom == NULL) {
117  PyErr_SetString(PyExc_ValueError, "Unable to load from file");
118  return NULL;
119  }
120  const int icon_id = BKE_icon_geom_ensure(geom);
121  return PyLong_FromLong(icon_id);
122 }
123 
124 PyDoc_STRVAR(bpy_app_icons_release_doc,
125  ".. function:: release(icon_id)"
126  "\n"
127  " Release the icon.\n");
128 static PyObject *bpy_app_icons_release(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
129 {
130  int icon_id;
131  static const char *_keywords[] = {"icon_id", NULL};
132  static _PyArg_Parser _parser = {"i:release", _keywords, 0};
133  if (!_PyArg_ParseTupleAndKeywordsFast(args, kw, &_parser, &icon_id)) {
134  return NULL;
135  }
136 
138  PyErr_SetString(PyExc_ValueError, "invalid icon_id");
139  return NULL;
140  }
141  Py_RETURN_NONE;
142 }
143 
144 static struct PyMethodDef M_AppIcons_methods[] = {
145  {"new_triangles",
146  (PyCFunction)bpy_app_icons_new_triangles,
147  METH_VARARGS | METH_KEYWORDS,
148  bpy_app_icons_new_triangles_doc},
149  {"new_triangles_from_file",
151  METH_VARARGS | METH_KEYWORDS,
152  bpy_app_icons_new_triangles_from_file_doc},
153  {"release",
154  (PyCFunction)bpy_app_icons_release,
155  METH_VARARGS | METH_KEYWORDS,
156  bpy_app_icons_release_doc},
157  {NULL, NULL, 0, NULL},
158 };
159 
160 static struct PyModuleDef M_AppIcons_module_def = {
161  PyModuleDef_HEAD_INIT,
162  "bpy.app.icons", /* m_name */
163  NULL, /* m_doc */
164  0, /* m_size */
165  M_AppIcons_methods, /* m_methods */
166  NULL, /* m_reload */
167  NULL, /* m_traverse */
168  NULL, /* m_clear */
169  NULL, /* m_free */
170 };
171 
172 PyObject *BPY_app_icons_module(void)
173 {
174  PyObject *sys_modules = PyImport_GetModuleDict();
175 
176  PyObject *mod = PyModule_Create(&M_AppIcons_module_def);
177 
178  PyDict_SetItem(sys_modules, PyModule_GetNameObject(mod), mod);
179 
180  return mod;
181 }
struct Icon_Geom * BKE_icon_geom_from_file(const char *filename)
Definition: icons.cc:1040
int BKE_icon_geom_ensure(struct Icon_Geom *geom)
Definition: icons.cc:985
bool BKE_icon_delete_unmanaged(const int icon_id)
Definition: icons.cc:957
unsigned char uchar
Definition: BLI_sys_types.h:86
#define UNUSED(x)
Read Guarded memory(de)allocation.
PyDoc_STRVAR(bpy_app_icons_new_triangles_doc, ".. function:: new_triangles(range, coords, colors)" "\n" " Create a new icon from triangle geometry.\n" "\n" " :arg range: Pair of ints.\n" " :type range: tuple.\n" " :arg coords: Sequence of bytes (6 floats for one triangle) for (X, Y) coordinates.\n" " :type coords: byte sequence.\n" " :arg colors: Sequence of ints (12 for one triangles) for RGBA.\n" " :type colors: byte sequence.\n" " :return: Unique icon value (pass to interface ``icon_value`` argument).\n" " :rtype: int\n")
PyObject * BPY_app_icons_module(void)
static PyObject * bpy_app_icons_release(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
static struct PyModuleDef M_AppIcons_module_def
static PyObject * bpy_app_icons_new_triangles(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
Definition: bpy_app_icons.c:50
static PyObject * bpy_app_icons_new_triangles_from_file(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
static struct PyMethodDef M_AppIcons_methods[]
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
int coords_len
Definition: BKE_icons.h:78
unsigned char(* colors)[4]
Definition: BKE_icons.h:81
int icon_id
Definition: BKE_icons.h:77
unsigned char(* coords)[2]
Definition: BKE_icons.h:80
int coords_range[2]
Definition: BKE_icons.h:79
ccl_device_inline int mod(int x, int m)
Definition: util_math.h:405