1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 /** \file
18  * \ingroup RNA
19  */
20 
21 #include <stdlib.h>
22 
23 #include "DNA_ID.h"
24 #include "DNA_userdef_types.h"
25 
26 #include "BKE_context.h"
27 #include "BLI_utildefines.h"
28 
29 #include "RNA_access.h"
30 #include "RNA_define.h"
31 #include "RNA_enum_types.h"
32 
33 #include "rna_internal.h" /* own include */
34 
35 const EnumPropertyItem rna_enum_context_mode_items[] = {
36     {CTX_MODE_EDIT_MESH, "EDIT_MESH", 0, "Mesh Edit", ""},
37     {CTX_MODE_EDIT_CURVE, "EDIT_CURVE", 0, "Curve Edit", ""},
38     {CTX_MODE_EDIT_SURFACE, "EDIT_SURFACE", 0, "Surface Edit", ""},
39     {CTX_MODE_EDIT_TEXT, "EDIT_TEXT", 0, "Edit Edit", ""},
40     /* PARSKEL reuse will give issues */
41     {CTX_MODE_EDIT_ARMATURE, "EDIT_ARMATURE", 0, "Armature Edit", ""},
42     {CTX_MODE_EDIT_METABALL, "EDIT_METABALL", 0, "Metaball Edit", ""},
43     {CTX_MODE_EDIT_LATTICE, "EDIT_LATTICE", 0, "Lattice Edit", ""},
44     {CTX_MODE_POSE, "POSE", 0, "Pose", ""},
45     {CTX_MODE_SCULPT, "SCULPT", 0, "Sculpt", ""},
46     {CTX_MODE_PAINT_WEIGHT, "PAINT_WEIGHT", 0, "Weight Paint", ""},
47     {CTX_MODE_PAINT_VERTEX, "PAINT_VERTEX", 0, "Vertex Paint", ""},
48     {CTX_MODE_PAINT_TEXTURE, "PAINT_TEXTURE", 0, "Texture Paint", ""},
49     {CTX_MODE_PARTICLE, "PARTICLE", 0, "Particle", ""},
50     {CTX_MODE_OBJECT, "OBJECT", 0, "Object", ""},
51     {CTX_MODE_PAINT_GPENCIL, "PAINT_GPENCIL", 0, "Grease Pencil Paint", ""},
52     {CTX_MODE_EDIT_GPENCIL, "EDIT_GPENCIL", 0, "Grease Pencil Edit", ""},
53     {CTX_MODE_SCULPT_GPENCIL, "SCULPT_GPENCIL", 0, "Grease Pencil Sculpt", ""},
54     {CTX_MODE_WEIGHT_GPENCIL, "WEIGHT_GPENCIL", 0, "Grease Pencil Weight Paint", ""},
55     {CTX_MODE_VERTEX_GPENCIL, "VERTEX_GPENCIL", 0, "Grease Pencil Vertex Paint", ""},
56     {0, NULL, 0, NULL, NULL},
57 };
58 
59 #ifdef RNA_RUNTIME
60 
61 #  ifdef WITH_PYTHON
62 #    include "BPY_extern.h"
63 #  endif
64 
65 #  include "RE_engine.h"
66 
rna_Context_manager_get(PointerRNA * ptr)67 static PointerRNA rna_Context_manager_get(PointerRNA *ptr)
68 {
69   bContext *C = (bContext *)ptr->data;
70   return rna_pointer_inherit_refine(ptr, &RNA_WindowManager, CTX_wm_manager(C));
71 }
72 
rna_Context_window_get(PointerRNA * ptr)73 static PointerRNA rna_Context_window_get(PointerRNA *ptr)
74 {
75   bContext *C = (bContext *)ptr->data;
76   return rna_pointer_inherit_refine(ptr, &RNA_Window, CTX_wm_window(C));
77 }
78 
rna_Context_workspace_get(PointerRNA * ptr)79 static PointerRNA rna_Context_workspace_get(PointerRNA *ptr)
80 {
81   bContext *C = (bContext *)ptr->data;
82   return rna_pointer_inherit_refine(ptr, &RNA_WorkSpace, CTX_wm_workspace(C));
83 }
84 
rna_Context_screen_get(PointerRNA * ptr)85 static PointerRNA rna_Context_screen_get(PointerRNA *ptr)
86 {
87   bContext *C = (bContext *)ptr->data;
88   return rna_pointer_inherit_refine(ptr, &RNA_Screen, CTX_wm_screen(C));
89 }
90 
rna_Context_area_get(PointerRNA * ptr)91 static PointerRNA rna_Context_area_get(PointerRNA *ptr)
92 {
93   bContext *C = (bContext *)ptr->data;
94   PointerRNA newptr;
95   RNA_pointer_create((ID *)CTX_wm_screen(C), &RNA_Area, CTX_wm_area(C), &newptr);
96   return newptr;
97 }
98 
rna_Context_space_data_get(PointerRNA * ptr)99 static PointerRNA rna_Context_space_data_get(PointerRNA *ptr)
100 {
101   bContext *C = (bContext *)ptr->data;
102   PointerRNA newptr;
103   RNA_pointer_create((ID *)CTX_wm_screen(C), &RNA_Space, CTX_wm_space_data(C), &newptr);
104   return newptr;
105 }
106 
rna_Context_region_get(PointerRNA * ptr)107 static PointerRNA rna_Context_region_get(PointerRNA *ptr)
108 {
109   bContext *C = (bContext *)ptr->data;
110   PointerRNA newptr;
111   RNA_pointer_create((ID *)CTX_wm_screen(C), &RNA_Region, CTX_wm_region(C), &newptr);
112   return newptr;
113 }
114 
rna_Context_region_data_get(PointerRNA * ptr)115 static PointerRNA rna_Context_region_data_get(PointerRNA *ptr)
116 {
117   bContext *C = (bContext *)ptr->data;
118 
119   /* only exists for one space still, no generic system yet */
120   if (CTX_wm_view3d(C)) {
121     PointerRNA newptr;
122     RNA_pointer_create((ID *)CTX_wm_screen(C), &RNA_RegionView3D, CTX_wm_region_data(C), &newptr);
123     return newptr;
124   }
125 
126   return PointerRNA_NULL;
127 }
128 
rna_Context_gizmo_group_get(PointerRNA * ptr)129 static PointerRNA rna_Context_gizmo_group_get(PointerRNA *ptr)
130 {
131   bContext *C = (bContext *)ptr->data;
132   PointerRNA newptr;
133   RNA_pointer_create(NULL, &RNA_GizmoGroup, CTX_wm_gizmo_group(C), &newptr);
134   return newptr;
135 }
136 
rna_Context_main_get(PointerRNA * ptr)137 static PointerRNA rna_Context_main_get(PointerRNA *ptr)
138 {
139   bContext *C = (bContext *)ptr->data;
140   return rna_pointer_inherit_refine(ptr, &RNA_BlendData, CTX_data_main(C));
141 }
142 
rna_Context_scene_get(PointerRNA * ptr)143 static PointerRNA rna_Context_scene_get(PointerRNA *ptr)
144 {
145   bContext *C = (bContext *)ptr->data;
146   return rna_pointer_inherit_refine(ptr, &RNA_Scene, CTX_data_scene(C));
147 }
148 
rna_Context_view_layer_get(PointerRNA * ptr)149 static PointerRNA rna_Context_view_layer_get(PointerRNA *ptr)
150 {
151   bContext *C = (bContext *)ptr->data;
152   Scene *scene = CTX_data_scene(C);
153   PointerRNA scene_ptr;
154 
155   RNA_id_pointer_create(&scene->id, &scene_ptr);
156   return rna_pointer_inherit_refine(&scene_ptr, &RNA_ViewLayer, CTX_data_view_layer(C));
157 }
158 
rna_Context_engine_get(PointerRNA * ptr,char * value)159 static void rna_Context_engine_get(PointerRNA *ptr, char *value)
160 {
161   bContext *C = (bContext *)ptr->data;
162   RenderEngineType *engine_type = CTX_data_engine_type(C);
163   strcpy(value, engine_type->idname);
164 }
165 
rna_Context_engine_length(PointerRNA * ptr)166 static int rna_Context_engine_length(PointerRNA *ptr)
167 {
168   bContext *C = (bContext *)ptr->data;
169   RenderEngineType *engine_type = CTX_data_engine_type(C);
170   return strlen(engine_type->idname);
171 }
172 
rna_Context_collection_get(PointerRNA * ptr)173 static PointerRNA rna_Context_collection_get(PointerRNA *ptr)
174 {
175   bContext *C = (bContext *)ptr->data;
176   return rna_pointer_inherit_refine(ptr, &RNA_Collection, CTX_data_collection(C));
177 }
178 
rna_Context_layer_collection_get(PointerRNA * ptr)179 static PointerRNA rna_Context_layer_collection_get(PointerRNA *ptr)
180 {
181   bContext *C = (bContext *)ptr->data;
182   ptr->owner_id = &CTX_data_scene(C)->id;
183   return rna_pointer_inherit_refine(ptr, &RNA_LayerCollection, CTX_data_layer_collection(C));
184 }
185 
rna_Context_tool_settings_get(PointerRNA * ptr)186 static PointerRNA rna_Context_tool_settings_get(PointerRNA *ptr)
187 {
188   bContext *C = (bContext *)ptr->data;
189   ptr->owner_id = &CTX_data_scene(C)->id;
190   return rna_pointer_inherit_refine(ptr, &RNA_ToolSettings, CTX_data_tool_settings(C));
191 }
192 
rna_Context_preferences_get(PointerRNA * UNUSED (ptr))193 static PointerRNA rna_Context_preferences_get(PointerRNA *UNUSED(ptr))
194 {
195   PointerRNA newptr;
196   RNA_pointer_create(NULL, &RNA_Preferences, &U, &newptr);
197   return newptr;
198 }
199 
rna_Context_mode_get(PointerRNA * ptr)200 static int rna_Context_mode_get(PointerRNA *ptr)
201 {
202   bContext *C = (bContext *)ptr->data;
203   return CTX_data_mode_enum(C);
204 }
205 
rna_Context_evaluated_depsgraph_get(bContext * C)206 static struct Depsgraph *rna_Context_evaluated_depsgraph_get(bContext *C)
207 {
208   struct Depsgraph *depsgraph;
209 
210 #  ifdef WITH_PYTHON
211   /* Allow drivers to be evaluated */
212   BPy_BEGIN_ALLOW_THREADS;
213 #  endif
214 
215   depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
216 
217 #  ifdef WITH_PYTHON
218   BPy_END_ALLOW_THREADS;
219 #  endif
220 
221   return depsgraph;
222 }
223 
224 #else
225 
RNA_def_context(BlenderRNA * brna)226 void RNA_def_context(BlenderRNA *brna)
227 {
228   StructRNA *srna;
229   PropertyRNA *prop;
230 
231   FunctionRNA *func;
232   PropertyRNA *parm;
233 
234   srna = RNA_def_struct(brna, "Context", NULL);
235   RNA_def_struct_ui_text(srna, "Context", "Current windowmanager and data context");
236   RNA_def_struct_sdna(srna, "bContext");
237 
238   /* WM */
239   prop = RNA_def_property(srna, "window_manager", PROP_POINTER, PROP_NONE);
240   RNA_def_property_clear_flag(prop, PROP_EDITABLE);
241   RNA_def_property_struct_type(prop, "WindowManager");
242   RNA_def_property_pointer_funcs(prop, "rna_Context_manager_get", NULL, NULL, NULL);
243 
244   prop = RNA_def_property(srna, "window", PROP_POINTER, PROP_NONE);
245   RNA_def_property_clear_flag(prop, PROP_EDITABLE);
246   RNA_def_property_struct_type(prop, "Window");
247   RNA_def_property_pointer_funcs(prop, "rna_Context_window_get", NULL, NULL, NULL);
248 
249   prop = RNA_def_property(srna, "workspace", PROP_POINTER, PROP_NONE);
250   RNA_def_property_clear_flag(prop, PROP_EDITABLE);
251   RNA_def_property_struct_type(prop, "WorkSpace");
252   RNA_def_property_pointer_funcs(prop, "rna_Context_workspace_get", NULL, NULL, NULL);
253 
254   prop = RNA_def_property(srna, "screen", PROP_POINTER, PROP_NONE);
255   RNA_def_property_clear_flag(prop, PROP_EDITABLE);
256   RNA_def_property_struct_type(prop, "Screen");
257   RNA_def_property_pointer_funcs(prop, "rna_Context_screen_get", NULL, NULL, NULL);
258 
259   prop = RNA_def_property(srna, "area", PROP_POINTER, PROP_NONE);
260   RNA_def_property_clear_flag(prop, PROP_EDITABLE);
261   RNA_def_property_struct_type(prop, "Area");
262   RNA_def_property_pointer_funcs(prop, "rna_Context_area_get", NULL, NULL, NULL);
263 
264   prop = RNA_def_property(srna, "space_data", PROP_POINTER, PROP_NONE);
265   RNA_def_property_clear_flag(prop, PROP_EDITABLE);
266   RNA_def_property_struct_type(prop, "Space");
267   RNA_def_property_pointer_funcs(prop, "rna_Context_space_data_get", NULL, NULL, NULL);
268 
269   prop = RNA_def_property(srna, "region", PROP_POINTER, PROP_NONE);
270   RNA_def_property_clear_flag(prop, PROP_EDITABLE);
271   RNA_def_property_struct_type(prop, "Region");
272   RNA_def_property_pointer_funcs(prop, "rna_Context_region_get", NULL, NULL, NULL);
273 
274   prop = RNA_def_property(srna, "region_data", PROP_POINTER, PROP_NONE);
275   RNA_def_property_clear_flag(prop, PROP_EDITABLE);
276   RNA_def_property_struct_type(prop, "RegionView3D");
277   RNA_def_property_pointer_funcs(prop, "rna_Context_region_data_get", NULL, NULL, NULL);
278 
279   prop = RNA_def_property(srna, "gizmo_group", PROP_POINTER, PROP_NONE);
280   RNA_def_property_clear_flag(prop, PROP_EDITABLE);
281   RNA_def_property_struct_type(prop, "GizmoGroup");
282   RNA_def_property_pointer_funcs(prop, "rna_Context_gizmo_group_get", NULL, NULL, NULL);
283 
284   /* Data */
285   prop = RNA_def_property(srna, "blend_data", PROP_POINTER, PROP_NONE);
286   RNA_def_property_clear_flag(prop, PROP_EDITABLE);
287   RNA_def_property_struct_type(prop, "BlendData");
288   RNA_def_property_pointer_funcs(prop, "rna_Context_main_get", NULL, NULL, NULL);
289 
290   prop = RNA_def_property(srna, "scene", PROP_POINTER, PROP_NONE);
291   RNA_def_property_clear_flag(prop, PROP_EDITABLE);
292   RNA_def_property_struct_type(prop, "Scene");
293   RNA_def_property_pointer_funcs(prop, "rna_Context_scene_get", NULL, NULL, NULL);
294 
295   prop = RNA_def_property(srna, "view_layer", PROP_POINTER, PROP_NONE);
296   RNA_def_property_clear_flag(prop, PROP_EDITABLE);
297   RNA_def_property_struct_type(prop, "ViewLayer");
298   RNA_def_property_pointer_funcs(prop, "rna_Context_view_layer_get", NULL, NULL, NULL);
299 
300   prop = RNA_def_property(srna, "engine", PROP_STRING, PROP_NONE);
301   RNA_def_property_clear_flag(prop, PROP_EDITABLE);
302   RNA_def_property_string_funcs(prop, "rna_Context_engine_get", "rna_Context_engine_length", NULL);
303 
304   prop = RNA_def_property(srna, "collection", PROP_POINTER, PROP_NONE);
305   RNA_def_property_clear_flag(prop, PROP_EDITABLE);
306   RNA_def_property_struct_type(prop, "Collection");
307   RNA_def_property_pointer_funcs(prop, "rna_Context_collection_get", NULL, NULL, NULL);
308 
309   prop = RNA_def_property(srna, "layer_collection", PROP_POINTER, PROP_NONE);
310   RNA_def_property_clear_flag(prop, PROP_EDITABLE);
311   RNA_def_property_struct_type(prop, "LayerCollection");
312   RNA_def_property_pointer_funcs(prop, "rna_Context_layer_collection_get", NULL, NULL, NULL);
313 
314   prop = RNA_def_property(srna, "tool_settings", PROP_POINTER, PROP_NONE);
315   RNA_def_property_clear_flag(prop, PROP_EDITABLE);
316   RNA_def_property_struct_type(prop, "ToolSettings");
317   RNA_def_property_pointer_funcs(prop, "rna_Context_tool_settings_get", NULL, NULL, NULL);
318 
319   prop = RNA_def_property(srna, "preferences", PROP_POINTER, PROP_NONE);
320   RNA_def_property_clear_flag(prop, PROP_EDITABLE);
321   RNA_def_property_struct_type(prop, "Preferences");
322   RNA_def_property_pointer_funcs(prop, "rna_Context_preferences_get", NULL, NULL, NULL);
323 
324   prop = RNA_def_property(srna, "mode", PROP_ENUM, PROP_NONE);
325   RNA_def_property_enum_items(prop, rna_enum_context_mode_items);
326   RNA_def_property_clear_flag(prop, PROP_EDITABLE);
327   RNA_def_property_enum_funcs(prop, "rna_Context_mode_get", NULL, NULL);
328 
329   func = RNA_def_function(srna, "evaluated_depsgraph_get", "rna_Context_evaluated_depsgraph_get");
330   RNA_def_function_ui_description(
331       func,
332       "Get the dependency graph for the current scene and view layer, to access to data-blocks "
333       "with animation and modifiers applied. If any data-blocks have been edited, the dependency "
334       "graph will be updated. This invalidates all references to evaluated data-blocks from the "
335       "dependency graph.");
336   parm = RNA_def_pointer(func, "depsgraph", "Depsgraph", "", "Evaluated dependency graph");
337   RNA_def_function_return(func, parm);
338 }
339 
340 #endif
341