1
$\begingroup$

I'm on Blender 3.6 where the old way to override context is deprecated. I know that some bpy.ops commands need to be run in a certain context. I'm fairly certain that I need to be in VIEW_3D context to run bpy.ops.view3d.view_axis(type='TOP', align_active=True) which is equivalent to Shift+NumPad-7 (View > Align View > Align View to Active > Top), but it does not work and is telling me failed, context is incorrect. If the context is not VIEW_3D, I wonder what it is then?

import bpy

area_type = 'VIEW_3D'
areas  = [area for area in bpy.context.window.screen.areas if area.type == area_type]

with bpy.context.temp_override(
    window=bpy.context.window,
    area=areas[0],
    regions=[region for region in areas[0].regions if region.type == 'WINDOW'][0],
    screen=bpy.context.window.screen
):
    bpy.ops.view3d.view_axis(type='TOP', align_active=True)
$\endgroup$
3
  • 1
    $\begingroup$ Not a problem of 3.6. Same in 3.3.2 for example. Just a little typo. The context['key'] must be region not regions in line 9. As you have done in your answer. $\endgroup$
    – relaxed
    Aug 6 at 16:11
  • $\begingroup$ @relaxed hey thank you for pointing that out. i fixed it in the context link as well and btw... i tested it now, and it actually fixes this problem for this question! please add as answer and i will accept it! :D $\endgroup$ Aug 6 at 16:20
  • 1
    $\begingroup$ thanks, but I think for other users it´s easier if you extend your answer with the first version. Then they have both override options. $\endgroup$
    – relaxed
    Aug 6 at 16:22

1 Answer 1

3
$\begingroup$

Thanks to @relaxed for pointing out the typo in my code, which actually was the root cause of the problem. Instead of regions, it should be region. Now it works:

import bpy

area_type = 'VIEW_3D'
areas  = [area for area in bpy.context.window.screen.areas if area.type == area_type]

with bpy.context.temp_override(
    window=bpy.context.window,
    area=areas[0],
    region=[region for region in areas[0].regions if region.type == 'WINDOW'][0],
    screen=bpy.context.window.screen
):
    bpy.ops.view3d.view_axis(type='TOP', align_active=True)

Although the deprecated way of context overriding should have stopped working in Blender 3.2, it still functions. However, it should be avoided, as it will no longer work in Blender 4.0 and beyond.

import bpy

area_type = 'VIEW_3D'
areas  = [area for area in bpy.context.window.screen.areas if area.type == area_type]

override = {
    'window': bpy.context.window,
    'screen': bpy.context.window.screen,
    'area': areas[0],
    'region': [region for region in areas[0].regions if region.type == 'WINDOW'][0],
}

bpy.ops.view3d.view_axis(override, type='BOTTOM', align_active=True)
$\endgroup$
4
  • $\begingroup$ Note the "old" context override will not work anymore in 4.0 FWIW $\endgroup$
    – Gorgious
    Aug 7 at 8:25
  • $\begingroup$ i thought it was supposed to be gone with 3.2. guess they extended to 4.0 hehe $\endgroup$ Aug 7 at 8:27
  • 1
    $\begingroup$ Yes they deprecated it but gave a grace period of a few versions to developers to update their code, also this is a pretty big breaking change so it made more sense to wait for a major release to completely remove it I guess. :) $\endgroup$
    – Gorgious
    Aug 7 at 8:29
  • $\begingroup$ thanks for this info :) i included this in the updated post $\endgroup$ Aug 7 at 8:34

You must log in to answer this question.

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