WIP: allow to load and use images on python scope #115543
49
scripts/modules/bpy/utils/images.py
Normal file
49
scripts/modules/bpy/utils/images.py
Normal file
@ -0,0 +1,49 @@
|
||||
# SPDX-FileCopyrightText: 2015-2023 Blender Authors
|
||||
#
|
||||
# SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
"""
|
||||
This module contains utility functions to handle custom images.
|
||||
"""
|
||||
|
||||
__all__ = (
|
||||
"load",
|
||||
"release"
|
||||
"list",
|
||||
)
|
||||
|
||||
from _bpy import _utils_images
|
||||
|
||||
|
||||
list = []
|
||||
|
||||
|
||||
def load(name, path):
|
||||
r = _utils_images.load(name, path)
|
||||
if r != None:
|
||||
data = {'id' : r , 'name' : name, 'path' : path}
|
||||
list.append(data)
|
||||
return data;
|
||||
else:
|
||||
return None;
|
||||
|
||||
load.__doc__ = _utils_images.load.__doc__;
|
||||
|
||||
def release(image_id):
|
||||
r = _utils_images.release(image_id)
|
||||
if r == True:
|
||||
for data in list:
|
||||
if data.get('id') == image_id:
|
||||
list.remove(data)
|
||||
|
||||
return r;
|
||||
release.__doc__ = _utils_images.release.__doc__
|
||||
|
||||
import atexit
|
||||
|
||||
def exit_clear():
|
||||
while len(list):
|
||||
release(list[0].get('id'))
|
||||
|
||||
atexit.register(exit_clear)
|
||||
del atexit, exit_clear
|
@ -2433,6 +2433,9 @@ void uiTemplatePreview(uiLayout *layout,
|
||||
MTex *slot,
|
||||
const char *preview_id);
|
||||
void uiTemplateColorRamp(uiLayout *layout, PointerRNA *ptr, const char *propname, bool expand);
|
||||
|
||||
void uiTemplateImageUI(uiLayout *layout, int image_id, float image_scale);
|
||||
|
||||
/**
|
||||
* \param icon_scale: Scale of the icon, 1x == button height.
|
||||
*/
|
||||
|
@ -78,6 +78,9 @@
|
||||
#include "ED_render.hh"
|
||||
#include "ED_screen.hh"
|
||||
#include "ED_undo.hh"
|
||||
#include "ED_datafiles.h"
|
||||
|
||||
#include "IMB_imbuf.h"
|
||||
|
||||
#include "RE_engine.h"
|
||||
|
||||
@ -93,6 +96,8 @@
|
||||
#include "UI_view2d.hh"
|
||||
#include "interface_intern.hh"
|
||||
|
||||
#include "BPY_extern.h"
|
||||
|
||||
#include "PIL_time.h"
|
||||
|
||||
/* we may want to make this optional, disable for now. */
|
||||
@ -3369,6 +3374,28 @@ void uiTemplatePreview(uiLayout *layout,
|
||||
|
||||
/** \} */
|
||||
|
||||
void uiTemplateImageUI(uiLayout* layout, int image_id,float image_scale) {
|
||||
|
||||
ImBuf *ibuf = IMB_dupImBuf((ImBuf *) BPY_utils_images_get(image_id));
|
||||
|
||||
if (ibuf) {
|
||||
int width = ibuf->x * image_scale;
|
||||
int height = (width * ibuf->y) / ibuf->x;
|
||||
|
||||
IMB_premultiply_alpha(ibuf);
|
||||
IMB_scaleImBuf(ibuf, width, height);
|
||||
|
||||
bTheme *btheme = UI_GetTheme();
|
||||
uchar *color = btheme->tui.wcol_menu_back.text_sel;
|
||||
//color = btheme->tui.wcol_box.inner;
|
||||
|
||||
uiBlock *block = uiLayoutAbsoluteBlock(layout);
|
||||
|
||||
uiBut *but = uiDefButImage(block, ibuf, 0, U.widget_unit, width, height, color);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------- */
|
||||
/** \name ColorRamp Template
|
||||
* \{ */
|
||||
|
@ -1666,6 +1666,21 @@ void RNA_api_ui_layout(StructRNA *srna)
|
||||
1.0f,
|
||||
100.0f);
|
||||
|
||||
|
||||
func = RNA_def_function(srna, "template_image_ui", "uiTemplateImageUI");
|
||||
RNA_def_function_ui_description(func, "A image on UI");
|
||||
parm = RNA_def_int(func, "image_value", 0, 0, INT_MAX, "image to display", "", 0, INT_MAX);
|
||||
RNA_def_parameter_flags(parm, PropertyFlag(0), PARM_REQUIRED);
|
||||
RNA_def_float(func,
|
||||
"scale",
|
||||
1.0f,
|
||||
1.0f,
|
||||
100.0f,
|
||||
"Scale",
|
||||
"Scale the icon size (by the button size)",
|
||||
1.0f,
|
||||
100.0f);
|
||||
|
||||
func = RNA_def_function(srna, "template_icon_view", "uiTemplateIconView");
|
||||
RNA_def_function_ui_description(func, "Enum. Large widget showing Icon previews");
|
||||
api_ui_item_rna_common(func);
|
||||
|
@ -133,6 +133,8 @@ bool BPY_string_is_keyword(const char *str);
|
||||
void BPY_callback_screen_free(struct ARegionType *art);
|
||||
void BPY_callback_wm_free(struct wmWindowManager *wm);
|
||||
|
||||
void* BPY_utils_images_get(int image_id);
|
||||
|
||||
/* I18n for addons */
|
||||
#ifdef WITH_INTERNATIONAL
|
||||
const char *BPY_app_translations_py_pgettext(const char *msgctxt, const char *msgid);
|
||||
|
@ -67,6 +67,7 @@ set(SRC
|
||||
bpy_rna_ui.cc
|
||||
bpy_traceback.cc
|
||||
bpy_utils_previews.cc
|
||||
bpy_utils_images.cc
|
||||
bpy_utils_units.cc
|
||||
stubs.cc
|
||||
|
||||
@ -109,6 +110,7 @@ set(SRC
|
||||
bpy_rna_ui.h
|
||||
bpy_traceback.h
|
||||
bpy_utils_previews.h
|
||||
bpy_utils_images.h
|
||||
bpy_utils_units.h
|
||||
../BPY_extern.h
|
||||
../BPY_extern_clog.h
|
||||
|
@ -46,6 +46,7 @@
|
||||
#include "bpy_rna_id_collection.h"
|
||||
#include "bpy_rna_types_capi.h"
|
||||
#include "bpy_utils_previews.h"
|
||||
#include "bpy_utils_images.h"
|
||||
#include "bpy_utils_units.h"
|
||||
|
||||
#include "../generic/py_capi_utils.h"
|
||||
@ -686,6 +687,7 @@ void BPy_init_modules(bContext *C)
|
||||
PyModule_AddObject(mod, "app", BPY_app_struct());
|
||||
PyModule_AddObject(mod, "_utils_units", BPY_utils_units());
|
||||
PyModule_AddObject(mod, "_utils_previews", BPY_utils_previews_module());
|
||||
PyModule_AddObject(mod, "_utils_images", BPY_utils_images_module());
|
||||
PyModule_AddObject(mod, "msgbus", BPY_msgbus_module());
|
||||
|
||||
PointerRNA ctx_ptr = RNA_pointer_create(nullptr, &RNA_Context, C);
|
||||
|
173
source/blender/python/intern/bpy_utils_images.cc
Normal file
173
source/blender/python/intern/bpy_utils_images.cc
Normal file
@ -0,0 +1,173 @@
|
||||
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0-or-later */
|
||||
|
||||
/** \file
|
||||
* \ingroup pythonintern
|
||||
*
|
||||
* This file defines a singleton py object accessed via 'bpy.utils.images',
|
||||
*/
|
||||
|
||||
#include <Python.h>
|
||||
#include <structmember.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "BLI_utildefines.h"
|
||||
#include "BLI_listbase.h"
|
||||
#include "BLI_string.h"
|
||||
|
||||
#include "RNA_access.hh"
|
||||
#include "RNA_prototypes.h"
|
||||
#include "RNA_types.hh"
|
||||
|
||||
#include "BPY_extern.h"
|
||||
#include "bpy_rna.h"
|
||||
#include "bpy_utils_images.h"
|
||||
|
||||
#include "../generic/py_capi_utils.h"
|
||||
|
||||
#include "MEM_guardedalloc.h"
|
||||
|
||||
#include "IMB_imbuf.h"
|
||||
|
||||
#include "../generic/python_utildefines.h"
|
||||
|
||||
|
||||
struct bpy_image_data{
|
||||
bpy_image_data *prev;
|
||||
bpy_image_data *next;
|
||||
|
||||
int id;
|
||||
ImBuf *ibuf;
|
||||
std::string name;
|
||||
std::string path;
|
||||
};
|
||||
|
||||
int bpy_images_last_id = 0;
|
||||
ListBase bpy_images_list;
|
||||
|
||||
|
||||
bpy_image_data* BPY_utils_images_get_data(int image_id) {
|
||||
return (bpy_image_data *)BLI_listbase_bytes_find(
|
||||
&bpy_images_list, &image_id, sizeof(int), sizeof(bpy_image_data *) * 2);
|
||||
}
|
||||
|
||||
void* BPY_utils_images_get(int image_id)
|
||||
{
|
||||
bpy_image_data *el = BPY_utils_images_get_data(image_id);
|
||||
return (el != nullptr) ? el->ibuf : nullptr;
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(bpy_utils_images_load_doc,
|
||||
".. method:: load(name, filepath)\n"
|
||||
"\n"
|
||||
" Generate a new preview from given file path.\n"
|
||||
"\n"
|
||||
" :arg name: The name identifying the image.\n"
|
||||
" :type name: string\n"
|
||||
" :arg filepath: The file path to the image.\n"
|
||||
" :type filepath: string or bytes\n"
|
||||
" :return: image id.\n"
|
||||
" :rtype: long`\n");
|
||||
static PyObject *bpy_utils_images_load(PyObject * /*self*/, PyObject *args)
|
||||
{
|
||||
char *name = NULL;
|
||||
PyC_UnicodeAsBytesAndSize_Data filepath_data = {nullptr};
|
||||
|
||||
if (!PyArg_ParseTuple(args,
|
||||
"s" /* `name` */
|
||||
"O&" /* `filepath` */
|
||||
":load",
|
||||
&name,
|
||||
PyC_ParseUnicodeAsBytesAndSize,
|
||||
&filepath_data,
|
||||
0))
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!filepath_data.value || !name) {
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
ImBuf *ibuf = IMB_loadiffname(filepath_data.value, 0, nullptr);
|
||||
|
||||
if (ibuf) {
|
||||
bpy_image_data *data = MEM_new<bpy_image_data>(__func__);
|
||||
data->id = ++bpy_images_last_id;
|
||||
data->ibuf = ibuf;
|
||||
data->name = name;
|
||||
data->path = filepath_data.value;
|
||||
|
||||
BLI_addtail(&bpy_images_list, data);
|
||||
|
||||
return PyLong_FromLong(data->id);
|
||||
}
|
||||
else {
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(bpy_utils_images_release_doc,
|
||||
".. method:: release(image_id)\n"
|
||||
"\n"
|
||||
" Release (free) a previously added image.\n"
|
||||
"\n"
|
||||
"\n"
|
||||
" :arg image_id: The id identifying the image.\n"
|
||||
" :type name: long\n"
|
||||
" :return: true if release.\n"
|
||||
" :rtype: bool`\n");
|
||||
static PyObject *bpy_utils_images_release(PyObject * /*self*/, PyObject *args)
|
||||
{
|
||||
int image_id = -1;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "i:release", &image_id)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bpy_image_data *el = BPY_utils_images_get_data(image_id);
|
||||
|
||||
if (el != nullptr) {
|
||||
BLI_remlink(&bpy_images_list, el);
|
||||
IMB_freeImBuf(el->ibuf);
|
||||
MEM_freeN(el);
|
||||
Py_RETURN_TRUE;
|
||||
}
|
||||
else {
|
||||
Py_RETURN_FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
static PyMethodDef bpy_utils_images_methods[] = {
|
||||
/* Can't use METH_KEYWORDS alone, see http://bugs.python.org/issue11587 */
|
||||
{"load", (PyCFunction)bpy_utils_images_load, METH_VARARGS, bpy_utils_images_load_doc},
|
||||
{"release",(PyCFunction)bpy_utils_images_release,METH_VARARGS, bpy_utils_images_release_doc},
|
||||
{nullptr, nullptr, 0, nullptr},
|
||||
};
|
||||
|
||||
PyDoc_STRVAR(
|
||||
bpy_utils_images_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 PyModuleDef bpy_utils_images_module = {
|
||||
/*m_base*/ PyModuleDef_HEAD_INIT,
|
||||
/*m_name*/ "bpy._utils_images",
|
||||
/*m_doc*/ bpy_utils_images_doc,
|
||||
/*m_size*/ 0,
|
||||
/*m_methods*/ bpy_utils_images_methods,
|
||||
/*m_slots*/ nullptr,
|
||||
/*m_traverse*/ nullptr,
|
||||
/*m_clear*/ nullptr,
|
||||
/*m_free*/ nullptr,
|
||||
};
|
||||
|
||||
PyObject *BPY_utils_images_module()
|
||||
{
|
||||
PyObject *submodule;
|
||||
|
||||
submodule = PyModule_Create(&bpy_utils_images_module);
|
||||
|
||||
return submodule;
|
||||
}
|
19
source/blender/python/intern/bpy_utils_images.h
Normal file
19
source/blender/python/intern/bpy_utils_images.h
Normal file
@ -0,0 +1,19 @@
|
||||
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0-or-later */
|
||||
|
||||
/** \file
|
||||
* \ingroup pythonintern
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
PyObject *BPY_utils_images_module(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user