Blender V5.0
view3d_context.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2008 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "DNA_object_types.h"
10#include "DNA_screen_types.h"
11#include "DNA_space_types.h"
12
13#include "BKE_context.hh"
14#include "BKE_layer.hh"
15#include "BKE_screen.hh"
16
17#include "ED_view3d.hh"
18
19#include "view3d_intern.hh"
20
21/* -------------------------------------------------------------------- */
24
25const char *view3d_context_dir[] = {
26 "active_object",
27 "selected_ids",
28 nullptr,
29};
30
31int view3d_context(const bContext *C, const char *member, bContextDataResult *result)
32{
33 /* fall back to the scene layer,
34 * allows duplicate and other object operators to run outside the 3d view */
35
36 if (CTX_data_dir(member)) {
38 return CTX_RESULT_OK;
39 }
40 if (CTX_data_equals(member, "active_object")) {
41 /* In most cases the active object is the `view_layer->basact->object`.
42 * For the 3D view however it can be nullptr when hidden.
43 *
44 * This is ignored in the case the object is in any mode (besides object-mode),
45 * since the object's mode impacts the current tool, cursor, gizmos etc.
46 * If we didn't have this exception, changing visibility would need to perform
47 * many of the same updates as changing the objects mode.
48 *
49 * Further, there are multiple ways to hide objects - by collection, by object type, etc.
50 * it's simplest if all these methods behave consistently - respecting the object-mode
51 * without showing the object.
52 *
53 * See #85532 for alternatives that were considered. */
54 const Scene *scene = CTX_data_scene(C);
55 ViewLayer *view_layer = CTX_data_view_layer(C);
56 BKE_view_layer_synced_ensure(scene, view_layer);
57 Base *base = BKE_view_layer_active_base_get(view_layer);
58 if (base) {
59 Object *ob = base->object;
60 /* if hidden but in edit mode, we still display, can happen with animation */
62 (ob->mode != OB_MODE_OBJECT))
63 {
65 }
66 }
67
68 return CTX_RESULT_OK;
69 }
70 if (CTX_data_equals(member, "selected_ids")) {
71 blender::Vector<PointerRNA> selected_objects;
72 CTX_data_selected_objects(C, &selected_objects);
73 for (const PointerRNA &ptr : selected_objects) {
74 ID *selected_id = ptr.owner_id;
75 CTX_data_id_list_add(result, selected_id);
76 }
78 return CTX_RESULT_OK;
79 }
80
82}
83
85
86/* -------------------------------------------------------------------- */
89
91{
93
94 if (rv3d == nullptr) {
95 ScrArea *area = CTX_wm_area(C);
96 if (area && area->spacetype == SPACE_VIEW3D) {
98 if (region) {
99 rv3d = static_cast<RegionView3D *>(region->regiondata);
100 }
101 }
102 }
103 return rv3d;
104}
105
107{
108 ScrArea *area = CTX_wm_area(C);
109
110 *r_v3d = nullptr;
111 *r_region = nullptr;
112
113 if (area && area->spacetype == SPACE_VIEW3D) {
114 ARegion *region = CTX_wm_region(C);
115 View3D *v3d = (View3D *)area->spacedata.first;
116
117 if (region) {
118 RegionView3D *rv3d;
119 if ((region->regiontype == RGN_TYPE_WINDOW) &&
120 (rv3d = static_cast<RegionView3D *>(region->regiondata)) &&
121 (rv3d->viewlock & RV3D_LOCK_ROTATION) == 0)
122 {
123 *r_v3d = v3d;
124 *r_region = region;
125 return true;
126 }
127
128 if (ED_view3d_area_user_region(area, v3d, r_region)) {
129 *r_v3d = v3d;
130 return true;
131 }
132 }
133 }
134
135 return false;
136}
137
void CTX_data_dir_set(bContextDataResult *result, const char **dir)
void CTX_data_id_list_add(bContextDataResult *result, ID *id)
bool CTX_data_equals(const char *member, const char *str)
bool CTX_data_dir(const char *member)
void CTX_data_id_pointer_set(bContextDataResult *result, ID *id)
void CTX_data_type_set(bContextDataResult *result, ContextDataType type)
ScrArea * CTX_wm_area(const bContext *C)
@ CTX_RESULT_MEMBER_NOT_FOUND
@ CTX_RESULT_OK
Scene * CTX_data_scene(const bContext *C)
bool CTX_data_selected_objects(const bContext *C, blender::Vector< PointerRNA > *list)
RegionView3D * CTX_wm_region_view3d(const bContext *C)
ARegion * CTX_wm_region(const bContext *C)
ViewLayer * CTX_data_view_layer(const bContext *C)
void BKE_view_layer_synced_ensure(const Scene *scene, ViewLayer *view_layer)
Base * BKE_view_layer_active_base_get(ViewLayer *view_layer)
ARegion * BKE_area_find_region_active_win(const ScrArea *area)
Definition screen.cc:859
@ BASE_ENABLED_AND_MAYBE_VISIBLE_IN_VIEWPORT
@ OB_MODE_OBJECT
Object is a sort of wrapper for general info.
@ RGN_TYPE_WINDOW
@ SPACE_VIEW3D
@ RV3D_LOCK_ROTATION
bool ED_view3d_area_user_region(const ScrArea *area, const View3D *v3d, ARegion **r_region)
#define C
Definition RandGen.cpp:29
void * regiondata
short flag
struct Object * object
Definition DNA_ID.h:414
void * first
ListBase spacedata
RegionView3D * ED_view3d_context_rv3d(bContext *C)
bool ED_view3d_context_user_region(bContext *C, View3D **r_v3d, ARegion **r_region)
const char * view3d_context_dir[]
int view3d_context(const bContext *C, const char *member, bContextDataResult *result)
PointerRNA * ptr
Definition wm_files.cc:4238