6
$\begingroup$

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!

$\endgroup$
4
  • $\begingroup$ Enable python tooltips in the preferences, then go to View > Viewpoint > Top and wait a second. $\endgroup$
    – brockmann
    Commented Aug 26, 2019 at 14:19
  • $\begingroup$ Thanks; when I tried the code it threw this error: RuntimeError: Operator bpy.ops.view3d.view_axis.poll() failed, context is incorrect $\endgroup$
    – vndep
    Commented Aug 26, 2019 at 14:27
  • $\begingroup$ Common question: blender.stackexchange.com/questions/6101/… $\endgroup$
    – brockmann
    Commented Aug 26, 2019 at 14:28
  • $\begingroup$ Why wouldn't this work: for area in bpy.context.screen.areas: if area.type=='VIEW_3D': bpy.ops.view3d.view_axis(type='TOP') $\endgroup$
    – vndep
    Commented Aug 26, 2019 at 14:44

1 Answer 1

11
$\begingroup$

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
$\endgroup$

You must log in to answer this question.

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