Blender V4.3
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
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 "BLI_listbase.h"
18
19#include "ED_view3d.hh"
20
21#include "view3d_intern.hh"
22
23/* -------------------------------------------------------------------- */
27const char *view3d_context_dir[] = {
28 "active_object",
29 "selected_ids",
30 nullptr,
31};
32
33int view3d_context(const bContext *C, const char *member, bContextDataResult *result)
34{
35 /* fallback to the scene layer,
36 * allows duplicate and other object operators to run outside the 3d view */
37
38 if (CTX_data_dir(member)) {
40 return CTX_RESULT_OK;
41 }
42 if (CTX_data_equals(member, "active_object")) {
43 /* In most cases the active object is the `view_layer->basact->object`.
44 * For the 3D view however it can be nullptr when hidden.
45 *
46 * This is ignored in the case the object is in any mode (besides object-mode),
47 * since the object's mode impacts the current tool, cursor, gizmos etc.
48 * If we didn't have this exception, changing visibility would need to perform
49 * many of the same updates as changing the objects mode.
50 *
51 * Further, there are multiple ways to hide objects - by collection, by object type, etc.
52 * it's simplest if all these methods behave consistently - respecting the object-mode
53 * without showing the object.
54 *
55 * See #85532 for alternatives that were considered. */
56 const Scene *scene = CTX_data_scene(C);
57 ViewLayer *view_layer = CTX_data_view_layer(C);
58 BKE_view_layer_synced_ensure(scene, view_layer);
59 Base *base = BKE_view_layer_active_base_get(view_layer);
60 if (base) {
61 Object *ob = base->object;
62 /* if hidden but in edit mode, we still display, can happen with animation */
64 (ob->mode != OB_MODE_OBJECT))
65 {
66 CTX_data_id_pointer_set(result, &ob->id);
67 }
68 }
69
70 return CTX_RESULT_OK;
71 }
72 if (CTX_data_equals(member, "selected_ids")) {
73 blender::Vector<PointerRNA> selected_objects;
74 CTX_data_selected_objects(C, &selected_objects);
75 for (const PointerRNA &ptr : selected_objects) {
76 ID *selected_id = ptr.owner_id;
77 CTX_data_id_list_add(result, selected_id);
78 }
80 return CTX_RESULT_OK;
81 }
82
84}
85
88/* -------------------------------------------------------------------- */
93{
95
96 if (rv3d == nullptr) {
97 ScrArea *area = CTX_wm_area(C);
98 if (area && area->spacetype == SPACE_VIEW3D) {
100 if (region) {
101 rv3d = static_cast<RegionView3D *>(region->regiondata);
102 }
103 }
104 }
105 return rv3d;
106}
107
109{
110 ScrArea *area = CTX_wm_area(C);
111
112 *r_v3d = nullptr;
113 *r_region = nullptr;
114
115 if (area && area->spacetype == SPACE_VIEW3D) {
116 ARegion *region = CTX_wm_region(C);
117 View3D *v3d = (View3D *)area->spacedata.first;
118
119 if (region) {
120 RegionView3D *rv3d;
121 if ((region->regiontype == RGN_TYPE_WINDOW) &&
122 (rv3d = static_cast<RegionView3D *>(region->regiondata)) &&
123 (rv3d->viewlock & RV3D_LOCK_ROTATION) == 0)
124 {
125 *r_v3d = v3d;
126 *r_region = region;
127 return true;
128 }
129
130 if (ED_view3d_area_user_region(area, v3d, r_region)) {
131 *r_v3d = v3d;
132 return true;
133 }
134 }
135 }
136
137 return false;
138}
139
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)
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)
@ CTX_DATA_TYPE_COLLECTION
RegionView3D * CTX_wm_region_view3d(const bContext *C)
ARegion * CTX_wm_region(const bContext *C)
void CTX_data_type_set(bContextDataResult *result, short type)
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:828
@ 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)
short flag
struct Object * object
Definition DNA_ID.h:413
ID * owner_id
Definition RNA_types.hh:40
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:4126