85cf7d8560dabfbbc65a915a3390acd68246c465
[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_depsgraph.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_image.h"
61
62 #include "DEG_depsgraph.h"
63
64 #ifdef WITH_LEGACY_DEPSGRAPH
65 #  define DEBUG_PRINT if (!DEG_depsgraph_use_legacy() && G.debug & G_DEBUG_DEPSGRAPH_EVAL) printf
66 #else
67 #  define DEBUG_PRINT if (G.debug & G_DEBUG_DEPSGRAPH_EVAL) printf
68 #endif
69
70 static ThreadMutex material_lock = BLI_MUTEX_INITIALIZER;
71
72 void BKE_object_eval_local_transform(EvaluationContext *UNUSED(eval_ctx),
73                                      Object *ob)
74 {
75         DEBUG_PRINT("%s on %s\n", __func__, ob->id.name);
76
77         /* calculate local matrix */
78         BKE_object_to_mat4(ob, ob->obmat);
79 }
80
81 /* Evaluate parent */
82 /* NOTE: based on solve_parenting(), but with the cruft stripped out */
83 void BKE_object_eval_parent(EvaluationContext *UNUSED(eval_ctx),
84                             Scene *scene,
85                             Object *ob)
86 {
87         Object *par = ob->parent;
88
89         float totmat[4][4];
90         float tmat[4][4];
91         float locmat[4][4];
92
93         DEBUG_PRINT("%s on %s\n", __func__, ob->id.name);
94
95         /* get local matrix (but don't calculate it, as that was done already!) */
96         // XXX: redundant?
97         copy_m4_m4(locmat, ob->obmat);
98
99         /* get parent effect matrix */
100         BKE_object_get_parent_matrix(scene, ob, par, totmat);
101
102         /* total */
103         mul_m4_m4m4(tmat, totmat, ob->parentinv);
104         mul_m4_m4m4(ob->obmat, tmat, locmat);
105
106         /* origin, for help line */
107         if ((ob->partype & PARTYPE) == PARSKEL) {
108                 copy_v3_v3(ob->orig, par->obmat[3]);
109         }
110         else {
111                 copy_v3_v3(ob->orig, totmat[3]);
112         }
113 }
114
115 void BKE_object_eval_constraints(EvaluationContext *UNUSED(eval_ctx),
116                                  Scene *scene,
117                                  Object *ob)
118 {
119         bConstraintOb *cob;
120         float ctime = BKE_scene_frame_get(scene);
121
122         DEBUG_PRINT("%s on %s\n", __func__, ob->id.name);
123
124         /* evaluate constraints stack */
125         /* TODO: split this into:
126          * - pre (i.e. BKE_constraints_make_evalob, per-constraint (i.e.
127          * - inner body of BKE_constraints_solve),
128          * - post (i.e. BKE_constraints_clear_evalob)
129          *
130          * Not sure why, this is from Joshua - sergey
131          *
132          */
133         cob = BKE_constraints_make_evalob(scene, ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
134         BKE_constraints_solve(&ob->constraints, cob, ctime);
135         BKE_constraints_clear_evalob(cob);
136 }
137
138 void BKE_object_eval_done(EvaluationContext *UNUSED(eval_ctx), Object *ob)
139 {
140         DEBUG_PRINT("%s on %s\n", __func__, ob->id.name);
141
142         /* Set negative scale flag in object. */
143         if (is_negative_m4(ob->obmat)) ob->transflag |= OB_NEG_SCALE;
144         else ob->transflag &= ~OB_NEG_SCALE;
145 }
146
147 void BKE_object_handle_data_update(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_EVAL)
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(scene, ob, em,  data_mask, false); /* was CD_MASK_BAREMESH */
187                         }
188                         else {
189                                 makeDerivedMesh(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(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(scene, ob, 0);
213                         break;
214
215                 case OB_LATTICE:
216                         BKE_lattice_modifiers_calc(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         /* related materials */
227         /* XXX: without depsgraph tagging, this will always need to be run, which will be slow!
228          * However, not doing anything (or trying to hack around this lack) is not an option
229          * anymore, especially due to Cycles [#31834]
230          */
231         if (ob->totcol) {
232                 int a;
233                 if (ob->totcol != 0) {
234                         BLI_mutex_lock(&material_lock);
235                         for (a = 1; a <= ob->totcol; a++) {
236                                 Material *ma = give_current_material(ob, a);
237                                 if (ma) {
238                                         /* recursively update drivers for this material */
239                                         material_drivers_update(scene, ma, ctime);
240                                 }
241                         }
242                         BLI_mutex_unlock(&material_lock);
243                 }
244         }
245         else if (ob->type == OB_LAMP)
246                 lamp_drivers_update(scene, ob->data, ctime);
247
248         /* particles */
249         if (ob != scene->obedit && ob->particlesystem.first) {
250                 ParticleSystem *tpsys, *psys;
251                 DerivedMesh *dm;
252                 ob->transflag &= ~OB_DUPLIPARTS;
253                 psys = ob->particlesystem.first;
254                 while (psys) {
255                         /* ensure this update always happens even if psys is disabled */
256                         if (psys->recalc & PSYS_RECALC_TYPE) {
257                                 psys_changed_type(ob, psys);
258                         }
259
260                         if (psys_check_enabled(ob, psys, eval_ctx->mode == DAG_EVAL_RENDER)) {
261                                 /* check use of dupli objects here */
262                                 if (psys->part && (psys->part->draw_as == PART_DRAW_REND || eval_ctx->mode == DAG_EVAL_RENDER) &&
263                                     ((psys->part->ren_as == PART_DRAW_OB && psys->part->dup_ob) ||
264                                      (psys->part->ren_as == PART_DRAW_GR && psys->part->dup_group)))
265                                 {
266                                         ob->transflag |= OB_DUPLIPARTS;
267                                 }
268
269                                 particle_system_update(scene, ob, psys, (eval_ctx->mode == DAG_EVAL_RENDER));
270                                 psys = psys->next;
271                         }
272                         else if (psys->flag & PSYS_DELETE) {
273                                 tpsys = psys->next;
274                                 BLI_remlink(&ob->particlesystem, psys);
275                                 psys_free(ob, psys);
276                                 psys = tpsys;
277                         }
278                         else
279                                 psys = psys->next;
280                 }
281
282                 if (eval_ctx->mode == DAG_EVAL_RENDER && ob->transflag & OB_DUPLIPARTS) {
283                         /* this is to make sure we get render level duplis in groups:
284                          * the derivedmesh must be created before init_render_mesh,
285                          * since object_duplilist does dupliparticles before that */
286                         CustomDataMask data_mask = CD_MASK_BAREMESH | CD_MASK_MFACE | CD_MASK_MTFACE | CD_MASK_MCOL;
287                         dm = mesh_create_derived_render(scene, ob, data_mask);
288                         dm->release(dm);
289
290                         for (psys = ob->particlesystem.first; psys; psys = psys->next)
291                                 psys_get_modifier(ob, psys)->flag &= ~eParticleSystemFlag_psys_updated;
292                 }
293         }
294
295         /* quick cache removed */
296 }
297
298 bool BKE_object_eval_proxy_copy(EvaluationContext *UNUSED(eval_ctx),
299                                 Object *object)
300 {
301         /* Handle proxy copy for target, */
302         if (ID_IS_LINKED(object) && object->proxy_from) {
303                 if (object->proxy_from->proxy_group) {
304                         /* Transform proxy into group space. */
305                         Object *obg = object->proxy_from->proxy_group;
306                         float imat[4][4];
307                         invert_m4_m4(imat, obg->obmat);
308                         mul_m4_m4m4(object->obmat, imat, object->proxy_from->obmat);
309                         /* Should always be true. */
310                         if (obg->dup_group) {
311                                 add_v3_v3(object->obmat[3], obg->dup_group->dupli_ofs);
312                         }
313                 }
314                 else {
315                         copy_m4_m4(object->obmat, object->proxy_from->obmat);
316                 }
317                 return true;
318         }
319         return false;
320 }
321
322 void BKE_object_eval_uber_transform(EvaluationContext *eval_ctx, Object *object)
323 {
324         BKE_object_eval_proxy_copy(eval_ctx, object);
325         object->recalc &= ~(OB_RECALC_OB | OB_RECALC_TIME);
326         if (object->data == NULL) {
327                 object->recalc &= ~OB_RECALC_DATA;
328         }
329 }
330
331 void BKE_object_eval_uber_data(EvaluationContext *eval_ctx,
332                                Scene *scene,
333                                Object *ob)
334 {
335         DEBUG_PRINT("%s on %s\n", __func__, ob->id.name);
336         BLI_assert(ob->type != OB_ARMATURE);
337         BKE_object_handle_data_update(eval_ctx, scene, ob);
338
339         ob->recalc &= ~(OB_RECALC_DATA | OB_RECALC_TIME);
340 }
341
342 void BKE_object_eval_cloth(EvaluationContext *UNUSED(eval_ctx),
343                            Scene *scene,
344                            Object *object)
345 {
346         DEBUG_PRINT("%s on %s\n", __func__, object->id.name);
347         BKE_ptcache_object_reset(scene, object, PTCACHE_RESET_DEPSGRAPH);
348 }
349
350 void BKE_object_eval_transform_all(EvaluationContext *eval_ctx,
351                                    Scene *scene,
352                                    Object *object)
353 {
354         /* This mimics full transform update chain from new depsgraph. */
355         BKE_object_eval_local_transform(eval_ctx, object);
356         if (object->parent != NULL) {
357                 BKE_object_eval_parent(eval_ctx, scene, object);
358         }
359         if (!BLI_listbase_is_empty(&object->constraints)) {
360                 BKE_object_eval_constraints(eval_ctx, scene, object);
361         }
362         BKE_object_eval_uber_transform(eval_ctx, object);
363         BKE_object_eval_done(eval_ctx, object);
364 }