Blender  V3.3
rna_context.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include <stdlib.h>
8 
9 #include "DNA_ID.h"
10 #include "DNA_userdef_types.h"
11 
12 #include "BKE_context.h"
13 #include "BLI_utildefines.h"
14 
15 #include "RNA_access.h"
16 #include "RNA_define.h"
17 #include "RNA_enum_types.h"
18 
19 #include "rna_internal.h" /* own include */
20 
22  {CTX_MODE_EDIT_MESH, "EDIT_MESH", 0, "Mesh Edit", ""},
23  {CTX_MODE_EDIT_CURVE, "EDIT_CURVE", 0, "Curve Edit", ""},
24  {CTX_MODE_EDIT_CURVES, "EDIT_CURVES", 0, "Curves Edit", ""},
25  {CTX_MODE_EDIT_SURFACE, "EDIT_SURFACE", 0, "Surface Edit", ""},
26  {CTX_MODE_EDIT_TEXT, "EDIT_TEXT", 0, "Text Edit", ""},
27  /* PARSKEL reuse will give issues */
28  {CTX_MODE_EDIT_ARMATURE, "EDIT_ARMATURE", 0, "Armature Edit", ""},
29  {CTX_MODE_EDIT_METABALL, "EDIT_METABALL", 0, "Metaball Edit", ""},
30  {CTX_MODE_EDIT_LATTICE, "EDIT_LATTICE", 0, "Lattice Edit", ""},
31  {CTX_MODE_POSE, "POSE", 0, "Pose", ""},
32  {CTX_MODE_SCULPT, "SCULPT", 0, "Sculpt", ""},
33  {CTX_MODE_PAINT_WEIGHT, "PAINT_WEIGHT", 0, "Weight Paint", ""},
34  {CTX_MODE_PAINT_VERTEX, "PAINT_VERTEX", 0, "Vertex Paint", ""},
35  {CTX_MODE_PAINT_TEXTURE, "PAINT_TEXTURE", 0, "Texture Paint", ""},
36  {CTX_MODE_PARTICLE, "PARTICLE", 0, "Particle", ""},
37  {CTX_MODE_OBJECT, "OBJECT", 0, "Object", ""},
38  {CTX_MODE_PAINT_GPENCIL, "PAINT_GPENCIL", 0, "Grease Pencil Paint", ""},
39  {CTX_MODE_EDIT_GPENCIL, "EDIT_GPENCIL", 0, "Grease Pencil Edit", ""},
40  {CTX_MODE_SCULPT_GPENCIL, "SCULPT_GPENCIL", 0, "Grease Pencil Sculpt", ""},
41  {CTX_MODE_WEIGHT_GPENCIL, "WEIGHT_GPENCIL", 0, "Grease Pencil Weight Paint", ""},
42  {CTX_MODE_VERTEX_GPENCIL, "VERTEX_GPENCIL", 0, "Grease Pencil Vertex Paint", ""},
43  {CTX_MODE_SCULPT_CURVES, "SCULPT_CURVES", 0, "Curves Sculpt", ""},
44  {0, NULL, 0, NULL, NULL},
45 };
46 
47 #ifdef RNA_RUNTIME
48 
49 # include "DNA_asset_types.h"
50 
51 # ifdef WITH_PYTHON
52 # include "BPY_extern.h"
53 # endif
54 
55 # include "RE_engine.h"
56 
57 static PointerRNA rna_Context_manager_get(PointerRNA *ptr)
58 {
59  bContext *C = (bContext *)ptr->data;
60  return rna_pointer_inherit_refine(ptr, &RNA_WindowManager, CTX_wm_manager(C));
61 }
62 
63 static PointerRNA rna_Context_window_get(PointerRNA *ptr)
64 {
65  bContext *C = (bContext *)ptr->data;
66  return rna_pointer_inherit_refine(ptr, &RNA_Window, CTX_wm_window(C));
67 }
68 
69 static PointerRNA rna_Context_workspace_get(PointerRNA *ptr)
70 {
71  bContext *C = (bContext *)ptr->data;
72  return rna_pointer_inherit_refine(ptr, &RNA_WorkSpace, CTX_wm_workspace(C));
73 }
74 
75 static PointerRNA rna_Context_screen_get(PointerRNA *ptr)
76 {
77  bContext *C = (bContext *)ptr->data;
78  return rna_pointer_inherit_refine(ptr, &RNA_Screen, CTX_wm_screen(C));
79 }
80 
81 static PointerRNA rna_Context_area_get(PointerRNA *ptr)
82 {
83  bContext *C = (bContext *)ptr->data;
84  PointerRNA newptr;
85  RNA_pointer_create((ID *)CTX_wm_screen(C), &RNA_Area, CTX_wm_area(C), &newptr);
86  return newptr;
87 }
88 
89 static PointerRNA rna_Context_space_data_get(PointerRNA *ptr)
90 {
91  bContext *C = (bContext *)ptr->data;
92  PointerRNA newptr;
93  RNA_pointer_create((ID *)CTX_wm_screen(C), &RNA_Space, CTX_wm_space_data(C), &newptr);
94  return newptr;
95 }
96 
97 static PointerRNA rna_Context_region_get(PointerRNA *ptr)
98 {
99  bContext *C = (bContext *)ptr->data;
100  PointerRNA newptr;
101  RNA_pointer_create((ID *)CTX_wm_screen(C), &RNA_Region, CTX_wm_region(C), &newptr);
102  return newptr;
103 }
104 
105 static PointerRNA rna_Context_region_data_get(PointerRNA *ptr)
106 {
107  bContext *C = (bContext *)ptr->data;
108 
109  /* only exists for one space still, no generic system yet */
110  if (CTX_wm_view3d(C)) {
111  PointerRNA newptr;
112  RNA_pointer_create((ID *)CTX_wm_screen(C), &RNA_RegionView3D, CTX_wm_region_data(C), &newptr);
113  return newptr;
114  }
115 
116  return PointerRNA_NULL;
117 }
118 
119 static PointerRNA rna_Context_gizmo_group_get(PointerRNA *ptr)
120 {
121  bContext *C = (bContext *)ptr->data;
122  PointerRNA newptr;
123  RNA_pointer_create(NULL, &RNA_GizmoGroup, CTX_wm_gizmo_group(C), &newptr);
124  return newptr;
125 }
126 
127 static PointerRNA rna_Context_asset_file_handle_get(PointerRNA *ptr)
128 {
129  bContext *C = (bContext *)ptr->data;
130  bool is_handle_valid;
131  AssetHandle asset_handle = CTX_wm_asset_handle(C, &is_handle_valid);
132  if (!is_handle_valid) {
133  return PointerRNA_NULL;
134  }
135 
136  PointerRNA newptr;
137  /* Have to cast away const, but the file entry API doesn't allow modifications anyway. */
139  NULL, &RNA_FileSelectEntry, (struct FileDirEntry *)asset_handle.file_data, &newptr);
140  return newptr;
141 }
142 
143 static PointerRNA rna_Context_main_get(PointerRNA *ptr)
144 {
145  bContext *C = (bContext *)ptr->data;
146  return rna_pointer_inherit_refine(ptr, &RNA_BlendData, CTX_data_main(C));
147 }
148 
149 static PointerRNA rna_Context_scene_get(PointerRNA *ptr)
150 {
151  bContext *C = (bContext *)ptr->data;
152  return rna_pointer_inherit_refine(ptr, &RNA_Scene, CTX_data_scene(C));
153 }
154 
155 static PointerRNA rna_Context_view_layer_get(PointerRNA *ptr)
156 {
157  bContext *C = (bContext *)ptr->data;
159  PointerRNA scene_ptr;
160 
161  RNA_id_pointer_create(&scene->id, &scene_ptr);
162  return rna_pointer_inherit_refine(&scene_ptr, &RNA_ViewLayer, CTX_data_view_layer(C));
163 }
164 
165 static void rna_Context_engine_get(PointerRNA *ptr, char *value)
166 {
167  bContext *C = (bContext *)ptr->data;
168  RenderEngineType *engine_type = CTX_data_engine_type(C);
169  strcpy(value, engine_type->idname);
170 }
171 
172 static int rna_Context_engine_length(PointerRNA *ptr)
173 {
174  bContext *C = (bContext *)ptr->data;
175  RenderEngineType *engine_type = CTX_data_engine_type(C);
176  return strlen(engine_type->idname);
177 }
178 
179 static PointerRNA rna_Context_collection_get(PointerRNA *ptr)
180 {
181  bContext *C = (bContext *)ptr->data;
182  return rna_pointer_inherit_refine(ptr, &RNA_Collection, CTX_data_collection(C));
183 }
184 
185 static PointerRNA rna_Context_layer_collection_get(PointerRNA *ptr)
186 {
187  bContext *C = (bContext *)ptr->data;
189  return rna_pointer_inherit_refine(ptr, &RNA_LayerCollection, CTX_data_layer_collection(C));
190 }
191 
192 static PointerRNA rna_Context_tool_settings_get(PointerRNA *ptr)
193 {
194  bContext *C = (bContext *)ptr->data;
196  return rna_pointer_inherit_refine(ptr, &RNA_ToolSettings, CTX_data_tool_settings(C));
197 }
198 
199 static PointerRNA rna_Context_preferences_get(PointerRNA *UNUSED(ptr))
200 {
201  PointerRNA newptr;
202  RNA_pointer_create(NULL, &RNA_Preferences, &U, &newptr);
203  return newptr;
204 }
205 
206 static int rna_Context_mode_get(PointerRNA *ptr)
207 {
208  bContext *C = (bContext *)ptr->data;
209  return CTX_data_mode_enum(C);
210 }
211 
212 static struct Depsgraph *rna_Context_evaluated_depsgraph_get(bContext *C)
213 {
214  struct Depsgraph *depsgraph;
215 
216 # ifdef WITH_PYTHON
217  /* Allow drivers to be evaluated */
219 # endif
220 
222 
223 # ifdef WITH_PYTHON
225 # endif
226 
227  return depsgraph;
228 }
229 
230 #else
231 
233 {
234  StructRNA *srna;
235  PropertyRNA *prop;
236 
237  FunctionRNA *func;
238  PropertyRNA *parm;
239 
240  srna = RNA_def_struct(brna, "Context", NULL);
241  RNA_def_struct_ui_text(srna, "Context", "Current windowmanager and data context");
242  RNA_def_struct_sdna(srna, "bContext");
243 
244  /* WM */
245  prop = RNA_def_property(srna, "window_manager", PROP_POINTER, PROP_NONE);
247  RNA_def_property_struct_type(prop, "WindowManager");
248  RNA_def_property_pointer_funcs(prop, "rna_Context_manager_get", NULL, NULL, NULL);
249 
250  prop = RNA_def_property(srna, "window", PROP_POINTER, PROP_NONE);
252  RNA_def_property_struct_type(prop, "Window");
253  RNA_def_property_pointer_funcs(prop, "rna_Context_window_get", NULL, NULL, NULL);
254 
255  prop = RNA_def_property(srna, "workspace", PROP_POINTER, PROP_NONE);
257  RNA_def_property_struct_type(prop, "WorkSpace");
258  RNA_def_property_pointer_funcs(prop, "rna_Context_workspace_get", NULL, NULL, NULL);
259 
260  prop = RNA_def_property(srna, "screen", PROP_POINTER, PROP_NONE);
262  RNA_def_property_struct_type(prop, "Screen");
263  RNA_def_property_pointer_funcs(prop, "rna_Context_screen_get", NULL, NULL, NULL);
264 
265  prop = RNA_def_property(srna, "area", PROP_POINTER, PROP_NONE);
267  RNA_def_property_struct_type(prop, "Area");
268  RNA_def_property_pointer_funcs(prop, "rna_Context_area_get", NULL, NULL, NULL);
269 
270  prop = RNA_def_property(srna, "space_data", PROP_POINTER, PROP_NONE);
272  RNA_def_property_struct_type(prop, "Space");
273  RNA_def_property_pointer_funcs(prop, "rna_Context_space_data_get", NULL, NULL, NULL);
274 
275  prop = RNA_def_property(srna, "region", PROP_POINTER, PROP_NONE);
277  RNA_def_property_struct_type(prop, "Region");
278  RNA_def_property_pointer_funcs(prop, "rna_Context_region_get", NULL, NULL, NULL);
279 
280  prop = RNA_def_property(srna, "region_data", PROP_POINTER, PROP_NONE);
282  RNA_def_property_struct_type(prop, "RegionView3D");
283  RNA_def_property_pointer_funcs(prop, "rna_Context_region_data_get", NULL, NULL, NULL);
284 
285  prop = RNA_def_property(srna, "gizmo_group", PROP_POINTER, PROP_NONE);
287  RNA_def_property_struct_type(prop, "GizmoGroup");
288  RNA_def_property_pointer_funcs(prop, "rna_Context_gizmo_group_get", NULL, NULL, NULL);
289 
290  /* TODO can't expose AssetHandle, since there is no permanent storage to it (so we can't
291  * return a pointer). Instead provide the FileDirEntry pointer it wraps. */
292  prop = RNA_def_property(srna, "asset_file_handle", PROP_POINTER, PROP_NONE);
294  RNA_def_property_struct_type(prop, "FileSelectEntry");
295  RNA_def_property_pointer_funcs(prop, "rna_Context_asset_file_handle_get", NULL, NULL, NULL);
297  "",
298  "The file of an active asset. Avoid using this, it will be replaced by "
299  "a proper AssetHandle design");
300 
301  /* Data */
302  prop = RNA_def_property(srna, "blend_data", PROP_POINTER, PROP_NONE);
304  RNA_def_property_struct_type(prop, "BlendData");
305  RNA_def_property_pointer_funcs(prop, "rna_Context_main_get", NULL, NULL, NULL);
306 
307  prop = RNA_def_property(srna, "scene", PROP_POINTER, PROP_NONE);
309  RNA_def_property_struct_type(prop, "Scene");
310  RNA_def_property_pointer_funcs(prop, "rna_Context_scene_get", NULL, NULL, NULL);
311 
312  prop = RNA_def_property(srna, "view_layer", PROP_POINTER, PROP_NONE);
314  RNA_def_property_struct_type(prop, "ViewLayer");
315  RNA_def_property_pointer_funcs(prop, "rna_Context_view_layer_get", NULL, NULL, NULL);
316 
317  prop = RNA_def_property(srna, "engine", PROP_STRING, PROP_NONE);
319  RNA_def_property_string_funcs(prop, "rna_Context_engine_get", "rna_Context_engine_length", NULL);
320 
321  prop = RNA_def_property(srna, "collection", PROP_POINTER, PROP_NONE);
323  RNA_def_property_struct_type(prop, "Collection");
324  RNA_def_property_pointer_funcs(prop, "rna_Context_collection_get", NULL, NULL, NULL);
325 
326  prop = RNA_def_property(srna, "layer_collection", PROP_POINTER, PROP_NONE);
328  RNA_def_property_struct_type(prop, "LayerCollection");
329  RNA_def_property_pointer_funcs(prop, "rna_Context_layer_collection_get", NULL, NULL, NULL);
330 
331  prop = RNA_def_property(srna, "tool_settings", PROP_POINTER, PROP_NONE);
333  RNA_def_property_struct_type(prop, "ToolSettings");
334  RNA_def_property_pointer_funcs(prop, "rna_Context_tool_settings_get", NULL, NULL, NULL);
335 
336  prop = RNA_def_property(srna, "preferences", PROP_POINTER, PROP_NONE);
338  RNA_def_property_struct_type(prop, "Preferences");
339  RNA_def_property_pointer_funcs(prop, "rna_Context_preferences_get", NULL, NULL, NULL);
340 
341  prop = RNA_def_property(srna, "mode", PROP_ENUM, PROP_NONE);
344  RNA_def_property_enum_funcs(prop, "rna_Context_mode_get", NULL, NULL);
345 
346  func = RNA_def_function(srna, "evaluated_depsgraph_get", "rna_Context_evaluated_depsgraph_get");
348  func,
349  "Get the dependency graph for the current scene and view layer, to access to data-blocks "
350  "with animation and modifiers applied. If any data-blocks have been edited, the dependency "
351  "graph will be updated. This invalidates all references to evaluated data-blocks from the "
352  "dependency graph.");
353  parm = RNA_def_pointer(func, "depsgraph", "Depsgraph", "", "Evaluated dependency graph");
354  RNA_def_function_return(func, parm);
355 }
356 
357 #endif
struct WorkSpace * CTX_wm_workspace(const bContext *C)
Definition: context.c:728
struct ScrArea * CTX_wm_area(const bContext *C)
Definition: context.c:738
struct Scene * CTX_data_scene(const bContext *C)
Definition: context.c:1090
struct RenderEngineType * CTX_data_engine_type(const bContext *C)
Definition: context.c:1120
struct LayerCollection * CTX_data_layer_collection(const bContext *C)
Definition: context.c:1126
struct wmWindowManager * CTX_wm_manager(const bContext *C)
Definition: context.c:713
@ CTX_MODE_EDIT_GPENCIL
Definition: BKE_context.h:120
@ CTX_MODE_EDIT_CURVE
Definition: BKE_context.h:105
@ CTX_MODE_PAINT_TEXTURE
Definition: BKE_context.h:116
@ CTX_MODE_EDIT_SURFACE
Definition: BKE_context.h:106
@ CTX_MODE_WEIGHT_GPENCIL
Definition: BKE_context.h:122
@ CTX_MODE_PARTICLE
Definition: BKE_context.h:117
@ CTX_MODE_SCULPT
Definition: BKE_context.h:113
@ CTX_MODE_VERTEX_GPENCIL
Definition: BKE_context.h:123
@ CTX_MODE_OBJECT
Definition: BKE_context.h:118
@ CTX_MODE_EDIT_MESH
Definition: BKE_context.h:104
@ CTX_MODE_SCULPT_CURVES
Definition: BKE_context.h:124
@ CTX_MODE_EDIT_TEXT
Definition: BKE_context.h:107
@ CTX_MODE_EDIT_CURVES
Definition: BKE_context.h:111
@ CTX_MODE_EDIT_ARMATURE
Definition: BKE_context.h:108
@ CTX_MODE_SCULPT_GPENCIL
Definition: BKE_context.h:121
@ CTX_MODE_EDIT_LATTICE
Definition: BKE_context.h:110
@ CTX_MODE_PAINT_GPENCIL
Definition: BKE_context.h:119
@ CTX_MODE_PAINT_VERTEX
Definition: BKE_context.h:115
@ CTX_MODE_EDIT_METABALL
Definition: BKE_context.h:109
@ CTX_MODE_PAINT_WEIGHT
Definition: BKE_context.h:114
@ CTX_MODE_POSE
Definition: BKE_context.h:112
struct ViewLayer * CTX_data_view_layer(const bContext *C)
Definition: context.c:1100
struct Collection * CTX_data_collection(const bContext *C)
Definition: context.c:1141
struct wmGizmoGroup * CTX_wm_gizmo_group(const bContext *C)
Definition: context.c:765
struct Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C)
Definition: context.c:1528
struct View3D * CTX_wm_view3d(const bContext *C)
Definition: context.c:784
struct bScreen * CTX_wm_screen(const bContext *C)
Definition: context.c:733
void * CTX_wm_region_data(const bContext *C)
Definition: context.c:754
struct AssetHandle CTX_wm_asset_handle(const bContext *C, bool *r_is_valid)
Definition: context.c:1480
struct SpaceLink * CTX_wm_space_data(const bContext *C)
Definition: context.c:743
struct ARegion * CTX_wm_region(const bContext *C)
Definition: context.c:749
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1074
struct ToolSettings * CTX_data_tool_settings(const bContext *C)
Definition: context.c:1282
struct wmWindow * CTX_wm_window(const bContext *C)
Definition: context.c:723
enum eContextObjectMode CTX_data_mode_enum(const bContext *C)
Definition: context.c:1228
#define UNUSED(x)
#define BPy_BEGIN_ALLOW_THREADS
Definition: BPY_extern.h:54
#define BPy_END_ALLOW_THREADS
Definition: BPY_extern.h:58
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:35
ID and Library types, which are fundamental for sdna.
@ PROP_ENUM
Definition: RNA_types.h:63
@ PROP_STRING
Definition: RNA_types.h:62
@ PROP_POINTER
Definition: RNA_types.h:64
@ PROP_EDITABLE
Definition: RNA_types.h:189
@ PROP_NONE
Definition: RNA_types.h:126
#define C
Definition: RandGen.cpp:25
unsigned int U
Definition: btGjkEpa3.h:78
Scene scene
const Depsgraph * depsgraph
void RNA_pointer_create(ID *id, StructRNA *type, void *data, PointerRNA *r_ptr)
Definition: rna_access.c:136
void RNA_id_pointer_create(ID *id, PointerRNA *r_ptr)
Definition: rna_access.c:112
const PointerRNA PointerRNA_NULL
Definition: rna_access.c:61
PointerRNA rna_pointer_inherit_refine(PointerRNA *ptr, StructRNA *type, void *data)
Definition: rna_access.c:186
const EnumPropertyItem rna_enum_context_mode_items[]
Definition: rna_context.c:21
void RNA_def_context(BlenderRNA *brna)
Definition: rna_context.c:232
PropertyRNA * RNA_def_pointer(StructOrFunctionRNA *cont_, const char *identifier, const char *type, const char *ui_name, const char *ui_description)
Definition: rna_define.c:4170
void RNA_def_property_string_funcs(PropertyRNA *prop, const char *get, const char *length, const char *set)
Definition: rna_define.c:3285
void RNA_def_function_return(FunctionRNA *func, PropertyRNA *ret)
Definition: rna_define.c:4312
void RNA_def_property_ui_text(PropertyRNA *prop, const char *name, const char *description)
Definition: rna_define.c:1645
FunctionRNA * RNA_def_function(StructRNA *srna, const char *identifier, const char *call)
Definition: rna_define.c:4273
void RNA_def_struct_ui_text(StructRNA *srna, const char *name, const char *description)
Definition: rna_define.c:1237
void RNA_def_property_enum_items(PropertyRNA *prop, const EnumPropertyItem *item)
Definition: rna_define.c:1872
void RNA_def_struct_sdna(StructRNA *srna, const char *structname)
Definition: rna_define.c:1048
void RNA_def_property_struct_type(PropertyRNA *prop, const char *type)
Definition: rna_define.c:1772
void RNA_def_function_ui_description(FunctionRNA *func, const char *description)
Definition: rna_define.c:4347
void RNA_def_property_enum_funcs(PropertyRNA *prop, const char *get, const char *set, const char *item)
Definition: rna_define.c:3224
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier, int type, int subtype)
Definition: rna_define.c:1257
void RNA_def_property_clear_flag(PropertyRNA *prop, PropertyFlag flag)
Definition: rna_define.c:1495
void RNA_def_property_pointer_funcs(PropertyRNA *prop, const char *get, const char *set, const char *type_fn, const char *poll)
Definition: rna_define.c:3385
StructRNA * RNA_def_struct(BlenderRNA *brna, const char *identifier, const char *from)
Definition: rna_define.c:1028
Definition: DNA_ID.h:368
void * data
Definition: RNA_types.h:38
struct ID * owner_id
Definition: RNA_types.h:36
PointerRNA * ptr
Definition: wm_files.c:3480