Blender V4.3
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
9#include "MEM_guardedalloc.h"
10
11#include "BKE_context.hh"
12#include "BKE_layer.hh"
13
15
16#include "WM_api.hh"
17
18#include "view3d_intern.hh"
19
20#include "view3d_navigate.hh" /* own include */
21
22/* -------------------------------------------------------------------- */
27{
28 View3D *v3d;
29 ARegion *region;
30 RegionView3D *rv3d;
31 const int smooth_viewtx = WM_operator_smooth_viewtx_get(op);
32
33 /* no nullptr check is needed, poll checks */
34 ED_view3d_context_user_region(C, &v3d, &region);
35 rv3d = static_cast<RegionView3D *>(region->regiondata);
36
38
39 if ((RV3D_LOCK_FLAGS(rv3d) & RV3D_LOCK_ANY_TRANSFORM) == 0) {
40 ViewLayer *view_layer = CTX_data_view_layer(C);
41 Scene *scene = CTX_data_scene(C);
42
43 if (rv3d->persp != RV3D_CAMOB) {
44 BKE_view_layer_synced_ensure(scene, view_layer);
46
47 if (!rv3d->smooth_timer) {
48 /* store settings of current view before allowing overwriting with camera view
49 * only if we're not currently in a view transition */
50
52 }
53
54 /* first get the default camera for the view lock type */
55 if (v3d->scenelock) {
56 /* sets the camera view if available */
57 v3d->camera = scene->camera;
58 }
59 else {
60 /* use scene camera if one is not set (even though we're unlocked) */
61 if (v3d->camera == nullptr) {
62 v3d->camera = scene->camera;
63 }
64 }
65
66 /* if the camera isn't found, check a number of options */
67 if (v3d->camera == nullptr && ob && ob->type == OB_CAMERA) {
68 v3d->camera = ob;
69 }
70
71 if (v3d->camera == nullptr) {
72 v3d->camera = BKE_view_layer_camera_find(scene, view_layer);
73 }
74
75 /* couldn't find any useful camera, bail out */
76 if (v3d->camera == nullptr) {
77 return OPERATOR_CANCELLED;
78 }
79
80 /* important these don't get out of sync for locked scenes */
81 if (v3d->scenelock && scene->camera != v3d->camera) {
82 scene->camera = v3d->camera;
84 }
85
86 /* finally do snazzy view zooming */
87 rv3d->persp = RV3D_CAMOB;
88
89 V3D_SmoothParams sview = {nullptr};
90 sview.camera = v3d->camera;
91 sview.ofs = rv3d->ofs;
92 sview.quat = rv3d->viewquat;
93 sview.dist = &rv3d->dist;
94 sview.lens = &v3d->lens;
95 /* No undo because this changes cameras (and wont move the camera). */
96 sview.undo_str = nullptr;
97
98 ED_view3d_smooth_view(C, v3d, region, smooth_viewtx, &sview);
99 }
100 else {
101 /* return to settings of last view */
102 /* does view3d_smooth_view too */
104 v3d,
105 region,
106 rv3d->lviewquat,
107 rv3d->lview,
108 rv3d->lview_axis_roll,
109 rv3d->lpersp,
110 nullptr,
111 smooth_viewtx);
112 }
113 }
114
115 return OPERATOR_FINISHED;
116}
117
119{
120 /* identifiers */
121 ot->name = "View Camera";
122 ot->description = "Toggle the camera view";
123 ot->idname = "VIEW3D_OT_view_camera";
124
125 /* api callbacks */
128
129 /* flags */
130 ot->flag = 0;
131}
132
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:1085
@ OB_CAMERA
#define RV3D_LOCK_FLAGS(rv3d)
@ RV3D_CAMOB
@ RV3D_LOCK_ANY_TRANSFORM
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)
Read Guarded memory(de)allocation.
struct wmTimer * smooth_timer
const char * undo_str
struct Object * camera
short scenelock
const char * name
Definition WM_types.hh:990
bool(* poll)(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1042
const char * idname
Definition WM_types.hh:992
int(* exec)(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1006
const char * description
Definition WM_types.hh:996
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)
static int view_camera_exec(bContext *C, wmOperator *op)
void VIEW3D_OT_view_camera(wmOperatorType *ot)
wmOperatorType * ot
Definition wm_files.cc:4125
int WM_operator_smooth_viewtx_get(const wmOperator *op)