Blender V4.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
14#include <Python.h>
15#include <structmember.h>
16
17#include "BLI_utildefines.h"
18
19#include "RNA_access.hh"
20#include "RNA_prototypes.hh"
21
22#include "bpy_rna.hh"
23#include "bpy_utils_previews.hh"
24
26
27#include "MEM_guardedalloc.h"
28
29#include "IMB_thumbs.hh"
30
31#include "BKE_preview_image.hh"
32
33#define STR_SOURCE_TYPES "'IMAGE', 'MOVIE', 'BLEND', 'FONT'"
34
36 /* Wrap. */
37 bpy_utils_previews_new_doc,
38 ".. method:: new(name)\n"
39 "\n"
40 " Generate a new empty preview.\n"
41 "\n"
42 " :arg name: The name (unique id) identifying the preview.\n"
43 " :type name: str\n"
44 " :return: The Preview matching given name, or a new empty one.\n"
45 " :rtype: :class:`bpy.types.ImagePreview`\n"
46 /* This is only true when accessed via 'bpy.utils.previews.ImagePreviewCollection.load',
47 * however this is the public API, allow this minor difference to the internal version here. */
48 " :raises KeyError: if ``name`` already exists.");
49static PyObject *bpy_utils_previews_new(PyObject * /*self*/, PyObject *args)
50{
51 char *name;
52 PreviewImage *prv;
53
54 if (!PyArg_ParseTuple(args, "s:new", &name)) {
55 return nullptr;
56 }
57
59 PointerRNA ptr = RNA_pointer_create(nullptr, &RNA_ImagePreview, prv);
60
62}
63
65 /* Wrap. */
66 bpy_utils_previews_load_doc,
67 ".. method:: load(name, filepath, filetype, force_reload=False)\n"
68 "\n"
69 " Generate a new preview from given file path.\n"
70 "\n"
71 " :arg name: The name (unique id) identifying the preview.\n"
72 " :type name: str\n"
73 " :arg filepath: The file path to generate the preview from.\n"
74 " :type filepath: str | bytes\n"
75 " :arg filetype: The type of file, needed to generate the preview in [" STR_SOURCE_TYPES
76 "].\n"
77 " :type filetype: str\n"
78 " :arg force_reload: If True, force running thumbnail manager even if preview already "
79 "exists in cache.\n"
80 " :type force_reload: bool\n"
81 " :return: The Preview matching given name, or a new empty one.\n"
82 " :rtype: :class:`bpy.types.ImagePreview`\n"
83 /* This is only true when accessed via 'bpy.utils.previews.ImagePreviewCollection.load',
84 * however this is the public API, allow this minor difference to the internal version here. */
85 " :raises KeyError: if ``name`` already exists.");
86static PyObject *bpy_utils_previews_load(PyObject * /*self*/, PyObject *args)
87{
88 char *name;
89 PyC_UnicodeAsBytesAndSize_Data filepath_data = {nullptr};
90 const PyC_StringEnumItems path_type_items[] = {
91 {THB_SOURCE_IMAGE, "IMAGE"},
92 {THB_SOURCE_MOVIE, "MOVIE"},
93 {THB_SOURCE_BLEND, "BLEND"},
94 {THB_SOURCE_FONT, "FONT"},
95 {THB_SOURCE_OBJECT_IO, "OBJECT_IO"},
96 {0, nullptr},
97 };
98 PyC_StringEnum path_type = {
99 path_type_items,
100 /* The default isn't used. */
101 0,
102 };
103 int force_reload = false;
104
105 if (!PyArg_ParseTuple(args,
106 "s" /* `name` */
107 "O&" /* `filepath` */
108 "O&" /* `filetype` */
109 "|" /* Optional arguments. */
110 "p" /* `force_reload` */
111 ":load",
112 &name,
114 &filepath_data,
116 &path_type,
117 &force_reload))
118 {
119 return nullptr;
120 }
121
123 name, filepath_data.value, path_type.value_found, force_reload);
124
125 Py_XDECREF(filepath_data.value_coerce);
126
127 PointerRNA ptr = RNA_pointer_create(nullptr, &RNA_ImagePreview, prv);
129}
130
132 /* Wrap. */
133 bpy_utils_previews_release_doc,
134 ".. method:: release(name)\n"
135 "\n"
136 " Release (free) a previously created preview.\n"
137 "\n"
138 "\n"
139 " :arg name: The name (unique id) identifying the preview.\n"
140 " :type name: str\n");
141static PyObject *bpy_utils_previews_release(PyObject * /*self*/, PyObject *args)
142{
143 char *name;
144
145 if (!PyArg_ParseTuple(args, "s:release", &name)) {
146 return nullptr;
147 }
148
150
151 Py_RETURN_NONE;
152}
153
154static PyMethodDef bpy_utils_previews_methods[] = {
155 /* Can't use METH_KEYWORDS alone, see http://bugs.python.org/issue11587 */
156 {"new", (PyCFunction)bpy_utils_previews_new, METH_VARARGS, bpy_utils_previews_new_doc},
157 {"load", (PyCFunction)bpy_utils_previews_load, METH_VARARGS, bpy_utils_previews_load_doc},
158 {"release",
159 (PyCFunction)bpy_utils_previews_release,
160 METH_VARARGS,
161 bpy_utils_previews_release_doc},
162 {nullptr, nullptr, 0, nullptr},
163};
164
166 /* Wrap. */
167 bpy_utils_previews_doc,
168 "This object contains basic static methods to handle cached (non-ID) previews in Blender\n"
169 "(low-level API, not exposed to final users).");
170static PyModuleDef bpy_utils_previews_module = {
171 /*m_base*/ PyModuleDef_HEAD_INIT,
172 /*m_name*/ "bpy._utils_previews",
173 /*m_doc*/ bpy_utils_previews_doc,
174 /*m_size*/ 0,
175 /*m_methods*/ bpy_utils_previews_methods,
176 /*m_slots*/ nullptr,
177 /*m_traverse*/ nullptr,
178 /*m_clear*/ nullptr,
179 /*m_free*/ nullptr,
180};
181
183{
184 PyObject *submodule;
185
186 submodule = PyModule_Create(&bpy_utils_previews_module);
187
188 return submodule;
189}
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:26
@ THB_SOURCE_FONT
Definition IMB_thumbs.hh:29
@ THB_SOURCE_BLEND
Definition IMB_thumbs.hh:28
@ THB_SOURCE_MOVIE
Definition IMB_thumbs.hh:27
@ THB_SOURCE_OBJECT_IO
Definition IMB_thumbs.hh:30
Read Guarded memory(de)allocation.
PyObject * pyrna_struct_CreatePyObject(PointerRNA *ptr)
Definition bpy_rna.cc:7694
PyObject * BPY_utils_previews_module()
static PyModuleDef bpy_utils_previews_module
static PyObject * bpy_utils_previews_load(PyObject *, PyObject *args)
static PyObject * bpy_utils_previews_release(PyObject *, PyObject *args)
static PyMethodDef bpy_utils_previews_methods[]
static PyObject * bpy_utils_previews_new(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.")
#define STR_SOURCE_TYPES
int PyC_ParseUnicodeAsBytesAndSize(PyObject *o, void *p)
int PyC_ParseStringEnum(PyObject *o, void *p)
PointerRNA RNA_pointer_create(ID *id, StructRNA *type, void *data)
PointerRNA * ptr
Definition wm_files.cc:4126