Merge branch 'master' into blender2.8
[blender.git] / source / blender / depsgraph / intern / depsgraph_query_iter.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) 2017 Blender Foundation.
19  * All rights reserved.
20  *
21  * Original Author: Dalai Felinto
22  * Contributor(s): Sergey Sharybin
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/depsgraph/intern/depsgraph_query_iter.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_utildefines.h"
37 #include "BLI_math.h"
38 #include "BKE_anim.h"
39 #include "BKE_idprop.h"
40 #include "BKE_layer.h"
41 #include "BKE_object.h"
42 } /* extern "C" */
43
44 #include "DNA_object_types.h"
45 #include "DNA_scene_types.h"
46
47 #include "DEG_depsgraph.h"
48 #include "DEG_depsgraph_query.h"
49
50 #include "intern/depsgraph_intern.h"
51 #include "util/deg_util_foreach.h"
52
53 #include "intern/nodes/deg_node_id.h"
54
55 #ifndef NDEBUG
56 #  include "intern/eval/deg_eval_copy_on_write.h"
57 #endif
58
59 /* ************************ DEG ITERATORS ********************* */
60
61 static bool deg_objects_dupli_iterator_next(BLI_Iterator *iter)
62 {
63         DEGObjectIterData *data = (DEGObjectIterData *)iter->data;
64         while (data->dupli_object_next != NULL) {
65                 DupliObject *dob = data->dupli_object_next;
66                 Object *obd = dob->ob;
67
68                 data->dupli_object_next = data->dupli_object_next->next;
69
70                 /* Group duplis need to set ob matrices correct, for deform. so no_draw
71                  * is part handled.
72                  */
73                 if ((obd->transflag & OB_RENDER_DUPLI) == 0 && dob->no_draw) {
74                         continue;
75                 }
76
77                 if (obd->type == OB_MBALL) {
78                         continue;
79                 }
80
81                 data->dupli_object_current = dob;
82
83                 /* Temporary object to evaluate. */
84                 Object *dupli_parent = data->dupli_parent;
85                 Object *temp_dupli_object = &data->temp_dupli_object;
86                 *temp_dupli_object = *dob->ob;
87                 temp_dupli_object->transflag &= ~OB_DUPLI;
88                 temp_dupli_object->select_color = dupli_parent->select_color;
89                 temp_dupli_object->base_flag = dupli_parent->base_flag | BASE_FROMDUPLI;
90
91                 if (dob->collection_properties != NULL) {
92                         temp_dupli_object->base_collection_properties = dob->collection_properties;
93                         IDP_MergeGroup(temp_dupli_object->base_collection_properties, dupli_parent->base_collection_properties, false);
94                 }
95                 else {
96                         temp_dupli_object->base_collection_properties = dupli_parent->base_collection_properties;
97                 }
98
99                 copy_m4_m4(data->temp_dupli_object.obmat, dob->mat);
100                 iter->current = &data->temp_dupli_object;
101                 BLI_assert(
102                         DEG::deg_validate_copy_on_write_datablock(
103                                 &data->temp_dupli_object.id));
104                 return true;
105         }
106
107         return false;
108 }
109
110 static void DEG_iterator_objects_step(BLI_Iterator *iter, DEG::IDDepsNode *id_node)
111 {
112         /* Set it early in case we need to exit and we are running from within a loop. */
113         iter->skip = true;
114
115         DEGObjectIterData *data = (DEGObjectIterData *)iter->data;
116         const ID_Type id_type = GS(id_node->id_orig->name);
117
118         if (id_type != ID_OB) {
119                 return;
120         }
121
122         switch (id_node->linked_state) {
123                 case DEG::DEG_ID_LINKED_DIRECTLY:
124                         if ((data->flag & DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY) == 0) {
125                                 return;
126                         }
127                         break;
128                 case DEG::DEG_ID_LINKED_VIA_SET:
129                         if ((data->flag & DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET) == 0) {
130                                 return;
131                         }
132                         break;
133                 case DEG::DEG_ID_LINKED_INDIRECTLY:
134                         if ((data->flag & DEG_ITER_OBJECT_FLAG_LINKED_INDIRECTLY) == 0) {
135                                 return;
136                         }
137                         break;
138         }
139
140         Object *object = (Object *)id_node->id_cow;
141         BLI_assert(DEG::deg_validate_copy_on_write_datablock(&object->id));
142
143         if ((BKE_object_is_visible(object, OB_VISIBILITY_CHECK_UNKNOWN_RENDER_MODE) == false) &&
144             ((data->flag & DEG_ITER_OBJECT_FLAG_VISIBLE) != 0))
145         {
146                 return;
147         }
148
149         if ((data->flag & DEG_ITER_OBJECT_FLAG_DUPLI) && (object->transflag & OB_DUPLI)) {
150                 data->dupli_parent = object;
151                 data->dupli_list = object_duplilist(&data->eval_ctx, data->scene, object);
152                 data->dupli_object_next = (DupliObject *)data->dupli_list->first;
153
154                 const eObjectVisibilityCheck mode = (data->mode == DEG_ITER_OBJECT_MODE_RENDER) ?
155                                                      OB_VISIBILITY_CHECK_FOR_RENDER :
156                                                      OB_VISIBILITY_CHECK_FOR_VIEWPORT;
157
158                 if (BKE_object_is_visible(object, mode) == false) {
159                         return;
160                 }
161         }
162
163         iter->current = object;
164         iter->skip = false;
165 }
166
167 void DEG_iterator_objects_begin(BLI_Iterator *iter, DEGObjectIterData *data)
168 {
169         Depsgraph *depsgraph = data->graph;
170         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(depsgraph);
171         const size_t num_id_nodes = deg_graph->id_nodes.size();
172
173         if (num_id_nodes == 0) {
174                 iter->valid = false;
175                 return;
176         }
177
178         /* TODO(sergey): What evaluation type we want here? */
179         /* TODO(dfelinto): Get rid of evaluation context here, it's only used to do
180          * direct dupli-objects update in group.c. Which is terribly bad, and all
181          * objects are expected to be evaluated already. */
182         DEG_evaluation_context_init(&data->eval_ctx, DAG_EVAL_VIEWPORT);
183         data->eval_ctx.view_layer = DEG_get_evaluated_view_layer(depsgraph);
184
185         iter->data = data;
186         data->dupli_parent = NULL;
187         data->dupli_list = NULL;
188         data->dupli_object_next = NULL;
189         data->dupli_object_current = NULL;
190         data->scene = DEG_get_evaluated_scene(depsgraph);
191         data->id_node_index = 0;
192         data->num_id_nodes = num_id_nodes;
193
194         DEG::IDDepsNode *id_node = deg_graph->id_nodes[data->id_node_index];
195         DEG_iterator_objects_step(iter, id_node);
196
197         if (iter->skip) {
198                 DEG_iterator_objects_next(iter);
199         }
200 }
201
202 void DEG_iterator_objects_next(BLI_Iterator *iter)
203 {
204         DEGObjectIterData *data = (DEGObjectIterData *)iter->data;
205         Depsgraph *depsgraph = data->graph;
206         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(depsgraph);
207         do {
208                 iter->skip = false;
209                 if (data->dupli_list) {
210                         if (deg_objects_dupli_iterator_next(iter)) {
211                                 return;
212                         }
213                         else {
214                                 free_object_duplilist(data->dupli_list);
215                                 data->dupli_parent = NULL;
216                                 data->dupli_list = NULL;
217                                 data->dupli_object_next = NULL;
218                                 data->dupli_object_current = NULL;
219                         }
220                 }
221
222                 ++data->id_node_index;
223                 if (data->id_node_index == data->num_id_nodes) {
224                         iter->valid = false;
225                         return;
226                 }
227
228                 DEG::IDDepsNode *id_node = deg_graph->id_nodes[data->id_node_index];
229                 DEG_iterator_objects_step(iter, id_node);
230         } while (iter->skip);
231 }
232
233 void DEG_iterator_objects_end(BLI_Iterator *iter)
234 {
235 #ifndef NDEBUG
236         DEGObjectIterData *data = (DEGObjectIterData *)iter->data;
237         /* Force crash in case the iterator data is referenced and accessed down the line. (T51718) */
238         memset(&data->temp_dupli_object, 0xff, sizeof(data->temp_dupli_object));
239 #else
240         (void) iter;
241 #endif
242 }