2
$\begingroup$

I have an IMAGE_EDITOR area opened. I change the loaded image's content from another area, but area.tag_refresh() doesn't update it automatically, only when I do something related to the IMAGE_EDITOR area (zooming, moving the image in it, making the area bigger or anything like that). Fortunately I can touch the area from code to make the image refresh itself without touching it manually, but it's hacky. Is there a better way to do this?

def img_redraw_handler(scene):
    ...
    img_bpy.pixels.foreach_set(img_np.ravel())
    for area in bpy.context.screen.areas:
        if area.type == 'IMAGE_EDITOR':
            #area.tag_refresh() # <-- This one doesn't work
            # HACK TO REDRAW - START
            area.type = 'OUTLINER'
            area.type = 'IMAGE_EDITOR'
            # HACK TO REDRAW- END
bpy.app.handlers.depsgraph_update_pre.append(img_redraw_handler)

SOLUTION:

def img_redraw_handler(scene):
    ...
    img_bpy.pixels.foreach_set(img_np.ravel())
    img_bpy.update()
bpy.app.handlers.depsgraph_update_pre.append(img_redraw_handler)

many thanks to Sietse Brouwer

$\endgroup$
3
  • 1
    $\begingroup$ Haven't tested it, but you can try an img_bpy.update() before the area.tag_refresh(). $\endgroup$ Jan 27, 2023 at 11:08
  • $\begingroup$ Yeess! img_bpy.update() is all that's required, not even area.tag_refresh() is necessary. Thank you very much! I would like to give you an upvote so much, sadly I cannot:( $\endgroup$ Jan 27, 2023 at 11:15
  • $\begingroup$ Glad I could help. I've added an answer, for others who come across this topic. $\endgroup$ Jan 27, 2023 at 12:31

1 Answer 1

5
$\begingroup$

The solution is already in the question, but for future reference I will post it as an 'official' answer.

After setting pixels of an image, an img_bpy.update() is required to reflect the changes in Blender.

def img_redraw_handler(scene):
    ...
    img_bpy.pixels.foreach_set(img_np.ravel())
    img_bpy.update()

bpy.app.handlers.depsgraph_update_pre.append(img_redraw_handler)
$\endgroup$

You must log in to answer this question.

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