2

Currently i am trying to change the viewport of the 3D View window using a python script. I am using this function

bpy.ops.view3d.viewnumpad(type='TOP');

But it always get this error

RuntimeError: Operator bpy.ops.view3d.viewnumpad.poll() failed, context is incorrect

The same error is gotten when I use this function

bpy.ops.view3d.view_persportho();

Waht is the problem?

Ray Mairlot
  • 29,192
  • 11
  • 103
  • 125
BetterEnglish
  • 2,000
  • 4
  • 29
  • 53

1 Answers1

3
import bpy

for area in bpy.context.screen.areas:
    if area.type == "VIEW_3D":
        break

for region in area.regions:
    if region.type == "WINDOW":
        break

space = area.spaces[0]

context = bpy.context.copy()
context['area'] = area
context['region'] = region
context['space_data'] = space

bpy.ops.view3d.viewnumpad(context, 'EXEC_DEFAULT', type='TOP')
bpy.ops.view3d.view_persportho(context, 'EXEC_DEFAULT')

or without operators:

from mathutils import Quaternion

r3d = space.region_3d
r3d.view_perspective = "ORTHO"
#default - look down z-axis
r3d.view_rotation = Quaternion((1.0, 0.0, 0.0, 0.0))
pink vertex
  • 9,896
  • 1
  • 25
  • 44