Blender V5.0
view3d_navigate_view_camera.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_context.hh"
10#include "BKE_layer.hh"
11
12#include "DEG_depsgraph.hh"
13
14#include "WM_api.hh"
15
16#include "view3d_intern.hh"
17
18#include "view3d_navigate.hh" /* own include */
19
20/* -------------------------------------------------------------------- */
23
25{
26 View3D *v3d;
27 ARegion *region;
28 RegionView3D *rv3d;
29 const int smooth_viewtx = WM_operator_smooth_viewtx_get(op);
30
31 /* no nullptr check is needed, poll checks */
32 ED_view3d_context_user_region(C, &v3d, &region);
33 rv3d = static_cast<RegionView3D *>(region->regiondata);
34
36
37 if ((RV3D_LOCK_FLAGS(rv3d) & RV3D_LOCK_ANY_TRANSFORM) == 0) {
38 ViewLayer *view_layer = CTX_data_view_layer(C);
39 Scene *scene = CTX_data_scene(C);
40
41 if (rv3d->persp != RV3D_CAMOB) {
42 BKE_view_layer_synced_ensure(scene, view_layer);
44
45 if (!rv3d->smooth_timer) {
46 /* store settings of current view before allowing overwriting with camera view
47 * only if we're not currently in a view transition */
48
50 }
51
52 /* first get the default camera for the view lock type */
53 if (v3d->scenelock) {
54 /* sets the camera view if available */
55 v3d->camera = scene->camera;
56 }
57 else {
58 /* use scene camera if one is not set (even though we're unlocked) */
59 if (v3d->camera == nullptr) {
60 v3d->camera = scene->camera;
61 }
62 }
63
64 /* if the camera isn't found, check a number of options */
65 if (v3d->camera == nullptr && ob && ob->type == OB_CAMERA) {
66 v3d->camera = ob;
67 }
68
69 if (v3d->camera == nullptr) {
70 v3d->camera = BKE_view_layer_camera_find(scene, view_layer);
71 }
72
73 /* couldn't find any useful camera, bail out */
74 if (v3d->camera == nullptr) {
75 return OPERATOR_CANCELLED;
76 }
77
78 /* important these don't get out of sync for locked scenes */
79 if (v3d->scenelock && scene->camera != v3d->camera) {
80 scene->camera = v3d->camera;
82 }
83
84 /* finally do snazzy view zooming */
85 rv3d->persp = RV3D_CAMOB;
86
87 V3D_SmoothParams sview = {nullptr};
88 sview.camera = v3d->camera;
89 sview.ofs = rv3d->ofs;
90 sview.quat = rv3d->viewquat;
91 sview.dist = &rv3d->dist;
92 sview.lens = &v3d->lens;
93 /* No undo because this changes cameras (and wont move the camera). */
94 sview.undo_str = nullptr;
95
96 ED_view3d_smooth_view(C, v3d, region, smooth_viewtx, &sview);
97 }
98 else {
99 /* return to settings of last view */
100 /* does view3d_smooth_view too */
102 v3d,
103 region,
104 rv3d->lviewquat,
105 rv3d->lview,
106 rv3d->lview_axis_roll,
107 rv3d->lpersp,
108 nullptr,
109 smooth_viewtx);
110 }
111 }
112
113 return OPERATOR_FINISHED;
114}
115
117{
118 /* identifiers */
119 ot->name = "View Camera";
120 ot->description = "Toggle the camera view";
121 ot->idname = "VIEW3D_OT_view_camera";
122
123 /* API callbacks. */
124 ot->exec = view_camera_exec;
126
127 /* flags */
128 ot->flag = 0;
129}
130
Scene * CTX_data_scene(const bContext *C)
ViewLayer * CTX_data_view_layer(const bContext *C)
void BKE_view_layer_synced_ensure(const Scene *scene, ViewLayer *view_layer)
Object * BKE_view_layer_camera_find(const Scene *scene, ViewLayer *view_layer)
Object * BKE_view_layer_active_object_get(const ViewLayer *view_layer)
void DEG_id_tag_update(ID *id, unsigned int flags)
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
@ OB_CAMERA
#define RV3D_LOCK_FLAGS(rv3d)
@ RV3D_CAMOB
@ RV3D_LOCK_ANY_TRANSFORM
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
bool ED_view3d_context_user_region(bContext *C, View3D **r_v3d, ARegion **r_region)
void ED_view3d_lastview_store(RegionView3D *rv3d)
bool ED_operator_rv3d_user_region_poll(bContext *C)
#define C
Definition RandGen.cpp:29
void * regiondata
struct wmTimer * smooth_timer
struct Object * camera
const char * undo_str
struct Object * camera
short scenelock
void axis_set_view(bContext *C, View3D *v3d, ARegion *region, const float quat_[4], char view, char view_axis_roll, int perspo, const float *align_to_quat, const int smooth_viewtx)
void ED_view3d_smooth_view(bContext *C, View3D *v3d, ARegion *region, int smooth_viewtx, const V3D_SmoothParams *sview)
void ED_view3d_smooth_view_force_finish(bContext *C, View3D *v3d, ARegion *region)
void VIEW3D_OT_view_camera(wmOperatorType *ot)
static wmOperatorStatus view_camera_exec(bContext *C, wmOperator *op)
wmOperatorType * ot
Definition wm_files.cc:4237
int WM_operator_smooth_viewtx_get(const wmOperator *op)