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.
1 Answers
Blender does not support creating a new window officially. However, there are some workarounds.
bpy.ops.screen.userpref_show()
One way is to open up a new User Preferences window and change its Area.type to 'IMAGE_EDITOR' using bpy.ops.screen.userpref_show() operator:
# Call user prefs window
bpy.ops.screen.userpref_show('INVOKE_DEFAULT')
# Change area type
area = bpy.context.window_manager.windows[-1].screen.areas[0]
area.type = 'IMAGE_EDITOR'
bpy.ops.screen.area_dupli()
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:
Blender 3.2+
import bpy
from bpy import context
Iterate through the areas
for area in 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
with context.temp_override(area=area): # Override the area member
bpy.ops.screen.area_dupli('INVOKE_DEFAULT')
area.type = old_area # Restore the old area
break
Blender 2.8+
# 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[0], height=size[1])
pixels = [None] * size[0] * size[1]
for x in range(size[0]):
for y in range(size[1]):
# assign RGBA to something useful
r = x / size[0]
g = y / size[1]
b = (1 - r) * g
a = 1.0
pixels[(y * size[0]) + 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[0]
area.type = 'IMAGE_EDITOR'
Assign the image
bpy.context.area.spaces.active.image = image
Note: bpy.ops.render.view_show("INVOKE_DEFAULT") might be interesting as well.
-
-
Shot in the dark, glad this helps though. FYI: In case you didn't read my comment above, updated my old operator on How do I get a popup UV Image Editor and force it to show the Active Paint Slot? for you, had to change the icon to make it work out ouf the box in Blender 2.8x... @chippwalters – brockmann Apr 27 '20 at 10:35
-
Is there any way to place image editor window in specific place when opens? – Simos Sigma Nov 06 '20 at 13:25
-
1Pops up where your mouse cursor is. Suggest ask a new question referring to this one @SimonetosTheGreek – brockmann Nov 06 '20 at 16:28
-

My take is it's not that easy to do, so I'll try and work around this. Turns out learning Python is the easy part-- learning the Blender API is much harder! haha
– chippwalters Apr 25 '20 at 10:28bpy.ops.render.view_show("INVOKE_DEFAULT")to open up the render window. Please elaborate on your goal and add more information to your question. – brockmann Apr 25 '20 at 10:55