Blender  V3.3
bpy_utils_previews.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
12 #include <Python.h>
13 #include <structmember.h>
14 
15 #include "BLI_utildefines.h"
16 
17 #include "RNA_access.h"
18 #include "RNA_prototypes.h"
19 #include "RNA_types.h"
20 
21 #include "BPY_extern.h"
22 #include "bpy_rna.h"
23 #include "bpy_utils_previews.h"
24 
25 #include "MEM_guardedalloc.h"
26 
27 #include "IMB_imbuf.h"
28 #include "IMB_imbuf_types.h"
29 #include "IMB_thumbs.h"
30 
31 #include "BKE_icons.h"
32 
33 #include "DNA_ID.h"
34 
35 #include "../generic/python_utildefines.h"
36 
37 #define STR_SOURCE_TYPES "'IMAGE', 'MOVIE', 'BLEND', 'FONT'"
38 
40  bpy_utils_previews_new_doc,
41  ".. method:: new(name)\n"
42  "\n"
43  " Generate a new empty preview.\n"
44  "\n"
45  " :arg name: The name (unique id) identifying the preview.\n"
46  " :type name: string\n"
47  " :return: The Preview matching given name, or a new empty one.\n"
48  " :rtype: :class:`bpy.types.ImagePreview`\n"
49  /* This is only true when accessed via 'bpy.utils.previews.ImagePreviewCollection.load',
50  * however this is the public API, allow this minor difference to the internal version here. */
51  " :raises KeyError: if ``name`` already exists.");
52 static PyObject *bpy_utils_previews_new(PyObject *UNUSED(self), PyObject *args)
53 {
54  char *name;
55  PreviewImage *prv;
57 
58  if (!PyArg_ParseTuple(args, "s:new", &name)) {
59  return NULL;
60  }
61 
62  prv = BKE_previewimg_cached_ensure(name);
63  RNA_pointer_create(NULL, &RNA_ImagePreview, prv, &ptr);
64 
66 }
67 
69  bpy_utils_previews_load_doc,
70  ".. method:: load(name, filepath, filetype, force_reload=False)\n"
71  "\n"
72  " Generate a new preview from given file path.\n"
73  "\n"
74  " :arg name: The name (unique id) identifying the preview.\n"
75  " :type name: string\n"
76  " :arg filepath: The file path to generate the preview from.\n"
77  " :type filepath: string\n"
78  " :arg filetype: The type of file, needed to generate the preview in [" STR_SOURCE_TYPES
79  "].\n"
80  " :type filetype: string\n"
81  " :arg force_reload: If True, force running thumbnail manager even if preview already "
82  "exists in cache.\n"
83  " :type force_reload: bool\n"
84  " :return: The Preview matching given name, or a new empty one.\n"
85  " :rtype: :class:`bpy.types.ImagePreview`\n"
86  /* This is only true when accessed via 'bpy.utils.previews.ImagePreviewCollection.load',
87  * however this is the public API, allow this minor difference to the internal version here. */
88  " :raises KeyError: if ``name`` already exists.");
89 static PyObject *bpy_utils_previews_load(PyObject *UNUSED(self), PyObject *args)
90 {
91  char *name, *path, *path_type_s;
92  int path_type, force_reload = false;
93 
94  PreviewImage *prv;
96 
97  if (!PyArg_ParseTuple(args, "sss|p:load", &name, &path, &path_type_s, &force_reload)) {
98  return NULL;
99  }
100 
101  if (STREQ(path_type_s, "IMAGE")) {
102  path_type = THB_SOURCE_IMAGE;
103  }
104  else if (STREQ(path_type_s, "MOVIE")) {
105  path_type = THB_SOURCE_MOVIE;
106  }
107  else if (STREQ(path_type_s, "BLEND")) {
108  path_type = THB_SOURCE_BLEND;
109  }
110  else if (STREQ(path_type_s, "FONT")) {
111  path_type = THB_SOURCE_FONT;
112  }
113  else {
114  PyErr_Format(PyExc_ValueError,
115  "load: invalid '%s' filetype, only [" STR_SOURCE_TYPES
116  "] "
117  "are supported",
118  path_type_s);
119  return NULL;
120  }
121 
122  prv = BKE_previewimg_cached_thumbnail_read(name, path, path_type, force_reload);
123  RNA_pointer_create(NULL, &RNA_ImagePreview, prv, &ptr);
124 
126 }
127 
128 PyDoc_STRVAR(bpy_utils_previews_release_doc,
129  ".. method:: release(name)\n"
130  "\n"
131  " Release (free) a previously created preview.\n"
132  "\n"
133  "\n"
134  " :arg name: The name (unique id) identifying the preview.\n"
135  " :type name: string\n");
136 static PyObject *bpy_utils_previews_release(PyObject *UNUSED(self), PyObject *args)
137 {
138  char *name;
139 
140  if (!PyArg_ParseTuple(args, "s:release", &name)) {
141  return NULL;
142  }
143 
145 
146  Py_RETURN_NONE;
147 }
148 
149 static struct PyMethodDef bpy_utils_previews_methods[] = {
150  /* Can't use METH_KEYWORDS alone, see http://bugs.python.org/issue11587 */
151  {"new", (PyCFunction)bpy_utils_previews_new, METH_VARARGS, bpy_utils_previews_new_doc},
152  {"load", (PyCFunction)bpy_utils_previews_load, METH_VARARGS, bpy_utils_previews_load_doc},
153  {"release",
154  (PyCFunction)bpy_utils_previews_release,
155  METH_VARARGS,
156  bpy_utils_previews_release_doc},
157  {NULL, NULL, 0, NULL},
158 };
159 
161  bpy_utils_previews_doc,
162  "This object contains basic static methods to handle cached (non-ID) previews in Blender\n"
163  "(low-level API, not exposed to final users).");
164 static struct PyModuleDef bpy_utils_previews_module = {
165  PyModuleDef_HEAD_INIT,
166  "bpy._utils_previews",
167  bpy_utils_previews_doc,
168  0,
170  NULL,
171  NULL,
172  NULL,
173  NULL,
174 };
175 
177 {
178  PyObject *submodule;
179 
180  submodule = PyModule_Create(&bpy_utils_previews_module);
181 
182  return submodule;
183 }
void BKE_previewimg_cached_release(const char *name)
Definition: icons.cc:508
struct PreviewImage * BKE_previewimg_cached_thumbnail_read(const char *name, const char *filepath, int source, bool force_update)
Definition: icons.cc:463
struct PreviewImage * BKE_previewimg_cached_ensure(const char *name)
Definition: icons.cc:446
#define UNUSED(x)
#define STREQ(a, b)
ID and Library types, which are fundamental for sdna.
Contains defines and structs used throughout the imbuf module.
@ THB_SOURCE_IMAGE
Definition: IMB_thumbs.h:29
@ THB_SOURCE_FONT
Definition: IMB_thumbs.h:32
@ THB_SOURCE_BLEND
Definition: IMB_thumbs.h:31
@ THB_SOURCE_MOVIE
Definition: IMB_thumbs.h:30
Read Guarded memory(de)allocation.
PyObject * pyrna_struct_CreatePyObject(PointerRNA *ptr)
Definition: bpy_rna.c:7505
static PyObject * bpy_utils_previews_load(PyObject *UNUSED(self), 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: string\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.")
static struct PyMethodDef bpy_utils_previews_methods[]
static PyObject * bpy_utils_previews_new(PyObject *UNUSED(self), PyObject *args)
static struct PyModuleDef bpy_utils_previews_module
PyObject * BPY_utils_previews_module(void)
static PyObject * bpy_utils_previews_release(PyObject *UNUSED(self), PyObject *args)
#define STR_SOURCE_TYPES
void RNA_pointer_create(ID *id, StructRNA *type, void *data, PointerRNA *r_ptr)
Definition: rna_access.c:136
PointerRNA * ptr
Definition: wm_files.c:3480