2
$\begingroup$

I need to close the "View Animation" window using python. I found something like bpy.ops.render.view_cancel() but when I try to execute it, I get this:

Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
  File "/Applications/Blender 6.app/Contents/Resources/4.1/scripts/modules/bpy/ops.py", line 109, in __call__
    ret = _op_call(self.idname_py(), kw)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Operator bpy.ops.render.view_cancel.poll() failed, context is incorrect

Is there a way to fix this or any other method to close that window?

$\endgroup$

1 Answer 1

2
$\begingroup$

Close the top rendering window

import bpy

window = None

for w in bpy.context.window_manager.windows:
    screen = w.screen
    if screen.name == "temp":
        if len(screen.areas) == 1:
            area = screen.areas[0]
            if area.type == "IMAGE_EDITOR":
                window = w
                break


if window:
    with bpy.context.temp_override(window=window, area=window.screen.areas[0]):
        bpy.ops.render.view_cancel()
        
    print("success")
    

For the animation window, if we look at the source code of the operator bpy.ops.render.play_rendered_anim: https://projects.blender.org/blender/blender/src/branch/main/scripts/startup/bl_operators/screen_play_rendered_anim.py#L195

In line 195 it open it via subprocess.Popen(cmd), So it is not part of Blender and is not stored in bpy.context.window_manager.windows.

To close external windows, maybe refer to: https://stackoverflow.com/questions/49593533/how-to-close-a-window-in-python-by-name

$\endgroup$
5
  • $\begingroup$ This does a good job at closing the rendering window, but I would like to instead close the "view animation" window that appears when bpy.ops.render.play_rendered_anim() is called. $\endgroup$
    – Quisque
    Commented Aug 3 at 12:19
  • $\begingroup$ It seems can't ,can you close it via blender operator shortcut to close it? $\endgroup$
    – X Y
    Commented Aug 3 at 15:49
  • $\begingroup$ Could you explain a bit further what you mean by that? $\endgroup$
    – Quisque
    Commented Aug 3 at 17:38
  • $\begingroup$ I mean bpy.ops.render.view_cancel is not used to close a View Animation window. The window cannot be closed when I trigger the operator from a keyboard shortcut (render.view_cancel). The operator description is "Cancel show render view" and I think it is for cancel rendering window only. $\endgroup$
    – X Y
    Commented Aug 3 at 19:52
  • $\begingroup$ Okay, that might be the case, yes. I managed to solve my problem by finding and terminating the right process using psutil. $\endgroup$
    – Quisque
    Commented Aug 4 at 12:05

You must log in to answer this question.

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