Blender V5.3
bpy_utils_previews.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
13
14#include <Python.h>
15#include <structmember.h>
16
17#include "RNA_access.hh"
18#include "RNA_prototypes.hh"
19
20#include "bpy_rna.hh"
21#include "bpy_utils_previews.hh"
22
24
25#include "IMB_thumbs.hh"
26
27#include "BKE_preview_image.hh"
28
29namespace blender {
30
31#define PYDOC_SOURCE_TYPES_LITERAL "Literal['IMAGE', 'MOVIE', 'BLEND', 'FONT', 'OBJECT_IO']"
32
34 /* Wrap. */
35 bpy_utils_previews_new_doc,
36 ".. method:: new(name)\n"
37 "\n"
38 " Generate a new empty preview.\n"
39 "\n"
40 " :param name: The name (unique id) identifying the preview.\n"
41 " :type name: str\n"
42 " :return: The Preview matching given name, or a new empty one.\n"
43 " :rtype: :class:`bpy.types.ImagePreview`\n"
44 /* This is only true when accessed via `bpy.utils.previews.ImagePreviewCollection.load`,
45 * however this is the public API, allow this minor difference to the internal version here. */
46 " :raises KeyError: if ``name`` already exists.\n");
47static PyObject *bpy_utils_previews_new(PyObject * /*self*/, PyObject *args)
48{
49 char *name;
50 PreviewImage *prv;
51
52 if (!PyArg_ParseTuple(args,
53 "s" /* `name` */
54 ":new",
55 &name))
56 {
57 return nullptr;
58 }
59
61 PointerRNA ptr = RNA_pointer_create_discrete(nullptr, RNA_ImagePreview, prv);
62
63 return pyrna_struct_CreatePyObject(&ptr);
64}
65
67 /* Wrap. */
68 bpy_utils_previews_load_doc,
69 ".. method:: load(name, filepath, file_type, force_reload=False)\n"
70 "\n"
71 " Generate a new preview from given file path.\n"
72 "\n"
73 " :param name: The name (unique id) identifying the preview.\n"
74 " :type name: str\n"
75 " :param filepath: The file path to generate the preview from.\n"
76 " :type filepath: str | bytes\n"
77 " :param file_type: The type of file, needed to generate the preview.\n"
78 " :type file_type: " PYDOC_SOURCE_TYPES_LITERAL
79 "\n"
80 " :param force_reload: If True, force running thumbnail manager even if preview already "
81 "exists in cache.\n"
82 " :type force_reload: bool\n"
83 " :return: The Preview matching given name, or a new empty one.\n"
84 " :rtype: :class:`bpy.types.ImagePreview`\n"
85 /* This is only true when accessed via 'bpy.utils.previews.ImagePreviewCollection.load',
86 * however this is the public API, allow this minor difference to the internal version here. */
87 " :raises KeyError: if ``name`` already exists.\n");
88static PyObject *bpy_utils_previews_load(PyObject * /*self*/, PyObject *args)
89{
90 char *name;
91 PyC_UnicodeAsBytesAndSize_Data filepath_data = {nullptr};
92 /* Be sure to keep these in sync with #PYDOC_SOURCE_TYPES_LITERAL. */
93 const PyC_StringEnumItems path_type_items[] = {
94 {THB_SOURCE_IMAGE, "IMAGE"},
95 {THB_SOURCE_MOVIE, "MOVIE"},
96 {THB_SOURCE_BLEND, "BLEND"},
97 {THB_SOURCE_FONT, "FONT"},
98 {THB_SOURCE_OBJECT_IO, "OBJECT_IO"},
99 {0, nullptr},
100 };
101 PyC_StringEnum path_type = {
102 path_type_items,
103 /* The default isn't used. */
104 0,
105 };
106 int force_reload = false;
107
108 if (!PyArg_ParseTuple(args,
109 "s" /* `name` */
110 "O&" /* `filepath` */
111 "O&" /* `file_type` */
112 "|" /* Optional arguments. */
113 "p" /* `force_reload` */
114 ":load",
115 &name,
117 &filepath_data,
119 &path_type,
120 &force_reload))
121 {
122 return nullptr;
123 }
124
126 name, filepath_data.value, path_type.value_found, force_reload);
127
128 Py_XDECREF(filepath_data.value_coerce);
129
130 PointerRNA ptr = RNA_pointer_create_discrete(nullptr, RNA_ImagePreview, prv);
131 return pyrna_struct_CreatePyObject(&ptr);
132}
133
135 /* Wrap. */
136 bpy_utils_previews_release_doc,
137 ".. method:: release(name)\n"
138 "\n"
139 " Release (free) a previously created preview.\n"
140 "\n"
141 "\n"
142 " :param name: The name (unique id) identifying the preview.\n"
143 " :type name: str\n");
144static PyObject *bpy_utils_previews_release(PyObject * /*self*/, PyObject *args)
145{
146 char *name;
147
148 if (!PyArg_ParseTuple(args,
149 "s" /* `name` */
150 ":release",
151 &name))
152 {
153 return nullptr;
154 }
155
157
158 Py_RETURN_NONE;
159}
160
161#ifdef __GNUC__
162# ifdef __clang__
163# pragma clang diagnostic push
164# pragma clang diagnostic ignored "-Wcast-function-type"
165# else
166# pragma GCC diagnostic push
167# pragma GCC diagnostic ignored "-Wcast-function-type"
168# endif
169#endif
170
171static PyMethodDef bpy_utils_previews_methods[] = {
172 /* Can't use METH_KEYWORDS alone, see http://bugs.python.org/issue11587 */
173 {"new",
174 static_cast<PyCFunction>(bpy_utils_previews_new),
175 METH_VARARGS,
176 bpy_utils_previews_new_doc},
177 {"load",
178 static_cast<PyCFunction>(bpy_utils_previews_load),
179 METH_VARARGS,
180 bpy_utils_previews_load_doc},
181 {"release",
182 static_cast<PyCFunction>(bpy_utils_previews_release),
183 METH_VARARGS,
184 bpy_utils_previews_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
197 /* Wrap. */
198 bpy_utils_previews_doc,
199 "This object contains basic static methods to handle cached (non-ID) previews in Blender\n"
200 "(low-level API, not exposed to final users).\n");
201static PyModuleDef bpy_utils_previews_module = {
202 /*m_base*/ PyModuleDef_HEAD_INIT,
203 /*m_name*/ "bpy._utils_previews",
204 /*m_doc*/ bpy_utils_previews_doc,
205 /*m_size*/ 0,
206 /*m_methods*/ bpy_utils_previews_methods,
207 /*m_slots*/ nullptr,
208 /*m_traverse*/ nullptr,
209 /*m_clear*/ nullptr,
210 /*m_free*/ nullptr,
211};
212
214{
215 PyObject *submodule;
216
217 submodule = PyModule_Create(&bpy_utils_previews_module);
218
219 return submodule;
220}
221
222} // namespace blender
#define PYDOC_SOURCE_TYPES_LITERAL
BMesh Iterators.
static PyObject * bpy_utils_previews_new(PyObject *, PyObject *args)
PreviewImage * BKE_previewimg_cached_thumbnail_read(const char *name, const char *filepath, int source, bool force_update, bool count_users=false)
static PyObject * bpy_utils_previews_release(PyObject *, PyObject *args)
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")
PreviewImage * BKE_previewimg_cached_ensure(const char *name)
PyObject * pyrna_struct_CreatePyObject(PointerRNA *ptr)
Definition bpy_rna.cc:8874
PyObject * BPY_utils_previews_module()
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
int PyC_ParseUnicodeAsBytesAndSize(PyObject *o, void *p)
@ THB_SOURCE_IMAGE
Definition IMB_thumbs.hh:33
@ THB_SOURCE_MOVIE
Definition IMB_thumbs.hh:34
@ THB_SOURCE_FONT
Definition IMB_thumbs.hh:36
@ THB_SOURCE_BLEND
Definition IMB_thumbs.hh:35
@ THB_SOURCE_OBJECT_IO
Definition IMB_thumbs.hh:37
static PyModuleDef bpy_utils_previews_module
void BKE_previewimg_cached_release(const char *name)
int PyC_ParseStringEnum(PyObject *o, void *p)
static PyMethodDef bpy_utils_previews_methods[]
static PyObject * bpy_utils_previews_load(PyObject *, PyObject *args)