1

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)

Harry McKenzie
  • 10,995
  • 8
  • 23
  • 51
  • 1
    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. – relaxed Aug 06 '23 at 16:11
  • @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 – Harry McKenzie Aug 06 '23 at 16:20
  • 1
    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. – relaxed Aug 06 '23 at 16:22

1 Answers1

3

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)

Harry McKenzie
  • 10,995
  • 8
  • 23
  • 51