Merge branch 'master' of git@git.blender.org:blender.git into blender2.8
[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), Main *bmain)
135 {
136         Scene *sce;
137         DEG_relations_tag_update(bmain);
138         for (sce = bmain->scene.first; sce; sce = sce->id.next) {
139                 DEG_scene_relations_rebuild(bmain, sce);
140                 DEG_graph_on_visible_update(bmain, sce);
141         }
142 }
143
144 static void rna_Depsgraph_debug_stats(Depsgraph *graph, ReportList *reports)
145 {
146         size_t outer, ops, rels;
147
148         DEG_stats_simple(graph, &outer, &ops, &rels);
149
150         // XXX: report doesn't seem to work
151         printf("Approx %lu Operations, %lu Relations, %lu Outer Nodes\n",
152                ops, rels, outer);
153
154         BKE_reportf(reports, RPT_WARNING, "Approx. %lu Operations, %lu Relations, %lu Outer Nodes",
155                     ops, rels, outer);
156 }
157
158 /* Iteration over objects, simple version */
159
160 static void rna_Depsgraph_objects_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
161 {
162         iter->internal.custom = MEM_callocN(sizeof(BLI_Iterator), __func__);
163         DEGObjectsIteratorData *data = MEM_callocN(sizeof(DEGObjectsIteratorData), __func__);
164
165         data->graph = (Depsgraph *)ptr->data;
166         data->flag = DEG_OBJECT_ITER_FLAG_SET;
167
168         DEG_objects_iterator_begin(iter->internal.custom, data);
169         iter->valid = ((BLI_Iterator *)iter->internal.custom)->valid;
170 }
171
172 static void rna_Depsgraph_objects_next(CollectionPropertyIterator *iter)
173 {
174         DEG_objects_iterator_next(iter->internal.custom);
175         iter->valid = ((BLI_Iterator *)iter->internal.custom)->valid;
176 }
177
178 static void rna_Depsgraph_objects_end(CollectionPropertyIterator *iter)
179 {
180         DEG_objects_iterator_end(iter->internal.custom);
181         MEM_freeN(((BLI_Iterator *)iter->internal.custom)->data);
182         MEM_freeN(iter->internal.custom);
183 }
184
185 static PointerRNA rna_Depsgraph_objects_get(CollectionPropertyIterator *iter)
186 {
187         Object *ob = ((BLI_Iterator *)iter->internal.custom)->current;
188         return rna_pointer_inherit_refine(&iter->parent, &RNA_Object, ob);
189 }
190
191 /* Iteration over objects, extended version
192  *
193  * Contains extra information about duplicator and persistent ID.
194  */
195
196 static void rna_Depsgraph_duplis_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
197 {
198         iter->internal.custom = MEM_callocN(sizeof(BLI_Iterator), __func__);
199         DEGObjectsIteratorData *data = MEM_callocN(sizeof(DEGObjectsIteratorData), __func__);
200
201         data->graph = (Depsgraph *)ptr->data;
202         data->flag = DEG_OBJECT_ITER_FLAG_ALL;
203
204         DEG_objects_iterator_begin(iter->internal.custom, data);
205         iter->valid = ((BLI_Iterator *)iter->internal.custom)->valid;
206 }
207
208 static void rna_Depsgraph_duplis_next(CollectionPropertyIterator *iter)
209 {
210         DEG_objects_iterator_next(iter->internal.custom);
211         iter->valid = ((BLI_Iterator *)iter->internal.custom)->valid;
212 }
213
214 static void rna_Depsgraph_duplis_end(CollectionPropertyIterator *iter)
215 {
216         DEG_objects_iterator_end(iter->internal.custom);
217         MEM_freeN(((BLI_Iterator *)iter->internal.custom)->data);
218         MEM_freeN(iter->internal.custom);
219 }
220
221 static PointerRNA rna_Depsgraph_duplis_get(CollectionPropertyIterator *iter)
222 {
223         BLI_Iterator *iterator = (BLI_Iterator *)iter->internal.custom;
224         return rna_pointer_inherit_refine(&iter->parent, &RNA_DepsgraphIter, iterator);
225 }
226
227 static ID *rna_Depsgraph_evaluated_id_get(Depsgraph *depsgraph, ID *id_orig)
228 {
229         return DEG_get_evaluated_id(depsgraph, id_orig);
230 }
231
232 #else
233
234 static void rna_def_depsgraph_iter(BlenderRNA *brna)
235 {
236         StructRNA *srna;
237         PropertyRNA *prop;
238
239         srna = RNA_def_struct(brna, "DepsgraphIter", NULL);
240         RNA_def_struct_ui_text(srna, "Dependency Graph Iterator",
241                                "Extended information about dependency graph object iterator");
242
243         prop = RNA_def_property(srna, "object", PROP_POINTER, PROP_NONE);
244         RNA_def_property_struct_type(prop, "Object");
245         RNA_def_property_ui_text(prop, "Object", "Object the iterator points to");
246         RNA_def_property_clear_flag(prop, PROP_ANIMATABLE | PROP_EDITABLE);
247         RNA_def_property_pointer_funcs(prop, "rna_DepsgraphIter_object_get", NULL, NULL, NULL);
248
249         prop = RNA_def_property(srna, "instance_object", PROP_POINTER, PROP_NONE);
250         RNA_def_property_struct_type(prop, "Object");
251         RNA_def_property_ui_text(prop, "Instance Object", "Object which is being instanced by this iterator");
252         RNA_def_property_clear_flag(prop, PROP_ANIMATABLE | PROP_EDITABLE);
253         RNA_def_property_pointer_funcs(prop, "rna_DepsgraphIter_instance_object_get", NULL, NULL, NULL);
254
255         prop = RNA_def_property(srna, "parent", PROP_POINTER, PROP_NONE);
256         RNA_def_property_struct_type(prop, "Object");
257         RNA_def_property_ui_text(prop, "Parent", "Parent of the duplication list");
258         RNA_def_property_clear_flag(prop, PROP_ANIMATABLE | PROP_EDITABLE);
259         RNA_def_property_pointer_funcs(prop, "rna_DepsgraphIter_parent_get", NULL, NULL, NULL);
260
261         prop = RNA_def_property(srna, "persistent_id", PROP_INT, PROP_NONE);
262         RNA_def_property_ui_text(prop, "Persistent ID",
263                                  "Persistent identifier for inter-frame matching of objects with motion blur");
264         RNA_def_property_array(prop, 2*MAX_DUPLI_RECUR);
265         RNA_def_property_clear_flag(prop, PROP_ANIMATABLE | PROP_EDITABLE);
266         RNA_def_property_int_funcs(prop, "rna_DepsgraphIter_persistent_id_get", NULL, NULL);
267
268         prop = RNA_def_property(srna, "orco", PROP_FLOAT, PROP_TRANSLATION);
269         RNA_def_property_clear_flag(prop, PROP_ANIMATABLE | PROP_EDITABLE);
270         /* Seems system is not smart enough to figure that getter function should return
271          * array for PROP_TRANSLATION.
272          */
273         RNA_def_property_array(prop, 3);
274         RNA_def_property_ui_text(prop, "Generated Coordinates", "Generated coordinates in parent object space");
275         RNA_def_property_float_funcs(prop, "rna_DepsgraphIter_orco_get", NULL, NULL);
276
277         prop = RNA_def_property(srna, "random_id", PROP_INT, PROP_UNSIGNED);
278         RNA_def_property_clear_flag(prop, PROP_ANIMATABLE | PROP_EDITABLE);
279         RNA_def_property_ui_text(prop, "Dupli random id", "Random id for this dupli object");
280         RNA_def_property_int_funcs(prop, "rna_DepsgraphIter_random_id_get", NULL, NULL);
281
282         prop = RNA_def_property(srna, "uv", PROP_FLOAT, PROP_NONE);
283         RNA_def_property_ui_text(prop, "UV Coordinates", "UV coordinates in parent object space");
284         RNA_def_property_array(prop, 2);
285         RNA_def_property_clear_flag(prop, PROP_ANIMATABLE | PROP_EDITABLE);
286         RNA_def_property_float_funcs(prop, "rna_DepsgraphIter_uv_get", NULL, NULL);
287
288         prop = RNA_def_property(srna, "is_instance", PROP_BOOLEAN, PROP_NONE);
289         RNA_def_property_clear_flag(prop, PROP_ANIMATABLE | PROP_EDITABLE);
290         RNA_def_property_ui_text(prop, "Is Instance", "Denotes whether the object is ocming from dupli-list");
291         RNA_def_property_boolean_funcs(prop, "rna_DepsgraphIter_is_instance_get", NULL);
292 }
293
294 static void rna_def_depsgraph(BlenderRNA *brna)
295 {
296         StructRNA *srna;
297         FunctionRNA *func;
298         PropertyRNA *parm;
299         PropertyRNA *prop;
300
301         srna = RNA_def_struct(brna, "Depsgraph", NULL);
302         RNA_def_struct_ui_text(srna, "Dependency Graph", "");
303
304         func = RNA_def_function(srna, "debug_graphviz", "rna_Depsgraph_debug_graphviz");
305         parm = RNA_def_string_file_path(func, "filename", NULL, FILE_MAX, "File Name",
306                                         "File in which to store graphviz debug output");
307         RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
308
309         func = RNA_def_function(srna, "debug_rebuild", "rna_Depsgraph_debug_rebuild");
310         RNA_def_function_flag(func, FUNC_USE_MAIN);
311
312         func = RNA_def_function(srna, "debug_stats", "rna_Depsgraph_debug_stats");
313         RNA_def_function_ui_description(func, "Report the number of elements in the Dependency Graph");
314         RNA_def_function_flag(func, FUNC_USE_REPORTS);
315
316         prop = RNA_def_property(srna, "objects", PROP_COLLECTION, PROP_NONE);
317         RNA_def_property_struct_type(prop, "Object");
318         RNA_def_property_collection_funcs(prop,
319                                           "rna_Depsgraph_objects_begin",
320                                           "rna_Depsgraph_objects_next",
321                                           "rna_Depsgraph_objects_end",
322                                           "rna_Depsgraph_objects_get",
323                                           NULL, NULL, NULL, NULL);
324
325         func = RNA_def_function(srna, "evaluated_id_get", "rna_Depsgraph_evaluated_id_get");
326         parm = RNA_def_pointer(func, "id", "ID", "", "Original ID to get evaluated complementary part for");
327         RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
328         parm = RNA_def_pointer(func, "evaluated_id", "ID", "", "Evaluated ID for the given original one");
329         RNA_def_function_return(func, parm);
330
331         /* TODO(sergey): Find a better name. */
332         prop = RNA_def_property(srna, "duplis", PROP_COLLECTION, PROP_NONE);
333         RNA_def_property_struct_type(prop, "DepsgraphIter");
334         RNA_def_property_collection_funcs(prop,
335                                           "rna_Depsgraph_duplis_begin",
336                                           "rna_Depsgraph_duplis_next",
337                                           "rna_Depsgraph_duplis_end",
338                                           "rna_Depsgraph_duplis_get",
339                                           NULL, NULL, NULL, NULL);
340 }
341
342 void RNA_def_depsgraph(BlenderRNA *brna)
343 {
344         rna_def_depsgraph_iter(brna);
345         rna_def_depsgraph(brna);
346 }
347
348 #endif