1
$\begingroup$

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.

enter image description here

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 would work

Either option above does what I need.

$\endgroup$
  • 3
    $\begingroup$ Add subtype='FILE_PATH' to your string property. $\endgroup$ – Ratt Dec 5 '17 at 4:58
2
$\begingroup$

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")
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.