852d8197a6c5e3113e5c389702799247dadf565a
[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(depsgraph, 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         if (DEG_is_active(depsgraph)) {
145                 Object *ob_orig = DEG_get_original_object(ob);
146                 copy_m4_m4(ob_orig->obmat, ob->obmat);
147                 ob_orig->transflag = ob->transflag;
148         }
149 }
150
151 void BKE_object_handle_data_update(
152         Depsgraph *depsgraph,
153         Scene *scene,
154         Object *ob)
155 {
156         ID *data_id = (ID *)ob->data;
157         AnimData *adt = BKE_animdata_from_id(data_id);
158         Key *key;
159         float ctime = BKE_scene_frame_get(scene);
160
161         if (G.debug & G_DEBUG_DEPSGRAPH_EVAL)
162                 printf("recalcdata %s\n", ob->id.name + 2);
163
164         /* TODO(sergey): Only used by legacy depsgraph. */
165         if (adt) {
166                 /* evaluate drivers - datalevel */
167                 /* XXX: for mesh types, should we push this to derivedmesh instead? */
168                 BKE_animsys_evaluate_animdata(depsgraph, scene, data_id, adt, ctime, ADT_RECALC_DRIVERS);
169         }
170
171         /* TODO(sergey): Only used by legacy depsgraph. */
172         key = BKE_key_from_object(ob);
173         if (key && key->block.first) {
174                 if (!(ob->shapeflag & OB_SHAPE_LOCK))
175                         BKE_animsys_evaluate_animdata(depsgraph, scene, &key->id, key->adt, ctime, ADT_RECALC_DRIVERS);
176         }
177
178         /* includes all keys and modifiers */
179         switch (ob->type) {
180                 case OB_MESH:
181                 {
182 #if 0
183                         BMEditMesh *em = (ob->mode & OB_MODE_EDIT) ? BKE_editmesh_from_object(ob) : NULL;
184 #else
185                         BMEditMesh *em = (ob->mode & OB_MODE_EDIT) ? ((Mesh *)ob->data)->edit_btmesh : NULL;
186                         if (em && em->ob != ob) {
187                                 em = NULL;
188                         }
189 #endif
190
191                         uint64_t data_mask = scene->customdata_mask | CD_MASK_BAREMESH;
192 #ifdef WITH_FREESTYLE
193                         /* make sure Freestyle edge/face marks appear in DM for render (see T40315) */
194                         if (DEG_get_mode(depsgraph) != DAG_EVAL_VIEWPORT) {
195                                 data_mask |= CD_MASK_FREESTYLE_EDGE | CD_MASK_FREESTYLE_FACE;
196                         }
197 #endif
198                         if (em) {
199                                 makeDerivedMesh(depsgraph, scene, ob, em,  data_mask, false); /* was CD_MASK_BAREMESH */
200                         }
201                         else {
202                                 makeDerivedMesh(depsgraph, scene, ob, NULL, data_mask, false);
203                         }
204                         break;
205                 }
206                 case OB_ARMATURE:
207                         if (ID_IS_LINKED(ob) && ob->proxy_from) {
208                                 if (BKE_pose_copy_result(ob->pose, ob->proxy_from->pose) == false) {
209                                         printf("Proxy copy error, lib Object: %s proxy Object: %s\n",
210                                                ob->id.name + 2, ob->proxy_from->id.name + 2);
211                                 }
212                         }
213                         else {
214                                 BKE_pose_where_is(depsgraph, scene, ob);
215                         }
216                         break;
217
218                 case OB_MBALL:
219                         BKE_displist_make_mball(depsgraph, scene, ob);
220                         break;
221
222                 case OB_CURVE:
223                 case OB_SURF:
224                 case OB_FONT:
225                         BKE_displist_make_curveTypes(depsgraph, scene, ob, 0);
226                         break;
227
228                 case OB_LATTICE:
229                         BKE_lattice_modifiers_calc(depsgraph, scene, ob);
230                         break;
231
232                 case OB_EMPTY:
233                         if (ob->empty_drawtype == OB_EMPTY_IMAGE && ob->data)
234                                 if (BKE_image_is_animated(ob->data))
235                                         BKE_image_user_check_frame_calc(ob->iuser, (int)ctime, 0);
236                         break;
237         }
238
239         /* particles */
240         if (!(ob->mode & OB_MODE_EDIT) && ob->particlesystem.first) {
241                 const bool use_render_params = (DEG_get_mode(depsgraph) == DAG_EVAL_RENDER);
242                 ParticleSystem *tpsys, *psys;
243                 DerivedMesh *dm;
244                 ob->transflag &= ~OB_DUPLIPARTS;
245                 psys = ob->particlesystem.first;
246                 while (psys) {
247                         if (psys_check_enabled(ob, psys, use_render_params)) {
248                                 /* check use of dupli objects here */
249                                 if (psys->part && (psys->part->draw_as == PART_DRAW_REND || use_render_params) &&
250                                     ((psys->part->ren_as == PART_DRAW_OB && psys->part->dup_ob) ||
251                                      (psys->part->ren_as == PART_DRAW_GR && psys->part->dup_group)))
252                                 {
253                                         ob->transflag |= OB_DUPLIPARTS;
254                                 }
255
256                                 particle_system_update(depsgraph, scene, ob, psys, use_render_params);
257                                 psys = psys->next;
258                         }
259                         else if (psys->flag & PSYS_DELETE) {
260                                 tpsys = psys->next;
261                                 BLI_remlink(&ob->particlesystem, psys);
262                                 psys_free(ob, psys);
263                                 psys = tpsys;
264                         }
265                         else
266                                 psys = psys->next;
267                 }
268
269                 if (use_render_params && ob->transflag & OB_DUPLIPARTS) {
270                         /* this is to make sure we get render level duplis in groups:
271                          * the derivedmesh must be created before init_render_mesh,
272                          * since object_duplilist does dupliparticles before that */
273                         CustomDataMask data_mask = CD_MASK_BAREMESH | CD_MASK_MFACE | CD_MASK_MTFACE | CD_MASK_MCOL;
274                         dm = mesh_create_derived_render(depsgraph, scene, ob, data_mask);
275                         dm->release(dm);
276
277                         for (psys = ob->particlesystem.first; psys; psys = psys->next)
278                                 psys_get_modifier(ob, psys)->flag &= ~eParticleSystemFlag_psys_updated;
279                 }
280         }
281
282         /* quick cache removed */
283 }
284
285 bool BKE_object_eval_proxy_copy(Depsgraph *depsgraph,
286                                 Object *object)
287 {
288         /* Handle proxy copy for target, */
289         if (ID_IS_LINKED(object) && object->proxy_from) {
290                 DEG_debug_print_eval(depsgraph, __func__, object->id.name, object);
291                 if (object->proxy_from->proxy_group) {
292                         /* Transform proxy into group space. */
293                         Object *obg = object->proxy_from->proxy_group;
294                         float imat[4][4];
295                         invert_m4_m4(imat, obg->obmat);
296                         mul_m4_m4m4(object->obmat, imat, object->proxy_from->obmat);
297                         /* Should always be true. */
298                         if (obg->dup_group) {
299                                 add_v3_v3(object->obmat[3], obg->dup_group->dupli_ofs);
300                         }
301                 }
302                 else {
303                         copy_m4_m4(object->obmat, object->proxy_from->obmat);
304                 }
305                 return true;
306         }
307         return false;
308 }
309
310 void BKE_object_eval_uber_transform(Depsgraph *depsgraph, Object *object)
311 {
312         BKE_object_eval_proxy_copy(depsgraph, object);
313 }
314
315 void BKE_object_eval_uber_data(Depsgraph *depsgraph,
316                                Scene *scene,
317                                Object *ob)
318 {
319         DEG_debug_print_eval(depsgraph, __func__, ob->id.name, ob);
320         BLI_assert(ob->type != OB_ARMATURE);
321         BKE_object_handle_data_update(depsgraph, scene, ob);
322
323         switch (ob->type) {
324                 case OB_MESH:
325                         BKE_mesh_batch_cache_dirty(ob->data, BKE_MESH_BATCH_DIRTY_ALL);
326                         break;
327                 case OB_LATTICE:
328                         BKE_lattice_batch_cache_dirty(ob->data, BKE_LATTICE_BATCH_DIRTY_ALL);
329                         break;
330                 case OB_CURVE:
331                 case OB_FONT:
332                 case OB_SURF:
333                         BKE_curve_batch_cache_dirty(ob->data, BKE_CURVE_BATCH_DIRTY_ALL);
334                         break;
335                 case OB_MBALL:
336                         BKE_mball_batch_cache_dirty(ob->data, BKE_MBALL_BATCH_DIRTY_ALL);
337                         break;
338         }
339 }
340
341 void BKE_object_eval_cloth(Depsgraph *depsgraph,
342                            Scene *scene,
343                            Object *object)
344 {
345         DEG_debug_print_eval(depsgraph, __func__, object->id.name, object);
346         BKE_ptcache_object_reset(scene, object, PTCACHE_RESET_DEPSGRAPH);
347 }
348
349 void BKE_object_eval_transform_all(Depsgraph *depsgraph,
350                                    Scene *scene,
351                                    Object *object)
352 {
353         /* This mimics full transform update chain from new depsgraph. */
354         BKE_object_eval_local_transform(depsgraph, object);
355         if (object->parent != NULL) {
356                 BKE_object_eval_parent(depsgraph, scene, object);
357         }
358         if (!BLI_listbase_is_empty(&object->constraints)) {
359                 BKE_object_eval_constraints(depsgraph, scene, object);
360         }
361         BKE_object_eval_uber_transform(depsgraph, object);
362         BKE_object_eval_done(depsgraph, object);
363 }
364
365 void BKE_object_eval_update_shading(Depsgraph *depsgraph, Object *object)
366 {
367         DEG_debug_print_eval(depsgraph, __func__, object->id.name, object);
368         if (object->type == OB_MESH) {
369                 BKE_mesh_batch_cache_dirty(object->data, BKE_MESH_BATCH_DIRTY_SHADING);
370         }
371 }
372
373 void BKE_object_data_select_update(Depsgraph *depsgraph, ID *object_data)
374 {
375         DEG_debug_print_eval(depsgraph, __func__, object_data->name, object_data);
376         switch (GS(object_data->name)) {
377                 case ID_ME:
378                         BKE_mesh_batch_cache_dirty((Mesh *)object_data,
379                                                    BKE_CURVE_BATCH_DIRTY_SELECT);
380                         break;
381                 case ID_CU:
382                         BKE_curve_batch_cache_dirty((Curve *)object_data,
383                                                     BKE_CURVE_BATCH_DIRTY_SELECT);
384                         break;
385                 case ID_LT:
386                         BKE_lattice_batch_cache_dirty((struct Lattice *)object_data,
387                                                       BKE_CURVE_BATCH_DIRTY_SELECT);
388                         break;
389                 default:
390                         break;
391         }
392 }
393
394 void BKE_object_eval_flush_base_flags(Depsgraph *depsgraph,
395                                       Scene *scene, const int view_layer_index,
396                                       Object *object, int base_index,
397                                       const bool is_from_set)
398 {
399         /* TODO(sergey): Avoid list lookup. */
400         BLI_assert(view_layer_index >= 0);
401         ViewLayer *view_layer = BLI_findlink(&scene->view_layers, view_layer_index);
402         BLI_assert(view_layer != NULL);
403         BLI_assert(view_layer->object_bases_array != NULL);
404         BLI_assert(base_index >= 0);
405         BLI_assert(base_index < MEM_allocN_len(view_layer->object_bases_array) / sizeof(Base *));
406         Base *base = view_layer->object_bases_array[base_index];
407         BLI_assert(base->object == object);
408
409         DEG_debug_print_eval(depsgraph, __func__, object->id.name, object);
410
411         /* Copy flags and settings from base. */
412         object->base_flag = base->flag;
413         if (is_from_set) {
414                 object->base_flag |= BASE_FROM_SET;
415                 object->base_flag &= ~(BASE_SELECTED | BASE_SELECTABLED);
416         }
417
418         if (object->mode == OB_MODE_PARTICLE_EDIT) {
419                 for (ParticleSystem *psys = object->particlesystem.first;
420                      psys != NULL;
421                      psys = psys->next)
422                 {
423                         BKE_particle_batch_cache_dirty(psys, BKE_PARTICLE_BATCH_DIRTY_ALL);
424                 }
425         }
426 }