Fix T55165: Driving camera lens with property behaves unexpectedly
[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_global.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_animsys.h"
49 #include "BKE_displist.h"
50 #include "BKE_effect.h"
51 #include "BKE_key.h"
52 #include "BKE_lamp.h"
53 #include "BKE_lattice.h"
54 #include "BKE_library.h"
55 #include "BKE_editmesh.h"
56 #include "BKE_object.h"
57 #include "BKE_particle.h"
58 #include "BKE_pointcache.h"
59 #include "BKE_scene.h"
60 #include "BKE_material.h"
61 #include "BKE_mball.h"
62 #include "BKE_mesh.h"
63 #include "BKE_image.h"
64
65 #include "MEM_guardedalloc.h"
66
67 #include "DEG_depsgraph.h"
68 #include "DEG_depsgraph_query.h"
69
70
71 void BKE_object_eval_local_transform(Depsgraph *depsgraph, Object *ob)
72 {
73         DEG_debug_print_eval(depsgraph, __func__, ob->id.name, ob);
74
75         /* calculate local matrix */
76         BKE_object_to_mat4(ob, ob->obmat);
77 }
78
79 /* Evaluate parent */
80 /* NOTE: based on solve_parenting(), but with the cruft stripped out */
81 void BKE_object_eval_parent(Depsgraph *depsgraph,
82                             Scene *scene,
83                             Object *ob)
84 {
85         Object *par = ob->parent;
86
87         float totmat[4][4];
88         float tmat[4][4];
89         float locmat[4][4];
90
91         DEG_debug_print_eval(depsgraph, __func__, ob->id.name, ob);
92
93         /* get local matrix (but don't calculate it, as that was done already!) */
94         // XXX: redundant?
95         copy_m4_m4(locmat, ob->obmat);
96
97         /* get parent effect matrix */
98         BKE_object_get_parent_matrix(scene, ob, par, totmat);
99
100         /* total */
101         mul_m4_m4m4(tmat, totmat, ob->parentinv);
102         mul_m4_m4m4(ob->obmat, tmat, locmat);
103
104         /* origin, for help line */
105         if ((ob->partype & PARTYPE) == PARSKEL) {
106                 copy_v3_v3(ob->orig, par->obmat[3]);
107         }
108         else {
109                 copy_v3_v3(ob->orig, totmat[3]);
110         }
111 }
112
113 void BKE_object_eval_constraints(Depsgraph *depsgraph,
114                                  Scene *scene,
115                                  Object *ob)
116 {
117         bConstraintOb *cob;
118         float ctime = BKE_scene_frame_get(scene);
119
120         DEG_debug_print_eval(depsgraph, __func__, ob->id.name, ob);
121
122         /* evaluate constraints stack */
123         /* TODO: split this into:
124          * - pre (i.e. BKE_constraints_make_evalob, per-constraint (i.e.
125          * - inner body of BKE_constraints_solve),
126          * - post (i.e. BKE_constraints_clear_evalob)
127          *
128          * Not sure why, this is from Joshua - sergey
129          *
130          */
131         cob = BKE_constraints_make_evalob(depsgraph, scene, ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
132         BKE_constraints_solve(depsgraph, &ob->constraints, cob, ctime);
133         BKE_constraints_clear_evalob(cob);
134 }
135
136 void BKE_object_eval_done(Depsgraph *depsgraph, Object *ob)
137 {
138         DEG_debug_print_eval(depsgraph, __func__, ob->id.name, ob);
139
140         /* Set negative scale flag in object. */
141         if (is_negative_m4(ob->obmat)) ob->transflag |= OB_NEG_SCALE;
142         else ob->transflag &= ~OB_NEG_SCALE;
143 }
144
145 void BKE_object_handle_data_update(
146         Depsgraph *depsgraph,
147         Scene *scene,
148         Object *ob)
149 {
150         ID *data_id = (ID *)ob->data;
151         AnimData *adt = BKE_animdata_from_id(data_id);
152         Key *key;
153         float ctime = BKE_scene_frame_get(scene);
154
155         if (G.debug & G_DEBUG_DEPSGRAPH_EVAL)
156                 printf("recalcdata %s\n", ob->id.name + 2);
157
158         /* TODO(sergey): Only used by legacy depsgraph. */
159         if (adt) {
160                 /* evaluate drivers - datalevel */
161                 /* XXX: for mesh types, should we push this to derivedmesh instead? */
162                 BKE_animsys_evaluate_animdata(scene, data_id, adt, ctime, ADT_RECALC_DRIVERS);
163         }
164
165         /* TODO(sergey): Only used by legacy depsgraph. */
166         key = BKE_key_from_object(ob);
167         if (key && key->block.first) {
168                 if (!(ob->shapeflag & OB_SHAPE_LOCK))
169                         BKE_animsys_evaluate_animdata(scene, &key->id, key->adt, ctime, ADT_RECALC_DRIVERS);
170         }
171
172         /* includes all keys and modifiers */
173         switch (ob->type) {
174                 case OB_MESH:
175                 {
176 #if 0
177                         BMEditMesh *em = (ob->mode & OB_MODE_EDIT) ? BKE_editmesh_from_object(ob) : NULL;
178 #else
179                         BMEditMesh *em = (ob->mode & OB_MODE_EDIT) ? ((Mesh *)ob->data)->edit_btmesh : NULL;
180                         if (em && em->ob != ob) {
181                                 em = NULL;
182                         }
183 #endif
184
185                         uint64_t data_mask = scene->customdata_mask | CD_MASK_BAREMESH;
186 #ifdef WITH_FREESTYLE
187                         /* make sure Freestyle edge/face marks appear in DM for render (see T40315) */
188                         if (DEG_get_mode(depsgraph) != DAG_EVAL_VIEWPORT) {
189                                 data_mask |= CD_MASK_FREESTYLE_EDGE | CD_MASK_FREESTYLE_FACE;
190                         }
191 #endif
192                         if (em) {
193                                 makeDerivedMesh(depsgraph, scene, ob, em,  data_mask, false); /* was CD_MASK_BAREMESH */
194                         }
195                         else {
196                                 makeDerivedMesh(depsgraph, scene, ob, NULL, data_mask, false);
197                         }
198                         break;
199                 }
200                 case OB_ARMATURE:
201                         if (ID_IS_LINKED(ob) && ob->proxy_from) {
202                                 if (BKE_pose_copy_result(ob->pose, ob->proxy_from->pose) == false) {
203                                         printf("Proxy copy error, lib Object: %s proxy Object: %s\n",
204                                                ob->id.name + 2, ob->proxy_from->id.name + 2);
205                                 }
206                         }
207                         else {
208                                 BKE_pose_where_is(depsgraph, scene, ob);
209                         }
210                         break;
211
212                 case OB_MBALL:
213                         BKE_displist_make_mball(depsgraph, scene, ob);
214                         break;
215
216                 case OB_CURVE:
217                 case OB_SURF:
218                 case OB_FONT:
219                         BKE_displist_make_curveTypes(depsgraph, scene, ob, 0);
220                         break;
221
222                 case OB_LATTICE:
223                         BKE_lattice_modifiers_calc(depsgraph, scene, ob);
224                         break;
225
226                 case OB_EMPTY:
227                         if (ob->empty_drawtype == OB_EMPTY_IMAGE && ob->data)
228                                 if (BKE_image_is_animated(ob->data))
229                                         BKE_image_user_check_frame_calc(ob->iuser, (int)ctime, 0);
230                         break;
231         }
232
233         /* particles */
234         if (!(ob->mode & OB_MODE_EDIT) && ob->particlesystem.first) {
235                 const bool use_render_params = (DEG_get_mode(depsgraph) == DAG_EVAL_RENDER);
236                 ParticleSystem *tpsys, *psys;
237                 DerivedMesh *dm;
238                 ob->transflag &= ~OB_DUPLIPARTS;
239                 psys = ob->particlesystem.first;
240                 while (psys) {
241                         if (psys_check_enabled(ob, psys, use_render_params)) {
242                                 /* check use of dupli objects here */
243                                 if (psys->part && (psys->part->draw_as == PART_DRAW_REND || use_render_params) &&
244                                     ((psys->part->ren_as == PART_DRAW_OB && psys->part->dup_ob) ||
245                                      (psys->part->ren_as == PART_DRAW_GR && psys->part->dup_group)))
246                                 {
247                                         ob->transflag |= OB_DUPLIPARTS;
248                                 }
249
250                                 particle_system_update(depsgraph, scene, ob, psys, use_render_params);
251                                 psys = psys->next;
252                         }
253                         else if (psys->flag & PSYS_DELETE) {
254                                 tpsys = psys->next;
255                                 BLI_remlink(&ob->particlesystem, psys);
256                                 psys_free(ob, psys);
257                                 psys = tpsys;
258                         }
259                         else
260                                 psys = psys->next;
261                 }
262
263                 if (use_render_params && ob->transflag & OB_DUPLIPARTS) {
264                         /* this is to make sure we get render level duplis in groups:
265                          * the derivedmesh must be created before init_render_mesh,
266                          * since object_duplilist does dupliparticles before that */
267                         CustomDataMask data_mask = CD_MASK_BAREMESH | CD_MASK_MFACE | CD_MASK_MTFACE | CD_MASK_MCOL;
268                         dm = mesh_create_derived_render(depsgraph, scene, ob, data_mask);
269                         dm->release(dm);
270
271                         for (psys = ob->particlesystem.first; psys; psys = psys->next)
272                                 psys_get_modifier(ob, psys)->flag &= ~eParticleSystemFlag_psys_updated;
273                 }
274         }
275
276         /* quick cache removed */
277 }
278
279 bool BKE_object_eval_proxy_copy(Depsgraph *UNUSED(depsgraph),
280                                 Object *object)
281 {
282         /* Handle proxy copy for target, */
283         if (ID_IS_LINKED(object) && object->proxy_from) {
284                 if (object->proxy_from->proxy_group) {
285                         /* Transform proxy into group space. */
286                         Object *obg = object->proxy_from->proxy_group;
287                         float imat[4][4];
288                         invert_m4_m4(imat, obg->obmat);
289                         mul_m4_m4m4(object->obmat, imat, object->proxy_from->obmat);
290                         /* Should always be true. */
291                         if (obg->dup_group) {
292                                 add_v3_v3(object->obmat[3], obg->dup_group->dupli_ofs);
293                         }
294                 }
295                 else {
296                         copy_m4_m4(object->obmat, object->proxy_from->obmat);
297                 }
298                 return true;
299         }
300         return false;
301 }
302
303 void BKE_object_eval_uber_transform(Depsgraph *depsgraph, Object *object)
304 {
305         BKE_object_eval_proxy_copy(depsgraph, object);
306 }
307
308 void BKE_object_eval_uber_data(Depsgraph *depsgraph,
309                                Scene *scene,
310                                Object *ob)
311 {
312         DEG_debug_print_eval(depsgraph, __func__, ob->id.name, ob);
313         BLI_assert(ob->type != OB_ARMATURE);
314         BKE_object_handle_data_update(depsgraph, scene, ob);
315
316         switch (ob->type) {
317                 case OB_MESH:
318                         BKE_mesh_batch_cache_dirty(ob->data, BKE_MESH_BATCH_DIRTY_ALL);
319                         break;
320                 case OB_LATTICE:
321                         BKE_lattice_batch_cache_dirty(ob->data, BKE_LATTICE_BATCH_DIRTY_ALL);
322                         break;
323                 case OB_CURVE:
324                 case OB_FONT:
325                 case OB_SURF:
326                         BKE_curve_batch_cache_dirty(ob->data, BKE_CURVE_BATCH_DIRTY_ALL);
327                         break;
328                 case OB_MBALL:
329                         BKE_mball_batch_cache_dirty(ob->data, BKE_MBALL_BATCH_DIRTY_ALL);
330                         break;
331         }
332
333         if (DEG_depsgraph_use_copy_on_write()) {
334                 if (ob->type == OB_MESH) {
335                         /* Quick hack to convert evaluated derivedMesh to Mesh. */
336                         DerivedMesh *dm = ob->derivedFinal;
337                         if (dm != NULL) {
338                                 Mesh *mesh = (Mesh *)ob->data;
339                                 Mesh *new_mesh = BKE_libblock_alloc_notest(ID_ME);
340                                 BKE_mesh_init(new_mesh);
341                                 /* Copy ID name so GS(new_mesh->id) works correct later on. */
342                                 BLI_strncpy(new_mesh->id.name, mesh->id.name, sizeof(new_mesh->id.name));
343                                 /* Copy materials so render engines can access them. */
344                                 new_mesh->mat = MEM_dupallocN(mesh->mat);
345                                 new_mesh->totcol = mesh->totcol;
346                                 DM_to_mesh(dm, new_mesh, ob, CD_MASK_MESH, true);
347                                 new_mesh->edit_btmesh = mesh->edit_btmesh;
348                                 /* Store result mesh as derived_mesh of object. This way we have
349                                  * explicit  way to query final object evaluated data and know for sure
350                                  * who owns the newly created mesh datablock.
351                                  */
352                                 ob->mesh_evaluated = new_mesh;
353                                 /* TODO(sergey): This is kind of compatibility thing, so all render
354                                  * engines can use object->data for mesh data for display. This is
355                                  * something what we might want to change in the future.
356                                  * XXX: This can sometimes cause modifiers to be applied twice!
357                                  */
358                                 ob->data = new_mesh;
359                                 /* Special flags to help debugging. */
360                                 new_mesh->id.tag |= LIB_TAG_COPY_ON_WRITE_EVAL;
361                                 /* Save some memory by throwing DerivedMesh away. */
362                                 /* NOTE: Watch out, some tools might need it!
363                                  * So keep around for now..
364                                  */
365                                 /* Store original ID as a pointer in evaluated ID.
366                                  * This way we can restore original object data when we are freeing
367                                  * evaluated mesh.
368                                  */
369                                 new_mesh->id.orig_id = &mesh->id;
370                                 /* Copy autosmooth settings from original mesh. */
371                                 new_mesh->flag |= (mesh->flag & ME_AUTOSMOOTH);
372                                 new_mesh->smoothresh = mesh->smoothresh;
373                         }
374 #if 0
375                         if (ob->derivedFinal != NULL) {
376                                 ob->derivedFinal->needsFree = 1;
377                                 ob->derivedFinal->release(ob->derivedFinal);
378                                 ob->derivedFinal = NULL;
379                         }
380                         if (ob->derivedDeform != NULL) {
381                                 ob->derivedDeform->needsFree = 1;
382                                 ob->derivedDeform->release(ob->derivedDeform);
383                                 ob->derivedDeform = NULL;
384                         }
385 #endif
386                 }
387         }
388 }
389
390 void BKE_object_eval_cloth(Depsgraph *depsgraph,
391                            Scene *scene,
392                            Object *object)
393 {
394         DEG_debug_print_eval(depsgraph, __func__, object->id.name, object);
395         BKE_ptcache_object_reset(scene, object, PTCACHE_RESET_DEPSGRAPH);
396 }
397
398 void BKE_object_eval_transform_all(Depsgraph *depsgraph,
399                                    Scene *scene,
400                                    Object *object)
401 {
402         /* This mimics full transform update chain from new depsgraph. */
403         BKE_object_eval_local_transform(depsgraph, object);
404         if (object->parent != NULL) {
405                 BKE_object_eval_parent(depsgraph, scene, object);
406         }
407         if (!BLI_listbase_is_empty(&object->constraints)) {
408                 BKE_object_eval_constraints(depsgraph, scene, object);
409         }
410         BKE_object_eval_uber_transform(depsgraph, object);
411         BKE_object_eval_done(depsgraph, object);
412 }
413
414 void BKE_object_eval_update_shading(Depsgraph *depsgraph, Object *object)
415 {
416         DEG_debug_print_eval(depsgraph, __func__, object->id.name, object);
417         if (object->type == OB_MESH) {
418                 BKE_mesh_batch_cache_dirty(object->data, BKE_MESH_BATCH_DIRTY_SHADING);
419         }
420 }
421
422 void BKE_object_data_select_update(Depsgraph *depsgraph, ID *object_data)
423 {
424         DEG_debug_print_eval(depsgraph, __func__, object_data->name, object_data);
425         switch (GS(object_data->name)) {
426                 case ID_ME:
427                         BKE_mesh_batch_cache_dirty((Mesh *)object_data,
428                                                    BKE_CURVE_BATCH_DIRTY_SELECT);
429                         break;
430                 case ID_CU:
431                         BKE_curve_batch_cache_dirty((Curve *)object_data,
432                                                     BKE_CURVE_BATCH_DIRTY_SELECT);
433                         break;
434                 case ID_LT:
435                         BKE_lattice_batch_cache_dirty((struct Lattice *)object_data,
436                                                       BKE_CURVE_BATCH_DIRTY_SELECT);
437                         break;
438                 default:
439                         break;
440         }
441 }
442
443 void BKE_object_eval_flush_base_flags(Depsgraph *depsgraph,
444                                       Scene *scene, const int view_layer_index,
445                                       Object *object, int base_index,
446                                       const bool is_from_set)
447 {
448         /* TODO(sergey): Avoid list lookup. */
449         BLI_assert(view_layer_index >= 0);
450         ViewLayer *view_layer = BLI_findlink(&scene->view_layers, view_layer_index);
451         BLI_assert(view_layer != NULL);
452         BLI_assert(view_layer->object_bases_array != NULL);
453         BLI_assert(base_index >= 0);
454         BLI_assert(base_index < MEM_allocN_len(view_layer->object_bases_array) / sizeof(Base *));
455         Base *base = view_layer->object_bases_array[base_index];
456         BLI_assert(base->object == object);
457
458         DEG_debug_print_eval(depsgraph, __func__, object->id.name, object);
459
460         /* Copy flags and settings from base. */
461         object->base_flag = base->flag;
462         if (is_from_set) {
463                 object->base_flag |= BASE_FROM_SET;
464                 object->base_flag &= ~(BASE_SELECTED | BASE_SELECTABLED);
465         }
466
467         if (object->mode == OB_MODE_PARTICLE_EDIT) {
468                 for (ParticleSystem *psys = object->particlesystem.first;
469                      psys != NULL;
470                      psys = psys->next)
471                 {
472                         BKE_particle_batch_cache_dirty(psys, BKE_PARTICLE_BATCH_DIRTY_ALL);
473                 }
474         }
475 }