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