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_scene_types.h"
36
37 #include "BLI_blenlib.h"
38 #include "BLI_utildefines.h"
39 #include "BLI_math.h"
40 #include "BLI_threads.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_editmesh.h"
55 #include "BKE_object.h"
56 #include "BKE_particle.h"
57 #include "BKE_pointcache.h"
58 #include "BKE_scene.h"
59 #include "BKE_material.h"
60 #include "BKE_mesh.h"
61 #include "BKE_image.h"
62
63 #include "DEG_depsgraph.h"
64
65 #define DEBUG_PRINT if (G.debug & G_DEBUG_DEPSGRAPH) printf
66
67 static ThreadMutex material_lock = BLI_MUTEX_INITIALIZER;
68
69 void BKE_object_eval_local_transform(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(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(EvaluationContext *UNUSED(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(&ob->constraints, cob, ctime);
133         BKE_constraints_clear_evalob(cob);
134 }
135
136 void BKE_object_eval_done(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(EvaluationContext *eval_ctx,
146                                    Scene *scene,
147                                    Object *ob)
148 {
149         ID *data_id = (ID *)ob->data;
150         AnimData *adt = BKE_animdata_from_id(data_id);
151         Key *key;
152         float ctime = BKE_scene_frame_get(scene);
153
154         if (G.debug & G_DEBUG_DEPSGRAPH)
155                 printf("recalcdata %s\n", ob->id.name + 2);
156
157         /* TODO(sergey): Only used by legacy depsgraph. */
158         if (adt) {
159                 /* evaluate drivers - datalevel */
160                 /* XXX: for mesh types, should we push this to derivedmesh instead? */
161                 BKE_animsys_evaluate_animdata(scene, data_id, adt, ctime, ADT_RECALC_DRIVERS);
162         }
163
164         /* TODO(sergey): Only used by legacy depsgraph. */
165         key = BKE_key_from_object(ob);
166         if (key && key->block.first) {
167                 if (!(ob->shapeflag & OB_SHAPE_LOCK))
168                         BKE_animsys_evaluate_animdata(scene, &key->id, key->adt, ctime, ADT_RECALC_DRIVERS);
169         }
170
171         /* includes all keys and modifiers */
172         switch (ob->type) {
173                 case OB_MESH:
174                 {
175                         BMEditMesh *em = (ob == scene->obedit) ? BKE_editmesh_from_object(ob) : NULL;
176                         uint64_t data_mask = scene->customdata_mask | CD_MASK_BAREMESH;
177 #ifdef WITH_FREESTYLE
178                         /* make sure Freestyle edge/face marks appear in DM for render (see T40315) */
179                         if (eval_ctx->mode != DAG_EVAL_VIEWPORT) {
180                                 data_mask |= CD_MASK_FREESTYLE_EDGE | CD_MASK_FREESTYLE_FACE;
181                         }
182 #endif
183                         if (em) {
184                                 makeDerivedMesh(scene, ob, em,  data_mask, false); /* was CD_MASK_BAREMESH */
185                         }
186                         else {
187                                 makeDerivedMesh(scene, ob, NULL, data_mask, false);
188                         }
189                         break;
190                 }
191                 case OB_ARMATURE:
192                         if (ID_IS_LINKED_DATABLOCK(ob) && ob->proxy_from) {
193                                 if (BKE_pose_copy_result(ob->pose, ob->proxy_from->pose) == false) {
194                                         printf("Proxy copy error, lib Object: %s proxy Object: %s\n",
195                                                ob->id.name + 2, ob->proxy_from->id.name + 2);
196                                 }
197                         }
198                         else {
199                                 BKE_pose_where_is(scene, ob);
200                         }
201                         break;
202
203                 case OB_MBALL:
204                         BKE_displist_make_mball(eval_ctx, scene, ob);
205                         break;
206
207                 case OB_CURVE:
208                 case OB_SURF:
209                 case OB_FONT:
210                         BKE_displist_make_curveTypes(scene, ob, 0);
211                         break;
212
213                 case OB_LATTICE:
214                         BKE_lattice_modifiers_calc(scene, ob);
215                         break;
216
217                 case OB_EMPTY:
218                         if (ob->empty_drawtype == OB_EMPTY_IMAGE && ob->data)
219                                 if (BKE_image_is_animated(ob->data))
220                                         BKE_image_user_check_frame_calc(ob->iuser, (int)ctime, 0);
221                         break;
222         }
223
224         /* related materials */
225         /* XXX: without depsgraph tagging, this will always need to be run, which will be slow!
226          * However, not doing anything (or trying to hack around this lack) is not an option
227          * anymore, especially due to Cycles [#31834]
228          */
229         if (ob->totcol) {
230                 int a;
231                 if (ob->totcol != 0) {
232                         BLI_mutex_lock(&material_lock);
233                         for (a = 1; a <= ob->totcol; a++) {
234                                 Material *ma = give_current_material(ob, a);
235                                 if (ma) {
236                                         /* recursively update drivers for this material */
237                                         material_drivers_update(scene, ma, ctime);
238                                 }
239                         }
240                         BLI_mutex_unlock(&material_lock);
241                 }
242         }
243         else if (ob->type == OB_LAMP)
244                 lamp_drivers_update(scene, ob->data, ctime);
245
246         /* particles */
247         if (ob != scene->obedit && ob->particlesystem.first) {
248                 ParticleSystem *tpsys, *psys;
249                 DerivedMesh *dm;
250                 ob->transflag &= ~OB_DUPLIPARTS;
251                 psys = ob->particlesystem.first;
252                 while (psys) {
253                         /* ensure this update always happens even if psys is disabled */
254                         if (psys->recalc & PSYS_RECALC_TYPE) {
255                                 psys_changed_type(ob, psys);
256                         }
257
258                         if (psys_check_enabled(ob, psys, eval_ctx->mode == DAG_EVAL_RENDER)) {
259                                 /* check use of dupli objects here */
260                                 if (psys->part && (psys->part->draw_as == PART_DRAW_REND || eval_ctx->mode == DAG_EVAL_RENDER) &&
261                                     ((psys->part->ren_as == PART_DRAW_OB && psys->part->dup_ob) ||
262                                      (psys->part->ren_as == PART_DRAW_GR && psys->part->dup_group)))
263                                 {
264                                         ob->transflag |= OB_DUPLIPARTS;
265                                 }
266
267                                 particle_system_update(scene, ob, psys, (eval_ctx->mode == DAG_EVAL_RENDER));
268                                 psys = psys->next;
269                         }
270                         else if (psys->flag & PSYS_DELETE) {
271                                 tpsys = psys->next;
272                                 BLI_remlink(&ob->particlesystem, psys);
273                                 psys_free(ob, psys);
274                                 psys = tpsys;
275                         }
276                         else
277                                 psys = psys->next;
278                 }
279
280                 if (eval_ctx->mode == DAG_EVAL_RENDER && ob->transflag & OB_DUPLIPARTS) {
281                         /* this is to make sure we get render level duplis in groups:
282                          * the derivedmesh must be created before init_render_mesh,
283                          * since object_duplilist does dupliparticles before that */
284                         CustomDataMask data_mask = CD_MASK_BAREMESH | CD_MASK_MFACE | CD_MASK_MTFACE | CD_MASK_MCOL;
285                         dm = mesh_create_derived_render(scene, ob, data_mask);
286                         dm->release(dm);
287
288                         for (psys = ob->particlesystem.first; psys; psys = psys->next)
289                                 psys_get_modifier(ob, psys)->flag &= ~eParticleSystemFlag_psys_updated;
290                 }
291         }
292
293         /* quick cache removed */
294 }
295
296 void BKE_object_eval_uber_transform(EvaluationContext *UNUSED(eval_ctx),
297                                     Scene *UNUSED(scene),
298                                     Object *ob)
299 {
300         /* TODO(sergey): Currently it's a duplicate of logic in BKE_object_handle_update_ex(). */
301         // XXX: it's almost redundant now...
302
303         /* Handle proxy copy for target, */
304         if (ID_IS_LINKED_DATABLOCK(ob) && ob->proxy_from) {
305                 if (ob->proxy_from->proxy_group) {
306                         /* Transform proxy into group space. */
307                         Object *obg = ob->proxy_from->proxy_group;
308                         float imat[4][4];
309                         invert_m4_m4(imat, obg->obmat);
310                         mul_m4_m4m4(ob->obmat, imat, ob->proxy_from->obmat);
311                         /* Should always be true. */
312                         if (obg->dup_group) {
313                                 add_v3_v3(ob->obmat[3], obg->dup_group->dupli_ofs);
314                         }
315                 }
316                 else
317                         copy_m4_m4(ob->obmat, ob->proxy_from->obmat);
318         }
319
320         ob->recalc &= ~(OB_RECALC_OB | OB_RECALC_TIME);
321         if (ob->data == NULL) {
322                 ob->recalc &= ~OB_RECALC_DATA;
323         }
324 }
325
326 void BKE_object_eval_uber_data(EvaluationContext *eval_ctx,
327                                Scene *scene,
328                                Object *ob)
329 {
330         DEBUG_PRINT("%s on %s\n", __func__, ob->id.name);
331         BLI_assert(ob->type != OB_ARMATURE);
332         BKE_object_handle_data_update(eval_ctx, scene, ob);
333
334         switch (ob->type) {
335                 case OB_MESH:
336                         BKE_mesh_batch_cache_dirty(ob->data, BKE_MESH_BATCH_DIRTY_ALL);
337                         break;
338                 case OB_LATTICE:
339                         BKE_lattice_batch_cache_dirty(ob->data, BKE_LATTICE_BATCH_DIRTY_ALL);
340                         break;
341                 case OB_CURVE:
342                 case OB_FONT:
343                 case OB_SURF:
344                         BKE_curve_batch_cache_dirty(ob->data, BKE_CURVE_BATCH_DIRTY_ALL);
345                         break;
346         }
347
348         ob->recalc &= ~(OB_RECALC_DATA | OB_RECALC_TIME);
349 }
350
351 void BKE_object_eval_cloth(EvaluationContext *UNUSED(eval_ctx), Scene *scene, Object *object)
352 {
353         DEBUG_PRINT("%s on %s\n", __func__, object->id.name);
354         BKE_ptcache_object_reset(scene, object, PTCACHE_RESET_DEPSGRAPH);
355 }