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_mesh.h"
62 #include "BKE_image.h"
63
64 #include "MEM_guardedalloc.h"
65 #include "DEG_depsgraph.h"
66
67 #define DEBUG_PRINT if (G.debug & G_DEBUG_DEPSGRAPH) printf
68
69 void BKE_object_eval_local_transform(const EvaluationContext *UNUSED(eval_ctx),
70                                      Scene *UNUSED(scene),
71                                      Object *ob)
72 {
73         DEBUG_PRINT("%s on %s\n", __func__, ob->id.name);
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(const EvaluationContext *UNUSED(eval_ctx),
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         DEBUG_PRINT("%s on %s\n", __func__, ob->id.name);
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(const EvaluationContext *eval_ctx,
114                                  Scene *scene,
115                                  Object *ob)
116 {
117         bConstraintOb *cob;
118         float ctime = BKE_scene_frame_get(scene);
119
120         DEBUG_PRINT("%s on %s\n", __func__, ob->id.name);
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(scene, ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
132         BKE_constraints_solve(eval_ctx, &ob->constraints, cob, ctime);
133         BKE_constraints_clear_evalob(cob);
134 }
135
136 void BKE_object_eval_done(const EvaluationContext *UNUSED(eval_ctx), Object *ob)
137 {
138         DEBUG_PRINT("%s on %s\n", __func__, ob->id.name);
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         const EvaluationContext *eval_ctx,
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)
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                         BMEditMesh *em = (ob == scene->obedit) ? BKE_editmesh_from_object(ob) : NULL;
177                         uint64_t data_mask = scene->customdata_mask | CD_MASK_BAREMESH;
178 #ifdef WITH_FREESTYLE
179                         /* make sure Freestyle edge/face marks appear in DM for render (see T40315) */
180                         if (eval_ctx->mode != DAG_EVAL_VIEWPORT) {
181                                 data_mask |= CD_MASK_FREESTYLE_EDGE | CD_MASK_FREESTYLE_FACE;
182                         }
183 #endif
184                         if (em) {
185                                 makeDerivedMesh(eval_ctx, scene, ob, em,  data_mask, false); /* was CD_MASK_BAREMESH */
186                         }
187                         else {
188                                 makeDerivedMesh(eval_ctx, scene, ob, NULL, data_mask, false);
189                         }
190                         break;
191                 }
192                 case OB_ARMATURE:
193                         if (ID_IS_LINKED(ob) && ob->proxy_from) {
194                                 if (BKE_pose_copy_result(ob->pose, ob->proxy_from->pose) == false) {
195                                         printf("Proxy copy error, lib Object: %s proxy Object: %s\n",
196                                                ob->id.name + 2, ob->proxy_from->id.name + 2);
197                                 }
198                         }
199                         else {
200                                 BKE_pose_where_is(eval_ctx, scene, ob);
201                         }
202                         break;
203
204                 case OB_MBALL:
205                         BKE_displist_make_mball(eval_ctx, scene, ob);
206                         break;
207
208                 case OB_CURVE:
209                 case OB_SURF:
210                 case OB_FONT:
211                         BKE_displist_make_curveTypes(eval_ctx, scene, ob, 0);
212                         break;
213
214                 case OB_LATTICE:
215                         BKE_lattice_modifiers_calc(eval_ctx, scene, ob);
216                         break;
217
218                 case OB_EMPTY:
219                         if (ob->empty_drawtype == OB_EMPTY_IMAGE && ob->data)
220                                 if (BKE_image_is_animated(ob->data))
221                                         BKE_image_user_check_frame_calc(ob->iuser, (int)ctime, 0);
222                         break;
223         }
224
225         /* particles */
226         if (ob != scene->obedit && ob->particlesystem.first) {
227                 ParticleSystem *tpsys, *psys;
228                 DerivedMesh *dm;
229                 ob->transflag &= ~OB_DUPLIPARTS;
230                 psys = ob->particlesystem.first;
231                 while (psys) {
232                         /* ensure this update always happens even if psys is disabled */
233                         if (psys->recalc & PSYS_RECALC_TYPE) {
234                                 psys_changed_type(ob, psys);
235                         }
236
237                         if (psys_check_enabled(ob, psys, eval_ctx->mode == DAG_EVAL_RENDER)) {
238                                 /* check use of dupli objects here */
239                                 if (psys->part && (psys->part->draw_as == PART_DRAW_REND || eval_ctx->mode == DAG_EVAL_RENDER) &&
240                                     ((psys->part->ren_as == PART_DRAW_OB && psys->part->dup_ob) ||
241                                      (psys->part->ren_as == PART_DRAW_GR && psys->part->dup_group)))
242                                 {
243                                         ob->transflag |= OB_DUPLIPARTS;
244                                 }
245
246                                 particle_system_update(eval_ctx, scene, ob, psys, (eval_ctx->mode == DAG_EVAL_RENDER));
247                                 psys = psys->next;
248                         }
249                         else if (psys->flag & PSYS_DELETE) {
250                                 tpsys = psys->next;
251                                 BLI_remlink(&ob->particlesystem, psys);
252                                 psys_free(ob, psys);
253                                 psys = tpsys;
254                         }
255                         else
256                                 psys = psys->next;
257                 }
258
259                 if (eval_ctx->mode == DAG_EVAL_RENDER && ob->transflag & OB_DUPLIPARTS) {
260                         /* this is to make sure we get render level duplis in groups:
261                          * the derivedmesh must be created before init_render_mesh,
262                          * since object_duplilist does dupliparticles before that */
263                         CustomDataMask data_mask = CD_MASK_BAREMESH | CD_MASK_MFACE | CD_MASK_MTFACE | CD_MASK_MCOL;
264                         dm = mesh_create_derived_render(eval_ctx, scene, ob, data_mask);
265                         dm->release(dm);
266
267                         for (psys = ob->particlesystem.first; psys; psys = psys->next)
268                                 psys_get_modifier(ob, psys)->flag &= ~eParticleSystemFlag_psys_updated;
269                 }
270         }
271
272         /* quick cache removed */
273 }
274
275 void BKE_object_eval_uber_transform(const EvaluationContext *UNUSED(eval_ctx),
276                                     Scene *UNUSED(scene),
277                                     Object *ob)
278 {
279         /* TODO(sergey): Currently it's a duplicate of logic in BKE_object_handle_update_ex(). */
280         // XXX: it's almost redundant now...
281
282         /* Handle proxy copy for target, */
283         if (ID_IS_LINKED(ob) && ob->proxy_from) {
284                 if (ob->proxy_from->proxy_group) {
285                         /* Transform proxy into group space. */
286                         Object *obg = ob->proxy_from->proxy_group;
287                         float imat[4][4];
288                         invert_m4_m4(imat, obg->obmat);
289                         mul_m4_m4m4(ob->obmat, imat, ob->proxy_from->obmat);
290                         /* Should always be true. */
291                         if (obg->dup_group) {
292                                 add_v3_v3(ob->obmat[3], obg->dup_group->dupli_ofs);
293                         }
294                 }
295                 else
296                         copy_m4_m4(ob->obmat, ob->proxy_from->obmat);
297         }
298
299         ob->recalc &= ~(OB_RECALC_OB | OB_RECALC_TIME);
300         if (ob->data == NULL) {
301                 ob->recalc &= ~OB_RECALC_DATA;
302         }
303 }
304
305 void BKE_object_eval_uber_data(const EvaluationContext *eval_ctx,
306                                Scene *scene,
307                                Object *ob)
308 {
309         DEBUG_PRINT("%s on %s\n", __func__, ob->id.name);
310         BLI_assert(ob->type != OB_ARMATURE);
311         BKE_object_handle_data_update(eval_ctx, scene, ob);
312
313         switch (ob->type) {
314                 case OB_MESH:
315                         BKE_mesh_batch_cache_dirty(ob->data, BKE_MESH_BATCH_DIRTY_ALL);
316                         break;
317                 case OB_LATTICE:
318                         BKE_lattice_batch_cache_dirty(ob->data, BKE_LATTICE_BATCH_DIRTY_ALL);
319                         break;
320                 case OB_CURVE:
321                 case OB_FONT:
322                 case OB_SURF:
323                         BKE_curve_batch_cache_dirty(ob->data, BKE_CURVE_BATCH_DIRTY_ALL);
324                         break;
325         }
326
327         if (DEG_depsgraph_use_copy_on_write()) {
328                 if (ob->type == OB_MESH) {
329                         /* Quick hack to convert evaluated derivedMesh to Mesh. */
330                         DerivedMesh *dm = ob->derivedFinal;
331                         if (dm != NULL) {
332                                 Mesh *mesh = (Mesh *)ob->data;
333                                 Mesh *new_mesh = BKE_libblock_alloc_notest(ID_ME);
334                                 BKE_mesh_init(new_mesh);
335                                 /* Copy ID name so GS(new_mesh->id) works correct later on. */
336                                 BLI_strncpy(new_mesh->id.name, mesh->id.name, sizeof(new_mesh->id.name));
337                                 /* Copy materials so render engines can access them. */
338                                 new_mesh->mat = MEM_dupallocN(mesh->mat);
339                                 new_mesh->totcol = mesh->totcol;
340                                 DM_to_mesh(dm, new_mesh, ob, CD_MASK_MESH, true);
341                                 new_mesh->edit_btmesh = mesh->edit_btmesh;
342                                 /* Store result mesh as derived_mesh of object. This way we have
343                                  * explicit  way to query final object evaluated data and know for sure
344                                  * who owns the newly created mesh datablock.
345                                  */
346                                 ob->mesh_evaluated = new_mesh;
347                                 /* TODO(sergey): This is kind of compatibility thing, so all render
348                                  * engines can use object->data for mesh data for display. This is
349                                  * something what we might want to change in the future.
350                                  */
351                                 ob->data = new_mesh;
352                                 /* Save some memory by throwing DerivedMesh away. */
353                                 /* NOTE: Watch out, some tools might need it!
354                                  * So keep around for now..
355                                  */
356                                 /* Store original ID as a pointer in evaluated ID.
357                                  * This way we can restore original object data when we are freeing
358                                  * evaluated mesh.
359                                  */
360                                 new_mesh->id.newid = &mesh->id;
361                         }
362 #if 0
363                         if (ob->derivedFinal != NULL) {
364                                 ob->derivedFinal->needsFree = 1;
365                                 ob->derivedFinal->release(ob->derivedFinal);
366                                 ob->derivedFinal = NULL;
367                         }
368                         if (ob->derivedDeform != NULL) {
369                                 ob->derivedDeform->needsFree = 1;
370                                 ob->derivedDeform->release(ob->derivedDeform);
371                                 ob->derivedDeform = NULL;
372                         }
373 #endif
374                 }
375         }
376
377         ob->recalc &= ~(OB_RECALC_DATA | OB_RECALC_TIME);
378 }
379
380 void BKE_object_eval_cloth(const EvaluationContext *UNUSED(eval_ctx), Scene *scene, Object *object)
381 {
382         DEBUG_PRINT("%s on %s\n", __func__, object->id.name);
383         BKE_ptcache_object_reset(scene, object, PTCACHE_RESET_DEPSGRAPH);
384 }
385
386 void BKE_object_eval_update_shading(const EvaluationContext *UNUSED(eval_ctx), Object *object)
387 {
388         DEBUG_PRINT("%s on %s\n", __func__, object->id.name);
389         if (object->type == OB_MESH) {
390                 BKE_mesh_batch_cache_dirty(object->data, BKE_MESH_BATCH_DIRTY_SHADING);
391         }
392 }