Depsgraph: Be consistent about id type variable name
[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 id_type)
57 {
58         return bmain->id_tag_update[BKE_idcode_to_index(id_type)] != 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_evaluated_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_evaluated_scene_layer(Depsgraph *graph)
92 {
93         Scene *scene = DEG_get_evaluated_scene(graph);
94         if (scene != NULL) {
95                 return BKE_scene_layer_context_active_PLACEHOLDER(scene);
96         }
97         return NULL;
98 }
99
100 Object *DEG_get_evaluated_object(Depsgraph *depsgraph, Object *object)
101 {
102         return (Object *)DEG_get_evaluated_id(depsgraph, &object->id);
103 }
104
105 ID *DEG_get_evaluated_id(struct Depsgraph *depsgraph, ID *id)
106 {
107         /* TODO(sergey): This is a duplicate of Depsgraph::get_cow_id(),
108          * but here we never do assert, since we don't know nature of the
109          * incoming ID datablock.
110          */
111         DEG::Depsgraph *deg_graph = (DEG::Depsgraph *)depsgraph;
112         DEG::IDDepsNode *id_node = deg_graph->find_id_node(id);
113         if (id_node == NULL) {
114                 return id;
115         }
116         return id_node->id_cow;
117 }
118
119 /* ************************ DAG ITERATORS ********************* */
120
121 #define BASE_FLUSH_FLAGS (BASE_FROM_SET | BASE_FROMDUPLI)
122
123 void DEG_objects_iterator_begin(BLI_Iterator *iter, DEGObjectsIteratorData *data)
124 {
125         Depsgraph *graph = data->graph;
126         SceneLayer *scene_layer = DEG_get_evaluated_scene_layer(graph);
127
128         iter->data = data;
129         iter->valid = true;
130
131         data->scene = DEG_get_evaluated_scene(graph);
132         DEG_evaluation_context_init(&data->eval_ctx, DAG_EVAL_RENDER);
133
134         /* TODO(sergey): It's really confusing to store pointer to a local data. */
135         Base base = {(Base *)scene_layer->object_bases.first, NULL};
136         data->base = &base;
137
138         data->base_flag = ~(BASE_FLUSH_FLAGS);
139
140         data->dupli_parent = NULL;
141         data->dupli_list = NULL;
142         data->dupli_object_next = NULL;
143         data->dupli_object_current = NULL;
144
145         DEG_objects_iterator_next(iter);
146 }
147
148 /**
149  * Temporary function to flush depsgraph until we get copy on write (CoW)
150  */
151 static void deg_flush_base_flags_and_settings(Object *ob, Base *base, const int flag)
152 {
153         ob->base_flag = (base->flag | BASE_FLUSH_FLAGS) & flag;
154         ob->base_collection_properties = base->collection_properties;
155 }
156
157 static bool deg_objects_dupli_iterator_next(BLI_Iterator *iter)
158 {
159         DEGObjectsIteratorData *data = (DEGObjectsIteratorData *)iter->data;
160         while (data->dupli_object_next != NULL) {
161                 DupliObject *dob = data->dupli_object_next;
162                 Object *obd = dob->ob;
163
164                 data->dupli_object_next = data->dupli_object_next->next;
165
166                 /* Group duplis need to set ob matrices correct, for deform. so no_draw
167                  * is part handled.
168                  */
169                 if ((obd->transflag & OB_RENDER_DUPLI) == 0 && dob->no_draw) {
170                         continue;
171                 }
172
173                 if (obd->type == OB_MBALL) {
174                         continue;
175                 }
176
177                 data->dupli_object_current = dob;
178
179                 /* Temporary object to evaluate. */
180                 data->temp_dupli_object = *dob->ob;
181                 data->temp_dupli_object.select_color = data->dupli_parent->select_color;
182                 copy_m4_m4(data->temp_dupli_object.obmat, dob->mat);
183
184                 deg_flush_base_flags_and_settings(&data->temp_dupli_object,
185                                                   data->base,
186                                                   data->base_flag | BASE_FROMDUPLI);
187                 iter->current = &data->temp_dupli_object;
188                 BLI_assert(DEG::deg_validate_copy_on_write_datablock(&data->temp_dupli_object.id));
189                 return true;
190         }
191
192         return false;
193 }
194
195 void DEG_objects_iterator_next(BLI_Iterator *iter)
196 {
197         DEGObjectsIteratorData *data = (DEGObjectsIteratorData *)iter->data;
198         Base *base;
199
200         if (data->dupli_list) {
201                 if (deg_objects_dupli_iterator_next(iter)) {
202                         return;
203                 }
204                 else {
205                         free_object_duplilist(data->dupli_list);
206                         data->dupli_parent = NULL;
207                         data->dupli_list = NULL;
208                         data->dupli_object_next = NULL;
209                         data->dupli_object_current = NULL;
210                 }
211         }
212
213         base = data->base->next;
214         while (base != NULL) {
215                 if ((base->flag & BASE_VISIBLED) != 0) {
216                         // Object *ob = DEG_get_evaluated_object(data->graph, base->object);
217                         Object *ob = base->object;
218                         iter->current = ob;
219                         data->base = base;
220
221                         BLI_assert(DEG::deg_validate_copy_on_write_datablock(&ob->id));
222
223                         /* Make sure we have the base collection settings is already populated.
224                          * This will fail when BKE_layer_eval_layer_collection_pre hasn't run yet
225                          * Which usually means a missing call to DAG_id_tag_update(). */
226                         BLI_assert(!BLI_listbase_is_empty(&base->collection_properties->data.group));
227
228                         /* Flushing depsgraph data. */
229                         deg_flush_base_flags_and_settings(ob,
230                                                           base,
231                                                           data->base_flag);
232
233                         if ((data->flag & DEG_OBJECT_ITER_FLAG_DUPLI) && (ob->transflag & OB_DUPLI)) {
234                                 data->dupli_parent = ob;
235                                 data->dupli_list = object_duplilist(&data->eval_ctx, data->scene, ob);
236                                 data->dupli_object_next = (DupliObject *)data->dupli_list->first;
237                         }
238                         return;
239                 }
240                 base = base->next;
241         }
242
243         /* Look for an object in the next set. */
244         if ((data->flag & DEG_OBJECT_ITER_FLAG_SET) && data->scene->set) {
245                 SceneLayer *scene_layer;
246                 data->scene = data->scene->set;
247                 data->base_flag = ~(BASE_SELECTED | BASE_SELECTABLED);
248
249                 /* For the sets we use the layer used for rendering. */
250                 scene_layer = BKE_scene_layer_from_scene_get(data->scene);
251
252                 /* TODO(sergey): It's really confusing to store pointer to a local data. */
253                 Base base = {(Base *)scene_layer->object_bases.first, NULL};
254                 data->base = &base;
255                 DEG_objects_iterator_next(iter);
256                 return;
257         }
258
259         iter->current = NULL;
260         iter->valid = false;
261 }
262
263 void DEG_objects_iterator_end(BLI_Iterator *iter)
264 {
265 #ifndef NDEBUG
266         DEGObjectsIteratorData *data = (DEGObjectsIteratorData *)iter->data;
267         /* Force crash in case the iterator data is referenced and accessed down the line. (T51718) */
268         memset(&data->temp_dupli_object, 0xff, sizeof(data->temp_dupli_object));
269 #else
270         (void) iter;
271 #endif
272 }