6

Is there a python function that sets ortho view such that X and Y axes are visible - the equivalent of numpad 7?

Thanks in advance!

brockmann
  • 12,613
  • 4
  • 50
  • 93
vndep
  • 713
  • 1
  • 9
  • 29
  • Enable python tooltips in the preferences, then go to View > Viewpoint > Top and wait a second. – brockmann Aug 26 '19 at 14:19
  • Thanks; when I tried the code it threw this error: RuntimeError: Operator bpy.ops.view3d.view_axis.poll() failed, context is incorrect – vndep Aug 26 '19 at 14:27
  • Common question: https://blender.stackexchange.com/questions/6101/poll-failed-context-incorrect-example-bpy-ops-view3d-background-image-add – brockmann Aug 26 '19 at 14:28
  • Why wouldn't this work: for area in bpy.context.screen.areas: if area.type=='VIEW_3D': bpy.ops.view3d.view_axis(type='TOP') – vndep Aug 26 '19 at 14:44

1 Answers1

11

You can override the context when calling view_axis() operator.

Blender 3.2+

import bpy
from bpy import context

for area in context.screen.areas: if area.type == 'VIEW_3D': with context.temp_override(area=area, region=area.regions[-1]): bpy.ops.view3d.view_axis(type='TOP') break

Blender 2.8+

import bpy
for area in bpy.context.screen.areas:
    if area.type == 'VIEW_3D':
        override = bpy.context.copy()
        override['area'] = area
        bpy.ops.view3d.view_axis(override, type='TOP')
        break
p2or
  • 15,860
  • 10
  • 83
  • 143
brockmann
  • 12,613
  • 4
  • 50
  • 93