# View3D operations problem [duplicate]

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?

• @Chebhou, I have already read this answer but it does not solved my question Jul 24 '15 at 18:11
• can you edit your post with the new code to see the new problem Jul 24 '15 at 18:13
• ok here is the exact answer blender.stackexchange.com/a/30645/5113 Jul 24 '15 at 18:17
• @Chebhou, I have already used this code to achieve the ORTHO view but how can I achieve the TOP view? Jul 24 '15 at 18:23
• @Chebhou, I need the code that do the same operation when click on 7 Jul 24 '15 at 18:25

## 1 Answer

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))

• This is helpful since this works in 2.80 beta now compared to missing the viewnumpad completely now. Dec 13 '18 at 20:06