Pass EvaluationContext argument everywhere
[blender.git] / source / blender / makesrna / intern / rna_depsgraph.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Blender Foundation (2014).
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/makesrna/intern/rna_depsgraph.c
24  *  \ingroup RNA
25  */
26
27 #include <stdlib.h>
28
29 #include "BLI_utildefines.h"
30 #include "BLI_path_util.h"
31
32 #include "RNA_define.h"
33 #include "RNA_enum_types.h"
34
35 #include "rna_internal.h"
36
37 #include "DEG_depsgraph.h"
38
39 #include "DNA_object_types.h"
40
41 #ifdef RNA_RUNTIME
42
43 #include "BLI_iterator.h"
44 #include "BKE_report.h"
45 #include "DNA_object_types.h"
46
47 #include "DEG_depsgraph_build.h"
48 #include "DEG_depsgraph_debug.h"
49 #include "DEG_depsgraph_query.h"
50
51 #include "MEM_guardedalloc.h"
52
53 /* **************** Depsgraph **************** */
54
55 static PointerRNA rna_DepsgraphIter_object_get(PointerRNA *ptr)
56 {
57         BLI_Iterator *iterator = ptr->data;
58         return rna_pointer_inherit_refine(ptr, &RNA_Object, iterator->current);
59 }
60
61 static PointerRNA rna_DepsgraphIter_instance_object_get(PointerRNA *ptr)
62 {
63         BLI_Iterator *iterator = ptr->data;
64         DEGObjectsIteratorData *deg_iter = (DEGObjectsIteratorData *)iterator->data;
65         Object *instance_object = NULL;
66         if (deg_iter->dupli_object_current != NULL) {
67                 instance_object = deg_iter->dupli_object_current->ob;
68         }
69         return rna_pointer_inherit_refine(ptr, &RNA_Object, instance_object);
70 }
71
72 static PointerRNA rna_DepsgraphIter_parent_get(PointerRNA *ptr)
73 {
74         BLI_Iterator *iterator = ptr->data;
75         DEGObjectsIteratorData *deg_iter = (DEGObjectsIteratorData *)iterator->data;
76         Object *dupli_parent = NULL;
77         if (deg_iter->dupli_object_current != NULL) {
78                 dupli_parent = deg_iter->dupli_parent;
79         }
80         return rna_pointer_inherit_refine(ptr, &RNA_Object, dupli_parent);
81 }
82
83 static void rna_DepsgraphIter_persistent_id_get(PointerRNA *ptr, int *persistent_id)
84 {
85         BLI_Iterator *iterator = ptr->data;
86         DEGObjectsIteratorData *deg_iter = (DEGObjectsIteratorData *)iterator->data;
87         memcpy(persistent_id, deg_iter->dupli_object_current->persistent_id,
88                sizeof(deg_iter->dupli_object_current->persistent_id));
89 }
90
91 static void rna_DepsgraphIter_orco_get(PointerRNA *ptr, float *orco)
92 {
93         BLI_Iterator *iterator = ptr->data;
94         DEGObjectsIteratorData *deg_iter = (DEGObjectsIteratorData *)iterator->data;
95         memcpy(orco, deg_iter->dupli_object_current->orco,
96                sizeof(deg_iter->dupli_object_current->orco));
97 }
98
99 static unsigned int rna_DepsgraphIter_random_id_get(PointerRNA *ptr)
100 {
101         BLI_Iterator *iterator = ptr->data;
102         DEGObjectsIteratorData *deg_iter = (DEGObjectsIteratorData *)iterator->data;
103         return deg_iter->dupli_object_current->random_id;
104 }
105
106 static void rna_DepsgraphIter_uv_get(PointerRNA *ptr, float *uv)
107 {
108         BLI_Iterator *iterator = ptr->data;
109         DEGObjectsIteratorData *deg_iter = (DEGObjectsIteratorData *)iterator->data;
110         memcpy(uv, deg_iter->dupli_object_current->uv,
111                sizeof(deg_iter->dupli_object_current->uv));
112 }
113
114 static int rna_DepsgraphIter_is_instance_get(PointerRNA *ptr)
115 {
116         BLI_Iterator *iterator = ptr->data;
117         DEGObjectsIteratorData *deg_iter = (DEGObjectsIteratorData *)iterator->data;
118         return (deg_iter->dupli_object_current != NULL);
119 }
120
121 /* **************** Depsgraph **************** */
122
123 static void rna_Depsgraph_debug_graphviz(Depsgraph *graph, const char *filename)
124 {
125         FILE *f = fopen(filename, "w");
126         if (f == NULL)
127                 return;
128
129         DEG_debug_graphviz(graph, f, "Depsgraph", false);
130
131         fclose(f);
132 }
133
134 static void rna_Depsgraph_debug_rebuild(Depsgraph *UNUSED(graph), bContext *C)
135 {
136         Main *bmain = CTX_data_main(C);
137         EvaluationContext eval_ctx;
138         Scene *sce;
139
140         CTX_data_eval_ctx(C, &eval_ctx);
141
142         DEG_relations_tag_update(bmain);
143         for (sce = bmain->scene.first; sce; sce = sce->id.next) {
144                 DEG_scene_relations_rebuild(bmain, sce);
145                 DEG_graph_on_visible_update(bmain, sce);
146         }
147 }
148
149 static void rna_Depsgraph_debug_stats(Depsgraph *graph, ReportList *reports)
150 {
151         size_t outer, ops, rels;
152
153         DEG_stats_simple(graph, &outer, &ops, &rels);
154
155         // XXX: report doesn't seem to work
156         printf("Approx %lu Operations, %lu Relations, %lu Outer Nodes\n",
157                ops, rels, outer);
158
159         BKE_reportf(reports, RPT_WARNING, "Approx. %lu Operations, %lu Relations, %lu Outer Nodes",
160                     ops, rels, outer);
161 }
162
163 /* Iteration over objects, simple version */
164
165 static void rna_Depsgraph_objects_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
166 {
167         iter->internal.custom = MEM_callocN(sizeof(BLI_Iterator), __func__);
168         DEGObjectsIteratorData *data = MEM_callocN(sizeof(DEGObjectsIteratorData), __func__);
169
170         data->graph = (Depsgraph *)ptr->data;
171         data->flag = DEG_OBJECT_ITER_FLAG_SET;
172
173         DEG_objects_iterator_begin(iter->internal.custom, data);
174         iter->valid = ((BLI_Iterator *)iter->internal.custom)->valid;
175 }
176
177 static void rna_Depsgraph_objects_next(CollectionPropertyIterator *iter)
178 {
179         DEG_objects_iterator_next(iter->internal.custom);
180         iter->valid = ((BLI_Iterator *)iter->internal.custom)->valid;
181 }
182
183 static void rna_Depsgraph_objects_end(CollectionPropertyIterator *iter)
184 {
185         DEG_objects_iterator_end(iter->internal.custom);
186         MEM_freeN(((BLI_Iterator *)iter->internal.custom)->data);
187         MEM_freeN(iter->internal.custom);
188 }
189
190 static PointerRNA rna_Depsgraph_objects_get(CollectionPropertyIterator *iter)
191 {
192         Object *ob = ((BLI_Iterator *)iter->internal.custom)->current;
193         return rna_pointer_inherit_refine(&iter->parent, &RNA_Object, ob);
194 }
195
196 /* Iteration over objects, extended version
197  *
198  * Contains extra information about duplicator and persistent ID.
199  */
200
201 static void rna_Depsgraph_duplis_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
202 {
203         iter->internal.custom = MEM_callocN(sizeof(BLI_Iterator), __func__);
204         DEGObjectsIteratorData *data = MEM_callocN(sizeof(DEGObjectsIteratorData), __func__);
205
206         data->graph = (Depsgraph *)ptr->data;
207         data->flag = DEG_OBJECT_ITER_FLAG_ALL;
208
209         DEG_objects_iterator_begin(iter->internal.custom, data);
210         iter->valid = ((BLI_Iterator *)iter->internal.custom)->valid;
211 }
212
213 static void rna_Depsgraph_duplis_next(CollectionPropertyIterator *iter)
214 {
215         DEG_objects_iterator_next(iter->internal.custom);
216         iter->valid = ((BLI_Iterator *)iter->internal.custom)->valid;
217 }
218
219 static void rna_Depsgraph_duplis_end(CollectionPropertyIterator *iter)
220 {
221         DEG_objects_iterator_end(iter->internal.custom);
222         MEM_freeN(((BLI_Iterator *)iter->internal.custom)->data);
223         MEM_freeN(iter->internal.custom);
224 }
225
226 static PointerRNA rna_Depsgraph_duplis_get(CollectionPropertyIterator *iter)
227 {
228         BLI_Iterator *iterator = (BLI_Iterator *)iter->internal.custom;
229         return rna_pointer_inherit_refine(&iter->parent, &RNA_DepsgraphIter, iterator);
230 }
231
232 static ID *rna_Depsgraph_evaluated_id_get(Depsgraph *depsgraph, ID *id_orig)
233 {
234         return DEG_get_evaluated_id(depsgraph, id_orig);
235 }
236
237 #else
238
239 static void rna_def_depsgraph_iter(BlenderRNA *brna)
240 {
241         StructRNA *srna;
242         PropertyRNA *prop;
243
244         srna = RNA_def_struct(brna, "DepsgraphIter", NULL);
245         RNA_def_struct_ui_text(srna, "Dependency Graph Iterator",
246                                "Extended information about dependency graph object iterator");
247
248         prop = RNA_def_property(srna, "object", PROP_POINTER, PROP_NONE);
249         RNA_def_property_struct_type(prop, "Object");
250         RNA_def_property_ui_text(prop, "Object", "Object the iterator points to");
251         RNA_def_property_clear_flag(prop, PROP_ANIMATABLE | PROP_EDITABLE);
252         RNA_def_property_pointer_funcs(prop, "rna_DepsgraphIter_object_get", NULL, NULL, NULL);
253
254         prop = RNA_def_property(srna, "instance_object", PROP_POINTER, PROP_NONE);
255         RNA_def_property_struct_type(prop, "Object");
256         RNA_def_property_ui_text(prop, "Instance Object", "Object which is being instanced by this iterator");
257         RNA_def_property_clear_flag(prop, PROP_ANIMATABLE | PROP_EDITABLE);
258         RNA_def_property_pointer_funcs(prop, "rna_DepsgraphIter_instance_object_get", NULL, NULL, NULL);
259
260         prop = RNA_def_property(srna, "parent", PROP_POINTER, PROP_NONE);
261         RNA_def_property_struct_type(prop, "Object");
262         RNA_def_property_ui_text(prop, "Parent", "Parent of the duplication list");
263         RNA_def_property_clear_flag(prop, PROP_ANIMATABLE | PROP_EDITABLE);
264         RNA_def_property_pointer_funcs(prop, "rna_DepsgraphIter_parent_get", NULL, NULL, NULL);
265
266         prop = RNA_def_property(srna, "persistent_id", PROP_INT, PROP_NONE);
267         RNA_def_property_ui_text(prop, "Persistent ID",
268                                  "Persistent identifier for inter-frame matching of objects with motion blur");
269         RNA_def_property_array(prop, 2*MAX_DUPLI_RECUR);
270         RNA_def_property_clear_flag(prop, PROP_ANIMATABLE | PROP_EDITABLE);
271         RNA_def_property_int_funcs(prop, "rna_DepsgraphIter_persistent_id_get", NULL, NULL);
272
273         prop = RNA_def_property(srna, "orco", PROP_FLOAT, PROP_TRANSLATION);
274         RNA_def_property_clear_flag(prop, PROP_ANIMATABLE | PROP_EDITABLE);
275         /* Seems system is not smart enough to figure that getter function should return
276          * array for PROP_TRANSLATION.
277          */
278         RNA_def_property_array(prop, 3);
279         RNA_def_property_ui_text(prop, "Generated Coordinates", "Generated coordinates in parent object space");
280         RNA_def_property_float_funcs(prop, "rna_DepsgraphIter_orco_get", NULL, NULL);
281
282         prop = RNA_def_property(srna, "random_id", PROP_INT, PROP_UNSIGNED);
283         RNA_def_property_clear_flag(prop, PROP_ANIMATABLE | PROP_EDITABLE);
284         RNA_def_property_ui_text(prop, "Dupli random id", "Random id for this dupli object");
285         RNA_def_property_int_funcs(prop, "rna_DepsgraphIter_random_id_get", NULL, NULL);
286
287         prop = RNA_def_property(srna, "uv", PROP_FLOAT, PROP_NONE);
288         RNA_def_property_ui_text(prop, "UV Coordinates", "UV coordinates in parent object space");
289         RNA_def_property_array(prop, 2);
290         RNA_def_property_clear_flag(prop, PROP_ANIMATABLE | PROP_EDITABLE);
291         RNA_def_property_float_funcs(prop, "rna_DepsgraphIter_uv_get", NULL, NULL);
292
293         prop = RNA_def_property(srna, "is_instance", PROP_BOOLEAN, PROP_NONE);
294         RNA_def_property_clear_flag(prop, PROP_ANIMATABLE | PROP_EDITABLE);
295         RNA_def_property_ui_text(prop, "Is Instance", "Denotes whether the object is ocming from dupli-list");
296         RNA_def_property_boolean_funcs(prop, "rna_DepsgraphIter_is_instance_get", NULL);
297 }
298
299 static void rna_def_depsgraph(BlenderRNA *brna)
300 {
301         StructRNA *srna;
302         FunctionRNA *func;
303         PropertyRNA *parm;
304         PropertyRNA *prop;
305
306         srna = RNA_def_struct(brna, "Depsgraph", NULL);
307         RNA_def_struct_ui_text(srna, "Dependency Graph", "");
308
309         func = RNA_def_function(srna, "debug_graphviz", "rna_Depsgraph_debug_graphviz");
310         parm = RNA_def_string_file_path(func, "filename", NULL, FILE_MAX, "File Name",
311                                         "File in which to store graphviz debug output");
312         RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
313
314         func = RNA_def_function(srna, "debug_rebuild", "rna_Depsgraph_debug_rebuild");
315         RNA_def_function_flag(func, FUNC_USE_CONTEXT);
316
317         func = RNA_def_function(srna, "debug_stats", "rna_Depsgraph_debug_stats");
318         RNA_def_function_ui_description(func, "Report the number of elements in the Dependency Graph");
319         RNA_def_function_flag(func, FUNC_USE_REPORTS);
320
321         prop = RNA_def_property(srna, "objects", PROP_COLLECTION, PROP_NONE);
322         RNA_def_property_struct_type(prop, "Object");
323         RNA_def_property_collection_funcs(prop,
324                                           "rna_Depsgraph_objects_begin",
325                                           "rna_Depsgraph_objects_next",
326                                           "rna_Depsgraph_objects_end",
327                                           "rna_Depsgraph_objects_get",
328                                           NULL, NULL, NULL, NULL);
329
330         func = RNA_def_function(srna, "evaluated_id_get", "rna_Depsgraph_evaluated_id_get");
331         parm = RNA_def_pointer(func, "id", "ID", "", "Original ID to get evaluated complementary part for");
332         RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
333         parm = RNA_def_pointer(func, "evaluated_id", "ID", "", "Evaluated ID for the given original one");
334         RNA_def_function_return(func, parm);
335
336         /* TODO(sergey): Find a better name. */
337         prop = RNA_def_property(srna, "duplis", PROP_COLLECTION, PROP_NONE);
338         RNA_def_property_struct_type(prop, "DepsgraphIter");
339         RNA_def_property_collection_funcs(prop,
340                                           "rna_Depsgraph_duplis_begin",
341                                           "rna_Depsgraph_duplis_next",
342                                           "rna_Depsgraph_duplis_end",
343                                           "rna_Depsgraph_duplis_get",
344                                           NULL, NULL, NULL, NULL);
345 }
346
347 void RNA_def_depsgraph(BlenderRNA *brna)
348 {
349         rna_def_depsgraph_iter(brna);
350         rna_def_depsgraph(brna);
351 }
352
353 #endif