WIP: allow to load and use images on python scope #115543

Closed
Jaume Bellet wants to merge 1 commits from (deleted):pr-0015-image-ui into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.

1 Commits

Author SHA1 Message Date
fd014cbe37 Allows use to load an image and use in a blender from python scope, without the need to
load in bpy.data.images and hide it to users.

Adds functions under bpy.utils.images to allow script manage the images.

Adds a function to show the image on Layout.

Example python code

import os
import bpy
import bpy.utils.images

img = None

class ImageExamplePanel(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "Image Example Panel"
    bl_idname = "OBJECT_PT_previews"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "object"

    def draw(self, context):
        row = self.layout.row()
        row.template_image_ui(image_value = img['id'], scale= 1)

def register():
    global img

    # Set path to an existing file
    path = os.path.join(os.path.dirname(bpy.app.binary_path), 'image.png')
    img = bpy.utils.images.load('my_image', path)

    bpy.utils.register_class(ImageExamplePanel)

def unregister():
    global img
    bpy.utils.unregister_class(ImageExamplePanel)
    bpy.utils.images.release(img['id'])

if __name__ == "__main__":
    register()
2023-11-28 22:03:54 +01:00