Compare commits

...

42 Commits

Author SHA1 Message Date
Jaume Bellet 75648946ca Merge branch 'bf-blender' into mb-0015-image-ui 2024-04-04 19:33:09 +02:00
Jaume Bellet 99261bcd1a Merge branch 'mb-0014-bpy-images' into mb-0015-image-ui 2024-03-12 07:27:36 +01:00
Jaume Bellet 183ad17df5 Merge branch 'bf-blender' into mb-0014-bpy-images 2024-03-12 07:27:04 +01:00
Jaume Bellet f0c51c2927 Merge branch 'bf-blender' into mb-0015-image-ui
Conflicts:
 	source/blender/editors/interface/interface_templates.cc

due to f4e670af2c and fdc367f842
2024-03-12 07:17:19 +01:00
Jaume Bellet 3c281ddf78 restoring original README.md file 2024-02-25 23:46:11 +01:00
Jaume Bellet 0223f93c2d Merge branch 'mb-0014-bpy-images' into mb-0015-image-ui 2024-02-25 23:44:26 +01:00
Jaume Bellet fc14101e67 removing mb-0014 changes, will be merged next 2024-02-25 23:43:54 +01:00
Jaume Bellet 8d178ec03d restoring original md file 2024-02-25 23:42:21 +01:00
Jaume Bellet d82734d3a1 removing patches.py 2024-02-25 23:40:10 +01:00
Jaume Bellet 7fcb34fa1d Merge branch 'bf-blender' into mb-0014-bpy-images 2024-02-25 23:39:46 +01:00
Jaume Bellet 76d136b561 removing stored patch and patches.py 2024-02-25 23:39:07 +01:00
Jaume Bellet 7eb9a490e5 Merge branch 'bf-blender' into mb-0015-image-ui
Conflicts:
	source/blender/editors/interface/interface_templates.cc
2024-02-23 23:33:02 +01:00
Jaume Bellet 5bf1925aec Merge branch 'bf-blender' into mb-0015-image-ui 2024-02-11 23:01:44 +01:00
Jaume Bellet 5ff326c5f4 IMB_imbuf.h > IMB_imbuf.hh 2024-01-20 00:39:26 +01:00
Jaume Bellet 22a9bb1cd3 patches file 2024-01-20 00:05:36 +01:00
Jaume Bellet 8d31fe702c Merge branch 'bf-blender' into mb-0014-bpy-images
Conflicts:
	README.md
2024-01-20 00:02:50 +01:00
Jaume Bellet 563ad93b39 IMB_imbuf.h -> IMB_imbuf.hh, missing files on previous commit 2024-01-19 23:42:22 +01:00
Jaume Bellet 963db09985 Merge branch 'bf-blender' into mb-0015-image-ui
Conflicts:
	README.md
	source/blender/editors/interface/interface_templates.cc

Also

IMB_imbuf.h > IMB_imbuf.hh
2024-01-19 23:37:31 +01:00
Jaume Bellet 6d7fd0cb9b Merge branch 'bf-blender' into mb-0015-image-ui 2024-01-09 21:51:32 +01:00
Jaume Bellet d00901e305 place file in correct place 2023-12-03 20:51:38 +01:00
Jaume Bellet c410d149da reaply patches 2023-12-03 18:13:01 +01:00
Jaume Bellet 8c28254ea4 rename mblender folder 2023-12-03 18:11:50 +01:00
Jaume Bellet 542ac1b43c Merge branch 'bf-blender' into mb-0015-image-ui 2023-12-03 18:11:11 +01:00
Jaume Bellet 0c4b16c3cd rename id folder 2023-12-03 18:10:33 +01:00
Jaume Bellet d31a0c060a Merge branch 'bf-blender' into mb-0014-bpy-images 2023-12-03 18:09:59 +01:00
Jaume Bellet 217992d312 modified README.md 2023-11-28 06:50:38 +01:00
Jaume Bellet fe4b73b603 files moved to utils 2023-11-20 19:14:10 +01:00
Jaume Bellet 53f625bf2f added ID file 2023-11-19 19:35:52 +01:00
Jaume Bellet d130c0eeb5 update MB-0014 2023-11-19 19:34:55 +01:00
Jaume Bellet 7f73f363b3 Rename file to MB_0014.h, build failed. 2023-11-19 19:27:54 +01:00
Jaume Bellet 3f8b87b49b apply new version of mb-0014 2023-11-19 17:27:02 +01:00
Jaume Bellet 9f440d1904 fixes was not unapplying patches 2023-11-19 17:25:38 +01:00
Jaume Bellet 4fd2cc482f end of lines, and traling spaces 2023-11-19 17:20:42 +01:00
Jaume Bellet b1ed1c5048 apply mb-0014 2023-11-19 16:57:43 +01:00
Jaume Bellet 50bc821d26 added interface to get imbuf 2023-11-19 16:24:28 +01:00
Jaume Bellet 18793361af ok 2023-11-19 16:11:52 +01:00
Jaume Bellet 6c74c139dc replace endings 2023-11-19 16:06:04 +01:00
Jaume Bellet eb44324cad applied mb-0014 2023-11-19 11:08:58 +01:00
Jaume Bellet 1cdc8ad52d added python scripts to apply, unapply 2023-11-19 11:08:06 +01:00
Jaume Bellet aea389f4a2 added MB-ID file 2023-11-19 01:03:33 +01:00
Jaume Bellet fef6af1266 Custom info about branch on README.md 2023-11-19 00:57:53 +01:00
Jaume Bellet 5978db8006 allows loading and unload images from python 2023-11-18 17:06:22 +01:00
11 changed files with 294 additions and 0 deletions

View 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

View File

@ -2455,6 +2455,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.
*/

View File

@ -78,6 +78,9 @@
#include "ED_render.hh"
#include "ED_screen.hh"
#include "ED_undo.hh"
#include "ED_datafiles.h"
#include "IMB_imbuf.hh"
#include "IMB_imbuf.hh"
#include "IMB_imbuf_types.hh"
@ -97,6 +100,8 @@
#include "UI_string_search.hh"
#include "interface_intern.hh"
#include "BPY_extern.h"
using blender::StringRef;
using blender::Vector;
@ -3349,6 +3354,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
* \{ */

View File

@ -1795,6 +1795,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);

View File

@ -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);

View File

@ -69,6 +69,7 @@ set(SRC
bpy_rna_ui.cc
bpy_traceback.cc
bpy_utils_previews.cc
bpy_utils_images.cc
bpy_utils_units.cc
stubs.cc
@ -112,6 +113,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

View File

@ -44,6 +44,7 @@
#include "bpy_rna_gizmo.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"
@ -705,6 +706,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);

View 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.hh"
#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;
}

View 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

View File

@ -0,0 +1 @@
/* Empty File */

View File

@ -0,0 +1 @@
/* Empty File */