Python UI: How to load an image from drive and select it? I am trying to create below UI panel to open the file browser and load / select the image into this field. Here is what I got so far, this opens the image and I can then select it from the available images, but I would like to turn it into a single step. So, click file browser, select image and it automatically populates the necessary areas.



import bpy

class OBJECT_PT_my_UI(bpy.types.Panel):
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_label = "MY_PANEL"
    bl_idname = 'OBJECT_myUI'
    bl_context = 'object'

    def draw(self, context):
        layout = self.layout
        col = layout.column()
        row = col.row(align=True)
        row.prop_search(context.scene, 'image_name', bpy.data, 'images')
        row.operator("image.open", icon="FILE_FOLDER", text="")

def register():
    bpy.types.Scene.image_name = bpy.props.StringProperty(name = '')

    bpy.utils.register_module(__name__)


def unregister():
    bpy.utils.unregister_module(__name__)

if __name__ == "__main__":
    register()


Blender already does it in the texture Panel, but I could not extract just the part I need from the python script:



Either option above does what I need.
You can use the updated PointerProperty from Blender 2.79a RC1 onward.
However, take a look at the issues surrounding them first and decide if these drawbacks (mostly related to compatibility) are acceptable in your usage scenario: https://wiki.blender.org/index.php/Dev:Ref/Release_Notes/2.79/PythonAPI
(Section Data-block Pointer Properties)

This is how you use them:

In your register() function:

Remove the StringProperty, instead use this:

# (Import Scene and Image just so the line is shorter)
from bpy.types import Scene, Image
Scene.my_image = bpy.props.PointerProperty(name="Image", type=Image)


In your draw() method:

Remove the prop_search and operator, instead use template_ID:

layout.template_ID(self, "my_image", open="image.open")