Blender V5.3
rna_context.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 <cstdlib>
10
11#include "BKE_context.hh"
12
13#include "RNA_define.hh"
14#include "RNA_enum_types.hh"
15
16#include "rna_internal.hh" /* own include */
17
18namespace blender {
19
21 {CTX_MODE_EDIT_MESH, "EDIT_MESH", 0, "Mesh Edit", ""},
22 {CTX_MODE_EDIT_CURVE, "EDIT_CURVE", 0, "Curve Edit", ""},
23 {CTX_MODE_EDIT_CURVES, "EDIT_CURVES", 0, "Curves Edit", ""},
24 {CTX_MODE_EDIT_SURFACE, "EDIT_SURFACE", 0, "Surface Edit", ""},
25 {CTX_MODE_EDIT_TEXT, "EDIT_TEXT", 0, "Text Edit", ""},
26 /* PARSKEL reuse will give issues */
27 {CTX_MODE_EDIT_ARMATURE, "EDIT_ARMATURE", 0, "Armature Edit", ""},
28 {CTX_MODE_EDIT_METABALL, "EDIT_METABALL", 0, "Metaball Edit", ""},
29 {CTX_MODE_EDIT_LATTICE, "EDIT_LATTICE", 0, "Lattice Edit", ""},
30 {CTX_MODE_EDIT_GREASE_PENCIL, "EDIT_GREASE_PENCIL", 0, "Grease Pencil Edit", ""},
31 {CTX_MODE_EDIT_POINTCLOUD, "EDIT_POINTCLOUD", 0, "Point Cloud Edit", ""},
32 {CTX_MODE_POSE, "POSE", 0, "Pose", ""},
33 {CTX_MODE_SCULPT, "SCULPT", 0, "Sculpt", ""},
34 {CTX_MODE_PAINT_WEIGHT, "PAINT_WEIGHT", 0, "Weight Paint", ""},
35 {CTX_MODE_PAINT_VERTEX, "PAINT_VERTEX", 0, "Vertex Paint", ""},
36 {CTX_MODE_PAINT_TEXTURE, "PAINT_TEXTURE", 0, "Texture Paint", ""},
37 {CTX_MODE_PARTICLE, "PARTICLE", 0, "Particle", ""},
38 {CTX_MODE_OBJECT, "OBJECT", 0, "Object", ""},
39 {CTX_MODE_PAINT_GPENCIL_LEGACY, "PAINT_GPENCIL", 0, "Grease Pencil Paint", ""},
40 {CTX_MODE_EDIT_GPENCIL_LEGACY, "EDIT_GPENCIL", 0, "Grease Pencil Edit", ""},
41 {CTX_MODE_SCULPT_GPENCIL_LEGACY, "SCULPT_GPENCIL", 0, "Grease Pencil Sculpt", ""},
42 {CTX_MODE_WEIGHT_GPENCIL_LEGACY, "WEIGHT_GPENCIL", 0, "Grease Pencil Weight Paint", ""},
43 {CTX_MODE_VERTEX_GPENCIL_LEGACY, "VERTEX_GPENCIL", 0, "Grease Pencil Vertex Paint", ""},
44 {CTX_MODE_SCULPT_CURVES, "SCULPT_CURVES", 0, "Curves Sculpt", ""},
45 {CTX_MODE_PAINT_GREASE_PENCIL, "PAINT_GREASE_PENCIL", 0, "Grease Pencil Paint", ""},
46 {CTX_MODE_SCULPT_GREASE_PENCIL, "SCULPT_GREASE_PENCIL", 0, "Grease Pencil Sculpt", ""},
47 {CTX_MODE_WEIGHT_GREASE_PENCIL, "WEIGHT_GREASE_PENCIL", 0, "Grease Pencil Weight Paint", ""},
48 {CTX_MODE_VERTEX_GREASE_PENCIL, "VERTEX_GREASE_PENCIL", 0, "Grease Pencil Vertex Paint", ""},
49 {0, nullptr, 0, nullptr, nullptr},
50};
51
52}
53
54#ifdef RNA_RUNTIME
55
56# include "DNA_asset_types.h"
57# include "DNA_userdef_types.h"
58
59# ifdef WITH_PYTHON
60# include "BPY_extern.hh"
61# endif
62
63# include "RE_engine.h"
64
65namespace blender {
66
67static PointerRNA rna_Context_manager_get(PointerRNA *ptr)
68{
69 bContext *C = static_cast<bContext *>(ptr->data);
70 return RNA_id_pointer_create(reinterpret_cast<ID *>(CTX_wm_manager(C)));
71}
72
73static PointerRNA rna_Context_window_get(PointerRNA *ptr)
74{
75 bContext *C = static_cast<bContext *>(ptr->data);
77 reinterpret_cast<ID *>(CTX_wm_manager(C)), RNA_Window, CTX_wm_window(C));
78}
79
80static PointerRNA rna_Context_workspace_get(PointerRNA *ptr)
81{
82 bContext *C = static_cast<bContext *>(ptr->data);
83 return RNA_id_pointer_create(reinterpret_cast<ID *>(CTX_wm_workspace(C)));
84}
85
86static PointerRNA rna_Context_screen_get(PointerRNA *ptr)
87{
88 bContext *C = static_cast<bContext *>(ptr->data);
89 return RNA_id_pointer_create(reinterpret_cast<ID *>(CTX_wm_screen(C)));
90}
91
92static PointerRNA rna_Context_area_get(PointerRNA *ptr)
93{
94 bContext *C = static_cast<bContext *>(ptr->data);
96 reinterpret_cast<ID *>(CTX_wm_screen(C)), RNA_Area, CTX_wm_area(C));
97 return newptr;
98}
99
100static PointerRNA rna_Context_space_data_get(PointerRNA *ptr)
101{
102 bContext *C = static_cast<bContext *>(ptr->data);
104 reinterpret_cast<ID *>(CTX_wm_screen(C)), RNA_Space, CTX_wm_space_data(C));
105 return newptr;
106}
107
108static PointerRNA rna_Context_region_get(PointerRNA *ptr)
109{
110 bContext *C = static_cast<bContext *>(ptr->data);
112 reinterpret_cast<ID *>(CTX_wm_screen(C)), RNA_Region, CTX_wm_region(C));
113 return newptr;
114}
115
116static PointerRNA rna_Context_region_data_get(PointerRNA *ptr)
117{
118 bContext *C = static_cast<bContext *>(ptr->data);
119
120 /* only exists for one space still, no generic system yet */
121 if (CTX_wm_view3d(C)) {
123 reinterpret_cast<ID *>(CTX_wm_screen(C)), RNA_RegionView3D, CTX_wm_region_data(C));
124 return newptr;
125 }
126
127 return {};
128}
129
130static PointerRNA rna_Context_region_popup_get(PointerRNA *ptr)
131{
132 bContext *C = static_cast<bContext *>(ptr->data);
134 reinterpret_cast<ID *>(CTX_wm_screen(C)), RNA_Region, CTX_wm_region_popup(C));
135 return newptr;
136}
137
138static PointerRNA rna_Context_gizmo_group_get(PointerRNA *ptr)
139{
140 bContext *C = static_cast<bContext *>(ptr->data);
141 PointerRNA newptr = RNA_pointer_create_discrete(nullptr, RNA_GizmoGroup, CTX_wm_gizmo_group(C));
142 return newptr;
143}
144
145static PointerRNA rna_Context_asset_get(PointerRNA *ptr)
146{
147 bContext *C = static_cast<bContext *>(ptr->data);
148 return RNA_pointer_create_discrete(nullptr, RNA_AssetRepresentation, CTX_wm_asset(C));
149}
150
151static PointerRNA rna_Context_main_get(PointerRNA *ptr)
152{
153 bContext *C = static_cast<bContext *>(ptr->data);
155}
156
157static PointerRNA rna_Context_scene_get(PointerRNA *ptr)
158{
159 bContext *C = static_cast<bContext *>(ptr->data);
160 return RNA_id_pointer_create(reinterpret_cast<ID *>(CTX_data_scene(C)));
161}
162
163static PointerRNA rna_Context_view_layer_get(PointerRNA *ptr)
164{
165 bContext *C = static_cast<bContext *>(ptr->data);
167 *reinterpret_cast<ID *>(CTX_data_scene(C)), RNA_ViewLayer, CTX_data_view_layer(C));
168}
169
170static void rna_Context_engine_get(PointerRNA *ptr, char *value)
171{
172 bContext *C = static_cast<bContext *>(ptr->data);
174 strcpy(value, engine_type->idname);
175}
176
177static int rna_Context_engine_length(PointerRNA *ptr)
178{
179 bContext *C = static_cast<bContext *>(ptr->data);
181 return strlen(engine_type->idname);
182}
183
184static PointerRNA rna_Context_collection_get(PointerRNA *ptr)
185{
186 bContext *C = static_cast<bContext *>(ptr->data);
187 return RNA_id_pointer_create(reinterpret_cast<ID *>(CTX_data_collection(C)));
188}
189
190static PointerRNA rna_Context_layer_collection_get(PointerRNA *ptr)
191{
192 bContext *C = static_cast<bContext *>(ptr->data);
193 return RNA_pointer_create_discrete(reinterpret_cast<ID *>(CTX_data_scene(C)),
194 RNA_LayerCollection,
196}
197
198static PointerRNA rna_Context_tool_settings_get(PointerRNA *ptr)
199{
200 bContext *C = static_cast<bContext *>(ptr->data);
201 const bool is_sequencer = CTX_wm_space_seq(C) != nullptr;
202 if (is_sequencer) {
204 if (scene) {
205 ToolSettings *toolsettings = scene->toolsettings;
207 *reinterpret_cast<ID *>(scene), RNA_ToolSettings, toolsettings);
208 }
209 }
211 *reinterpret_cast<ID *>(CTX_data_scene(C)), RNA_ToolSettings, CTX_data_tool_settings(C));
212}
213
214static PointerRNA rna_Context_preferences_get(PointerRNA * /*ptr*/)
215{
216 PointerRNA newptr = RNA_pointer_create_discrete(nullptr, RNA_Preferences, &U);
217 return newptr;
218}
219
220static int rna_Context_mode_get(PointerRNA *ptr)
221{
222 bContext *C = static_cast<bContext *>(ptr->data);
223 return CTX_data_mode_enum(C);
224}
225
226static Depsgraph *rna_Context_evaluated_depsgraph_get(bContext *C)
227{
228 Depsgraph *depsgraph;
229
230# ifdef WITH_PYTHON
231 /* Allow drivers to be evaluated */
233# endif
234
235 depsgraph = CTX_data_ensure_evaluated_depsgraph(C, true);
236
237# ifdef WITH_PYTHON
239# endif
240
241 return depsgraph;
242}
243
244} // namespace blender
245
246#else
247
248namespace blender {
249
251{
252 StructRNA *srna;
253 PropertyRNA *prop;
254
255 FunctionRNA *func;
256 PropertyRNA *parm;
257
258 srna = RNA_def_struct(brna, "Context", nullptr);
259 RNA_def_struct_ui_text(srna, "Context", "Current windowmanager and data context");
260 RNA_def_struct_sdna(srna, "bContext");
261
262 /* WM */
263 prop = RNA_def_property(srna, "window_manager", PROP_POINTER, PROP_NONE);
265 RNA_def_property_struct_type(prop, "WindowManager");
266 RNA_def_property_pointer_funcs(prop, "rna_Context_manager_get", nullptr, nullptr, nullptr);
267
268 prop = RNA_def_property(srna, "window", PROP_POINTER, PROP_NONE);
270 RNA_def_property_struct_type(prop, "Window");
271 RNA_def_property_pointer_funcs(prop, "rna_Context_window_get", nullptr, nullptr, nullptr);
272
273 prop = RNA_def_property(srna, "workspace", PROP_POINTER, PROP_NONE);
275 RNA_def_property_struct_type(prop, "WorkSpace");
276 RNA_def_property_pointer_funcs(prop, "rna_Context_workspace_get", nullptr, nullptr, nullptr);
277
278 prop = RNA_def_property(srna, "screen", PROP_POINTER, PROP_NONE);
280 RNA_def_property_struct_type(prop, "Screen");
281 RNA_def_property_pointer_funcs(prop, "rna_Context_screen_get", nullptr, nullptr, nullptr);
282
283 prop = RNA_def_property(srna, "area", PROP_POINTER, PROP_NONE);
285 RNA_def_property_struct_type(prop, "Area");
286 RNA_def_property_pointer_funcs(prop, "rna_Context_area_get", nullptr, nullptr, nullptr);
287
288 prop = RNA_def_property(srna, "space_data", PROP_POINTER, PROP_NONE);
290 RNA_def_property_struct_type(prop, "Space");
291 RNA_def_property_pointer_funcs(prop, "rna_Context_space_data_get", nullptr, nullptr, nullptr);
293 "",
294 "The current space, may be None in background-mode, "
295 "when the cursor is outside the window or "
296 "when using menu-search");
297
298 prop = RNA_def_property(srna, "region", PROP_POINTER, PROP_NONE);
300 RNA_def_property_struct_type(prop, "Region");
301 RNA_def_property_pointer_funcs(prop, "rna_Context_region_get", nullptr, nullptr, nullptr);
302
303 prop = RNA_def_property(srna, "region_popup", PROP_POINTER, PROP_NONE);
305 RNA_def_property_struct_type(prop, "Region");
306 RNA_def_property_pointer_funcs(prop, "rna_Context_region_popup_get", nullptr, nullptr, nullptr);
308 prop, "Popup Region", "The temporary region for pop-ups (including menus and pop-overs)");
309
310 prop = RNA_def_property(srna, "region_data", PROP_POINTER, PROP_NONE);
312 RNA_def_property_struct_type(prop, "RegionView3D");
313 RNA_def_property_pointer_funcs(prop, "rna_Context_region_data_get", nullptr, nullptr, nullptr);
314
315 prop = RNA_def_property(srna, "gizmo_group", PROP_POINTER, PROP_NONE);
317 RNA_def_property_struct_type(prop, "GizmoGroup");
318 RNA_def_property_pointer_funcs(prop, "rna_Context_gizmo_group_get", nullptr, nullptr, nullptr);
319
320 prop = RNA_def_property(srna, "asset", PROP_POINTER, PROP_NONE);
322 RNA_def_property_struct_type(prop, "AssetRepresentation");
323 RNA_def_property_pointer_funcs(prop, "rna_Context_asset_get", nullptr, nullptr, nullptr);
324
325 /* Data */
326 prop = RNA_def_property(srna, "blend_data", PROP_POINTER, PROP_NONE);
328 RNA_def_property_struct_type(prop, "BlendData");
329 RNA_def_property_pointer_funcs(prop, "rna_Context_main_get", nullptr, nullptr, nullptr);
330
331 prop = RNA_def_property(srna, "scene", PROP_POINTER, PROP_NONE);
333 RNA_def_property_struct_type(prop, "Scene");
334 RNA_def_property_pointer_funcs(prop, "rna_Context_scene_get", nullptr, nullptr, nullptr);
335
336 prop = RNA_def_property(srna, "view_layer", PROP_POINTER, PROP_NONE);
338 RNA_def_property_struct_type(prop, "ViewLayer");
339 RNA_def_property_pointer_funcs(prop, "rna_Context_view_layer_get", nullptr, nullptr, nullptr);
340
341 prop = RNA_def_property(srna, "engine", PROP_STRING, PROP_NONE);
344 prop, "rna_Context_engine_get", "rna_Context_engine_length", nullptr);
345
346 prop = RNA_def_property(srna, "collection", PROP_POINTER, PROP_NONE);
348 RNA_def_property_struct_type(prop, "Collection");
349 RNA_def_property_pointer_funcs(prop, "rna_Context_collection_get", nullptr, nullptr, nullptr);
350
351 prop = RNA_def_property(srna, "layer_collection", PROP_POINTER, PROP_NONE);
353 RNA_def_property_struct_type(prop, "LayerCollection");
355 prop, "rna_Context_layer_collection_get", nullptr, nullptr, nullptr);
356
357 prop = RNA_def_property(srna, "tool_settings", PROP_POINTER, PROP_NONE);
359 RNA_def_property_struct_type(prop, "ToolSettings");
360 RNA_def_property_pointer_funcs(prop, "rna_Context_tool_settings_get", nullptr, nullptr, nullptr);
361
362 prop = RNA_def_property(srna, "preferences", PROP_POINTER, PROP_NONE);
364 RNA_def_property_struct_type(prop, "Preferences");
365 RNA_def_property_pointer_funcs(prop, "rna_Context_preferences_get", nullptr, nullptr, nullptr);
366
367 prop = RNA_def_property(srna, "mode", PROP_ENUM, PROP_NONE);
370 RNA_def_property_enum_funcs(prop, "rna_Context_mode_get", nullptr, nullptr);
371
372 func = RNA_def_function(srna, "evaluated_depsgraph_get", "rna_Context_evaluated_depsgraph_get");
374 func,
375 "Get the dependency graph for the current scene and view layer, to access to data-blocks "
376 "with animation and modifiers applied. If any data-blocks have been edited, the dependency "
377 "graph will be updated. This invalidates all references to evaluated data-blocks from the "
378 "dependency graph.");
379 parm = RNA_def_pointer(func, "depsgraph", "Depsgraph", "", "Evaluated dependency graph");
381 RNA_def_function_return(func, parm);
382}
383
384} // namespace blender
385
386#endif
#define BPy_BEGIN_ALLOW_THREADS
Definition BPY_extern.hh:52
#define BPy_END_ALLOW_THREADS
Definition BPY_extern.hh:56
#define C
Definition RandGen.cpp:29
BMesh Iterators.
UserDef U
Definition blender.cc:58
void RNA_def_struct_ui_text(StructRNA *srna, const char *name, const char *description)
ScrArea * CTX_wm_area(const bContext *C)
void RNA_def_property_ui_text(PropertyRNA *prop, const char *name, const char *description)
Scene * CTX_data_sequencer_scene(const bContext *C)
PointerRNA RNA_main_pointer_create(Main *main)
wmGizmoGroup * CTX_wm_gizmo_group(const bContext *C)
WorkSpace * CTX_wm_workspace(const bContext *C)
void RNA_def_property_pointer_funcs(PropertyRNA *prop, const char *get, const char *set, const char *type_fn, const char *poll)
PointerRNA RNA_id_pointer_create(ID *id)
class asset_system::AssetRepresentation * CTX_wm_asset(const bContext *C)
void RNA_def_function_ui_description(FunctionRNA *func, const char *description)
PropertyRNA * RNA_def_pointer(StructOrFunctionRNA *cont_, const char *identifier, const char *type, const char *ui_name, const char *ui_description)
wmWindow * CTX_wm_window(const bContext *C)
enum eContextObjectMode CTX_data_mode_enum(const bContext *C)
void RNA_def_property_struct_type(PropertyRNA *prop, const char *type)
const EnumPropertyItem rna_enum_context_mode_items[]
RenderEngineType * CTX_data_engine_type(const bContext *C)
void * CTX_wm_region_data(const bContext *C)
@ CTX_MODE_VERTEX_GREASE_PENCIL
@ CTX_MODE_PARTICLE
@ CTX_MODE_EDIT_TEXT
@ CTX_MODE_EDIT_SURFACE
@ CTX_MODE_PAINT_VERTEX
@ CTX_MODE_EDIT_GPENCIL_LEGACY
@ CTX_MODE_VERTEX_GPENCIL_LEGACY
@ CTX_MODE_WEIGHT_GPENCIL_LEGACY
@ CTX_MODE_SCULPT_CURVES
@ CTX_MODE_EDIT_ARMATURE
@ CTX_MODE_SCULPT
@ CTX_MODE_EDIT_CURVES
@ CTX_MODE_EDIT_LATTICE
@ CTX_MODE_PAINT_GREASE_PENCIL
@ CTX_MODE_PAINT_GPENCIL_LEGACY
@ CTX_MODE_SCULPT_GREASE_PENCIL
@ CTX_MODE_EDIT_POINTCLOUD
@ CTX_MODE_EDIT_MESH
@ CTX_MODE_PAINT_TEXTURE
@ CTX_MODE_SCULPT_GPENCIL_LEGACY
@ CTX_MODE_EDIT_METABALL
@ CTX_MODE_OBJECT
@ CTX_MODE_EDIT_GREASE_PENCIL
@ CTX_MODE_WEIGHT_GREASE_PENCIL
@ CTX_MODE_EDIT_CURVE
@ CTX_MODE_PAINT_WEIGHT
ViewLayer * CTX_data_view_layer(const bContext *C)
SpaceSeq * CTX_wm_space_seq(const bContext *C)
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier_c_str, int type, int subtype)
void RNA_def_struct_sdna(StructRNA *srna, const char *structname)
void RNA_def_property_enum_items(PropertyRNA *prop, const EnumPropertyItem *item)
@ PROP_POINTER
Definition RNA_types.hh:215
@ PROP_STRING
Definition RNA_types.hh:213
void RNA_def_parameter_flags(PropertyRNA *prop, PropertyFlag flag_property, ParameterFlag flag_parameter)
wmWindowManager * CTX_wm_manager(const bContext *C)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
void RNA_def_function_return(FunctionRNA *func, PropertyRNA *ret)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
ToolSettings * CTX_data_tool_settings(const bContext *C)
void RNA_def_property_enum_funcs(PropertyRNA *prop, const char *get, const char *set, const char *item)
FunctionRNA * RNA_def_function(StructRNA *srna, const char *identifier_c_str, const char *call)
void RNA_def_property_clear_flag(PropertyRNA *prop, PropertyFlag flag)
SpaceLink * CTX_wm_space_data(const bContext *C)
Main * CTX_data_main(const bContext *C)
StructRNA * RNA_def_struct(BlenderRNA *brna, const char *identifier, const char *from)
@ PROP_EDITABLE
Definition RNA_types.hh:376
@ PROP_NEVER_NULL
Definition RNA_types.hh:447
ARegion * CTX_wm_region_popup(const bContext *C)
PointerRNA RNA_pointer_create_id_subdata(ID &id, StructRNA *type, void *data)
Scene * CTX_data_scene(const bContext *C)
bScreen * CTX_wm_screen(const bContext *C)
void RNA_def_property_string_funcs(PropertyRNA *prop, const char *get, const char *length, const char *set)
void RNA_def_context(BlenderRNA *brna)
ARegion * CTX_wm_region(const bContext *C)
View3D * CTX_wm_view3d(const bContext *C)
LayerCollection * CTX_data_layer_collection(const bContext *C)
Collection * CTX_data_collection(const bContext *C)