| 
									
										
										
										
											2022-02-11 09:07:11 +11:00
										 |  |  | /* SPDX-License-Identifier: GPL-2.0-or-later */ | 
					
						
							| 
									
										
										
										
											2015-05-11 16:29:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-18 08:08:12 +11:00
										 |  |  | /** \file
 | 
					
						
							|  |  |  |  * \ingroup pythonintern | 
					
						
							| 
									
										
										
										
											2015-05-11 16:29:12 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This file defines a singleton py object accessed via 'bpy.utils.previews', | 
					
						
							|  |  |  |  * which exposes low-level API for custom previews/icons. | 
					
						
							|  |  |  |  * It is replaced in final API by an higher-level python wrapper, that handles previews by addon, | 
					
						
							|  |  |  |  * and automatically release them on deletion. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <Python.h>
 | 
					
						
							|  |  |  | #include <structmember.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "BLI_utildefines.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "RNA_access.h"
 | 
					
						
							| 
									
										
										
										
											2020-03-19 09:33:03 +01:00
										 |  |  | #include "RNA_types.h"
 | 
					
						
							| 
									
										
										
										
											2015-05-11 16:29:12 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "BPY_extern.h"
 | 
					
						
							|  |  |  | #include "bpy_rna.h"
 | 
					
						
							| 
									
										
										
										
											2020-03-19 09:33:03 +01:00
										 |  |  | #include "bpy_utils_previews.h"
 | 
					
						
							| 
									
										
										
										
											2015-05-11 16:29:12 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "MEM_guardedalloc.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "IMB_imbuf.h"
 | 
					
						
							|  |  |  | #include "IMB_imbuf_types.h"
 | 
					
						
							|  |  |  | #include "IMB_thumbs.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "BKE_icons.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "DNA_ID.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "../generic/python_utildefines.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define STR_SOURCE_TYPES "'IMAGE', 'MOVIE', 'BLEND', 'FONT'"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-17 12:42:07 +10:00
										 |  |  | 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" | 
					
						
							| 
									
										
										
										
											2020-09-29 15:05:45 -04:00
										 |  |  |     "   :rtype: :class:`bpy.types.ImagePreview`\n" | 
					
						
							|  |  |  |     /* This is only true when accessed via 'bpy.utils.previews.ImagePreviewCollection.load',
 | 
					
						
							|  |  |  |      * however this is the public API, allow this minor difference to the internal version here. */ | 
					
						
							|  |  |  |     "   :raises KeyError: if ``name`` already exists."); | 
					
						
							| 
									
										
										
										
											2015-05-11 16:29:12 +02:00
										 |  |  | static PyObject *bpy_utils_previews_new(PyObject *UNUSED(self), PyObject *args) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   char *name; | 
					
						
							|  |  |  |   PreviewImage *prv; | 
					
						
							|  |  |  |   PointerRNA ptr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!PyArg_ParseTuple(args, "s:new", &name)) { | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   prv = BKE_previewimg_cached_ensure(name); | 
					
						
							|  |  |  |   RNA_pointer_create(NULL, &RNA_ImagePreview, prv, &ptr); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return pyrna_struct_CreatePyObject(&ptr); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyDoc_STRVAR( | 
					
						
							|  |  |  |     bpy_utils_previews_load_doc, | 
					
						
							| 
									
										
										
										
											2015-05-12 17:59:37 +10:00
										 |  |  |     ".. method:: load(name, filepath, filetype, force_reload=False)\n" | 
					
						
							| 
									
										
										
										
											2015-05-11 16:29:12 +02:00
										 |  |  |     "\n" | 
					
						
							| 
									
										
										
										
											2020-09-17 12:42:07 +10:00
										 |  |  |     "   Generate a new preview from given file path.\n" | 
					
						
							|  |  |  |     "\n" | 
					
						
							| 
									
										
										
										
											2015-05-11 16:29:12 +02:00
										 |  |  |     "   :arg name: The name (unique id) identifying the preview.\n" | 
					
						
							|  |  |  |     "   :type name: string\n" | 
					
						
							| 
									
										
										
										
											2015-05-12 17:59:37 +10:00
										 |  |  |     "   :arg filepath: The file path to generate the preview from.\n" | 
					
						
							|  |  |  |     "   :type filepath: string\n" | 
					
						
							|  |  |  |     "   :arg filetype: The type of file, needed to generate the preview in [" STR_SOURCE_TYPES | 
					
						
							|  |  |  |     "].\n" | 
					
						
							|  |  |  |     "   :type filetype: string\n" | 
					
						
							| 
									
										
										
										
											2015-05-11 16:29:12 +02:00
										 |  |  |     "   :arg force_reload: If True, force running thumbnail manager even if preview already " | 
					
						
							|  |  |  |     "exists in cache.\n" | 
					
						
							|  |  |  |     "   :type force_reload: bool\n" | 
					
						
							|  |  |  |     "   :return: The Preview matching given name, or a new empty one.\n" | 
					
						
							| 
									
										
										
										
											2020-09-29 15:05:45 -04:00
										 |  |  |     "   :rtype: :class:`bpy.types.ImagePreview`\n" | 
					
						
							|  |  |  |     /* This is only true when accessed via 'bpy.utils.previews.ImagePreviewCollection.load',
 | 
					
						
							|  |  |  |      * however this is the public API, allow this minor difference to the internal version here. */ | 
					
						
							|  |  |  |     "   :raises KeyError: if ``name`` already exists."); | 
					
						
							| 
									
										
										
										
											2015-05-11 16:29:12 +02:00
										 |  |  | static PyObject *bpy_utils_previews_load(PyObject *UNUSED(self), PyObject *args) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   char *name, *path, *path_type_s; | 
					
						
							|  |  |  |   int path_type, force_reload = false; | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-11 16:29:12 +02:00
										 |  |  |   PreviewImage *prv; | 
					
						
							|  |  |  |   PointerRNA ptr; | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-11 16:29:12 +02:00
										 |  |  |   if (!PyArg_ParseTuple(args, "sss|p:load", &name, &path, &path_type_s, &force_reload)) { | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-11 16:29:12 +02:00
										 |  |  |   if (STREQ(path_type_s, "IMAGE")) { | 
					
						
							|  |  |  |     path_type = THB_SOURCE_IMAGE; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else if (STREQ(path_type_s, "MOVIE")) { | 
					
						
							|  |  |  |     path_type = THB_SOURCE_MOVIE; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else if (STREQ(path_type_s, "BLEND")) { | 
					
						
							|  |  |  |     path_type = THB_SOURCE_BLEND; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else if (STREQ(path_type_s, "FONT")) { | 
					
						
							|  |  |  |     path_type = THB_SOURCE_FONT; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else { | 
					
						
							|  |  |  |     PyErr_Format(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2015-05-12 17:59:37 +10:00
										 |  |  |                  "load: invalid '%s' filetype, only [" STR_SOURCE_TYPES | 
					
						
							|  |  |  |                  "] " | 
					
						
							| 
									
										
										
										
											2015-05-11 16:29:12 +02:00
										 |  |  |                  "are supported", | 
					
						
							|  |  |  |                  path_type_s); | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-11 16:29:12 +02:00
										 |  |  |   prv = BKE_previewimg_cached_thumbnail_read(name, path, path_type, force_reload); | 
					
						
							|  |  |  |   RNA_pointer_create(NULL, &RNA_ImagePreview, prv, &ptr); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-11 16:29:12 +02:00
										 |  |  |   return pyrna_struct_CreatePyObject(&ptr); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyDoc_STRVAR(bpy_utils_previews_release_doc, | 
					
						
							|  |  |  |              ".. method:: release(name)\n" | 
					
						
							|  |  |  |              "\n" | 
					
						
							|  |  |  |              "   Release (free) a previously created preview.\n" | 
					
						
							|  |  |  |              "\n" | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |              "\n" | 
					
						
							| 
									
										
										
										
											2015-05-11 16:29:12 +02:00
										 |  |  |              "   :arg name: The name (unique id) identifying the preview.\n" | 
					
						
							|  |  |  |              "   :type name: string\n"); | 
					
						
							|  |  |  | static PyObject *bpy_utils_previews_release(PyObject *UNUSED(self), PyObject *args) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   char *name; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!PyArg_ParseTuple(args, "s:release", &name)) { | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   BKE_previewimg_cached_release(name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct PyMethodDef bpy_utils_previews_methods[] = { | 
					
						
							|  |  |  |     /* Can't use METH_KEYWORDS alone, see http://bugs.python.org/issue11587 */ | 
					
						
							|  |  |  |     {"new", (PyCFunction)bpy_utils_previews_new, METH_VARARGS, bpy_utils_previews_new_doc}, | 
					
						
							|  |  |  |     {"load", (PyCFunction)bpy_utils_previews_load, METH_VARARGS, bpy_utils_previews_load_doc}, | 
					
						
							|  |  |  |     {"release", | 
					
						
							|  |  |  |      (PyCFunction)bpy_utils_previews_release, | 
					
						
							|  |  |  |      METH_VARARGS, | 
					
						
							|  |  |  |      bpy_utils_previews_release_doc}, | 
					
						
							| 
									
										
										
										
											2019-02-03 14:01:45 +11:00
										 |  |  |     {NULL, NULL, 0, NULL}, | 
					
						
							| 
									
										
										
										
											2015-05-11 16:29:12 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyDoc_STRVAR( | 
					
						
							|  |  |  |     bpy_utils_previews_doc, | 
					
						
							|  |  |  |     "This object contains basic static methods to handle cached (non-ID) previews in Blender\n" | 
					
						
							|  |  |  |     "(low-level API, not exposed to final users)."); | 
					
						
							|  |  |  | static struct PyModuleDef bpy_utils_previews_module = { | 
					
						
							|  |  |  |     PyModuleDef_HEAD_INIT, | 
					
						
							|  |  |  |     "bpy._utils_previews", | 
					
						
							|  |  |  |     bpy_utils_previews_doc, | 
					
						
							|  |  |  |     0, | 
					
						
							|  |  |  |     bpy_utils_previews_methods, | 
					
						
							| 
									
										
										
										
											2019-01-19 13:21:18 +11:00
										 |  |  |     NULL, | 
					
						
							|  |  |  |     NULL, | 
					
						
							|  |  |  |     NULL, | 
					
						
							|  |  |  |     NULL, | 
					
						
							| 
									
										
										
										
											2015-05-11 16:29:12 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyObject *BPY_utils_previews_module(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   PyObject *submodule; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   submodule = PyModule_Create(&bpy_utils_previews_module); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return submodule; | 
					
						
							|  |  |  | } |