I'm looking to open a new image editor window and display a new image from a file in it -- all via python script. What is the best way to do this? Searched but only found how to load and display an image, but not how to do it in a separate window. Thanks.
Blender does not support creating a new window officially. However, there are some workarounds.
# Call user prefs window bpy.ops.screen.userpref_show('INVOKE_DEFAULT') # Change area type area = bpy.context.window_manager.windows[-1].screen.areas area.type = 'IMAGE_EDITOR'
Another way is using
bpy.ops.screen.area_dupli(), which creates a new window from a specific area of your screen. Find an area of interest, store the Area.type, set it to
'IMAGE_EDITOR' call the the opeator and reset the area:
# Copy context member context = bpy.context.copy() # Iterate through the areas for area in bpy.context.screen.areas: if area.type in ('IMAGE_EDITOR', 'VIEW_3D', 'NODE_EDITOR'): old_area = area.type # Store current area type area.type = 'IMAGE_EDITOR' # Set the area to the Image editor context['area'] = area # Override the area member bpy.ops.screen.area_dupli(context, 'INVOKE_DEFAULT') area.type = old_area # Restore the old area break
Assigning the image
In order to display an image in that window, just load an image and assign its data block it to
SpaceImageEditor.image eg using the active space:
bpy.context.area.spaces.active.image = image. Complete example using the code to generate an image from: Is it possible to create image data and save to a file from a script?:
size = 640, 480 image = bpy.data.images.new("MyImage", width=size, height=size) pixels = [None] * size * size for x in range(size): for y in range(size): # assign RGBA to something useful r = x / size g = y / size b = (1 - r) * g a = 1.0 pixels[(y * size) + x] = [r, g, b, a] pixels = [chan for px in pixels for chan in px] image.pixels = pixels # Call user prefs window bpy.ops.screen.userpref_show('INVOKE_DEFAULT') # Change area type area = bpy.context.window_manager.windows[-1].screen.areas area.type = 'IMAGE_EDITOR' # Assign the image bpy.context.area.spaces.active.image = image
bpy.ops.render.view_show("INVOKE_DEFAULT") might be interesting as well.