detect viewport setting ?

is there a way to detect if we are in Top Front or Side view in the viewport ?

thanks
happy bl

I’m no expert on matrix math, but possibly you should be looking at ‘AREA.spaces.active.region_3d.view_rotation.to_matrix()’. For example in ‘Default’ screen layout, you would access that matrix with this path if executing from outside View3D area (such as console):

bpy.context.screen.areas[4].spaces.active.region_3d.view_rotation.to_matrix()

I don’t know whether the view matrix’ z axis points in the same direction as the world’s z axis when the view is pointing down (top view), up (bottom view), or foreward (front view), etc… I just aligned the view to various directions and got these rotation matrices:

front view:


( 1.0000,  0.0000, -0.0000)
(-0.0000, -0.0000, -1.0000)
( 0.0000,  1.0000, -0.0000)

right view:


(-0.0000,  0.0000,  1.0000)
( 1.0000, -0.0000,  0.0000)
( 0.0000,  1.0000, -0.0000)

top view:


(1.0000, 0.0000, 0.0000)
(0.0000, 1.0000, 0.0000)
(0.0000, 0.0000, 1.0000)

Solve the riddle. It’s fun :slight_smile:

does not seems to work in latest 2.78!

got this old one too but not working either
there must be some API changes

get viewport matrix

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

break

matrix = area.active_space.region_3d.perspective_matrix
print (’ 3D matrix =’, matrix )

thanks
happy bl

def getView(self, context, event):	region = context.region
	rv3d = context.region_data
	coord = event.mouse_region_x, event.mouse_region_y
	#view_vector = view3d_utils.region_2d_to_vector_3d(region, rv3d, coord)
	return rv3d.view_rotation * Vector((0.0, 0.0, -1.0))


def findView(self, context, event):
	vector = getView(self, context, event)
	if vector == Vector((0.0, -1.0, 0.0)):
		bpy.ops.view3d.viewnumpad(type='BACK', align_active=False)
	elif vector == Vector((0.0, 1.0, 0.0)):
		bpy.ops.view3d.viewnumpad(type='FRONT', align_active=False)
	elif vector == Vector((1.0, 0.0, 0.0)):
		bpy.ops.view3d.viewnumpad(type='RIGHT', align_active=False)
	elif vector == Vector((-1.0, 0.0, 0.0)):
		bpy.ops.view3d.viewnumpad(type='LEFT', align_active=False)
	elif vector == Vector((0.0, 0.0, 1.0)):
		bpy.ops.view3d.viewnumpad(type='TOP', align_active=False)
	elif vector == Vector((0.0, 0.0, -1.0)):
		bpy.ops.view3d.viewnumpad(type='BOTTOM', align_active=False)