I have two examples of doing this. One is a pre-built Blender operator and another is a manual operator. I like to work with manual operators that's why I also created it for you.
Pre-built Operator Code:
import bpy
class SelectImagePanel(bpy.types.Panel):
"""Creates a Panel in the World properties window"""
bl_label = "World Image Panel"
bl_idname = "OBJECT_PT_select_image"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "world"
def draw(self, context):
layout = self.layout
# **Context here?????**
row = layout.row()
row.label(text="Select an image", icon='WORLD_DATA')
row = layout.row(align=True)
row.prop(context.scene, 'select_image',icon='IMAGE_DATA')
row.operator('image.new',text='New',icon='ADD')
row.operator('image.open',icon='FILE_FOLDER',text='Open')
def register():
bpy.utils.register_class(SelectImagePanel)
bpy.types.Scene.select_image = bpy.props.PointerProperty(name='Image',type=bpy.types.Image)
def unregister():
bpy.utils.unregister_class(HelloWorldPanel)
if __name__ == "__main__":
register()
Manual Operator Code:
from bpy_extras.io_utils import ImportHelper
import os
class SelectImagePanel(bpy.types.Panel):
"""Creates a Panel in the World properties window"""
bl_label = "World Image Panel"
bl_idname = "OBJECT_PT_select_image"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "world"
def draw(self, context):
layout = self.layout
row = layout.row()
row.label(text="Select an image", icon='WORLD_DATA')
row = layout.row(align=True)
row.prop(context.scene, 'select_image',icon='IMAGE_DATA',icon_only=True)
row.operator('image.new_custom',text='New',icon='ADD')
row.operator('image.open_custom',icon='FILE_FOLDER',text='Open')
class Simple_Open_OT_Operator(bpy.types.Operator,ImportHelper):
bl_label = "Open Image"
bl_idname = "image.open_custom"
filter_glob: bpy.props.StringProperty(
default="*.png;*.jpg")
def execute(self,context):
bpy.data.images.load(filepath=self.filepath)
bpy.context.scene.select_image = bpy.data.images[os.path.basename(self.filepath)]
return {'FINISHED'}
class Simple_NEW_OT_Operator(bpy.types.Operator):
bl_label = "New Image"
bl_idname = "image.new_custom"
name: bpy.props.StringProperty(name='Name',default='YourName')
width: bpy.props.IntProperty(name='Width',default=1024)
height: bpy.props.IntProperty(name='Height',default=1024)
color: bpy.props.FloatVectorProperty(name='Color', subtype='COLOR')
alpha: bpy.props.BoolProperty(name='Alpha',default=True)
def execute(self,context):
bpy.data.images.new(name=self.name,width=self.width, height=self.height, alpha=self.alpha)
bpy.context.scene.select_image = bpy.data.images[self.name]
return {'FINISHED'}
def invoke(self,context,event):
return context.window_manager.invoke_props_dialog(self)
def register():
bpy.utils.register_class(SelectImagePanel)
bpy.utils.register_class(Simple_Open_OT_Operator)
bpy.utils.register_class(Simple_NEW_OT_Operator)
bpy.types.Scene.select_image = bpy.props.PointerProperty(name='Image',type=bpy.types.Image)
def unregister():
bpy.utils.unregister_class(HelloWorldPanel)
bpy.utils.unregister_class(Simple_Open_OT_Operator)
bpy.utils.unregister_class(Simple_NEW_OT_Operator)
if __name__ == "__main__":
register()
```