5
$\begingroup$

I have been writing an addon that utilizes an imported image. So far the only way to import was to to to the UV/Image Editor and import the image. Then copy the name of the imported image to a text input in my addon. This is cumbersome and I would rather have something that looks like this:

Image

From which I can access the image through

bpy.data.images[<image name here>]

Is this possible to do? I have only been successful in accessing the image path, but that won't work for my script.

$\endgroup$
1
  • $\begingroup$ It is supposed to happen in a panel? Do you need to limit it to a certain image format (based on file extension)? $\endgroup$
    – CodeManX
    Oct 15, 2014 at 1:19

2 Answers 2

6
$\begingroup$

You can get access to the file browser in Blender by using the StringProperty with the subtype "FILE_PATH". As documented here. And example of using this would be

file_path = StringProperty(name = "File", subtype = "FILE_PATH")

Then you can just use file_path like this:

bpy.data.images.load(file_path) #most of the time you will have to do self.file_path

If you need to use that image elsewhere just set all that equal to a variable.

$\endgroup$
5
$\begingroup$

If you want the user to append a picture to bpy.data.images and do not need a reference to that image, you may add the operator bpy.ops.image.open() operator to a panel:

layout.operator("image.open")

If you want to call it yourself, use

bpy.ops.image.open('INVOKE_DEFAULT')

so the file selector shows up.


The widget like in your screenshot is template_ID(), but we can't really utilize it, as we can't create pointer properties to store a datablock reference in.

$\endgroup$

You must log in to answer this question.

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