I can't seem to trigger operations on a certain viewport view

I have installed pyautogui, so I can run hotkeys via python, it's working, as in triggering from scripts. I have tied the hotkeys to external scripts thay auto transform.resize locked in the a constraint Axis. (means I don't need to press scale key, X,Y,Z)

When I have an object selected and in Front Orthographic, I use the UP menu on my custom pie-menu to call my script that will auto scale on the Z-axis (scales it up), feels very natural. The problem arises when I change to Top Orthographic Because the viewers perspective has changed and not the Z-axis the desired behaviour is wrong.


//Check if Front Orthographic //If True //Run pyautogui.hotkey('ctrl', 'alt', 'S')
//Check if Top Orthographic //If True //Run pyautogui.hotkey('ctrl', 'alt', 'O')

Chat GTP tried this, but it fails but it looks like it should work.

# Check if the current view is set to Front or Ortho View
if bpy.context.space_data.region_3d.view_perspective in ('FRONT', 'ORTHO'):
    # Resize along X and Y axes
    pyautogui.hotkey('ctrl', 'alt', 'S')
if bpy.context.space_data.region_3d.view_perspective in ('TOP', 'ORTHO'):
    # Resize along X and Z axes
    pyautogui.hotkey('ctrl', 'alt', 'O') 

This code snippet does not work, but it may help another user understand what I may be after

  • $\begingroup$ Hi. Please wrap your code using code tags so it is easier to read $\endgroup$ Commented Apr 1, 2023 at 14:44

1 Answer 1


The answer was here: Blender Stack Exchange

With help from Chat GTP, I edited to create this:

def get_view_orientation_from_matrix(view_matrix):
    r = lambda x: round(x, 2)
    view_rot = view_matrix.to_euler()

    orientation_dict = {(0.0, 0.0, 0.0): 'TOP',
                        (r(math.pi), 0.0, 0.0): 'BOTTOM',
                        (r(-math.pi/2), 0.0, 0.0): 'FRONT',
                        (r(math.pi/2), 0.0, r(-math.pi)): 'BACK',
                        (r(-math.pi/2), r(math.pi/2), 0.0): 'LEFT',
                        (r(-math.pi/2), r(-math.pi/2), 0.0): 'RIGHT'}

    return orientation_dict.get(tuple(map(r, view_rot)), 'USER')

class ViewOrientationOperator(bpy.types.Operator):
    bl_idname = "view_orientation.operator"
    bl_label = "View Orientation"

    def execute(self, context):
        view_orientations = []
        # Find the active 3D view area
        view_area = next((a for a in context.screen.areas if a.type == 'VIEW_3D'), None)
        if view_area is not None:
            # Get the view orientation and perspective
            r3d = view_area.spaces.active.region_3d
            view_matrix = r3d.view_matrix
            view_orientation = get_view_orientation_from_matrix(view_matrix).capitalize()
            view_perspective = r3d.view_perspective.capitalize()

            # Append the view information to the list1
            view_orientations.append(f"{view_orientation} {view_perspective}")        
            if view_orientation == "Top":
                pyautogui.hotkey('ctrl', 'alt', 'O')
            if view_orientation == "Front":
                pyautogui.hotkey('ctrl', 'alt', 'S')

            # Check if local view is enabled
            if view_area.spaces.active.local_view is not None:
                view_orientations[-1] += " (Local)"

        return {'FINISHED'}

def register():

def unregister():

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .