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