2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2004 Blender Foundation.
19 * All rights reserved.
21 * Contributor(s): none yet.
23 * ***** END GPL LICENSE BLOCK *****
26 /** \file blender/blenkernel/intern/depsgraph.c
36 #include "MEM_guardedalloc.h"
39 # include "BLI_winstuff.h"
42 #include "BLI_utildefines.h"
43 #include "BLI_listbase.h"
44 #include "BLI_ghash.h"
45 #include "BLI_threads.h"
47 #include "DNA_anim_types.h"
48 #include "DNA_camera_types.h"
49 #include "DNA_group_types.h"
50 #include "DNA_lamp_types.h"
51 #include "DNA_lattice_types.h"
52 #include "DNA_key_types.h"
53 #include "DNA_material_types.h"
54 #include "DNA_mesh_types.h"
55 #include "DNA_node_types.h"
56 #include "DNA_scene_types.h"
57 #include "DNA_screen_types.h"
58 #include "DNA_windowmanager_types.h"
59 #include "DNA_movieclip_types.h"
60 #include "DNA_mask_types.h"
63 #include "BKE_animsys.h"
64 #include "BKE_action.h"
65 #include "BKE_DerivedMesh.h"
66 #include "BKE_effect.h"
67 #include "BKE_fcurve.h"
68 #include "BKE_global.h"
69 #include "BKE_image.h"
71 #include "BKE_library.h"
74 #include "BKE_material.h"
75 #include "BKE_mball.h"
76 #include "BKE_modifier.h"
77 #include "BKE_object.h"
78 #include "BKE_paint.h"
79 #include "BKE_particle.h"
80 #include "BKE_pointcache.h"
81 #include "BKE_scene.h"
82 #include "BKE_screen.h"
83 #include "BKE_tracking.h"
85 #include "GPU_buffers.h"
87 #include "atomic_ops.h"
89 #include "depsgraph_private.h"
91 static SpinLock threaded_update_lock;
95 BLI_spin_init(&threaded_update_lock);
100 BLI_spin_end(&threaded_update_lock);
103 /* Queue and stack operations for dag traversal
105 * the queue store a list of freenodes to avoid successive alloc/dealloc
108 DagNodeQueue *queue_create(int slots)
111 DagNodeQueueElem *elem;
114 queue = MEM_mallocN(sizeof(DagNodeQueue), "DAG queue");
115 queue->freenodes = MEM_mallocN(sizeof(DagNodeQueue), "DAG queue");
118 queue->first = queue->last = NULL;
119 elem = MEM_mallocN(sizeof(DagNodeQueueElem), "DAG queue elem3");
122 queue->freenodes->first = queue->freenodes->last = elem;
124 for (i = 1; i < slots; i++) {
125 elem = MEM_mallocN(sizeof(DagNodeQueueElem), "DAG queue elem4");
128 queue->freenodes->last->next = elem;
129 queue->freenodes->last = elem;
131 queue->freenodes->count = slots;
135 void queue_raz(DagNodeQueue *queue)
137 DagNodeQueueElem *elem;
140 if (queue->freenodes->last)
141 queue->freenodes->last->next = elem;
143 queue->freenodes->first = queue->freenodes->last = elem;
146 queue->freenodes->count++;
150 queue->freenodes->count++;
152 queue->freenodes->last = elem;
156 void queue_delete(DagNodeQueue *queue)
158 DagNodeQueueElem *elem;
159 DagNodeQueueElem *temp;
168 elem = queue->freenodes->first;
175 MEM_freeN(queue->freenodes);
179 /* insert in queue, remove in front */
180 void push_queue(DagNodeQueue *queue, DagNode *node)
182 DagNodeQueueElem *elem;
186 fprintf(stderr, "pushing null node\n");
189 /*fprintf(stderr, "BFS push : %s %d\n", ((ID *) node->ob)->name, queue->count);*/
191 elem = queue->freenodes->first;
193 queue->freenodes->first = elem->next;
194 if (queue->freenodes->last == elem) {
195 queue->freenodes->last = NULL;
196 queue->freenodes->first = NULL;
198 queue->freenodes->count--;
200 else { /* alllocating more */
201 elem = MEM_mallocN(sizeof(DagNodeQueueElem), "DAG queue elem1");
204 queue->freenodes->first = queue->freenodes->last = elem;
206 for (i = 1; i < DAGQUEUEALLOC; i++) {
207 elem = MEM_mallocN(sizeof(DagNodeQueueElem), "DAG queue elem2");
210 queue->freenodes->last->next = elem;
211 queue->freenodes->last = elem;
213 queue->freenodes->count = DAGQUEUEALLOC;
215 elem = queue->freenodes->first;
216 queue->freenodes->first = elem->next;
220 if (queue->last != NULL)
221 queue->last->next = elem;
223 if (queue->first == NULL) {
230 /* insert in front, remove in front */
231 void push_stack(DagNodeQueue *queue, DagNode *node)
233 DagNodeQueueElem *elem;
236 elem = queue->freenodes->first;
238 queue->freenodes->first = elem->next;
239 if (queue->freenodes->last == elem) {
240 queue->freenodes->last = NULL;
241 queue->freenodes->first = NULL;
243 queue->freenodes->count--;
245 else { /* alllocating more */
246 elem = MEM_mallocN(sizeof(DagNodeQueueElem), "DAG queue elem1");
249 queue->freenodes->first = queue->freenodes->last = elem;
251 for (i = 1; i < DAGQUEUEALLOC; i++) {
252 elem = MEM_mallocN(sizeof(DagNodeQueueElem), "DAG queue elem2");
255 queue->freenodes->last->next = elem;
256 queue->freenodes->last = elem;
258 queue->freenodes->count = DAGQUEUEALLOC;
260 elem = queue->freenodes->first;
261 queue->freenodes->first = elem->next;
263 elem->next = queue->first;
266 if (queue->last == NULL)
272 DagNode *pop_queue(DagNodeQueue *queue)
274 DagNodeQueueElem *elem;
279 queue->first = elem->next;
280 if (queue->last == elem) {
285 if (queue->freenodes->last)
286 queue->freenodes->last->next = elem;
287 queue->freenodes->last = elem;
288 if (queue->freenodes->first == NULL)
289 queue->freenodes->first = elem;
293 queue->freenodes->count++;
297 fprintf(stderr, "return null\n");
302 DagNode *get_top_node_queue(DagNodeQueue *queue)
304 return queue->first->node;
307 DagForest *dag_init(void)
310 /* use callocN to init all zero */
311 forest = MEM_callocN(sizeof(DagForest), "DAG root");
312 forest->ugly_hack_sorry = true;
316 /* isdata = object data... */
317 /* XXX this needs to be extended to be more flexible (so that not only objects are evaluated via depsgraph)... */
318 static void dag_add_driver_relation(AnimData *adt, DagForest *dag, DagNode *node, int isdata)
323 for (fcu = adt->drivers.first; fcu; fcu = fcu->next) {
324 ChannelDriver *driver = fcu->driver;
326 int isdata_fcu = (isdata) || (fcu->rna_path && strstr(fcu->rna_path, "modifiers["));
328 /* loop over variables to get the target relationships */
329 for (dvar = driver->variables.first; dvar; dvar = dvar->next) {
330 /* only used targets */
331 DRIVER_TARGETS_USED_LOOPER(dvar)
334 /* FIXME: other data types need to be added here so that they can work! */
335 if (GS(dtar->id->name) == ID_OB) {
336 Object *ob = (Object *)dtar->id;
338 /* normal channel-drives-channel */
339 node1 = dag_get_node(dag, dtar->id);
341 /* check if bone... */
342 if ((ob->type == OB_ARMATURE) &&
343 ( ((dtar->rna_path) && strstr(dtar->rna_path, "pose.bones[")) ||
344 ((dtar->flag & DTAR_FLAG_STRUCT_REF) && (dtar->pchan_name[0])) ))
346 dag_add_relation(dag, node1, node, isdata_fcu ? DAG_RL_DATA_DATA : DAG_RL_DATA_OB, "Driver");
348 /* check if ob data */
349 else if (dtar->rna_path && strstr(dtar->rna_path, "data."))
350 dag_add_relation(dag, node1, node, isdata_fcu ? DAG_RL_DATA_DATA : DAG_RL_DATA_OB, "Driver");
353 dag_add_relation(dag, node1, node, isdata_fcu ? DAG_RL_OB_DATA : DAG_RL_OB_OB, "Driver");
357 DRIVER_TARGETS_LOOPER_END
362 /* XXX: forward def for material driver handling... */
363 static void dag_add_material_driver_relations(DagForest *dag, DagNode *node, Material *ma);
365 /* recursive handling for shader nodetree drivers */
366 static void dag_add_shader_nodetree_driver_relations(DagForest *dag, DagNode *node, bNodeTree *ntree)
370 /* nodetree itself */
372 dag_add_driver_relation(ntree->adt, dag, node, 1);
375 /* nodetree's nodes... */
376 for (n = ntree->nodes.first; n; n = n->next) {
378 if (GS(n->id->name) == ID_MA) {
379 dag_add_material_driver_relations(dag, node, (Material *)n->id);
381 else if (n->type == NODE_GROUP) {
382 dag_add_shader_nodetree_driver_relations(dag, node, (bNodeTree *)n->id);
388 /* recursive handling for material drivers */
389 static void dag_add_material_driver_relations(DagForest *dag, DagNode *node, Material *ma)
391 /* Prevent infinite recursion by checking (and tagging the material) as having been visited
392 * already (see build_dag()). This assumes ma->id.flag & LIB_DOIT isn't set by anything else
393 * in the meantime... [#32017]
395 if (ma->id.flag & LIB_DOIT)
398 ma->id.flag |= LIB_DOIT;
400 /* material itself */
402 dag_add_driver_relation(ma->adt, dag, node, 1);
406 //dag_add_texture_driver_relations(DagForest *dag, DagNode *node, ID *id);
408 /* material's nodetree */
410 dag_add_shader_nodetree_driver_relations(dag, node, ma->nodetree);
412 ma->id.flag &= ~LIB_DOIT;
415 /* recursive handling for lamp drivers */
416 static void dag_add_lamp_driver_relations(DagForest *dag, DagNode *node, Lamp *la)
418 /* Prevent infinite recursion by checking (and tagging the lamp) as having been visited
419 * already (see build_dag()). This assumes la->id.flag & LIB_DOIT isn't set by anything else
420 * in the meantime... [#32017]
422 if (la->id.flag & LIB_DOIT)
425 la->id.flag |= LIB_DOIT;
429 dag_add_driver_relation(la->adt, dag, node, 1);
433 //dag_add_texture_driver_relations(DagForest *dag, DagNode *node, ID *id);
435 /* lamp's nodetree */
437 dag_add_shader_nodetree_driver_relations(dag, node, la->nodetree);
439 la->id.flag &= ~LIB_DOIT;
442 static void check_and_create_collision_relation(DagForest *dag, Object *ob, DagNode *node, Object *ob1, int skip_forcefield, bool no_collision)
445 if (ob1->pd && (ob1->pd->deflect || ob1->pd->forcefield) && (ob1 != ob)) {
446 if ((skip_forcefield && ob1->pd->forcefield == skip_forcefield) || (no_collision && ob1->pd->forcefield == 0))
448 node2 = dag_get_node(dag, ob1);
449 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "Field Collision");
453 static void dag_add_collision_field_relation(DagForest *dag, Scene *scene, Object *ob, DagNode *node, int skip_forcefield, bool no_collision)
456 ParticleSystem *particle_system;
458 for (particle_system = ob->particlesystem.first;
460 particle_system = particle_system->next)
462 EffectorWeights *effector_weights = particle_system->part->effector_weights;
463 if (effector_weights->group) {
464 GroupObject *group_object;
466 for (group_object = effector_weights->group->gobject.first;
468 group_object = group_object->next)
470 if ((group_object->ob->lay & ob->lay)) {
471 check_and_create_collision_relation(dag, ob, node, group_object->ob, skip_forcefield, no_collision);
477 /* would be nice to have a list of colliders here
478 * so for now walk all objects in scene check 'same layer rule' */
479 for (base = scene->base.first; base; base = base->next) {
480 if ((base->lay & ob->lay)) {
481 Object *ob1 = base->object;
482 check_and_create_collision_relation(dag, ob, node, ob1, skip_forcefield, no_collision);
487 static void build_dag_object(DagForest *dag, DagNode *scenenode, Main *bmain, Scene *scene, Object *ob, int mask)
494 ParticleSystem *psys;
497 node = dag_get_node(dag, ob);
499 if ((ob->data) && (mask & DAG_RL_DATA)) {
500 node2 = dag_get_node(dag, ob->data);
501 dag_add_relation(dag, node, node2, DAG_RL_DATA, "Object-Data Relation");
502 node2->first_ancestor = ob;
503 node2->ancestor_count += 1;
506 /* also build a custom data mask for dependencies that need certain layers */
507 node->customdata_mask = 0;
509 if (ob->type == OB_ARMATURE) {
513 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
514 for (con = pchan->constraints.first; con; con = con->next) {
515 const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_get(con);
516 ListBase targets = {NULL, NULL};
517 bConstraintTarget *ct;
519 if (cti && cti->get_constraint_targets) {
520 cti->get_constraint_targets(con, &targets);
522 for (ct = targets.first; ct; ct = ct->next) {
523 if (ct->tar && ct->tar != ob) {
524 // fprintf(stderr, "armature %s target :%s\n", ob->id.name, target->id.name);
525 node3 = dag_get_node(dag, ct->tar);
527 if (ct->subtarget[0]) {
528 dag_add_relation(dag, node3, node, DAG_RL_OB_DATA | DAG_RL_DATA_DATA, cti->name);
529 if (ct->tar->type == OB_MESH)
530 node3->customdata_mask |= CD_MASK_MDEFORMVERT;
532 else if (ELEM(con->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO, CONSTRAINT_TYPE_SPLINEIK))
533 dag_add_relation(dag, node3, node, DAG_RL_DATA_DATA | DAG_RL_OB_DATA, cti->name);
535 dag_add_relation(dag, node3, node, DAG_RL_OB_DATA, cti->name);
539 if (cti->flush_constraint_targets)
540 cti->flush_constraint_targets(con, &targets, 1);
548 /* driver dependencies, nla modifiers */
549 #if 0 // XXX old animation system
550 if (ob->nlastrips.first) {
552 bActionChannel *chan;
553 for (strip = ob->nlastrips.first; strip; strip = strip->next) {
554 if (strip->modifiers.first) {
555 bActionModifier *amod;
556 for (amod = strip->modifiers.first; amod; amod = amod->next) {
558 node2 = dag_get_node(dag, amod->ob);
559 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "NLA Strip Modifier");
565 #endif // XXX old animation system
567 dag_add_driver_relation(ob->adt, dag, node, (ob->type == OB_ARMATURE)); // XXX isdata arg here doesn't give an accurate picture of situation
569 key = BKE_key_from_object(ob);
571 dag_add_driver_relation(key->adt, dag, node, 1);
573 if (ob->modifiers.first) {
576 for (md = ob->modifiers.first; md; md = md->next) {
577 const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
579 if (mti->updateDepgraph) mti->updateDepgraph(md, dag, bmain, scene, ob, node);
583 node2 = dag_get_node(dag, ob->parent);
585 switch (ob->partype) {
587 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA | DAG_RL_OB_OB, "Parent");
589 case PARVERT1: case PARVERT3:
590 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB | DAG_RL_OB_OB, "Vertex Parent");
591 node2->customdata_mask |= CD_MASK_ORIGINDEX;
594 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB | DAG_RL_OB_OB, "Bone Parent");
597 if (ob->parent->type == OB_LATTICE)
598 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA | DAG_RL_OB_OB, "Lattice Parent");
599 else if (ob->parent->type == OB_CURVE) {
600 Curve *cu = ob->parent->data;
601 if (cu->flag & CU_PATH)
602 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB | DAG_RL_OB_OB, "Curve Parent");
604 dag_add_relation(dag, node2, node, DAG_RL_OB_OB, "Curve Parent");
607 dag_add_relation(dag, node2, node, DAG_RL_OB_OB, "Parent");
610 /* exception case: parent is duplivert */
611 if (ob->type == OB_MBALL && (ob->parent->transflag & OB_DUPLIVERTS)) {
612 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA | DAG_RL_OB_OB, "Duplivert");
618 node2 = dag_get_node(dag, ob->proxy);
619 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA | DAG_RL_OB_OB, "Proxy");
620 /* inverted relation, so addtoroot shouldn't be set to zero */
623 if (ob->transflag & OB_DUPLI) {
624 if ((ob->transflag & OB_DUPLIGROUP) && ob->dup_group) {
626 for (go = ob->dup_group->gobject.first; go; go = go->next) {
628 node2 = dag_get_node(dag, go->ob);
629 /* node2 changes node1, this keeps animations updated in groups?? not logical? */
630 dag_add_relation(dag, node2, node, DAG_RL_OB_OB, "Dupligroup");
636 /* softbody collision */
637 if ((ob->type == OB_MESH) || (ob->type == OB_CURVE) || (ob->type == OB_LATTICE)) {
638 if (ob->particlesystem.first ||
639 modifiers_isModifierEnabled(ob, eModifierType_Softbody) ||
640 modifiers_isModifierEnabled(ob, eModifierType_Cloth) ||
641 modifiers_isModifierEnabled(ob, eModifierType_DynamicPaint))
643 dag_add_collision_field_relation(dag, scene, ob, node, 0, false); /* TODO: use effectorweight->group */
645 else if (modifiers_isModifierEnabled(ob, eModifierType_Smoke)) {
646 dag_add_collision_field_relation(dag, scene, ob, node, PFIELD_SMOKEFLOW, false);
648 else if (ob->rigidbody_object) {
649 dag_add_collision_field_relation(dag, scene, ob, node, 0, true);
653 /* object data drivers */
655 AnimData *adt = BKE_animdata_from_id((ID *)ob->data);
657 dag_add_driver_relation(adt, dag, node, 1);
660 /* object type/data relationships */
664 Camera *cam = (Camera *)ob->data;
667 node2 = dag_get_node(dag, cam->dof_ob);
668 dag_add_relation(dag, node2, node, DAG_RL_OB_OB, "Camera DoF");
674 Object *mom = BKE_mball_basis_find(scene, ob);
677 node2 = dag_get_node(dag, mom);
678 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "Metaball"); /* mom depends on children! */
685 Curve *cu = ob->data;
688 node2 = dag_get_node(dag, cu->bevobj);
689 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "Curve Bevel");
692 node2 = dag_get_node(dag, cu->taperobj);
693 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "Curve Taper");
695 if (ob->type == OB_FONT) {
696 /* Really rather dirty hack. needs to support font family to work
697 * reliably on render export.
699 * This totally mimics behavior of regular verts duplication with
700 * parenting. The only tricky thing here is to get list of objects
701 * used for the custom "font".
703 * This shouldn't harm so much because this code only runs on DAG
704 * rebuild and this feature is not that commonly used.
708 if (cu->family[0] != '\n') {
711 duplilist = object_duplilist(G.main->eval_ctx, scene, ob);
712 for (dob = duplilist->first; dob; dob = dob->next) {
713 node2 = dag_get_node(dag, dob->ob);
714 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "Object Font");
716 free_object_duplilist(duplilist);
719 if (cu->textoncurve) {
720 node2 = dag_get_node(dag, cu->textoncurve);
721 /* Text on curve requires path to be evaluated for the target curve. */
722 node2->eval_flags |= DAG_EVAL_NEED_CURVE_PATH;
723 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "Texture On Curve");
730 /* material drivers */
734 for (a = 1; a <= ob->totcol; a++) {
735 Material *ma = give_current_material(ob, a);
738 /* recursively figure out if there are drivers, and hook these up to this object */
739 dag_add_material_driver_relations(dag, node, ma);
743 else if (ob->type == OB_LAMP) {
744 dag_add_lamp_driver_relations(dag, node, ob->data);
748 psys = ob->particlesystem.first;
752 for (; psys; psys = psys->next) {
753 BoidRule *rule = NULL;
754 BoidState *state = NULL;
755 ParticleSettings *part = psys->part;
756 ListBase *effectors = NULL;
759 dag_add_relation(dag, node, node, DAG_RL_OB_DATA, "Particle-Object Relation");
761 if (!psys_check_enabled(ob, psys))
764 if (ELEM(part->phystype, PART_PHYS_KEYED, PART_PHYS_BOIDS)) {
765 ParticleTarget *pt = psys->targets.first;
767 for (; pt; pt = pt->next) {
768 if (pt->ob && BLI_findlink(&pt->ob->particlesystem, pt->psys - 1)) {
769 node2 = dag_get_node(dag, pt->ob);
770 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "Particle Targets");
775 if (part->ren_as == PART_DRAW_OB && part->dup_ob) {
776 node2 = dag_get_node(dag, part->dup_ob);
777 /* note that this relation actually runs in the wrong direction, the problem
778 * is that dupli system all have this (due to parenting), and the render
779 * engine instancing assumes particular ordering of objects in list */
780 dag_add_relation(dag, node, node2, DAG_RL_OB_OB, "Particle Object Visualization");
781 if (part->dup_ob->type == OB_MBALL)
782 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA, "Particle Object Visualization");
785 if (part->ren_as == PART_DRAW_GR && part->dup_group) {
786 for (go = part->dup_group->gobject.first; go; go = go->next) {
787 node2 = dag_get_node(dag, go->ob);
788 dag_add_relation(dag, node2, node, DAG_RL_OB_OB, "Particle Group Visualization");
792 effectors = pdInitEffectors(scene, ob, psys, part->effector_weights, false);
795 for (eff = effectors->first; eff; eff = eff->next) {
797 node2 = dag_get_node(dag, eff->ob);
798 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "Particle Field");
803 pdEndEffectors(&effectors);
806 for (state = part->boids->states.first; state; state = state->next) {
807 for (rule = state->rules.first; rule; rule = rule->next) {
808 Object *ruleob = NULL;
809 if (rule->type == eBoidRuleType_Avoid)
810 ruleob = ((BoidRuleGoalAvoid *)rule)->ob;
811 else if (rule->type == eBoidRuleType_FollowLeader)
812 ruleob = ((BoidRuleFollowLeader *)rule)->ob;
815 node2 = dag_get_node(dag, ruleob);
816 dag_add_relation(dag, node2, node, DAG_RL_OB_DATA, "Boid Rule");
824 /* object constraints */
825 for (con = ob->constraints.first; con; con = con->next) {
826 const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_get(con);
827 ListBase targets = {NULL, NULL};
828 bConstraintTarget *ct;
833 /* special case for camera tracking -- it doesn't use targets to define relations */
834 if (ELEM(cti->type, CONSTRAINT_TYPE_FOLLOWTRACK, CONSTRAINT_TYPE_CAMERASOLVER, CONSTRAINT_TYPE_OBJECTSOLVER)) {
835 int depends_on_camera = 0;
837 if (cti->type == CONSTRAINT_TYPE_FOLLOWTRACK) {
838 bFollowTrackConstraint *data = (bFollowTrackConstraint *)con->data;
840 if ((data->clip || data->flag & FOLLOWTRACK_ACTIVECLIP) && data->track[0])
841 depends_on_camera = 1;
843 if (data->depth_ob) {
844 node2 = dag_get_node(dag, data->depth_ob);
845 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB | DAG_RL_OB_OB, cti->name);
848 else if (cti->type == CONSTRAINT_TYPE_OBJECTSOLVER)
849 depends_on_camera = 1;
851 if (depends_on_camera && scene->camera) {
852 node2 = dag_get_node(dag, scene->camera);
853 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB | DAG_RL_OB_OB, cti->name);
856 dag_add_relation(dag, scenenode, node, DAG_RL_SCENE, "Scene Relation");
859 else if (cti->get_constraint_targets) {
860 cti->get_constraint_targets(con, &targets);
862 for (ct = targets.first; ct; ct = ct->next) {
870 node2 = dag_get_node(dag, obt);
871 if (ELEM(con->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO))
872 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB | DAG_RL_OB_OB, cti->name);
874 if (ELEM(obt->type, OB_ARMATURE, OB_MESH, OB_LATTICE) && (ct->subtarget[0])) {
875 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB | DAG_RL_OB_OB, cti->name);
876 if (obt->type == OB_MESH)
877 node2->customdata_mask |= CD_MASK_MDEFORMVERT;
880 dag_add_relation(dag, node2, node, DAG_RL_OB_OB, cti->name);
885 if (cti->flush_constraint_targets)
886 cti->flush_constraint_targets(con, &targets, 1);
891 dag_add_relation(dag, scenenode, node, DAG_RL_SCENE, "Scene Relation");
894 static void build_dag_group(DagForest *dag, DagNode *scenenode, Main *bmain, Scene *scene, Group *group, short mask)
898 if (group->id.flag & LIB_DOIT)
901 group->id.flag |= LIB_DOIT;
903 for (go = group->gobject.first; go; go = go->next) {
904 build_dag_object(dag, scenenode, bmain, scene, go->ob, mask);
905 if (go->ob->dup_group)
906 build_dag_group(dag, scenenode, bmain, scene, go->ob->dup_group, mask);
910 DagForest *build_dag(Main *bmain, Scene *sce, short mask)
927 /* clear "LIB_DOIT" flag from all materials, to prevent infinite recursion problems later [#32017] */
928 BKE_main_id_tag_idcode(bmain, ID_MA, false);
929 BKE_main_id_tag_idcode(bmain, ID_LA, false);
930 BKE_main_id_tag_idcode(bmain, ID_GR, false);
932 /* add base node for scene. scene is always the first node in DAG */
933 scenenode = dag_add_node(dag, sce);
935 /* add current scene objects */
936 for (base = sce->base.first; base; base = base->next) {
939 build_dag_object(dag, scenenode, bmain, sce, ob, mask);
941 build_dag_object(dag, scenenode, bmain, sce, ob->proxy, mask);
943 build_dag_group(dag, scenenode, bmain, sce, ob->dup_group, mask);
946 BKE_main_id_tag_idcode(bmain, ID_GR, false);
948 /* Now all relations were built, but we need to solve 1 exceptional case;
949 * When objects have multiple "parents" (for example parent + constraint working on same object)
950 * the relation type has to be synced. One of the parents can change, and should give same event to child */
952 /* nodes were callocced, so we can use node->color for temporal storage */
953 for (node = sce->theDag->DagNode.first; node; node = node->next) {
954 if (node->type == ID_OB) {
955 for (itA = node->child; itA; itA = itA->next) {
956 if (itA->node->type == ID_OB) {
957 itA->node->color |= itA->type;
961 /* also flush custom data mask */
962 ((Object *)node->ob)->customdata_mask = node->customdata_mask;
965 /* now set relations equal, so that when only one parent changes, the correct recalcs are found */
966 for (node = sce->theDag->DagNode.first; node; node = node->next) {
967 if (node->type == ID_OB) {
968 for (itA = node->child; itA; itA = itA->next) {
969 if (itA->node->type == ID_OB) {
970 itA->type |= itA->node->color;
976 /* cycle detection and solving */
977 // solve_cycles(dag);
983 void free_forest(DagForest *Dag)
984 { /* remove all nodes and deps */
988 DagNode *itN = Dag->DagNode.first;
1010 BLI_ghash_free(Dag->nodeHash, NULL, NULL);
1011 Dag->nodeHash = NULL;
1012 Dag->DagNode.first = NULL;
1013 Dag->DagNode.last = NULL;
1018 DagNode *dag_find_node(DagForest *forest, void *fob)
1020 if (forest->nodeHash)
1021 return BLI_ghash_lookup(forest->nodeHash, fob);
1026 static int dag_print_dependencies = 0; /* debugging */
1028 /* no checking of existence, use dag_find_node first or dag_get_node */
1029 DagNode *dag_add_node(DagForest *forest, void *fob)
1033 node = MEM_callocN(sizeof(DagNode), "DAG node");
1036 node->color = DAG_WHITE;
1038 if (forest->ugly_hack_sorry) node->type = GS(((ID *) fob)->name); /* sorry, done for pose sorting */
1039 if (forest->numNodes) {
1040 ((DagNode *) forest->DagNode.last)->next = node;
1041 forest->DagNode.last = node;
1045 forest->DagNode.last = node;
1046 forest->DagNode.first = node;
1047 forest->numNodes = 1;
1050 if (!forest->nodeHash)
1051 forest->nodeHash = BLI_ghash_ptr_new("dag_add_node gh");
1052 BLI_ghash_insert(forest->nodeHash, fob, node);
1058 DagNode *dag_get_node(DagForest *forest, void *fob)
1062 node = dag_find_node(forest, fob);
1064 node = dag_add_node(forest, fob);
1070 DagNode *dag_get_sub_node(DagForest *forest, void *fob)
1073 DagAdjList *mainchild, *prev = NULL;
1075 mainchild = ((DagNode *) forest->DagNode.first)->child;
1076 /* remove from first node (scene) adj list if present */
1078 if (mainchild->node == fob) {
1080 prev->next = mainchild->next;
1081 MEM_freeN(mainchild);
1085 ((DagNode *) forest->DagNode.first)->child = mainchild->next;
1086 MEM_freeN(mainchild);
1091 mainchild = mainchild->next;
1093 node = dag_find_node(forest, fob);
1095 node = dag_add_node(forest, fob);
1099 static void dag_add_parent_relation(DagForest *UNUSED(forest), DagNode *fob1, DagNode *fob2, short rel, const char *name)
1101 DagAdjList *itA = fob2->parent;
1103 while (itA) { /* search if relation exist already */
1104 if (itA->node == fob1) {
1111 /* create new relation and insert at head. MALLOC alert! */
1112 itA = MEM_mallocN(sizeof(DagAdjList), "DAG adj list");
1116 itA->next = fob2->parent;
1121 void dag_add_relation(DagForest *forest, DagNode *fob1, DagNode *fob2, short rel, const char *name)
1123 DagAdjList *itA = fob1->child;
1125 /* parent relation is for cycle checking */
1126 dag_add_parent_relation(forest, fob1, fob2, rel, name);
1128 while (itA) { /* search if relation exist already */
1129 if (itA->node == fob2) {
1136 /* create new relation and insert at head. MALLOC alert! */
1137 itA = MEM_mallocN(sizeof(DagAdjList), "DAG adj list");
1141 itA->next = fob1->child;
1146 static const char *dag_node_name(DagForest *dag, DagNode *node)
1148 if (node->ob == NULL)
1150 else if (dag->ugly_hack_sorry)
1151 return ((ID *)(node->ob))->name + 2;
1153 return ((bPoseChannel *)(node->ob))->name;
1156 static void dag_node_print_dependencies(DagForest *dag, DagNode *node)
1160 printf("%s depends on:\n", dag_node_name(dag, node));
1162 for (itA = node->parent; itA; itA = itA->next)
1163 printf(" %s through %s\n", dag_node_name(dag, itA->node), itA->name);
1167 static int dag_node_print_dependency_recurs(DagForest *dag, DagNode *node, DagNode *endnode)
1171 if (node->color == DAG_BLACK)
1174 node->color = DAG_BLACK;
1176 if (node == endnode)
1179 for (itA = node->parent; itA; itA = itA->next) {
1180 if (dag_node_print_dependency_recurs(dag, itA->node, endnode)) {
1181 printf(" %s depends on %s through %s.\n", dag_node_name(dag, node), dag_node_name(dag, itA->node), itA->name);
1189 static void dag_node_print_dependency_cycle(DagForest *dag, DagNode *startnode, DagNode *endnode, const char *name)
1193 for (node = dag->DagNode.first; node; node = node->next)
1194 node->color = DAG_WHITE;
1196 printf(" %s depends on %s through %s.\n", dag_node_name(dag, endnode), dag_node_name(dag, startnode), name);
1197 dag_node_print_dependency_recurs(dag, startnode, endnode);
1201 static int dag_node_recurs_level(DagNode *node, int level)
1206 node->color = DAG_BLACK; /* done */
1209 for (itA = node->parent; itA; itA = itA->next) {
1210 if (itA->node->color == DAG_WHITE) {
1211 itA->node->ancestor_count = dag_node_recurs_level(itA->node, level);
1212 newlevel = MAX2(newlevel, level + itA->node->ancestor_count);
1215 newlevel = MAX2(newlevel, level + itA->node->ancestor_count);
1221 static void dag_check_cycle(DagForest *dag)
1226 dag->is_acyclic = true;
1228 /* debugging print */
1229 if (dag_print_dependencies)
1230 for (node = dag->DagNode.first; node; node = node->next)
1231 dag_node_print_dependencies(dag, node);
1233 /* tag nodes unchecked */
1234 for (node = dag->DagNode.first; node; node = node->next)
1235 node->color = DAG_WHITE;
1237 for (node = dag->DagNode.first; node; node = node->next) {
1238 if (node->color == DAG_WHITE) {
1239 node->ancestor_count = dag_node_recurs_level(node, 0);
1243 /* check relations, and print errors */
1244 for (node = dag->DagNode.first; node; node = node->next) {
1245 for (itA = node->parent; itA; itA = itA->next) {
1246 if (itA->node->ancestor_count > node->ancestor_count) {
1247 if (node->ob && itA->node->ob) {
1248 dag->is_acyclic = false;
1249 printf("Dependency cycle detected:\n");
1250 dag_node_print_dependency_cycle(dag, itA->node, node, itA->name);
1256 /* parent relations are only needed for cycle checking, so free now */
1257 for (node = dag->DagNode.first; node; node = node->next) {
1258 while (node->parent) {
1259 itA = node->parent->next;
1260 MEM_freeN(node->parent);
1266 /* debug test functions */
1268 void graph_print_queue(DagNodeQueue *nqueue)
1270 DagNodeQueueElem *queueElem;
1272 queueElem = nqueue->first;
1274 fprintf(stderr, "** %s %i %i-%i ", ((ID *) queueElem->node->ob)->name, queueElem->node->color, queueElem->node->DFS_dvtm, queueElem->node->DFS_fntm);
1275 queueElem = queueElem->next;
1277 fprintf(stderr, "\n");
1280 void graph_print_queue_dist(DagNodeQueue *nqueue)
1282 DagNodeQueueElem *queueElem;
1285 queueElem = nqueue->first;
1288 fprintf(stderr, "** %25s %2.2i-%2.2i ", ((ID *) queueElem->node->ob)->name, queueElem->node->DFS_dvtm, queueElem->node->DFS_fntm);
1289 while (count < queueElem->node->DFS_dvtm - 1) { fputc(' ', stderr); count++; }
1291 while (count < queueElem->node->DFS_fntm - 2) { fputc('-', stderr); count++; }
1293 fputc('\n', stderr);
1295 queueElem = queueElem->next;
1297 fprintf(stderr, "\n");
1300 void graph_print_adj_list(DagForest *dag)
1305 node = dag->DagNode.first;
1307 fprintf(stderr, "node : %s col: %i", ((ID *) node->ob)->name, node->color);
1310 fprintf(stderr, "-- %s ", ((ID *) itA->node->ob)->name);
1314 fprintf(stderr, "\n");
1319 /* ************************ API *********************** */
1321 /* mechanism to allow editors to be informed of depsgraph updates,
1322 * to do their own updates based on changes... */
1323 static void (*EditorsUpdateIDCb)(Main *bmain, ID *id) = NULL;
1324 static void (*EditorsUpdateSceneCb)(Main *bmain, Scene *scene, int updated) = NULL;
1326 void DAG_editors_update_cb(void (*id_func)(Main *bmain, ID *id), void (*scene_func)(Main *bmain, Scene *scene, int updated))
1328 EditorsUpdateIDCb = id_func;
1329 EditorsUpdateSceneCb = scene_func;
1332 static void dag_editors_id_update(Main *bmain, ID *id)
1334 if (EditorsUpdateIDCb)
1335 EditorsUpdateIDCb(bmain, id);
1338 static void dag_editors_scene_update(Main *bmain, Scene *scene, int updated)
1340 if (EditorsUpdateSceneCb)
1341 EditorsUpdateSceneCb(bmain, scene, updated);
1344 /* groups with objects in this scene need to be put in the right order as well */
1345 static void scene_sort_groups(Main *bmain, Scene *sce)
1352 /* test; are group objects all in this scene? */
1353 for (ob = bmain->object.first; ob; ob = ob->id.next) {
1354 ob->id.flag &= ~LIB_DOIT;
1355 ob->id.newid = NULL; /* newid abuse for GroupObject */
1357 for (base = sce->base.first; base; base = base->next)
1358 base->object->id.flag |= LIB_DOIT;
1360 for (group = bmain->group.first; group; group = group->id.next) {
1361 for (go = group->gobject.first; go; go = go->next) {
1362 if ((go->ob->id.flag & LIB_DOIT) == 0)
1365 /* this group is entirely in this scene */
1367 ListBase listb = {NULL, NULL};
1369 for (go = group->gobject.first; go; go = go->next)
1370 go->ob->id.newid = (ID *)go;
1372 /* in order of sorted bases we reinsert group objects */
1373 for (base = sce->base.first; base; base = base->next) {
1375 if (base->object->id.newid) {
1376 go = (GroupObject *)base->object->id.newid;
1377 base->object->id.newid = NULL;
1378 BLI_remlink(&group->gobject, go);
1379 BLI_addtail(&listb, go);
1382 /* copy the newly sorted listbase */
1383 group->gobject = listb;
1388 /* free the depency graph */
1389 static void dag_scene_free(Scene *sce)
1392 free_forest(sce->theDag);
1393 MEM_freeN(sce->theDag);
1398 /* Chech whether object data needs to be evaluated before it
1399 * might be used by others.
1401 * Means that mesh object needs to have proper derivedFinal,
1402 * curves-typed objects are to have proper curve cache.
1404 * Other objects or objects which are tagged for data update are
1405 * not considered to be in need of evaluation.
1407 static bool check_object_needs_evaluation(Object *object)
1409 if (object->recalc & OB_RECALC_ALL) {
1410 /* Object is tagged for update anyway, no need to re-tag it. */
1414 if (object->type == OB_MESH) {
1415 return object->derivedFinal == NULL;
1417 else if (ELEM(object->type, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL, OB_LATTICE)) {
1418 return object->curve_cache == NULL;
1424 /* Check whether object data is tagged for update. */
1425 static bool check_object_tagged_for_update(Object *object)
1427 if (object->recalc & OB_RECALC_ALL) {
1431 if (ELEM(object->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL, OB_LATTICE)) {
1432 ID *data_id = object->data;
1433 return (data_id->flag & (LIB_ID_RECALC_DATA | LIB_ID_RECALC)) != 0;
1439 /* Flush changes from tagged objects in the scene to their
1440 * dependencies which are not evaluated yet.
1442 * This is needed to ensure all the dependencies are met
1443 * before objects gets handled by object_handle_update(),
1445 * This is needed when visible layers are changed or changing
1446 * scene graph layout which involved usage of objects which
1447 * aren't in the scene or weren't visible yet.
1449 static void dag_invisible_dependencies_flush(Scene *scene)
1451 DagNode *root_node = scene->theDag->DagNode.first, *node;
1452 DagNodeQueue *queue;
1454 for (node = root_node; node != NULL; node = node->next) {
1455 node->color = DAG_WHITE;
1458 queue = queue_create(DAGQUEUEALLOC);
1460 for (node = root_node; node != NULL; node = node->next) {
1461 if (node->color == DAG_WHITE) {
1462 push_stack(queue, node);
1463 node->color = DAG_GRAY;
1465 while (queue->count) {
1466 DagNode *current_node = get_top_node_queue(queue);
1470 for (itA = current_node->child; itA; itA = itA->next) {
1471 if (itA->node->color == DAG_WHITE) {
1472 itA->node->color = DAG_GRAY;
1473 push_stack(queue, itA->node);
1480 current_node = pop_queue(queue);
1482 if (current_node->type == ID_OB) {
1483 Object *current_object = current_node->ob;
1484 if (check_object_needs_evaluation(current_object)) {
1485 for (itA = current_node->child; itA; itA = itA->next) {
1486 if (itA->node->type == ID_OB) {
1487 Object *object = itA->node->ob;
1488 if (check_object_tagged_for_update(object)) {
1489 current_object->recalc |= OB_RECALC_OB | OB_RECALC_DATA;
1495 node->color = DAG_BLACK;
1501 queue_delete(queue);
1504 static void dag_invisible_dependencies_check_flush(Main *bmain, Scene *scene)
1506 if (DAG_id_type_tagged(bmain, ID_OB) ||
1507 DAG_id_type_tagged(bmain, ID_ME) || /* Mesh */
1508 DAG_id_type_tagged(bmain, ID_CU) || /* Curve */
1509 DAG_id_type_tagged(bmain, ID_MB) || /* MetaBall */
1510 DAG_id_type_tagged(bmain, ID_LT)) /* Lattice */
1512 dag_invisible_dependencies_flush(scene);
1516 /* sort the base list on dependency order */
1517 static void dag_scene_build(Main *bmain, Scene *sce)
1519 DagNode *node, *rootnode;
1520 DagNodeQueue *nqueue;
1527 BLI_listbase_clear(&tempbase);
1529 build_dag(bmain, sce, DAG_RL_ALL_BUT_DATA);
1531 dag_check_cycle(sce->theDag);
1533 nqueue = queue_create(DAGQUEUEALLOC);
1535 for (node = sce->theDag->DagNode.first; node; node = node->next) {
1536 node->color = DAG_WHITE;
1541 rootnode = sce->theDag->DagNode.first;
1542 rootnode->color = DAG_GRAY;
1544 push_stack(nqueue, rootnode);
1546 while (nqueue->count) {
1549 node = get_top_node_queue(nqueue);
1552 while (itA != NULL) {
1553 if (itA->node->color == DAG_WHITE) {
1554 itA->node->DFS_dvtm = time;
1555 itA->node->color = DAG_GRAY;
1558 push_stack(nqueue, itA->node);
1567 node = pop_queue(nqueue);
1568 if (node->ob == sce) /* we are done */
1570 node->color = DAG_BLACK;
1573 base = sce->base.first;
1574 while (base && base->object != node->ob)
1577 BLI_remlink(&sce->base, base);
1578 BLI_addhead(&tempbase, base);
1584 /* temporal correction for circular dependencies */
1585 base = sce->base.first;
1587 BLI_remlink(&sce->base, base);
1588 BLI_addhead(&tempbase, base);
1589 //if (G.debug & G_DEBUG)
1590 printf("cyclic %s\n", base->object->id.name);
1591 base = sce->base.first;
1594 sce->base = tempbase;
1595 queue_delete(nqueue);
1597 /* all groups with objects in this scene gets resorted too */
1598 scene_sort_groups(bmain, sce);
1600 if (G.debug & G_DEBUG) {
1601 printf("\nordered\n");
1602 for (base = sce->base.first; base; base = base->next) {
1603 printf(" %s\n", base->object->id.name);
1608 sce->recalc |= SCE_PRV_CHANGED; /* test for 3d preview */
1610 /* Make sure that new dependencies which came from invisble layers
1611 * are tagged for update (if they're needed for objects which were
1612 * tagged for update).
1614 dag_invisible_dependencies_check_flush(bmain, sce);
1617 /* clear all dependency graphs */
1618 void DAG_relations_tag_update(Main *bmain)
1622 for (sce = bmain->scene.first; sce; sce = sce->id.next)
1623 dag_scene_free(sce);
1626 /* rebuild dependency graph only for a given scene */
1627 void DAG_scene_relations_rebuild(Main *bmain, Scene *sce)
1629 dag_scene_free(sce);
1630 DAG_scene_relations_update(bmain, sce);
1633 /* create dependency graph if it was cleared or didn't exist yet */
1634 void DAG_scene_relations_update(Main *bmain, Scene *sce)
1637 dag_scene_build(bmain, sce);
1640 void DAG_scene_free(Scene *sce)
1643 free_forest(sce->theDag);
1644 MEM_freeN(sce->theDag);
1649 static void lib_id_recalc_tag(Main *bmain, ID *id)
1651 id->flag |= LIB_ID_RECALC;
1652 DAG_id_type_tag(bmain, GS(id->name));
1655 static void lib_id_recalc_data_tag(Main *bmain, ID *id)
1657 id->flag |= LIB_ID_RECALC_DATA;
1658 DAG_id_type_tag(bmain, GS(id->name));
1661 /* node was checked to have lasttime != curtime and is if type ID_OB */
1662 static void flush_update_node(Main *bmain, DagNode *node, unsigned int layer, int curtime)
1667 bool changed = false;
1668 unsigned int all_layer;
1670 node->lasttime = curtime;
1673 if (ob && (ob->recalc & OB_RECALC_ALL)) {
1674 all_layer = node->scelay;
1676 /* got an object node that changes, now check relations */
1677 for (itA = node->child; itA; itA = itA->next) {
1678 all_layer |= itA->lay;
1679 /* the relationship is visible */
1680 if ((itA->lay & layer)) { // XXX || (itA->node->ob == obedit)
1681 if (itA->node->type == ID_OB) {
1682 obc = itA->node->ob;
1683 oldflag = obc->recalc;
1685 /* got a ob->obc relation, now check if flag needs flush */
1686 if (ob->recalc & OB_RECALC_OB) {
1687 if (itA->type & DAG_RL_OB_OB) {
1688 //printf("ob %s changes ob %s\n", ob->id.name, obc->id.name);
1689 obc->recalc |= OB_RECALC_OB;
1690 lib_id_recalc_tag(bmain, &obc->id);
1692 if (itA->type & DAG_RL_OB_DATA) {
1693 //printf("ob %s changes obdata %s\n", ob->id.name, obc->id.name);
1694 obc->recalc |= OB_RECALC_DATA;
1695 lib_id_recalc_data_tag(bmain, &obc->id);
1698 if (ob->recalc & OB_RECALC_DATA) {
1699 if (itA->type & DAG_RL_DATA_OB) {
1700 //printf("obdata %s changes ob %s\n", ob->id.name, obc->id.name);
1701 obc->recalc |= OB_RECALC_OB;
1702 lib_id_recalc_tag(bmain, &obc->id);
1704 if (itA->type & DAG_RL_DATA_DATA) {
1705 //printf("obdata %s changes obdata %s\n", ob->id.name, obc->id.name);
1706 obc->recalc |= OB_RECALC_DATA;
1707 lib_id_recalc_data_tag(bmain, &obc->id);
1710 if (oldflag != obc->recalc) changed = 1;
1714 /* even nicer, we can clear recalc flags... */
1715 if ((all_layer & layer) == 0) { // XXX && (ob != obedit)) {
1716 /* but existing displaylists or derivedmesh should be freed */
1717 if (ob->recalc & OB_RECALC_DATA)
1718 BKE_object_free_derived_caches(ob);
1720 ob->recalc &= ~OB_RECALC_ALL;
1724 /* check case where child changes and parent forcing obdata to change */
1725 /* should be done regardless if this ob has recalc set */
1726 /* could merge this in with loop above...? (ton) */
1727 for (itA = node->child; itA; itA = itA->next) {
1728 /* the relationship is visible */
1729 if ((itA->lay & layer)) { // XXX || (itA->node->ob == obedit)
1730 if (itA->node->type == ID_OB) {
1731 obc = itA->node->ob;
1733 if ((obc->recalc & OB_RECALC_ALL) == OB_RECALC_OB) {
1734 /* parent has deforming info */
1735 if (itA->type & (DAG_RL_OB_DATA | DAG_RL_DATA_DATA)) {
1736 // printf("parent %s changes ob %s\n", ob->id.name, obc->id.name);
1737 obc->recalc |= OB_RECALC_DATA;
1738 lib_id_recalc_data_tag(bmain, &obc->id);
1745 /* we only go deeper if node not checked or something changed */
1746 for (itA = node->child; itA; itA = itA->next) {
1747 if (changed || itA->node->lasttime != curtime)
1748 flush_update_node(bmain, itA->node, layer, curtime);
1753 /* node was checked to have lasttime != curtime, and is of type ID_OB */
1754 static unsigned int flush_layer_node(Scene *sce, DagNode *node, int curtime)
1758 node->lasttime = curtime;
1759 node->lay = node->scelay;
1761 for (itA = node->child; itA; itA = itA->next) {
1762 if (itA->node->type == ID_OB) {
1763 if (itA->node->lasttime != curtime) {
1764 itA->lay = flush_layer_node(sce, itA->node, curtime); /* lay is only set once for each relation */
1767 itA->lay = itA->node->lay;
1770 node->lay |= itA->lay;
1777 /* node was checked to have lasttime != curtime, and is of type ID_OB */
1778 static void flush_pointcache_reset(Main *bmain, Scene *scene, DagNode *node,
1779 int curtime, unsigned int lay, bool reset)
1784 node->lasttime = curtime;
1786 for (itA = node->child; itA; itA = itA->next) {
1787 if (itA->node->type == ID_OB) {
1788 if (itA->node->lasttime != curtime) {
1789 ob = (Object *)(itA->node->ob);
1791 if (reset || (ob->recalc & OB_RECALC_ALL)) {
1792 if (BKE_ptcache_object_reset(scene, ob, PTCACHE_RESET_DEPSGRAPH)) {
1793 /* Don't tag nodes which are on invisible layer. */
1794 if (itA->node->lay & lay) {
1795 ob->recalc |= OB_RECALC_DATA;
1796 lib_id_recalc_data_tag(bmain, &ob->id);
1800 flush_pointcache_reset(bmain, scene, itA->node, curtime, lay, true);
1803 flush_pointcache_reset(bmain, scene, itA->node, curtime, lay, false);
1809 /* flush layer flags to dependencies */
1810 static void dag_scene_flush_layers(Scene *sce, int lay)
1812 DagNode *node, *firstnode;
1817 firstnode = sce->theDag->DagNode.first; /* always scene node */
1819 for (itA = firstnode->child; itA; itA = itA->next)
1822 sce->theDag->time++; /* so we know which nodes were accessed */
1823 lasttime = sce->theDag->time;
1825 /* update layer flags in nodes */
1826 for (base = sce->base.first; base; base = base->next) {
1827 node = dag_get_node(sce->theDag, base->object);
1828 node->scelay = base->object->lay;
1831 /* ensure cameras are set as if they are on a visible layer, because
1832 * they ared still used for rendering or setting the camera view
1834 * XXX, this wont work for local view / unlocked camera's */
1836 node = dag_get_node(sce->theDag, sce->camera);
1837 node->scelay |= lay;
1840 #ifdef DURIAN_CAMERA_SWITCH
1844 for (m = sce->markers.first; m; m = m->next) {
1846 node = dag_get_node(sce->theDag, m->camera);
1847 node->scelay |= lay;
1853 /* flush layer nodes to dependencies */
1854 for (itA = firstnode->child; itA; itA = itA->next)
1855 if (itA->node->lasttime != lasttime && itA->node->type == ID_OB)
1856 flush_layer_node(sce, itA->node, lasttime);
1859 static void dag_tag_renderlayers(Scene *sce, unsigned int lay)
1861 if (sce->nodetree) {
1864 unsigned int lay_changed = 0;
1866 for (base = sce->base.first; base; base = base->next)
1867 if (base->lay & lay)
1868 if (base->object->recalc)
1869 lay_changed |= base->lay;
1871 for (node = sce->nodetree->nodes.first; node; node = node->next) {
1872 if (node->id == (ID *)sce) {
1873 SceneRenderLayer *srl = BLI_findlink(&sce->r.layers, node->custom1);
1874 if (srl && (srl->lay & lay_changed))
1875 nodeUpdate(sce->nodetree, node);
1881 /* flushes all recalc flags in objects down the dependency tree */
1882 void DAG_scene_flush_update(Main *bmain, Scene *sce, unsigned int lay, const short time)
1889 if (sce->theDag == NULL) {
1890 printf("DAG zero... not allowed to happen!\n");
1891 DAG_scene_relations_update(bmain, sce);
1894 firstnode = sce->theDag->DagNode.first; /* always scene node */
1896 /* first we flush the layer flags */
1897 dag_scene_flush_layers(sce, lay);
1899 /* then we use the relationships + layer info to flush update events */
1900 sce->theDag->time++; /* so we know which nodes were accessed */
1901 lasttime = sce->theDag->time;
1902 for (itA = firstnode->child; itA; itA = itA->next)
1903 if (itA->node->lasttime != lasttime && itA->node->type == ID_OB)
1904 flush_update_node(bmain, itA->node, lay, lasttime);
1906 /* if update is not due to time change, do pointcache clears */
1908 sce->theDag->time++; /* so we know which nodes were accessed */
1909 lasttime = sce->theDag->time;
1910 for (itA = firstnode->child; itA; itA = itA->next) {
1911 if (itA->node->lasttime != lasttime && itA->node->type == ID_OB) {
1912 ob = (Object *)(itA->node->ob);
1914 if (ob->recalc & OB_RECALC_ALL) {
1915 if (BKE_ptcache_object_reset(sce, ob, PTCACHE_RESET_DEPSGRAPH)) {
1916 ob->recalc |= OB_RECALC_DATA;
1917 lib_id_recalc_data_tag(bmain, &ob->id);
1920 flush_pointcache_reset(bmain, sce, itA->node, lasttime,
1924 flush_pointcache_reset(bmain, sce, itA->node, lasttime,
1930 dag_tag_renderlayers(sce, lay);
1933 static int object_modifiers_use_time(Object *ob)
1937 /* check if a modifier in modifier stack needs time input */
1938 for (md = ob->modifiers.first; md; md = md->next)
1939 if (modifier_dependsOnTime(md))
1942 /* check whether any modifiers are animated */
1944 AnimData *adt = ob->adt;
1947 /* action - check for F-Curves with paths containing 'modifiers[' */
1949 for (fcu = adt->action->curves.first; fcu; fcu = fcu->next) {
1950 if (fcu->rna_path && strstr(fcu->rna_path, "modifiers["))
1955 /* This here allows modifier properties to get driven and still update properly
1957 * Workaround to get [#26764] (e.g. subsurf levels not updating when animated/driven)
1958 * working, without the updating problems ([#28525] [#28690] [#28774] [#28777]) caused
1959 * by the RNA updates cache introduced in r.38649
1961 for (fcu = adt->drivers.first; fcu; fcu = fcu->next) {
1962 if (fcu->rna_path && strstr(fcu->rna_path, "modifiers["))
1966 /* XXX: also, should check NLA strips, though for now assume that nobody uses
1967 * that and we can omit that for performance reasons... */
1973 static short animdata_use_time(AnimData *adt)
1977 if (adt == NULL) return 0;
1979 /* check action - only if assigned, and it has anim curves */
1980 if (adt->action && adt->action->curves.first)
1983 /* check NLA tracks + strips */
1984 for (nlt = adt->nla_tracks.first; nlt; nlt = nlt->next) {
1985 if (nlt->strips.first)
1989 /* If we have drivers, more likely than not, on a frame change
1990 * they'll need updating because their owner changed
1992 * This is kindof a hack to get around a whole host of problems
1993 * involving drivers using non-object datablock data (which the
1994 * depsgraph currently has no way of representing let alone correctly
1995 * dependency sort+tagging). By doing this, at least we ensure that
1996 * some commonly attempted drivers (such as scene -> current frame;
1997 * see "Driver updates fail" thread on Bf-committers dated July 2)
1998 * will work correctly, and that other non-object datablocks will have
1999 * their drivers update at least on frame change.
2001 * -- Aligorith, July 4 2011
2003 if (adt->drivers.first)
2009 static void dag_object_time_update_flags(Main *bmain, Scene *scene, Object *ob)
2011 if (ob->constraints.first) {
2013 for (con = ob->constraints.first; con; con = con->next) {
2014 const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_get(con);
2015 ListBase targets = {NULL, NULL};
2016 bConstraintTarget *ct;
2019 /* special case for camera tracking -- it doesn't use targets to define relations */
2020 if (ELEM(cti->type, CONSTRAINT_TYPE_FOLLOWTRACK, CONSTRAINT_TYPE_CAMERASOLVER, CONSTRAINT_TYPE_OBJECTSOLVER)) {
2021 ob->recalc |= OB_RECALC_OB;
2023 else if (cti->get_constraint_targets) {
2024 cti->get_constraint_targets(con, &targets);
2026 for (ct = targets.first; ct; ct = ct->next) {
2028 ob->recalc |= OB_RECALC_OB;
2033 if (cti->flush_constraint_targets)
2034 cti->flush_constraint_targets(con, &targets, 1);
2042 /* motion path or bone child */
2043 if (ob->parent->type == OB_CURVE || ob->parent->type == OB_ARMATURE) ob->recalc |= OB_RECALC_OB;
2046 #if 0 // XXX old animation system
2047 if (ob->nlastrips.first) {
2048 if (ob->dup_group) {
2049 bActionStrip *strip;
2050 /* this case is for groups with nla, whilst nla target has no action or nla */
2051 for (strip = ob->nlastrips.first; strip; strip = strip->next) {
2053 strip->object->recalc |= OB_RECALC_ALL;
2057 #endif // XXX old animation system
2059 if (animdata_use_time(ob->adt)) {
2060 ob->recalc |= OB_RECALC_OB;
2061 ob->adt->recalc |= ADT_RECALC_ANIM;
2064 if ((ob->adt) && (ob->type == OB_ARMATURE)) ob->recalc |= OB_RECALC_DATA;
2066 if (object_modifiers_use_time(ob)) ob->recalc |= OB_RECALC_DATA;
2067 if ((ob->pose) && (ob->pose->flag & POSE_CONSTRAINTS_TIMEDEPEND)) ob->recalc |= OB_RECALC_DATA;
2069 // XXX: scene here may not be the scene that contains the rigidbody world affecting this!
2070 if (ob->rigidbody_object && BKE_scene_check_rigidbody_active(scene))
2071 ob->recalc |= OB_RECALC_OB;
2074 AnimData *adt = BKE_animdata_from_id((ID *)ob->data);
2083 if (!(ob->shapeflag & OB_SHAPE_LOCK)) {
2084 ob->recalc |= OB_RECALC_DATA;
2087 if (ob->particlesystem.first)
2088 ob->recalc |= OB_RECALC_DATA;
2094 if (!(ob->shapeflag & OB_SHAPE_LOCK)) {
2095 ob->recalc |= OB_RECALC_DATA;
2101 if (BLI_listbase_is_empty(&cu->nurb) && cu->str && cu->vfont)
2102 ob->recalc |= OB_RECALC_DATA;
2107 if (!(ob->shapeflag & OB_SHAPE_LOCK)) {
2108 ob->recalc |= OB_RECALC_DATA;
2113 if (ob->transflag & OB_DUPLI) ob->recalc |= OB_RECALC_DATA;
2116 /* update animated images */
2117 if (ob->empty_drawtype == OB_EMPTY_IMAGE && ob->data)
2118 if (BKE_image_is_animated(ob->data))
2119 ob->recalc |= OB_RECALC_DATA;
2123 if (animdata_use_time(adt)) {
2124 ob->recalc |= OB_RECALC_DATA;
2125 adt->recalc |= ADT_RECALC_ANIM;
2128 if (ob->particlesystem.first) {
2129 ParticleSystem *psys = ob->particlesystem.first;
2131 for (; psys; psys = psys->next) {
2132 if (psys_check_enabled(ob, psys)) {
2133 ob->recalc |= OB_RECALC_DATA;
2140 if (ob->recalc & OB_RECALC_OB)
2141 lib_id_recalc_tag(bmain, &ob->id);
2142 if (ob->recalc & OB_RECALC_DATA)
2143 lib_id_recalc_data_tag(bmain, &ob->id);
2147 /* recursively update objects in groups, each group is done at most once */
2148 static void dag_group_update_flags(Main *bmain, Scene *scene, Group *group, const bool do_time)
2152 if (group->id.flag & LIB_DOIT)
2155 group->id.flag |= LIB_DOIT;
2157 for (go = group->gobject.first; go; go = go->next) {
2159 dag_object_time_update_flags(bmain, scene, go->ob);
2160 if (go->ob->dup_group)
2161 dag_group_update_flags(bmain, scene, go->ob->dup_group, do_time);
2165 /* flag all objects that need recalc, for changes in time for example */
2166 /* do_time: make this optional because undo resets objects to their animated locations without this */
2167 void DAG_scene_update_flags(Main *bmain, Scene *scene, unsigned int lay, const bool do_time, const bool do_invisible_flush)
2175 BKE_main_id_tag_idcode(bmain, ID_GR, false);
2177 /* set ob flags where animated systems are */
2178 for (SETLOOPER(scene, sce_iter, base)) {
2182 /* now if DagNode were part of base, the node->lay could be checked... */
2183 /* we do all now, since the scene_flush checks layers and clears recalc flags even */
2185 /* NOTE: "sce_iter" not "scene" so that rigidbodies in background scenes work
2186 * (i.e. muting + rbw availability can be checked and tagged properly) [#33970]
2188 dag_object_time_update_flags(bmain, sce_iter, ob);
2191 /* recursively tag groups with LIB_DOIT, and update flags for objects */
2193 dag_group_update_flags(bmain, scene, ob->dup_group, do_time);
2196 for (sce_iter = scene; sce_iter; sce_iter = sce_iter->set)
2197 DAG_scene_flush_update(bmain, sce_iter, lay, 1);
2200 /* test: set time flag, to disable baked systems to update */
2201 for (SETLOOPER(scene, sce_iter, base)) {
2203 if (ob->recalc & OB_RECALC_ALL)
2204 ob->recalc |= OB_RECALC_TIME;
2207 /* hrmf... an exception to look at once, for invisible camera object we do it over */
2209 dag_object_time_update_flags(bmain, scene, scene->camera);
2212 /* and store the info in groupobject */
2213 for (group = bmain->group.first; group; group = group->id.next) {
2214 if (group->id.flag & LIB_DOIT) {
2215 for (go = group->gobject.first; go; go = go->next) {
2216 go->recalc = go->ob->recalc;
2217 // printf("ob %s recalc %d\n", go->ob->id.name, go->recalc);
2219 group->id.flag &= ~LIB_DOIT;
2223 if (do_invisible_flush) {
2224 dag_invisible_dependencies_check_flush(bmain, scene);
2228 /* struct returned by DagSceneLayer */
2229 typedef struct DagSceneLayer {
2230 struct DagSceneLayer *next, *prev;
2235 /* returns visible scenes with valid DAG */
2236 static void dag_current_scene_layers(Main *bmain, ListBase *lb)
2238 wmWindowManager *wm;
2241 BLI_listbase_clear(lb);
2243 /* if we have a windowmanager, look into windows */
2244 if ((wm = bmain->wm.first)) {
2246 BKE_main_id_flag_listbase(&bmain->scene, LIB_DOIT, 1);
2248 for (win = wm->windows.first; win; win = win->next) {
2249 if (win->screen && win->screen->scene->theDag) {
2250 Scene *scene = win->screen->scene;
2253 if (scene->id.flag & LIB_DOIT) {
2254 dsl = MEM_mallocN(sizeof(DagSceneLayer), "dag scene layer");
2256 BLI_addtail(lb, dsl);
2259 dsl->layer = BKE_screen_visible_layers(win->screen, scene);
2261 scene->id.flag &= ~LIB_DOIT;
2264 /* It is possible that multiple windows shares the same scene
2265 * and have different layers visible.
2267 * Here we deal with such cases by squashing layers bits from
2268 * multiple windoew to the DagSceneLayer.
2270 * TODO(sergey): Such a lookup could be optimized perhaps,
2271 * however should be fine for now since we usually have only
2274 for (dsl = lb->first; dsl; dsl = dsl->next) {
2275 if (dsl->scene == scene) {
2276 dsl->layer |= BKE_screen_visible_layers(win->screen, scene);
2285 /* if not, use the first sce */
2286 DagSceneLayer *dsl = MEM_mallocN(sizeof(DagSceneLayer), "dag scene layer");
2288 BLI_addtail(lb, dsl);
2290 dsl->scene = bmain->scene.first;
2291 dsl->layer = dsl->scene->lay;
2293 /* XXX for background mode, we should get the scene
2294 * from somewhere, for the -S option, but it's in
2295 * the context, how to get it here? */
2299 static void dag_group_on_visible_update(Group *group)
2303 if (group->id.flag & LIB_DOIT)
2306 group->id.flag |= LIB_DOIT;
2308 for (go = group->gobject.first; go; go = go->next) {
2309 if (ELEM(go->ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL, OB_LATTICE)) {
2310 go->ob->recalc |= OB_RECALC_DATA;
2311 go->ob->id.flag |= LIB_DOIT;
2312 lib_id_recalc_tag(G.main, &go->ob->id);
2314 if (go->ob->proxy_from) {
2315 go->ob->recalc |= OB_RECALC_OB;
2316 go->ob->id.flag |= LIB_DOIT;
2317 lib_id_recalc_tag(G.main, &go->ob->id);
2320 if (go->ob->dup_group)
2321 dag_group_on_visible_update(go->ob->dup_group);
2325 void DAG_on_visible_update(Main *bmain, const bool do_time)
2330 /* get list of visible scenes and layers */
2331 dag_current_scene_layers(bmain, &listbase);
2333 for (dsl = listbase.first; dsl; dsl = dsl->next) {
2334 Scene *scene = dsl->scene;
2339 unsigned int lay = dsl->layer, oblay;
2341 /* derivedmeshes and displists are not saved to file so need to be
2342 * remade, tag them so they get remade in the scene update loop,
2343 * note armature poses or object matrices are preserved and do not
2344 * require updates, so we skip those */
2345 for (sce_iter = scene; sce_iter; sce_iter = sce_iter->set)
2346 dag_scene_flush_layers(sce_iter, lay);
2348 BKE_main_id_tag_idcode(bmain, ID_GR, false);
2350 for (SETLOOPER(scene, sce_iter, base)) {
2352 node = (sce_iter->theDag) ? dag_get_node(sce_iter->theDag, ob) : NULL;
2353 oblay = (node) ? node->lay : ob->lay;
2355 if ((oblay & lay) & ~scene->lay_updated) {
2356 if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL, OB_LATTICE)) {
2357 ob->recalc |= OB_RECALC_DATA;
2358 lib_id_recalc_tag(bmain, &ob->id);
2360 /* This should not be needed here, but in some cases, like after a redo, we can end up with
2361 * a wrong final matrix (see T42472).
2362 * Quoting Sergey, this comes from BKE_object_handle_update_ex, which is calling
2363 * BKE_object_where_is_calc_ex when it shouldn't, but that issue is not easily fixable.
2366 ob->recalc |= OB_RECALC_OB;
2367 lib_id_recalc_tag(bmain, &ob->id);
2369 if (ob->proxy && (ob->proxy_group == NULL)) {
2370 ob->proxy->recalc |= OB_RECALC_DATA;
2371 lib_id_recalc_tag(bmain, &ob->id);
2374 dag_group_on_visible_update(ob->dup_group);
2378 BKE_main_id_tag_idcode(bmain, ID_GR, false);
2380 /* now tag update flags, to ensure deformers get calculated on redraw */
2381 DAG_scene_update_flags(bmain, scene, lay, do_time, true);
2382 scene->lay_updated |= lay;
2385 BLI_freelistN(&listbase);
2387 /* hack to get objects updating on layer changes */
2388 DAG_id_type_tag(bmain, ID_OB);
2390 /* so masks update on load */
2391 if (bmain->mask.first) {
2394 for (mask = bmain->mask.first; mask; mask = mask->id.next) {
2395 DAG_id_tag_update(&mask->id, 0);
2400 static void dag_id_flush_update__isDependentTexture(void *userData, Object *UNUSED(ob), ID **idpoin)
2402 struct { ID *id; bool is_dependent; } *data = userData;
2404 if (*idpoin && GS((*idpoin)->name) == ID_TE) {
2405 if (data->id == (*idpoin))
2406 data->is_dependent = 1;
2410 static void dag_id_flush_update(Main *bmain, Scene *sce, ID *id)
2412 Object *obt, *ob = NULL;
2415 /* here we flush a few things before actual scene wide flush, mostly
2416 * due to only objects and not other datablocks being in the depsgraph */
2418 /* set flags & pointcache for object */
2419 if (GS(id->name) == ID_OB) {
2421 BKE_ptcache_object_reset(sce, ob, PTCACHE_RESET_DEPSGRAPH);
2423 /* So if someone tagged object recalc directly,
2424 * id_tag_update bit-field stays relevant
2426 if (ob->recalc & OB_RECALC_ALL) {
2427 DAG_id_type_tag(bmain, GS(id->name));
2430 if (ob->recalc & OB_RECALC_DATA) {
2431 /* all users of this ob->data should be checked */
2434 /* no point in trying in this cases */
2435 if (id && id->us <= 1) {
2436 dag_editors_id_update(bmain, id);
2442 /* set flags & pointcache for object data */
2444 idtype = GS(id->name);
2447 if (OB_DATA_SUPPORT_ID(idtype)) {
2448 for (obt = bmain->object.first; obt; obt = obt->id.next) {
2449 if (!(ob && obt == ob) && obt->data == id) {
2450 obt->recalc |= OB_RECALC_DATA;
2451 lib_id_recalc_data_tag(bmain, &obt->id);
2452 BKE_ptcache_object_reset(sce, obt, PTCACHE_RESET_DEPSGRAPH);
2457 /* set flags based on textures - can influence depgraph via modifiers */
2458 if (idtype == ID_TE) {
2459 for (obt = bmain->object.first; obt; obt = obt->id.next) {
2460 struct { ID *id; bool is_dependent; } data;
2462 data.is_dependent = 0;
2464 modifiers_foreachIDLink(obt, dag_id_flush_update__isDependentTexture, &data);
2465 if (data.is_dependent) {
2466 obt->recalc |= OB_RECALC_DATA;
2467 lib_id_recalc_data_tag(bmain, &obt->id);
2470 /* particle settings can use the texture as well */
2471 if (obt->particlesystem.first) {
2472 ParticleSystem *psys = obt->particlesystem.first;
2473 MTex **mtexp, *mtex;
2475 for (; psys; psys = psys->next) {
2476 mtexp = psys->part->mtex;
2477 for (a = 0; a < MAX_MTEX; a++, mtexp++) {
2479 if (mtex && mtex->tex == (Tex *)id) {
2480 obt->recalc |= OB_RECALC_DATA;
2481 lib_id_recalc_data_tag(bmain, &obt->id);
2483 if (mtex->mapto & PAMAP_INIT)
2484 psys->recalc |= PSYS_RECALC_RESET;
2485 if (mtex->mapto & PAMAP_CHILD)
2486 psys->recalc |= PSYS_RECALC_CHILD;
2488 BKE_ptcache_object_reset(sce, obt, PTCACHE_RESET_DEPSGRAPH);
2496 /* set flags based on ShapeKey */
2497 if (idtype == ID_KE) {
2498 for (obt = bmain->object.first; obt; obt = obt->id.next) {
2499 Key *key = BKE_key_from_object(obt);
2500 if (!(ob && obt == ob) && ((ID *)key == id)) {
2501 obt->flag |= (OB_RECALC_OB | OB_RECALC_DATA);
2502 lib_id_recalc_tag(bmain, &obt->id);
2503 lib_id_recalc_data_tag(bmain, &obt->id);
2504 BKE_ptcache_object_reset(sce, obt, PTCACHE_RESET_DEPSGRAPH);
2509 /* set flags based on particle settings */
2510 if (idtype == ID_PA) {
2511 ParticleSystem *psys;
2512 for (obt = bmain->object.first; obt; obt = obt->id.next)
2513 for (psys = obt->particlesystem.first; psys; psys = psys->next)
2514 if (&psys->part->id == id)
2515 BKE_ptcache_object_reset(sce, obt, PTCACHE_RESET_DEPSGRAPH);
2518 if (ELEM(idtype, ID_MA, ID_TE)) {
2519 obt = sce->basact ? sce->basact->object : NULL;
2520 if (obt && obt->mode & OB_MODE_TEXTURE_PAINT) {
2521 BKE_texpaint_slots_refresh_object(sce, obt);
2522 BKE_paint_proj_mesh_data_check(sce, obt, NULL, NULL, NULL, NULL);
2523 GPU_drawobject_free(obt->derivedFinal);
2527 if (idtype == ID_MC) {
2528 MovieClip *clip = (MovieClip *) id;
2530 BKE_tracking_dopesheet_tag_update(&clip->tracking);
2532 for (obt = bmain->object.first; obt; obt = obt->id.next) {
2534 for (con = obt->constraints.first; con; con = con->next) {
2535 const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_get(con);
2536 if (ELEM(cti->type, CONSTRAINT_TYPE_FOLLOWTRACK, CONSTRAINT_TYPE_CAMERASOLVER,
2537 CONSTRAINT_TYPE_OBJECTSOLVER))
2539 obt->recalc |= OB_RECALC_OB;
2540 lib_id_recalc_tag(bmain, &obt->id);
2546 if (sce->nodetree) {
2549 for (node = sce->nodetree->nodes.first; node; node = node->next) {
2550 if (node->id == id) {
2551 nodeUpdate(sce->nodetree, node);
2557 /* Not pretty to iterate all the nodes here, but it's as good as it
2558 * could be with the current depsgraph design/
2560 if (idtype == ID_IM) {
2561 FOREACH_NODETREE(bmain, ntree, parent_id) {
2562 if (ntree->type == NTREE_SHADER) {
2564 for (node = ntree->nodes.first; node; node = node->next) {
2565 if (node->id == id) {
2566 lib_id_recalc_tag(bmain, &ntree->id);
2571 } FOREACH_NODETREE_END
2574 if (idtype == ID_MSK) {
2575 if (sce->nodetree) {
2578 for (node = sce->nodetree->nodes.first; node; node = node->next) {
2579 if (node->id == id) {
2580 nodeUpdate(sce->nodetree, node);
2586 /* camera's matrix is used to orient reconstructed stuff,
2587 * so it should happen tracking-related constraints recalculation
2588 * when camera is changing (sergey) */
2589 if (sce->camera && &sce->camera->id == id) {
2590 MovieClip *clip = BKE_object_movieclip_get(sce, sce->camera, true);
2593 dag_id_flush_update(bmain, sce, &clip->id);
2596 /* update editors */
2597 dag_editors_id_update(bmain, id);
2601 void DAG_ids_flush_tagged(Main *bmain)
2605 ListBase *lbarray[MAX_LIBARRAY];
2607 bool do_flush = false;
2609 /* get list of visible scenes and layers */
2610 dag_current_scene_layers(bmain, &listbase);
2612 if (BLI_listbase_is_empty(&listbase))
2615 /* loop over all ID types */
2616 a = set_listbasepointers(bmain, lbarray);
2619 ListBase *lb = lbarray[a];
2622 /* we tag based on first ID type character to avoid
2623 * looping over all ID's in case there are no tags */
2624 if (id && bmain->id_tag_update[id->name[0]]) {
2625 for (; id; id = id->next) {
2626 if (id->flag & (LIB_ID_RECALC | LIB_ID_RECALC_DATA)) {
2628 for (dsl = listbase.first; dsl; dsl = dsl->next)
2629 dag_id_flush_update(bmain, dsl->scene, id);
2637 /* flush changes to other objects */
2639 for (dsl = listbase.first; dsl; dsl = dsl->next)
2640 DAG_scene_flush_update(bmain, dsl->scene, dsl->layer, 0);
2643 BLI_freelistN(&listbase);
2646 void DAG_ids_check_recalc(Main *bmain, Scene *scene, bool time)
2648 ListBase *lbarray[MAX_LIBARRAY];
2650 bool updated = false;
2652 /* loop over all ID types */
2653 a = set_listbasepointers(bmain, lbarray);
2656 ListBase *lb = lbarray[a];
2659 /* we tag based on first ID type character to avoid
2660 * looping over all ID's in case there are no tags */
2661 if (id && bmain->id_tag_update[id->name[0]]) {
2667 dag_editors_scene_update(bmain, scene, (updated || time));
2670 /* It is possible that scene_update_post and frame_update_post handlers
2671 * will modify objects. The issue is that DAG_ids_clear_recalc is called
2672 * just after callbacks, which leaves objects with recalc flags but no
2673 * corresponding bit in ID recalc bitfield. This leads to some kind of
2674 * regression when using ID type tag fields to check whether there objects
2675 * to be updated internally comparing threaded DAG with legacy one.
2677 * For now let's have a workaround which will preserve tag for ID_OB
2678 * if there're objects with OB_RECALC_ALL bits. This keeps behavior
2679 * unchanged comparing with 2.69 release.
2681 * TODO(sergey): Need to get rid of such a workaround.
2686 #define POST_UPDATE_HANDLER_WORKAROUND
2688 void DAG_ids_clear_recalc(Main *bmain)
2690 ListBase *lbarray[MAX_LIBARRAY];
2694 #ifdef POST_UPDATE_HANDLER_WORKAROUND
2695 bool have_updated_objects = false;
2697 if (DAG_id_type_tagged(bmain, ID_OB)) {
2701 /* We need to check all visible scenes, otherwise resetting
2702 * OB_ID changed flag will only work fine for first scene of
2703 * multiple visible and all the rest will skip update.
2705 * This could also lead to wrong behavior scene update handlers
2706 * because of missing ID datablock changed flags.
2708 * This is a bit of a bummer to allocate list here, but likely
2709 * it wouldn't become too much bad because it only happens when
2710 * objects were actually changed.
2712 dag_current_scene_layers(bmain, &listbase);
2714 for (dsl = listbase.first; dsl; dsl = dsl->next) {
2715 Scene *scene = dsl->scene;
2717 for (node = scene->theDag->DagNode.first;
2718 node != NULL && have_updated_objects == false;
2721 if (node->type == ID_OB) {
2722 Object *object = (Object *) node->ob;
2723 if (object->recalc & OB_RECALC_ALL) {
2724 have_updated_objects = true;
2731 BLI_freelistN(&listbase);
2735 /* loop over all ID types */
2736 a = set_listbasepointers(bmain, lbarray);
2739 ListBase *lb = lbarray[a];
2742 /* we tag based on first ID type character to avoid
2743 * looping over all ID's in case there are no tags */
2744 if (id && bmain->id_tag_update[id->name[0]]) {
2745 for (; id; id = id->next) {
2746 if (id->flag & (LIB_ID_RECALC | LIB_ID_RECALC_DATA))
2747 id->flag &= ~(LIB_ID_RECALC | LIB_ID_RECALC_DATA);
2749 /* some ID's contain semi-datablock nodetree */
2750 ntree = ntreeFromID(id);
2751 if (ntree && (ntree->id.flag & (LIB_ID_RECALC | LIB_ID_RECALC_DATA)))
2752 ntree->id.flag &= ~(LIB_ID_RECALC | LIB_ID_RECALC_DATA);
2757 memset(bmain->id_tag_update, 0, sizeof(bmain->id_tag_update));
2759 #ifdef POST_UPDATE_HANDLER_WORKAROUND
2760 if (have_updated_objects) {
2761 DAG_id_type_tag(bmain, ID_OB);
2766 void DAG_id_tag_update_ex(Main *bmain, ID *id, short flag)
2768 if (id == NULL) return;
2770 if (G.debug & G_DEBUG_DEPSGRAPH) {
2771 printf("%s: id=%s flag=%d\n", __func__, id->name, flag);
2774 /* tag ID for update */
2776 if (flag & OB_RECALC_OB)
2777 lib_id_recalc_tag(bmain, id);
2778 if (flag & (OB_RECALC_DATA | PSYS_RECALC))
2779 lib_id_recalc_data_tag(bmain, id);
2782 lib_id_recalc_tag(bmain, id);
2784 /* flag is for objects and particle systems */
2787 short idtype = GS(id->name);
2789 if (idtype == ID_OB) {
2790 /* only quick tag */
2792 ob->recalc |= (flag & OB_RECALC_ALL);
2794 else if (idtype == ID_PA) {
2795 ParticleSystem *psys;
2796 /* this is weak still, should be done delayed as well */
2797 for (ob = bmain->object.first; ob; ob = ob->id.next) {
2798 for (psys = ob->particlesystem.first; psys; psys = psys->next) {
2799 if (&psys->part->id == id) {
2800 ob->recalc |= (flag & OB_RECALC_ALL);
2801 psys->recalc |= (flag & PSYS_RECALC);
2802 lib_id_recalc_tag(bmain, &ob->id);
2803 lib_id_recalc_data_tag(bmain, &ob->id);
2808 else if (idtype == ID_VF) {
2809 /* this is weak still, should be done delayed as well */
2810 for (ob = bmain->object.first; ob; ob = ob->id.next) {
2811 if (ob->type == OB_FONT) {
2812 Curve *cu = ob->data;
2814 if (ELEM((struct VFont *)id, cu->vfont, cu->vfontb, cu->vfonti, cu->vfontbi)) {
2815 ob->recalc |= (flag & OB_RECALC_ALL);
2821 /* disable because this is called on various ID types automatically.
2822 * where printing warning is not useful. for now just ignore */
2823 /* BLI_assert(!"invalid flag for this 'idtype'"); */
2828 void DAG_id_tag_update(ID *id, short flag)
2830 DAG_id_tag_update_ex(G.main, id, flag);
2833 void DAG_id_type_tag(Main *bmain, short idtype)
2835 if (idtype == ID_NT) {
2836 /* stupid workaround so parent datablocks of nested nodetree get looped
2837 * over when we loop over tagged datablock types */
2838 DAG_id_type_tag(bmain, ID_MA);
2839 DAG_id_type_tag(bmain, ID_TE);
2840 DAG_id_type_tag(bmain, ID_LA);
2841 DAG_id_type_tag(bmain, ID_WO);
2842 DAG_id_type_tag(bmain, ID_SCE);
2845 bmain->id_tag_update[((char *)&idtype)[0]] = 1;
2848 int DAG_id_type_tagged(Main *bmain, short idtype)
2850 return bmain->id_tag_update[((char *)&idtype)[0]];
2854 /* recursively descends tree, each node only checked once */
2855 /* node is checked to be of type object */
2856 static int parent_check_node(DagNode *node, int curtime)
2860 node->lasttime = curtime;
2862 if (node->color == DAG_GRAY)
2865 for (itA = node->child; itA; itA = itA->next) {
2866 if (itA->node->type == ID_OB) {
2868 if (itA->node->color == DAG_GRAY)