Merge branch 'master' into blender2.8
[blender.git] / source / blender / blenkernel / intern / object_update.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  * The Original Code is Copyright (C) 20014 by Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Sergey Sharybin.
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/blenkernel/intern/object_update.c
27  *  \ingroup bke
28  */
29
30 #include "DNA_anim_types.h"
31 #include "DNA_constraint_types.h"
32 #include "DNA_group_types.h"
33 #include "DNA_key_types.h"
34 #include "DNA_material_types.h"
35 #include "DNA_mesh_types.h"
36 #include "DNA_scene_types.h"
37
38 #include "BLI_blenlib.h"
39 #include "BLI_utildefines.h"
40 #include "BLI_math.h"
41
42 #include "BKE_animsys.h"
43 #include "BKE_armature.h"
44 #include "BKE_action.h"
45 #include "BKE_constraint.h"
46 #include "BKE_curve.h"
47 #include "BKE_DerivedMesh.h"
48 #include "BKE_displist.h"
49 #include "BKE_editmesh.h"
50 #include "BKE_effect.h"
51 #include "BKE_global.h"
52 #include "BKE_image.h"
53 #include "BKE_key.h"
54 #include "BKE_lamp.h"
55 #include "BKE_lattice.h"
56 #include "BKE_library.h"
57 #include "BKE_main.h"
58 #include "BKE_material.h"
59 #include "BKE_mball.h"
60 #include "BKE_mesh.h"
61 #include "BKE_object.h"
62 #include "BKE_particle.h"
63 #include "BKE_pointcache.h"
64 #include "BKE_scene.h"
65
66 #include "MEM_guardedalloc.h"
67
68 #include "DEG_depsgraph.h"
69 #include "DEG_depsgraph_query.h"
70
71
72 void BKE_object_eval_local_transform(Depsgraph *depsgraph, Object *ob)
73 {
74         DEG_debug_print_eval(depsgraph, __func__, ob->id.name, ob);
75
76         /* calculate local matrix */
77         BKE_object_to_mat4(ob, ob->obmat);
78 }
79
80 /* Evaluate parent */
81 /* NOTE: based on solve_parenting(), but with the cruft stripped out */
82 void BKE_object_eval_parent(Depsgraph *depsgraph,
83                             Scene *scene,
84                             Object *ob)
85 {
86         Object *par = ob->parent;
87
88         float totmat[4][4];
89         float tmat[4][4];
90         float locmat[4][4];
91
92         DEG_debug_print_eval(depsgraph, __func__, ob->id.name, ob);
93
94         /* get local matrix (but don't calculate it, as that was done already!) */
95         // XXX: redundant?
96         copy_m4_m4(locmat, ob->obmat);
97
98         /* get parent effect matrix */
99         BKE_object_get_parent_matrix(depsgraph, scene, ob, par, totmat);
100
101         /* total */
102         mul_m4_m4m4(tmat, totmat, ob->parentinv);
103         mul_m4_m4m4(ob->obmat, tmat, locmat);
104
105         /* origin, for help line */
106         if ((ob->partype & PARTYPE) == PARSKEL) {
107                 copy_v3_v3(ob->orig, par->obmat[3]);
108         }
109         else {
110                 copy_v3_v3(ob->orig, totmat[3]);
111         }
112 }
113
114 void BKE_object_eval_constraints(Depsgraph *depsgraph,
115                                  Scene *scene,
116                                  Object *ob)
117 {
118         bConstraintOb *cob;
119         float ctime = BKE_scene_frame_get(scene);
120
121         DEG_debug_print_eval(depsgraph, __func__, ob->id.name, ob);
122
123         /* evaluate constraints stack */
124         /* TODO: split this into:
125          * - pre (i.e. BKE_constraints_make_evalob, per-constraint (i.e.
126          * - inner body of BKE_constraints_solve),
127          * - post (i.e. BKE_constraints_clear_evalob)
128          *
129          * Not sure why, this is from Joshua - sergey
130          *
131          */
132         cob = BKE_constraints_make_evalob(depsgraph, scene, ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
133         BKE_constraints_solve(depsgraph, &ob->constraints, cob, ctime);
134         BKE_constraints_clear_evalob(cob);
135 }
136
137 void BKE_object_eval_done(Depsgraph *depsgraph, Object *ob)
138 {
139         DEG_debug_print_eval(depsgraph, __func__, ob->id.name, ob);
140
141         /* Set negative scale flag in object. */
142         if (is_negative_m4(ob->obmat)) ob->transflag |= OB_NEG_SCALE;
143         else ob->transflag &= ~OB_NEG_SCALE;
144
145         if (DEG_is_active(depsgraph)) {
146                 Object *ob_orig = DEG_get_original_object(ob);
147                 copy_m4_m4(ob_orig->obmat, ob->obmat);
148                 ob_orig->transflag = ob->transflag;
149                 ob_orig->flag = ob->flag;
150         }
151 }
152
153 void BKE_object_handle_data_update(
154         Depsgraph *depsgraph,
155         Scene *scene,
156         Object *ob)
157 {
158         ID *data_id = (ID *)ob->data;
159         AnimData *adt = BKE_animdata_from_id(data_id);
160         Key *key;
161         float ctime = BKE_scene_frame_get(scene);
162
163         if (G.debug & G_DEBUG_DEPSGRAPH_EVAL)
164                 printf("recalcdata %s\n", ob->id.name + 2);
165
166         /* TODO(sergey): Only used by legacy depsgraph. */
167         if (adt) {
168                 /* evaluate drivers - datalevel */
169                 /* XXX: for mesh types, should we push this to derivedmesh instead? */
170                 BKE_animsys_evaluate_animdata(depsgraph, scene, data_id, adt, ctime, ADT_RECALC_DRIVERS);
171         }
172
173         /* TODO(sergey): Only used by legacy depsgraph. */
174         key = BKE_key_from_object(ob);
175         if (key && key->block.first) {
176                 if (!(ob->shapeflag & OB_SHAPE_LOCK))
177                         BKE_animsys_evaluate_animdata(depsgraph, scene, &key->id, key->adt, ctime, ADT_RECALC_DRIVERS);
178         }
179
180         /* includes all keys and modifiers */
181         switch (ob->type) {
182                 case OB_MESH:
183                 {
184 #if 0
185                         BMEditMesh *em = (ob->mode & OB_MODE_EDIT) ? BKE_editmesh_from_object(ob) : NULL;
186 #else
187                         BMEditMesh *em = (ob->mode & OB_MODE_EDIT) ? ((Mesh *)ob->data)->edit_btmesh : NULL;
188                         if (em && em->ob != ob) {
189                                 em = NULL;
190                         }
191 #endif
192
193                         uint64_t data_mask = scene->customdata_mask | CD_MASK_BAREMESH;
194 #ifdef WITH_FREESTYLE
195                         /* make sure Freestyle edge/face marks appear in DM for render (see T40315) */
196                         if (DEG_get_mode(depsgraph) != DAG_EVAL_VIEWPORT) {
197                                 data_mask |= CD_MASK_FREESTYLE_EDGE | CD_MASK_FREESTYLE_FACE;
198                         }
199 #endif
200                         if (em) {
201                                 makeDerivedMesh(depsgraph, scene, ob, em,  data_mask, false); /* was CD_MASK_BAREMESH */
202                         }
203                         else {
204                                 makeDerivedMesh(depsgraph, scene, ob, NULL, data_mask, false);
205                         }
206                         break;
207                 }
208                 case OB_ARMATURE:
209                         if (ID_IS_LINKED(ob) && ob->proxy_from) {
210                                 if (BKE_pose_copy_result(ob->pose, ob->proxy_from->pose) == false) {
211                                         printf("Proxy copy error, lib Object: %s proxy Object: %s\n",
212                                                ob->id.name + 2, ob->proxy_from->id.name + 2);
213                                 }
214                         }
215                         else {
216                                 BKE_pose_where_is(depsgraph, scene, ob);
217                         }
218                         break;
219
220                 case OB_MBALL:
221                         BKE_displist_make_mball(depsgraph, scene, ob);
222                         break;
223
224                 case OB_CURVE:
225                 case OB_SURF:
226                 case OB_FONT:
227                         BKE_displist_make_curveTypes(depsgraph, scene, ob, 0);
228                         break;
229
230                 case OB_LATTICE:
231                         BKE_lattice_modifiers_calc(depsgraph, scene, ob);
232                         break;
233
234                 case OB_EMPTY:
235                         if (ob->empty_drawtype == OB_EMPTY_IMAGE && ob->data)
236                                 if (BKE_image_is_animated(ob->data))
237                                         BKE_image_user_check_frame_calc(ob->iuser, (int)ctime, 0);
238                         break;
239         }
240
241         /* particles */
242         if (!(ob->mode & OB_MODE_EDIT) && ob->particlesystem.first) {
243                 const bool use_render_params = (DEG_get_mode(depsgraph) == DAG_EVAL_RENDER);
244                 ParticleSystem *tpsys, *psys;
245                 DerivedMesh *dm;
246                 ob->transflag &= ~OB_DUPLIPARTS;
247                 psys = ob->particlesystem.first;
248                 while (psys) {
249                         if (psys_check_enabled(ob, psys, use_render_params)) {
250                                 /* check use of dupli objects here */
251                                 if (psys->part && (psys->part->draw_as == PART_DRAW_REND || use_render_params) &&
252                                     ((psys->part->ren_as == PART_DRAW_OB && psys->part->dup_ob) ||
253                                      (psys->part->ren_as == PART_DRAW_GR && psys->part->dup_group)))
254                                 {
255                                         ob->transflag |= OB_DUPLIPARTS;
256                                 }
257
258                                 particle_system_update(depsgraph, scene, ob, psys, use_render_params);
259                                 psys = psys->next;
260                         }
261                         else if (psys->flag & PSYS_DELETE) {
262                                 tpsys = psys->next;
263                                 BLI_remlink(&ob->particlesystem, psys);
264                                 psys_free(ob, psys);
265                                 psys = tpsys;
266                         }
267                         else
268                                 psys = psys->next;
269                 }
270
271                 if (use_render_params && ob->transflag & OB_DUPLIPARTS) {
272                         /* this is to make sure we get render level duplis in groups:
273                          * the derivedmesh must be created before init_render_mesh,
274                          * since object_duplilist does dupliparticles before that */
275                         CustomDataMask data_mask = CD_MASK_BAREMESH | CD_MASK_MFACE | CD_MASK_MTFACE | CD_MASK_MCOL;
276                         dm = mesh_create_derived_render(depsgraph, scene, ob, data_mask);
277                         dm->release(dm);
278
279                         for (psys = ob->particlesystem.first; psys; psys = psys->next)
280                                 psys_get_modifier(ob, psys)->flag &= ~eParticleSystemFlag_psys_updated;
281                 }
282         }
283
284         /* quick cache removed */
285 }
286
287 bool BKE_object_eval_proxy_copy(Depsgraph *depsgraph,
288                                 Object *object)
289 {
290         /* Handle proxy copy for target, */
291         if (ID_IS_LINKED(object) && object->proxy_from) {
292                 DEG_debug_print_eval(depsgraph, __func__, object->id.name, object);
293                 if (object->proxy_from->proxy_group) {
294                         /* Transform proxy into group space. */
295                         Object *obg = object->proxy_from->proxy_group;
296                         float imat[4][4];
297                         invert_m4_m4(imat, obg->obmat);
298                         mul_m4_m4m4(object->obmat, imat, object->proxy_from->obmat);
299                         /* Should always be true. */
300                         if (obg->dup_group) {
301                                 add_v3_v3(object->obmat[3], obg->dup_group->dupli_ofs);
302                         }
303                 }
304                 else {
305                         copy_m4_m4(object->obmat, object->proxy_from->obmat);
306                 }
307                 return true;
308         }
309         return false;
310 }
311
312 void BKE_object_eval_uber_transform(Depsgraph *depsgraph, Object *object)
313 {
314         BKE_object_eval_proxy_copy(depsgraph, object);
315 }
316
317 void BKE_object_eval_uber_data(Depsgraph *depsgraph,
318                                Scene *scene,
319                                Object *ob)
320 {
321         DEG_debug_print_eval(depsgraph, __func__, ob->id.name, ob);
322         BLI_assert(ob->type != OB_ARMATURE);
323         BKE_object_handle_data_update(depsgraph, scene, ob);
324
325         switch (ob->type) {
326                 case OB_MESH:
327                         BKE_mesh_batch_cache_dirty(ob->data, BKE_MESH_BATCH_DIRTY_ALL);
328                         break;
329                 case OB_LATTICE:
330                         BKE_lattice_batch_cache_dirty(ob->data, BKE_LATTICE_BATCH_DIRTY_ALL);
331                         break;
332                 case OB_CURVE:
333                 case OB_FONT:
334                 case OB_SURF:
335                         BKE_curve_batch_cache_dirty(ob->data, BKE_CURVE_BATCH_DIRTY_ALL);
336                         break;
337                 case OB_MBALL:
338                         BKE_mball_batch_cache_dirty(ob->data, BKE_MBALL_BATCH_DIRTY_ALL);
339                         break;
340         }
341 }
342
343 void BKE_object_eval_cloth(Depsgraph *depsgraph,
344                            Scene *scene,
345                            Object *object)
346 {
347         DEG_debug_print_eval(depsgraph, __func__, object->id.name, object);
348         BKE_ptcache_object_reset(scene, object, PTCACHE_RESET_DEPSGRAPH);
349 }
350
351 void BKE_object_eval_transform_all(Depsgraph *depsgraph,
352                                    Scene *scene,
353                                    Object *object)
354 {
355         /* This mimics full transform update chain from new depsgraph. */
356         BKE_object_eval_local_transform(depsgraph, object);
357         if (object->parent != NULL) {
358                 BKE_object_eval_parent(depsgraph, scene, object);
359         }
360         if (!BLI_listbase_is_empty(&object->constraints)) {
361                 BKE_object_eval_constraints(depsgraph, scene, object);
362         }
363         BKE_object_eval_uber_transform(depsgraph, object);
364         BKE_object_eval_done(depsgraph, object);
365 }
366
367 void BKE_object_eval_update_shading(Depsgraph *depsgraph, Object *object)
368 {
369         DEG_debug_print_eval(depsgraph, __func__, object->id.name, object);
370         if (object->type == OB_MESH) {
371                 BKE_mesh_batch_cache_dirty(object->data, BKE_MESH_BATCH_DIRTY_SHADING);
372         }
373 }
374
375 void BKE_object_data_select_update(Depsgraph *depsgraph, ID *object_data)
376 {
377         DEG_debug_print_eval(depsgraph, __func__, object_data->name, object_data);
378         switch (GS(object_data->name)) {
379                 case ID_ME:
380                         BKE_mesh_batch_cache_dirty((Mesh *)object_data,
381                                                    BKE_CURVE_BATCH_DIRTY_SELECT);
382                         break;
383                 case ID_CU:
384                         BKE_curve_batch_cache_dirty((Curve *)object_data,
385                                                     BKE_CURVE_BATCH_DIRTY_SELECT);
386                         break;
387                 case ID_LT:
388                         BKE_lattice_batch_cache_dirty((struct Lattice *)object_data,
389                                                       BKE_CURVE_BATCH_DIRTY_SELECT);
390                         break;
391                 default:
392                         break;
393         }
394 }
395
396 void BKE_object_eval_flush_base_flags(Depsgraph *depsgraph,
397                                       Scene *scene, const int view_layer_index,
398                                       Object *object, int base_index,
399                                       const bool is_from_set)
400 {
401         /* TODO(sergey): Avoid list lookup. */
402         BLI_assert(view_layer_index >= 0);
403         ViewLayer *view_layer = BLI_findlink(&scene->view_layers, view_layer_index);
404         BLI_assert(view_layer != NULL);
405         BLI_assert(view_layer->object_bases_array != NULL);
406         BLI_assert(base_index >= 0);
407         BLI_assert(base_index < MEM_allocN_len(view_layer->object_bases_array) / sizeof(Base *));
408         Base *base = view_layer->object_bases_array[base_index];
409         BLI_assert(base->object == object);
410
411         DEG_debug_print_eval(depsgraph, __func__, object->id.name, object);
412
413         /* Copy flags and settings from base. */
414         object->base_flag = base->flag;
415         if (is_from_set) {
416                 object->base_flag |= BASE_FROM_SET;
417                 object->base_flag &= ~(BASE_SELECTED | BASE_SELECTABLED);
418         }
419
420         if (object->mode == OB_MODE_PARTICLE_EDIT) {
421                 for (ParticleSystem *psys = object->particlesystem.first;
422                      psys != NULL;
423                      psys = psys->next)
424                 {
425                         BKE_particle_batch_cache_dirty(psys, BKE_PARTICLE_BATCH_DIRTY_ALL);
426                 }
427         }
428 }