Merge branch 'master' into blender2.8
[blender.git] / source / blender / depsgraph / intern / depsgraph_query.cc
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  * The Original Code is Copyright (C) 2013 Blender Foundation.
19  * All rights reserved.
20  *
21  * Original Author: Joshua Leung
22  * Contributor(s): None Yet
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/depsgraph/intern/depsgraph_query.cc
28  *  \ingroup depsgraph
29  *
30  * Implementation of Querying and Filtering API's
31  */
32
33 #include "MEM_guardedalloc.h"
34
35 extern "C" {
36 #include "BLI_math.h"
37 #include "BKE_anim.h"
38 #include "BKE_idcode.h"
39 #include "BKE_layer.h"
40 #include "BKE_main.h"
41 } /* extern "C" */
42
43 #include "DNA_object_types.h"
44 #include "DNA_scene_types.h"
45
46 #include "DEG_depsgraph.h"
47 #include "DEG_depsgraph_query.h"
48
49 #include "intern/depsgraph_intern.h"
50 #include "util/deg_util_foreach.h"
51
52 #ifndef NDEBUG
53 #  include "intern/eval/deg_eval_copy_on_write.h"
54 #endif
55
56 bool DEG_id_type_tagged(Main *bmain, short idtype)
57 {
58         return bmain->id_tag_update[BKE_idcode_to_index(idtype)] != 0;
59 }
60
61 short DEG_get_eval_flags_for_id(Depsgraph *graph, ID *id)
62 {
63         if (graph == NULL) {
64                 /* Happens when converting objects to mesh from a python script
65                  * after modifying scene graph.
66                  *
67                  * Currently harmless because it's only called for temporary
68                  * objects which are out of the DAG anyway.
69                  */
70                 return 0;
71         }
72
73         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(graph);
74
75         DEG::IDDepsNode *id_node = deg_graph->find_id_node(id);
76         if (id_node == NULL) {
77                 /* TODO(sergey): Does it mean we need to check set scene? */
78                 return 0;
79         }
80
81         return id_node->eval_flags;
82 }
83
84 Scene *DEG_get_scene(Depsgraph *graph)
85 {
86         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(graph);
87         Scene *scene_orig = deg_graph->scene;
88         return reinterpret_cast<Scene *>(deg_graph->get_cow_id(&scene_orig->id));
89 }
90
91 SceneLayer *DEG_get_scene_layer(Depsgraph *graph)
92 {
93         Scene *scene = DEG_get_scene(graph);
94         if (scene != NULL) {
95                 DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(graph);
96                 return BKE_scene_layer_context_active_ex(deg_graph->bmain, scene);
97         }
98         return NULL;
99 }
100
101 Object *DEG_get_object(Depsgraph *depsgraph, Object *ob)
102 {
103         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(depsgraph);
104         return (Object *)deg_graph->get_cow_id(&ob->id);
105 }
106
107 ID *DEG_get_evaluated_id(struct Depsgraph *depsgraph, ID *id)
108 {
109         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(depsgraph);
110         return deg_graph->get_cow_id(id);
111 }
112
113 /* ************************ DAG ITERATORS ********************* */
114
115 #define BASE_FLUSH_FLAGS (BASE_FROM_SET | BASE_FROMDUPLI)
116
117 void DEG_objects_iterator_begin(BLI_Iterator *iter, DEGObjectsIteratorData *data)
118 {
119         Depsgraph *graph = data->graph;
120         SceneLayer *scene_layer = DEG_get_scene_layer(graph);
121
122         iter->data = data;
123         iter->valid = true;
124
125         data->scene = DEG_get_scene(graph);
126         DEG_evaluation_context_init(&data->eval_ctx, DAG_EVAL_RENDER);
127
128         /* TODO(sergey): It's really confusing to store pointer to a local data. */
129         Base base = {(Base *)scene_layer->object_bases.first, NULL};
130         data->base = &base;
131
132         data->base_flag = ~(BASE_FLUSH_FLAGS);
133
134         data->dupli_parent = NULL;
135         data->dupli_list = NULL;
136         data->dupli_object_next = NULL;
137         data->dupli_object_current = NULL;
138
139         DEG_objects_iterator_next(iter);
140 }
141
142 /**
143  * Temporary function to flush depsgraph until we get copy on write (CoW)
144  */
145 static void deg_flush_base_flags_and_settings(Object *ob, Base *base, const int flag)
146 {
147         ob->base_flag = (base->flag | BASE_FLUSH_FLAGS) & flag;
148         ob->base_collection_properties = base->collection_properties;
149 }
150
151 static bool deg_objects_dupli_iterator_next(BLI_Iterator *iter)
152 {
153         DEGObjectsIteratorData *data = (DEGObjectsIteratorData *)iter->data;
154         while (data->dupli_object_next != NULL) {
155                 DupliObject *dob = data->dupli_object_next;
156                 Object *obd = dob->ob;
157
158                 data->dupli_object_next = data->dupli_object_next->next;
159
160                 /* Group duplis need to set ob matrices correct, for deform. so no_draw
161                  * is part handled.
162                  */
163                 if ((obd->transflag & OB_RENDER_DUPLI) == 0 && dob->no_draw) {
164                         continue;
165                 }
166
167                 if (obd->type == OB_MBALL) {
168                         continue;
169                 }
170
171                 data->dupli_object_current = dob;
172
173                 /* Temporary object to evaluate. */
174                 data->temp_dupli_object = *dob->ob;
175                 data->temp_dupli_object.select_color = data->dupli_parent->select_color;
176                 copy_m4_m4(data->temp_dupli_object.obmat, dob->mat);
177
178                 deg_flush_base_flags_and_settings(&data->temp_dupli_object,
179                                                   data->base,
180                                                   data->base_flag | BASE_FROMDUPLI);
181                 iter->current = &data->temp_dupli_object;
182                 BLI_assert(DEG::deg_validate_copy_on_write_datablock(&data->temp_dupli_object.id));
183                 return true;
184         }
185
186         return false;
187 }
188
189 void DEG_objects_iterator_next(BLI_Iterator *iter)
190 {
191         DEGObjectsIteratorData *data = (DEGObjectsIteratorData *)iter->data;
192         Base *base;
193
194         if (data->dupli_list) {
195                 if (deg_objects_dupli_iterator_next(iter)) {
196                         return;
197                 }
198                 else {
199                         free_object_duplilist(data->dupli_list);
200                         data->dupli_parent = NULL;
201                         data->dupli_list = NULL;
202                         data->dupli_object_next = NULL;
203                         data->dupli_object_current = NULL;
204                 }
205         }
206
207         base = data->base->next;
208         while (base != NULL) {
209                 if ((base->flag & BASE_VISIBLED) != 0) {
210                         // Object *ob = DEG_get_object(data->graph, base->object);
211                         Object *ob = base->object;
212                         iter->current = ob;
213                         data->base = base;
214
215                         BLI_assert(DEG::deg_validate_copy_on_write_datablock(&ob->id));
216
217                         /* Make sure we have the base collection settings is already populated.
218                          * This will fail when BKE_layer_eval_layer_collection_pre hasn't run yet
219                          * Which usually means a missing call to DAG_id_tag_update(). */
220                         BLI_assert(!BLI_listbase_is_empty(&base->collection_properties->data.group));
221
222                         /* Flushing depsgraph data. */
223                         deg_flush_base_flags_and_settings(ob,
224                                                           base,
225                                                           data->base_flag);
226
227                         if ((data->flag & DEG_OBJECT_ITER_FLAG_DUPLI) && (ob->transflag & OB_DUPLI)) {
228                                 data->dupli_parent = ob;
229                                 data->dupli_list = object_duplilist(&data->eval_ctx, data->scene, ob);
230                                 data->dupli_object_next = (DupliObject *)data->dupli_list->first;
231                         }
232                         return;
233                 }
234                 base = base->next;
235         }
236
237         /* Look for an object in the next set. */
238         if ((data->flag & DEG_OBJECT_ITER_FLAG_SET) && data->scene->set) {
239                 SceneLayer *scene_layer;
240                 data->scene = data->scene->set;
241                 data->base_flag = ~(BASE_SELECTED | BASE_SELECTABLED);
242
243                 /* For the sets we use the layer used for rendering. */
244                 scene_layer = BKE_scene_layer_render_active(data->scene);
245
246                 /* TODO(sergey): It's really confusing to store pointer to a local data. */
247                 Base base = {(Base *)scene_layer->object_bases.first, NULL};
248                 data->base = &base;
249                 DEG_objects_iterator_next(iter);
250                 return;
251         }
252
253         iter->current = NULL;
254         iter->valid = false;
255 }
256
257 void DEG_objects_iterator_end(BLI_Iterator *iter)
258 {
259 #ifndef NDEBUG
260         DEGObjectsIteratorData *data = (DEGObjectsIteratorData *)iter->data;
261         /* Force crash in case the iterator data is referenced and accessed down the line. (T51718) */
262         memset(&data->temp_dupli_object, 0xff, sizeof(data->temp_dupli_object));
263 #else
264         (void) iter;
265 #endif
266 }