1
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ @Chebhou, I have already read this answer but it does not solved my question $\endgroup$ – BetterEnglish 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$ – BetterEnglish Jul 24 '15 at 18:23
  • $\begingroup$ @Chebhou, I need the code that do the same operation when click on 7 $\endgroup$ – BetterEnglish Jul 24 '15 at 18:25
3
$\begingroup$
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))
$\endgroup$
  • $\begingroup$ This is helpful since this works in 2.80 beta now compared to missing the viewnumpad completely now. $\endgroup$ – Craig D Jones Dec 13 '18 at 20:06

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