1
$\begingroup$

I'm trying to open an image through this UI element via code, but Blender info doesn't return all the necessary steps to do it.

All it shows is this function which does not load the image and assign it:

bpy.ops.image.open(filepath="C:\\Users\\xande\\Desktop\\uvchecker.png", directory="C:\\Users\\xande\\Desktop\\", files=[{"name":"uvchecker.png", "name":"uvchecker.png"}], show_multiview=False)

Does anyone know how to do this?

The images below are the before and after states of the result I want to achieve through script.

Before: strong text

After: enter image description here

$\endgroup$

1 Answer 1

0
$\begingroup$

The following script will load the image file uvchecker.png located in C:\Users\xande\Desktop\ and then set it in the UV Editor (make sure it is open or else it won't work) using area type IMAGE_EDITOR since UV Editor uses the same area type name as the Image Editor.

import bpy

image_name = "uvchecker.png"
image_path = f"C:/Users/xande/Desktop/{image_name}"

def get_image():
    return bpy.data.images.get(image_name)

image = get_image()

if not image:
    bpy.ops.image.open(filepath=image_path)
    image = get_image()

area  = next(area for area in bpy.context.window.screen.areas if area.type == 'IMAGE_EDITOR')
area.spaces.active.image = image
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .