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


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


Waht is the problem?

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

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

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

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))
  • $\begingroup$ This is helpful since this works in 2.80 beta now compared to missing the viewnumpad completely now. $\endgroup$ Dec 13 '18 at 20:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.