1
$\begingroup$

I've been looking for ages and I've come to my wits end. Basically, all I need is to find the location of the Viewport so I can spawn a camera in its position. The idea is the same as if you spawned a camera and used the Ctrl+Alt+Num 0 shortcut. I know I can do it that way manually, but this is for college for a personal project for my tutor so I need it scripted. I can handle the rest I just need to know how I can pull the location from the Viewport.

If it's not possible, that's fine too.

Thanks in advance.

$\endgroup$
4
  • $\begingroup$ What if your script make the camera spawn with Ctrl+Alt+Num 0 (as you say) and then extract the location of the camera ? $\endgroup$ Jun 6, 2022 at 10:43
  • $\begingroup$ @pons de leon The main reason is because I need the camera to spawn where the viewport is. Taking the position after spawning it isn't helpful. However, your idea might still work, is it possible to invoke a keybind in a script? $\endgroup$
    – 50541499
    Jun 6, 2022 at 12:23
  • $\begingroup$ I am not into scripting, maybe this can help you blender.stackexchange.com/questions/3426/…. Otherwise, no idea $\endgroup$ Jun 6, 2022 at 12:49
  • $\begingroup$ I'll look into that, thanks $\endgroup$
    – 50541499
    Jun 6, 2022 at 13:04

1 Answer 1

5
$\begingroup$

Get Viewport data

import bpy

def find_area():
    try:
        for a in bpy.data.window_managers[0].windows[0].screen.areas:
            if a.type == "VIEW_3D":
                return a
        return None
    except:
        return None

area = find_area()

if area is None:
    print("area not find")
else:
    # print(dir(area))
    r3d = area.spaces[0].region_3d
    view_mat = r3d.view_matrix
    print("view matrix: ", view_mat)

    loc, rot, sca = view_mat.decompose()
    print("location xyz: ", loc)
    print("rotation wxyz: ", rot)
    print("scale xyz: ", sca)
    print("")
    print("view_distance: ", r3d.view_distance)
    print("view_location: ", r3d.view_location)
    print("view_rotation: ", r3d.view_rotation)
    print("view_camera_zoom: ", r3d.view_camera_zoom)
    print("view_distance: ", r3d.view_distance)
    print("view_camera_offset: ", r3d.view_camera_offset)


# ['__doc__', '__module__', '__slots__', 'bl_rna', 'clip_planes', 'is_orthographic_side_view', 'is_perspective', 'lock_rotation', 'perspective_matrix', 'rna_type', 'show_sync_view', 'update', 'use_box_clip', 'use_clip_planes', 'view_camera_offset', 'view_camera_zoom', 'view_distance', 'view_location', 'view_matrix', 'view_perspective', 'view_rotation', 'window_matrix']
$\endgroup$
1
  • $\begingroup$ Thanks so much, just what I need. $\endgroup$
    – 50541499
    Jun 6, 2022 at 13:10

You must log in to answer this question.

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