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.
First-time contributor

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()
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() ```
Jaume Bellet added 1 commit 2023-11-28 22:12:38 +01:00
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()
casey-bianco-davis added the
Interest
Images & Movies
Interest
Python API
labels 2023-12-14 19:58:19 +01:00
casey-bianco-davis added this to the Python API project 2023-12-14 19:58:35 +01:00
Jaume Bellet closed this pull request 2024-02-01 19:03:13 +01:00

Pull request closed

Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#115543
No description provided.