Cleanup: remove redundant doxygen \file argument
[blender.git] / source / blender / blenkernel / intern / object_update.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 20014 by Blender Foundation.
17  * All rights reserved.
18  */
19
20 /** \file \ingroup bke
21  */
22
23 #include "DNA_anim_types.h"
24 #include "DNA_collection_types.h"
25 #include "DNA_constraint_types.h"
26 #include "DNA_key_types.h"
27 #include "DNA_material_types.h"
28 #include "DNA_mesh_types.h"
29 #include "DNA_scene_types.h"
30
31 #include "BLI_blenlib.h"
32 #include "BLI_utildefines.h"
33 #include "BLI_math.h"
34
35 #include "BKE_animsys.h"
36 #include "BKE_armature.h"
37 #include "BKE_action.h"
38 #include "BKE_constraint.h"
39 #include "BKE_curve.h"
40 #include "BKE_DerivedMesh.h"
41 #include "BKE_displist.h"
42 #include "BKE_editmesh.h"
43 #include "BKE_effect.h"
44 #include "BKE_image.h"
45 #include "BKE_key.h"
46 #include "BKE_lamp.h"
47 #include "BKE_lattice.h"
48 #include "BKE_material.h"
49 #include "BKE_mball.h"
50 #include "BKE_mesh.h"
51 #include "BKE_object.h"
52 #include "BKE_particle.h"
53 #include "BKE_pointcache.h"
54 #include "BKE_scene.h"
55 #include "BKE_gpencil.h"
56
57 #include "MEM_guardedalloc.h"
58
59 #include "DEG_depsgraph.h"
60 #include "DEG_depsgraph_query.h"
61
62 /**
63  * Restore the object->data to a non-modifier evaluated state.
64  *
65  * Some changes done directly in evaluated object require them to be reset
66  * before being re-evaluated.
67  * For example, we need to call this before BKE_mesh_new_from_object(),
68  * in case we removed/added modifiers in the evaluated object.
69  **/
70 void BKE_object_eval_reset(Object *ob_eval)
71 {
72         BKE_object_free_derived_caches(ob_eval);
73 }
74
75 void BKE_object_eval_local_transform(Depsgraph *depsgraph, Object *ob)
76 {
77         DEG_debug_print_eval(depsgraph, __func__, ob->id.name, ob);
78
79         /* calculate local matrix */
80         BKE_object_to_mat4(ob, ob->obmat);
81 }
82
83 /* Evaluate parent */
84 /* NOTE: based on solve_parenting(), but with the cruft stripped out */
85 void BKE_object_eval_parent(Depsgraph *depsgraph, 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         DEG_debug_print_eval(depsgraph, __func__, ob->id.name, ob);
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(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(Depsgraph *depsgraph,
116                                  Scene *scene,
117                                  Object *ob)
118 {
119         bConstraintOb *cob;
120         float ctime = BKE_scene_frame_get(scene);
121
122         DEG_debug_print_eval(depsgraph, __func__, ob->id.name, ob);
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(depsgraph, scene, ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
134         BKE_constraints_solve(depsgraph, &ob->constraints, cob, ctime);
135         BKE_constraints_clear_evalob(cob);
136 }
137
138 void BKE_object_eval_transform_final(Depsgraph *depsgraph, Object *ob)
139 {
140         DEG_debug_print_eval(depsgraph, __func__, ob->id.name, ob);
141         /* Make sure inverse matrix is always up to date. This way users of it
142          * do not need to worry about relcalculating it. */
143         invert_m4_m4(ob->imat, ob->obmat);
144         /* Set negative scale flag in object. */
145         if (is_negative_m4(ob->obmat)) ob->transflag |= OB_NEG_SCALE;
146         else ob->transflag &= ~OB_NEG_SCALE;
147 }
148
149 void BKE_object_handle_data_update(
150         Depsgraph *depsgraph,
151         Scene *scene,
152         Object *ob)
153 {
154         float ctime = BKE_scene_frame_get(scene);
155
156         DEG_debug_print_eval(depsgraph, __func__, ob->id.name, ob);
157
158         /* includes all keys and modifiers */
159         switch (ob->type) {
160                 case OB_MESH:
161                 {
162 #if 0
163                         BMEditMesh *em = (ob->mode & OB_MODE_EDIT) ? BKE_editmesh_from_object(ob) : NULL;
164 #else
165                         BMEditMesh *em = (ob->mode & OB_MODE_EDIT) ? ((Mesh *)ob->data)->edit_btmesh : NULL;
166                         if (em && em->ob != ob) {
167                                 em = NULL;
168                         }
169 #endif
170
171                         uint64_t data_mask = scene->customdata_mask | CD_MASK_BAREMESH;
172 #ifdef WITH_FREESTYLE
173                         /* make sure Freestyle edge/face marks appear in DM for render (see T40315) */
174                         if (DEG_get_mode(depsgraph) != DAG_EVAL_VIEWPORT) {
175                                 data_mask |= CD_MASK_FREESTYLE_EDGE | CD_MASK_FREESTYLE_FACE;
176                         }
177 #endif
178                         if (em) {
179                                 makeDerivedMesh(depsgraph, scene, ob, em,  data_mask, false); /* was CD_MASK_BAREMESH */
180                         }
181                         else {
182                                 makeDerivedMesh(depsgraph, scene, ob, NULL, data_mask, false);
183                         }
184                         break;
185                 }
186                 case OB_ARMATURE:
187                         if (ID_IS_LINKED(ob) && ob->proxy_from) {
188                                 if (BKE_pose_copy_result(ob->pose, ob->proxy_from->pose) == false) {
189                                         printf("Proxy copy error, lib Object: %s proxy Object: %s\n",
190                                                ob->id.name + 2, ob->proxy_from->id.name + 2);
191                                 }
192                         }
193                         else {
194                                 BKE_pose_where_is(depsgraph, scene, ob);
195                         }
196                         break;
197
198                 case OB_MBALL:
199                         BKE_displist_make_mball(depsgraph, scene, ob);
200                         break;
201
202                 case OB_CURVE:
203                 case OB_SURF:
204                 case OB_FONT:
205                         BKE_displist_make_curveTypes(depsgraph, scene, ob, false, false);
206                         break;
207
208                 case OB_LATTICE:
209                         BKE_lattice_modifiers_calc(depsgraph, scene, ob);
210                         break;
211
212                 case OB_EMPTY:
213                         if (ob->empty_drawtype == OB_EMPTY_IMAGE && ob->data)
214                                 if (BKE_image_is_animated(ob->data))
215                                         BKE_image_user_check_frame_calc(ob->iuser, (int)ctime);
216                         break;
217         }
218
219         /* particles */
220         if (!(ob->mode & OB_MODE_EDIT) && ob->particlesystem.first) {
221                 const bool use_render_params = (DEG_get_mode(depsgraph) == DAG_EVAL_RENDER);
222                 ParticleSystem *tpsys, *psys;
223                 ob->transflag &= ~OB_DUPLIPARTS;
224                 psys = ob->particlesystem.first;
225                 while (psys) {
226                         if (psys_check_enabled(ob, psys, use_render_params)) {
227                                 /* check use of dupli objects here */
228                                 if (psys->part && (psys->part->draw_as == PART_DRAW_REND || use_render_params) &&
229                                     ((psys->part->ren_as == PART_DRAW_OB && psys->part->dup_ob) ||
230                                      (psys->part->ren_as == PART_DRAW_GR && psys->part->dup_group)))
231                                 {
232                                         ob->transflag |= OB_DUPLIPARTS;
233                                 }
234
235                                 particle_system_update(depsgraph, scene, ob, psys, use_render_params);
236                                 psys = psys->next;
237                         }
238                         else if (psys->flag & PSYS_DELETE) {
239                                 tpsys = psys->next;
240                                 BLI_remlink(&ob->particlesystem, psys);
241                                 psys_free(ob, psys);
242                                 psys = tpsys;
243                         }
244                         else
245                                 psys = psys->next;
246                 }
247         }
248         BKE_object_eval_boundbox(depsgraph, ob);
249 }
250
251 /* TODO(sergey): Ensure that bounding box is already calculated, and move this
252  * into BKE_object_synchronize_to_original(). */
253 void BKE_object_eval_boundbox(Depsgraph *depsgraph, Object *object)
254 {
255         if (!DEG_is_active(depsgraph)) {
256                 return;
257         }
258         Object *ob_orig = DEG_get_original_object(object);
259         BoundBox *bb = BKE_object_boundbox_get(object);
260         if (bb != NULL) {
261                 if (ob_orig->bb == NULL) {
262                         ob_orig->bb = MEM_mallocN(sizeof(*ob_orig->bb), __func__);
263                 }
264                 *ob_orig->bb = *bb;
265         }
266 }
267
268 void BKE_object_synchronize_to_original(Depsgraph *depsgraph, Object *object)
269 {
270         if (!DEG_is_active(depsgraph)) {
271                 return;
272         }
273         Object *object_orig = DEG_get_original_object(object);
274         /* Base flags. */
275         object_orig->base_flag = object->base_flag;
276         /* Transformation flags. */
277         copy_m4_m4(object_orig->obmat, object->obmat);
278         copy_m4_m4(object_orig->imat, object->imat);
279         copy_m4_m4(object_orig->constinv, object->constinv);
280         object_orig->transflag = object->transflag;
281         object_orig->flag = object->flag;
282
283         /* Copy back error messages from modifiers. */
284         for (ModifierData *md = object->modifiers.first, *md_orig = object_orig->modifiers.first;
285              md != NULL && md_orig != NULL;
286              md = md->next, md_orig = md_orig->next)
287         {
288                 BLI_assert(md->type == md_orig->type && STREQ(md->name, md_orig->name));
289                 MEM_SAFE_FREE(md_orig->error);
290                 if (md->error != NULL) {
291                         md_orig->error = BLI_strdup(md->error);
292                 }
293         }
294 }
295
296 bool BKE_object_eval_proxy_copy(Depsgraph *depsgraph,
297                                 Object *object)
298 {
299         /* Handle proxy copy for target, */
300         if (ID_IS_LINKED(object) && object->proxy_from) {
301                 DEG_debug_print_eval(depsgraph, __func__, object->id.name, object);
302                 if (object->proxy_from->proxy_group) {
303                         /* Transform proxy into group space. */
304                         Object *obg = object->proxy_from->proxy_group;
305                         float imat[4][4];
306                         invert_m4_m4(imat, obg->obmat);
307                         mul_m4_m4m4(object->obmat, imat, object->proxy_from->obmat);
308                         /* Should always be true. */
309                         if (obg->dup_group) {
310                                 add_v3_v3(object->obmat[3], obg->dup_group->dupli_ofs);
311                         }
312                 }
313                 else {
314                         copy_m4_m4(object->obmat, object->proxy_from->obmat);
315                 }
316                 return true;
317         }
318         return false;
319 }
320
321 void BKE_object_eval_uber_transform(Depsgraph *depsgraph, Object *object)
322 {
323         BKE_object_eval_proxy_copy(depsgraph, object);
324 }
325
326 void BKE_object_batch_cache_dirty_tag(Object *ob)
327 {
328         switch (ob->type) {
329                 case OB_MESH:
330                         BKE_mesh_batch_cache_dirty_tag(ob->data, BKE_MESH_BATCH_DIRTY_ALL);
331                         break;
332                 case OB_LATTICE:
333                         BKE_lattice_batch_cache_dirty_tag(ob->data, BKE_LATTICE_BATCH_DIRTY_ALL);
334                         break;
335                 case OB_CURVE:
336                 case OB_FONT:
337                 case OB_SURF:
338                         BKE_curve_batch_cache_dirty_tag(ob->data, BKE_CURVE_BATCH_DIRTY_ALL);
339                         break;
340                 case OB_MBALL:
341                         BKE_mball_batch_cache_dirty_tag(ob->data, BKE_MBALL_BATCH_DIRTY_ALL);
342                         break;
343                 case OB_GPENCIL:
344                         BKE_gpencil_batch_cache_dirty_tag(ob->data);
345                         break;
346         }
347 }
348
349 void BKE_object_eval_uber_data(Depsgraph *depsgraph,
350                                Scene *scene,
351                                Object *ob)
352 {
353         DEG_debug_print_eval(depsgraph, __func__, ob->id.name, ob);
354         BLI_assert(ob->type != OB_ARMATURE);
355         BKE_object_handle_data_update(depsgraph, scene, ob);
356         BKE_object_batch_cache_dirty_tag(ob);
357 }
358
359 void BKE_object_eval_ptcache_reset(Depsgraph *depsgraph,
360                                    Scene *scene,
361                                    Object *object)
362 {
363         DEG_debug_print_eval(depsgraph, __func__, object->id.name, object);
364         BKE_ptcache_object_reset(scene, object, PTCACHE_RESET_DEPSGRAPH);
365 }
366
367 void BKE_object_eval_transform_all(Depsgraph *depsgraph,
368                                    Scene *scene,
369                                    Object *object)
370 {
371         /* This mimics full transform update chain from new depsgraph. */
372         BKE_object_eval_local_transform(depsgraph, object);
373         if (object->parent != NULL) {
374                 BKE_object_eval_parent(depsgraph, object);
375         }
376         if (!BLI_listbase_is_empty(&object->constraints)) {
377                 BKE_object_eval_constraints(depsgraph, scene, object);
378         }
379         BKE_object_eval_uber_transform(depsgraph, object);
380         BKE_object_eval_transform_final(depsgraph, object);
381 }
382
383 void BKE_object_eval_update_shading(Depsgraph *depsgraph, Object *object)
384 {
385         DEG_debug_print_eval(depsgraph, __func__, object->id.name, object);
386         if (object->type == OB_MESH) {
387                 BKE_mesh_batch_cache_dirty_tag(object->data, BKE_MESH_BATCH_DIRTY_SHADING);
388         }
389 }
390
391 void BKE_object_data_select_update(Depsgraph *depsgraph, ID *object_data)
392 {
393         DEG_debug_print_eval(depsgraph, __func__, object_data->name, object_data);
394         switch (GS(object_data->name)) {
395                 case ID_ME:
396                         BKE_mesh_batch_cache_dirty_tag(
397                                 (Mesh *)object_data,
398                                 BKE_MESH_BATCH_DIRTY_SELECT);
399                         break;
400                 case ID_CU:
401                         BKE_curve_batch_cache_dirty_tag(
402                                 (Curve *)object_data,
403                                 BKE_CURVE_BATCH_DIRTY_SELECT);
404                         break;
405                 case ID_LT:
406                         BKE_lattice_batch_cache_dirty_tag(
407                                 (struct Lattice *)object_data,
408                                 BKE_LATTICE_BATCH_DIRTY_SELECT);
409                         break;
410                 default:
411                         break;
412         }
413 }
414
415 void BKE_object_eval_flush_base_flags(Depsgraph *depsgraph,
416                                       Scene *scene, const int view_layer_index,
417                                       Object *object, int base_index,
418                                       const bool is_from_set)
419 {
420         /* TODO(sergey): Avoid list lookup. */
421         BLI_assert(view_layer_index >= 0);
422         ViewLayer *view_layer = BLI_findlink(&scene->view_layers, view_layer_index);
423         BLI_assert(view_layer != NULL);
424         BLI_assert(view_layer->object_bases_array != NULL);
425         BLI_assert(base_index >= 0);
426         BLI_assert(base_index < MEM_allocN_len(view_layer->object_bases_array) / sizeof(Base *));
427         Base *base = view_layer->object_bases_array[base_index];
428         BLI_assert(base->object == object);
429
430         DEG_debug_print_eval(depsgraph, __func__, object->id.name, object);
431
432         /* Copy flags and settings from base. */
433         object->base_flag = base->flag;
434         if (is_from_set) {
435                 object->base_flag |= BASE_FROM_SET;
436                 object->base_flag &= ~(BASE_SELECTED | BASE_SELECTABLE);
437         }
438         object->base_local_view_bits = base->local_view_bits;
439
440         if (object->mode == OB_MODE_PARTICLE_EDIT) {
441                 for (ParticleSystem *psys = object->particlesystem.first;
442                      psys != NULL;
443                      psys = psys->next)
444                 {
445                         BKE_particle_batch_cache_dirty_tag(psys, BKE_PARTICLE_BATCH_DIRTY_ALL);
446                 }
447         }
448 }