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_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 #include "DEG_depsgraph.h"
67
68 #define DEBUG_PRINT if (G.debug & G_DEBUG_DEPSGRAPH) printf
69
70 void BKE_object_eval_local_transform(const EvaluationContext *UNUSED(eval_ctx),
71                                      Scene *UNUSED(scene),
72                                      Object *ob)
73 {
74         DEBUG_PRINT("%s on %s (%p)\n", __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(const EvaluationContext *UNUSED(eval_ctx),
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         DEBUG_PRINT("%s on %s (%p)\n", __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(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(const EvaluationContext *eval_ctx,
115                                  Scene *scene,
116                                  Object *ob)
117 {
118         bConstraintOb *cob;
119         float ctime = BKE_scene_frame_get(scene);
120
121         DEBUG_PRINT("%s on %s (%p)\n", __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(scene, ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
133         BKE_constraints_solve(eval_ctx, &ob->constraints, cob, ctime);
134         BKE_constraints_clear_evalob(cob);
135 }
136
137 void BKE_object_eval_done(const EvaluationContext *UNUSED(eval_ctx), Object *ob)
138 {
139         DEBUG_PRINT("%s on %s (%p)\n", __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
146 void BKE_object_handle_data_update(
147         const EvaluationContext *eval_ctx,
148         Scene *scene,
149         Object *ob)
150 {
151         ID *data_id = (ID *)ob->data;
152         AnimData *adt = BKE_animdata_from_id(data_id);
153         Key *key;
154         float ctime = BKE_scene_frame_get(scene);
155
156         if (G.debug & G_DEBUG_DEPSGRAPH)
157                 printf("recalcdata %s\n", ob->id.name + 2);
158
159         /* TODO(sergey): Only used by legacy depsgraph. */
160         if (adt) {
161                 /* evaluate drivers - datalevel */
162                 /* XXX: for mesh types, should we push this to derivedmesh instead? */
163                 BKE_animsys_evaluate_animdata(scene, data_id, adt, ctime, ADT_RECALC_DRIVERS);
164         }
165
166         /* TODO(sergey): Only used by legacy depsgraph. */
167         key = BKE_key_from_object(ob);
168         if (key && key->block.first) {
169                 if (!(ob->shapeflag & OB_SHAPE_LOCK))
170                         BKE_animsys_evaluate_animdata(scene, &key->id, key->adt, ctime, ADT_RECALC_DRIVERS);
171         }
172
173         /* includes all keys and modifiers */
174         switch (ob->type) {
175                 case OB_MESH:
176                 {
177                         BMEditMesh *em = (ob == scene->obedit) ? BKE_editmesh_from_object(ob) : NULL;
178                         uint64_t data_mask = scene->customdata_mask | CD_MASK_BAREMESH;
179 #ifdef WITH_FREESTYLE
180                         /* make sure Freestyle edge/face marks appear in DM for render (see T40315) */
181                         if (eval_ctx->mode != DAG_EVAL_VIEWPORT) {
182                                 data_mask |= CD_MASK_FREESTYLE_EDGE | CD_MASK_FREESTYLE_FACE;
183                         }
184 #endif
185                         if (em) {
186                                 makeDerivedMesh(eval_ctx, scene, ob, em,  data_mask, false); /* was CD_MASK_BAREMESH */
187                         }
188                         else {
189                                 makeDerivedMesh(eval_ctx, scene, ob, NULL, data_mask, false);
190                         }
191                         break;
192                 }
193                 case OB_ARMATURE:
194                         if (ID_IS_LINKED(ob) && ob->proxy_from) {
195                                 if (BKE_pose_copy_result(ob->pose, ob->proxy_from->pose) == false) {
196                                         printf("Proxy copy error, lib Object: %s proxy Object: %s\n",
197                                                ob->id.name + 2, ob->proxy_from->id.name + 2);
198                                 }
199                         }
200                         else {
201                                 BKE_pose_where_is(eval_ctx, scene, ob);
202                         }
203                         break;
204
205                 case OB_MBALL:
206                         BKE_displist_make_mball(eval_ctx, scene, ob);
207                         break;
208
209                 case OB_CURVE:
210                 case OB_SURF:
211                 case OB_FONT:
212                         BKE_displist_make_curveTypes(eval_ctx, scene, ob, 0);
213                         break;
214
215                 case OB_LATTICE:
216                         BKE_lattice_modifiers_calc(eval_ctx, scene, ob);
217                         break;
218
219                 case OB_EMPTY:
220                         if (ob->empty_drawtype == OB_EMPTY_IMAGE && ob->data)
221                                 if (BKE_image_is_animated(ob->data))
222                                         BKE_image_user_check_frame_calc(ob->iuser, (int)ctime, 0);
223                         break;
224         }
225
226         /* particles */
227         if (ob != scene->obedit && ob->particlesystem.first) {
228                 ParticleSystem *tpsys, *psys;
229                 DerivedMesh *dm;
230                 ob->transflag &= ~OB_DUPLIPARTS;
231                 psys = ob->particlesystem.first;
232                 while (psys) {
233                         /* ensure this update always happens even if psys is disabled */
234                         if (psys->recalc & PSYS_RECALC_TYPE) {
235                                 psys_changed_type(ob, psys);
236                         }
237
238                         if (psys_check_enabled(ob, psys, eval_ctx->mode == DAG_EVAL_RENDER)) {
239                                 /* check use of dupli objects here */
240                                 if (psys->part && (psys->part->draw_as == PART_DRAW_REND || eval_ctx->mode == DAG_EVAL_RENDER) &&
241                                     ((psys->part->ren_as == PART_DRAW_OB && psys->part->dup_ob) ||
242                                      (psys->part->ren_as == PART_DRAW_GR && psys->part->dup_group)))
243                                 {
244                                         ob->transflag |= OB_DUPLIPARTS;
245                                 }
246
247                                 particle_system_update(eval_ctx, scene, ob, psys, (eval_ctx->mode == DAG_EVAL_RENDER));
248                                 psys = psys->next;
249                         }
250                         else if (psys->flag & PSYS_DELETE) {
251                                 tpsys = psys->next;
252                                 BLI_remlink(&ob->particlesystem, psys);
253                                 psys_free(ob, psys);
254                                 psys = tpsys;
255                         }
256                         else
257                                 psys = psys->next;
258                 }
259
260                 if (eval_ctx->mode == DAG_EVAL_RENDER && ob->transflag & OB_DUPLIPARTS) {
261                         /* this is to make sure we get render level duplis in groups:
262                          * the derivedmesh must be created before init_render_mesh,
263                          * since object_duplilist does dupliparticles before that */
264                         CustomDataMask data_mask = CD_MASK_BAREMESH | CD_MASK_MFACE | CD_MASK_MTFACE | CD_MASK_MCOL;
265                         dm = mesh_create_derived_render(eval_ctx, scene, ob, data_mask);
266                         dm->release(dm);
267
268                         for (psys = ob->particlesystem.first; psys; psys = psys->next)
269                                 psys_get_modifier(ob, psys)->flag &= ~eParticleSystemFlag_psys_updated;
270                 }
271         }
272
273         /* quick cache removed */
274 }
275
276 void BKE_object_eval_uber_transform(const EvaluationContext *UNUSED(eval_ctx),
277                                     Scene *UNUSED(scene),
278                                     Object *ob)
279 {
280         /* TODO(sergey): Currently it's a duplicate of logic in BKE_object_handle_update_ex(). */
281         // XXX: it's almost redundant now...
282
283         /* Handle proxy copy for target, */
284         if (ID_IS_LINKED(ob) && ob->proxy_from) {
285                 if (ob->proxy_from->proxy_group) {
286                         /* Transform proxy into group space. */
287                         Object *obg = ob->proxy_from->proxy_group;
288                         float imat[4][4];
289                         invert_m4_m4(imat, obg->obmat);
290                         mul_m4_m4m4(ob->obmat, imat, ob->proxy_from->obmat);
291                         /* Should always be true. */
292                         if (obg->dup_group) {
293                                 add_v3_v3(ob->obmat[3], obg->dup_group->dupli_ofs);
294                         }
295                 }
296                 else
297                         copy_m4_m4(ob->obmat, ob->proxy_from->obmat);
298         }
299
300         ob->recalc &= ~(OB_RECALC_OB | OB_RECALC_TIME);
301         if (ob->data == NULL) {
302                 ob->recalc &= ~OB_RECALC_DATA;
303         }
304 }
305
306 void BKE_object_eval_uber_data(const EvaluationContext *eval_ctx,
307                                Scene *scene,
308                                Object *ob)
309 {
310         DEBUG_PRINT("%s on %s (%p)\n", __func__, ob->id.name, ob);
311         BLI_assert(ob->type != OB_ARMATURE);
312         BKE_object_handle_data_update(eval_ctx, scene, ob);
313
314         switch (ob->type) {
315                 case OB_MESH:
316                         BKE_mesh_batch_cache_dirty(ob->data, BKE_MESH_BATCH_DIRTY_ALL);
317                         break;
318                 case OB_LATTICE:
319                         BKE_lattice_batch_cache_dirty(ob->data, BKE_LATTICE_BATCH_DIRTY_ALL);
320                         break;
321                 case OB_CURVE:
322                 case OB_FONT:
323                 case OB_SURF:
324                         BKE_curve_batch_cache_dirty(ob->data, BKE_CURVE_BATCH_DIRTY_ALL);
325                         break;
326                 case OB_MBALL:
327                         BKE_mball_batch_cache_dirty(ob->data, BKE_MBALL_BATCH_DIRTY_ALL);
328                         break;
329         }
330
331         if (DEG_depsgraph_use_copy_on_write()) {
332                 if (ob->type == OB_MESH) {
333                         /* Quick hack to convert evaluated derivedMesh to Mesh. */
334                         DerivedMesh *dm = ob->derivedFinal;
335                         if (dm != NULL) {
336                                 Mesh *mesh = (Mesh *)ob->data;
337                                 Mesh *new_mesh = BKE_libblock_alloc_notest(ID_ME);
338                                 BKE_mesh_init(new_mesh);
339                                 /* Copy ID name so GS(new_mesh->id) works correct later on. */
340                                 BLI_strncpy(new_mesh->id.name, mesh->id.name, sizeof(new_mesh->id.name));
341                                 /* Copy materials so render engines can access them. */
342                                 new_mesh->mat = MEM_dupallocN(mesh->mat);
343                                 new_mesh->totcol = mesh->totcol;
344                                 DM_to_mesh(dm, new_mesh, ob, CD_MASK_MESH, true);
345                                 new_mesh->edit_btmesh = mesh->edit_btmesh;
346                                 /* Store result mesh as derived_mesh of object. This way we have
347                                  * explicit  way to query final object evaluated data and know for sure
348                                  * who owns the newly created mesh datablock.
349                                  */
350                                 ob->mesh_evaluated = new_mesh;
351                                 /* TODO(sergey): This is kind of compatibility thing, so all render
352                                  * engines can use object->data for mesh data for display. This is
353                                  * something what we might want to change in the future.
354                                  */
355                                 ob->data = new_mesh;
356                                 /* Special flags to help debugging. */
357                                 new_mesh->id.tag |= LIB_TAG_COPY_ON_WRITE_EVAL;
358                                 /* Save some memory by throwing DerivedMesh away. */
359                                 /* NOTE: Watch out, some tools might need it!
360                                  * So keep around for now..
361                                  */
362                                 /* Store original ID as a pointer in evaluated ID.
363                                  * This way we can restore original object data when we are freeing
364                                  * evaluated mesh.
365                                  */
366                                 new_mesh->id.newid = &mesh->id;
367                         }
368 #if 0
369                         if (ob->derivedFinal != NULL) {
370                                 ob->derivedFinal->needsFree = 1;
371                                 ob->derivedFinal->release(ob->derivedFinal);
372                                 ob->derivedFinal = NULL;
373                         }
374                         if (ob->derivedDeform != NULL) {
375                                 ob->derivedDeform->needsFree = 1;
376                                 ob->derivedDeform->release(ob->derivedDeform);
377                                 ob->derivedDeform = NULL;
378                         }
379 #endif
380                 }
381         }
382
383         ob->recalc &= ~(OB_RECALC_DATA | OB_RECALC_TIME);
384 }
385
386 void BKE_object_eval_cloth(const EvaluationContext *UNUSED(eval_ctx),
387                            Scene *scene,
388                            Object *object)
389 {
390         DEBUG_PRINT("%s on %s (%p)\n", __func__, object->id.name, object);
391         BKE_ptcache_object_reset(scene, object, PTCACHE_RESET_DEPSGRAPH);
392 }
393
394 void BKE_object_eval_transform_all(EvaluationContext *eval_ctx,
395                                    Scene *scene,
396                                    Object *object)
397 {
398         /* This mimics full transform update chain from new depsgraph. */
399         BKE_object_eval_local_transform(eval_ctx, scene, object);
400         if (object->parent != NULL) {
401                 BKE_object_eval_parent(eval_ctx, scene, object);
402         }
403         if (!BLI_listbase_is_empty(&object->constraints)) {
404                 BKE_object_eval_constraints(eval_ctx, scene, object);
405         }
406         BKE_object_eval_uber_transform(eval_ctx, scene, object);
407         BKE_object_eval_done(eval_ctx, object);
408 }
409
410 void BKE_object_eval_update_shading(const EvaluationContext *UNUSED(eval_ctx),
411                                     Object *object)
412 {
413         DEBUG_PRINT("%s on %s (%p)\n", __func__, object->id.name, object);
414         if (object->type == OB_MESH) {
415                 BKE_mesh_batch_cache_dirty(object->data, BKE_MESH_BATCH_DIRTY_SHADING);
416         }
417 }
418
419 void BKE_object_data_select_update(const EvaluationContext *UNUSED(eval_ctx),
420                                    struct ID *object_data)
421 {
422         DEBUG_PRINT("%s on %s (%p)\n", __func__, object_data->name, object_data);
423         switch (GS(object_data->name)) {
424                 case ID_ME:
425                         BKE_mesh_batch_cache_dirty((Mesh *)object_data,
426                                                    BKE_CURVE_BATCH_DIRTY_SELECT);
427                         break;
428                 case ID_CU:
429                         BKE_curve_batch_cache_dirty((Curve *)object_data,
430                                                     BKE_CURVE_BATCH_DIRTY_SELECT);
431                         break;
432                 case ID_LT:
433                         BKE_lattice_batch_cache_dirty((struct Lattice *)object_data,
434                                                       BKE_CURVE_BATCH_DIRTY_SELECT);
435                         break;
436                 default:
437                         break;
438         }
439 }
440
441 void BKE_object_eval_flush_base_flags(const EvaluationContext *UNUSED(eval_ctx),
442                                       Object *object, Base *base, bool is_from_set)
443 {
444         DEBUG_PRINT("%s on %s (%p)\n", __func__, object->id.name, object);
445         /* Make sure we have the base collection settings is already populated.
446          * This will fail when BKE_layer_eval_layer_collection_pre hasn't run yet
447          * Which usually means a missing call to DEG_id_tag_update(). */
448         BLI_assert(!BLI_listbase_is_empty(&base->collection_properties->data.group));
449         /* Copy flags and settings from base. */
450         object->base_flag = base->flag;
451         if (is_from_set) {
452                 object->base_flag |= BASE_FROM_SET;
453                 object->base_flag &= ~(BASE_SELECTED | BASE_SELECTABLED);
454         }
455         object->base_collection_properties = base->collection_properties;
456 }