Blender V5.3
view3d_vr_location_scouting.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_context.hh"
10#include "BKE_lib_id.hh"
11#include "BKE_object.hh"
12#include "BKE_object_types.hh"
13
15#include "BLI_math_vector_c.hh"
16
17#include "BLT_translation.hh"
18
19#include "DNA_camera_types.h"
20
21#include "ED_screen.hh"
22#include "ED_view3d.hh"
23
24#include "RNA_access.hh"
25
26#include "UI_interface.hh"
27
28#include "WM_api.hh"
29
30#ifdef WITH_XR_OPENXR
31# include "wm_xr.hh"
32#endif
33
34#include "view3d_intern.hh"
35
36namespace blender {
37
38/* -------------------------------------------------------------------- */
47
48/* NOTE: these defines are saved in keymap files, do not change values but just add new ones */
49enum {
51
54
57};
58
60{
61 static const EnumPropertyItem modal_items[] = {
62 {CAPTURE_REVIEW_MODAL_EXIT, "EXIT", 0, "Exit", ""},
63
64 {CAPTURE_REVIEW_MODAL_PREV, "PREVIOUS", 0, "Previous Capture", "Switch to previous capture"},
65 {CAPTURE_REVIEW_MODAL_NEXT, "NEXT", 0, "Next Capture", "Switch to next capture"},
66
68 "ADD_CAMERA",
69 0,
70 "Add Camera",
71 "Add Camera from current capture"},
73 "ADD_MARKER",
74 0,
75 "Add Marker",
76 "Add Marker from current capture"},
77
78 {0, nullptr, 0, nullptr, nullptr},
79 };
80
81 wmKeyMap *keymap = WM_modalkeymap_find(keyconf,
82 "View3D VR Location Scouting Capture Review Modal");
83
84 /* This function is called for each space-type, only needs to add map once. */
85 if (keymap && keymap->modal_items) {
86 return;
87 }
88
89 keymap = WM_modalkeymap_ensure(
90 keyconf, "View3D VR Location Scouting Capture Review Modal", modal_items);
91
92 /* Assign map to operators. */
93#ifdef WITH_XR_OPENXR
94 WM_modalkeymap_assign(keymap, "VIEW3D_OT_vr_location_scouting_capture_review");
95#endif
96}
97
98#ifdef WITH_XR_OPENXR
99
100struct CaptureReviewData {
101 /* Context. */
102 ScrArea *area;
103 RegionView3D *rv3d;
104 View3D *v3d;
105
106 /* Previous camera to restore on exit. */
107 Object *prev_view3d_cam_ob;
108 eRegionView3D_Persp prev_view3d_persp;
109
110 /* Fake camera object to set the View3D. */
111 Object *cam_ob;
112 Camera *cam_data;
113};
114
115static void location_scouting_review_draw_status(bContext *C, wmOperator *op)
116{
117 WorkspaceStatus status(C);
118
119 status.opmodal(IFACE_("Exit"), op->type, CAPTURE_REVIEW_MODAL_EXIT);
120
121 status.opmodal("", op->type, CAPTURE_REVIEW_MODAL_PREV);
122 status.item(IFACE_("Previous"), ICON_NONE);
123
124 status.opmodal("", op->type, CAPTURE_REVIEW_MODAL_NEXT);
125 status.item(IFACE_("Next"), ICON_NONE);
126
127 status.opmodal("", op->type, CAPTURE_REVIEW_MODAL_ADD_CAMERA);
128 status.item(IFACE_("Add Camera"), ICON_NONE);
129
130 status.opmodal("", op->type, CAPTURE_REVIEW_MODAL_ADD_MARKER);
131 status.item(IFACE_("Add Marker"), ICON_NONE);
132}
133
134static bool vr_location_scouting_capture_review_poll(bContext *C)
135{
138}
139
140/* The capture review running property is stored on the WM, registered by the Python VR add-on. */
141static bool vr_location_scouting_capture_review_get_running_state(bContext *C)
142{
144 PropertyRNA *state_prop = RNA_struct_find_property(&wm_ptr, "vr_capture_review_running");
145 BLI_assert(state_prop != nullptr);
146
147 return RNA_property_boolean_get(&wm_ptr, state_prop);
148}
149
150static void vr_location_scouting_capture_review_set_running_state(bContext *C, const bool state)
151{
153 PropertyRNA *state_prop = RNA_struct_find_property(&wm_ptr, "vr_capture_review_running");
154 BLI_assert(state_prop != nullptr);
155
156 RNA_property_boolean_set(&wm_ptr, state_prop, state);
157}
158
159static wmOperatorStatus vr_location_scouting_capture_review_invoke(bContext *C,
160 wmOperator *op,
161 const wmEvent * /*event*/)
162{
163 /* Operator invoked while already running, toggle off. */
164 if (vr_location_scouting_capture_review_get_running_state(C)) {
165 /* Set the running state (stored on the WM) to false, cached by the running operator modal. */
166 vr_location_scouting_capture_review_set_running_state(C, false);
167 return OPERATOR_CANCELLED;
168 }
169
170 View3D *v3d;
171 ARegion *region;
172 ED_view3d_context_user_region(C, &v3d, &region);
173
174 RegionView3D *rv3d = static_cast<RegionView3D *>(region->regiondata);
175 ScrArea *area = CTX_wm_area(C);
176
178 return OPERATOR_CANCELLED;
179 }
180
181 CaptureReviewData *op_data = MEM_new_zeroed<CaptureReviewData>("View3DReviewCaptureData");
182 op_data->area = area;
183 op_data->rv3d = rv3d;
184 op_data->v3d = v3d;
185
186 op_data->prev_view3d_cam_ob = v3d->camera;
188 /* Store previous persp separately from #ED_view3d_lastview_store as setting rv3d->lpersp to
189 * CAMOB is unexpected by navigation logic. */
190 op_data->prev_view3d_persp = rv3d->persp;
191
192 /* Build a fake Camera object to set on the View3D. */
193 op_data->cam_ob = BKE_id_new_nomain<Object>("ReviewCaptureCamera");
194 op_data->cam_ob->type = OB_CAMERA;
195
196 /* Lock rotation to prevent user from exiting the review camera view.
197 * Re-using quad-view flags. */
198 rv3d->viewlock |= RV3D_LOCK_ROTATION;
199
200 op_data->cam_data = BKE_id_new_nomain<Camera>("ReviewCaptureCameraData");
201 op_data->cam_ob->data = id_cast<ID *>(op_data->cam_data);
202
203 op->customdata = op_data;
204
205 /* Set running state. */
206 vr_location_scouting_capture_review_set_running_state(C, true);
207
209 location_scouting_review_draw_status(C, op);
210
212}
213
214static void vr_location_scouting_capture_review_exit(bContext *C, wmOperator *op)
215{
216 CaptureReviewData *op_data = static_cast<CaptureReviewData *>(op->customdata);
217 RegionView3D *rv3d = op_data->rv3d;
218
219 /* Restore viewport, last view stored by #ED_view3d_lastview_store */
220 copy_qt_qt(rv3d->viewquat, rv3d->lviewquat);
221 rv3d->view = rv3d->lview;
222 rv3d->view_axis_roll = rv3d->lview_axis_roll;
223 rv3d->persp = op_data->prev_view3d_persp;
224 rv3d->viewlock &= ~RV3D_LOCK_ROTATION;
225
226 op_data->v3d->camera = op_data->prev_view3d_cam_ob;
227
228 vr_location_scouting_capture_review_set_running_state(C, false);
229
230 /* Redraw entire area (both the viewport and sidebar regions), clear status text. */
232 ED_workspace_status_text(C, nullptr);
233
234 /* Free data. */
235 BKE_id_free(nullptr, id_cast<ID *>(op_data->cam_ob));
236 BKE_id_free(nullptr, id_cast<ID *>(op_data->cam_data));
237
238 MEM_delete(op_data);
239}
240
241static void vr_location_scouting_capture_review_cancel(bContext *C, wmOperator *op)
242{
243 vr_location_scouting_capture_review_exit(C, op);
244}
245
246static bool vr_location_scouting_capture_review_event(bContext *C, const wmEvent *event)
247{
248 /* Return true if event is handled, false otherwise. */
249 if (event->type != EVT_MODAL_MAP) {
250 return false;
251 }
252
253 switch (event->val) {
255 vr_location_scouting_capture_review_set_running_state(C, false);
256 break;
259 wmOperatorType *ot = WM_operatortype_find("VIEW3D_OT_vr_location_scouting_browse_captures",
260 false);
261
263 RNA_boolean_set(&props_ptr, "backward", event->val == CAPTURE_REVIEW_MODAL_PREV);
264
266 WM_operator_properties_free(&props_ptr);
267
268 break;
269 }
272 "VIEW3D_OT_vr_location_scouting_add_camera_from_capture",
274 nullptr,
275 nullptr);
276 break;
279 "VIEW3D_OT_vr_location_scouting_add_marker_from_capture",
281 nullptr,
282 nullptr);
283 break;
284 }
285
286 return true;
287}
288
289static wmOperatorStatus vr_location_scouting_capture_review_modal(bContext *C,
290 wmOperator *op,
291 const wmEvent *event)
292{
293 CaptureReviewData *op_data = static_cast<CaptureReviewData *>(op->customdata);
294
295 /* Exit review if the mouse leaves the viewport area, or if the active area editor changes.
296 * This prevents undefined behavior caused by the operator running without an active View3D
297 * area while still allowing the operator to be non-blocking for interactive sidebar UI. */
298 if ((ED_area_find_under_cursor(C, SPACE_TYPE_ANY, event->xy) != op_data->area) ||
299 (op_data->area->spacetype != SPACE_VIEW3D))
300 {
301 vr_location_scouting_capture_review_exit(C, op);
302 return OPERATOR_FINISHED;
303 }
304
305 /* Get the current capture. */
306 Scene *scene = CTX_data_scene(C);
307 auto capture = wm_xr_location_scouting_get_active_capture(scene);
308
309 if (!capture.has_value()) {
310 BKE_report(op->reports, RPT_INFO, "No VR captures to display, exiting capture review...");
311 vr_location_scouting_capture_review_exit(C, op);
312 return OPERATOR_FINISHED;
313 }
314
315 const bool event_handled = vr_location_scouting_capture_review_event(C, event);
316
317 /* Exit requested, state set by the operator invoke from UI, or event function from keymap. */
318 if (!vr_location_scouting_capture_review_get_running_state(C)) {
319 vr_location_scouting_capture_review_exit(C, op);
320 return OPERATOR_FINISHED;
321 }
322
323 /* Force perspective to camera. */
324 op_data->rv3d->persp = RV3D_CAMOB;
325
326 /* Set Camera data from capture. */
327 op_data->cam_data->lens = capture->lens_focal;
328 SET_FLAG_FROM_TEST(op_data->cam_data->dof.flag, capture->dof_enabled, CAM_DOF_ENABLED);
329 op_data->cam_data->dof.aperture_fstop = capture->dof_fstop;
330 op_data->cam_data->dof.focus_distance = capture->dof_distance;
331
332 /* Camera viewport display settings, set passepartout to emphasize that the modal is enabled. */
333 op_data->cam_data->flag |= CAM_SHOWPASSEPARTOUT;
334 op_data->cam_data->passepartalpha = 0.99f; /* *Almost* completely opaque. */
335
336 float capture_cam_mat[4][4];
337 quat_to_mat4(capture_cam_mat, capture->orientation_quat);
338 copy_v3_v3(capture_cam_mat[3], capture->position);
339
340 BKE_object_apply_mat4(op_data->cam_ob, capture_cam_mat, false, false);
341 /* Minimum eval without going through the depsgraph. */
342 BKE_object_to_mat4(op_data->cam_ob, op_data->cam_ob->runtime->object_to_world.ptr());
343
344 /* Set fake Camera object as the View3D camera. */
345 op_data->v3d->camera = op_data->cam_ob;
346
348 location_scouting_review_draw_status(C, op);
349
350 if (event_handled) {
352 }
353
355}
356
358{
359 /* Identifiers. */
360 ot->name = "Location Scouting Capture Review";
361 ot->description = "Interactively review Location Scouting VR Captures";
362 ot->idname = "VIEW3D_OT_vr_location_scouting_capture_review";
363
364 /* Callbacks. */
365 ot->invoke = vr_location_scouting_capture_review_invoke;
366 ot->cancel = vr_location_scouting_capture_review_cancel;
367 ot->modal = vr_location_scouting_capture_review_modal;
368 ot->poll = vr_location_scouting_capture_review_poll;
369}
370
371#endif /* WITH_XR_OPENXR */
372
374
375} // namespace blender
General operations, lookup, etc. for blender objects.
#define IFACE_(msgid)
#define SPACE_TYPE_ANY
#define RV3D_LOCK_FLAGS(rv3d)
#define C
Definition RandGen.cpp:29
#define SET_FLAG_FROM_TEST(value, test, flag)
const int state
BMesh Iterators.
bool ED_view3d_context_user_region(bContext *C, View3D **r_v3d, ARegion **r_region)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
ScrArea * CTX_wm_area(const bContext *C)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
wmKeyMap * WM_modalkeymap_find(wmKeyConfig *keyconf, const char *idname)
Definition wm_keymap.cc:999
MINLINE void copy_v3_v3(float r[3], const float a[3])
PointerRNA RNA_id_pointer_create(ID *id)
void WM_operator_properties_free(PointerRNA *ptr)
PointerRNA WM_operator_properties_create_ptr(wmOperatorType *ot)
@ RV3D_LOCK_ANY_TRANSFORM
void vr_location_scouting_capture_review_modal_keymap(wmKeyConfig *keyconf)
void BKE_object_apply_mat4(Object *ob, const float mat[4][4], bool use_compat, bool use_parent)
Definition object.cc:3663
bool RNA_property_boolean_get(PointerRNA *ptr, PropertyRNA *prop)
bool wm_xr_location_scouting_is_captures_empty(Scene *scene)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void copy_qt_qt(float q[4], const float a[4])
void BKE_object_to_mat4(const Object *ob, float r_mat[4][4])
Definition object.cc:3125
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
wmWindowManager * CTX_wm_manager(const bContext *C)
wmOperatorStatus WM_operator_name_call(bContext *C, const char *opstring, wm::OpCallContext context, PointerRNA *properties, const wmEvent *event)
void VIEW3D_OT_vr_location_scouting_capture_review(wmOperatorType *ot)
void BKE_id_free(Main *bmain, void *idv)
void RNA_boolean_set(PointerRNA *ptr, const char *name, bool value)
void * BKE_id_new_nomain(short type, const char *name)
Definition lib_id.cc:1617
ScrArea * ED_area_find_under_cursor(const bContext *C, int spacetype, const int event_xy[2])
Definition area.cc:4456
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
wmOperatorStatus WM_operator_name_call_ptr(bContext *C, wmOperatorType *ot, wm::OpCallContext context, PointerRNA *properties, const wmEvent *event)
bool ED_operator_region_view3d_active(bContext *C)
void WM_modalkeymap_assign(wmKeyMap *km, const char *opname)
void ED_view3d_lastview_store(RegionView3D *rv3d)
void RNA_property_boolean_set(PointerRNA *ptr, PropertyRNA *prop, bool value)
Scene * CTX_data_scene(const bContext *C)
wmKeyMap * WM_modalkeymap_ensure(wmKeyConfig *keyconf, const char *idname, const EnumPropertyItem *items)
Definition wm_keymap.cc:972
std::optional< XrLocationScoutingCapture > wm_xr_location_scouting_get_active_capture(Scene *scene)
void ED_workspace_status_text(bContext *C, const char *str)
Definition area.cc:1153
void quat_to_mat4(float m[4][4], const float q[4])
void ED_area_tag_redraw(ScrArea *area)
Definition area.cc:784