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