4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2004 Blender Foundation.
21 * All rights reserved.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
33 #include "BLI_winstuff.h"
36 #include "BLI_blenlib.h"
37 #include "BLI_arithb.h"
39 #include "DNA_anim_types.h"
40 #include "DNA_action_types.h"
41 #include "DNA_armature_types.h"
42 #include "DNA_curve_types.h"
43 #include "DNA_camera_types.h"
45 #include "DNA_effect_types.h"
46 #include "DNA_group_types.h"
47 #include "DNA_lattice_types.h"
48 #include "DNA_lamp_types.h"
49 #include "DNA_key_types.h"
50 #include "DNA_mesh_types.h"
51 #include "DNA_modifier_types.h"
52 #include "DNA_nla_types.h"
53 #include "DNA_object_types.h"
54 #include "DNA_object_force.h"
55 #include "DNA_object_fluidsim.h"
56 #include "DNA_outliner_types.h"
57 #include "DNA_particle_types.h"
58 #include "DNA_scene_types.h"
59 #include "DNA_screen_types.h"
60 #include "DNA_space_types.h"
61 #include "DNA_view2d_types.h"
62 #include "DNA_view3d_types.h"
64 #include "BLI_ghash.h"
66 #include "BKE_action.h"
67 #include "BKE_effect.h"
68 #include "BKE_global.h"
69 #include "BKE_group.h"
72 #include "BKE_mball.h"
73 #include "BKE_modifier.h"
74 #include "BKE_object.h"
75 #include "BKE_particle.h"
76 #include "BKE_pointcache.h"
77 #include "BKE_utildefines.h"
78 #include "BKE_scene.h"
80 #include "MEM_guardedalloc.h"
82 #ifndef DISABLE_PYTHON
83 #include "BPY_extern.h"
86 #include "depsgraph_private.h"
88 /* Queue and stack operations for dag traversal
90 * the queue store a list of freenodes to avoid successives alloc/dealloc
93 DagNodeQueue * queue_create (int slots)
96 DagNodeQueueElem * elem;
99 queue = MEM_mallocN(sizeof(DagNodeQueue),"DAG queue");
100 queue->freenodes = MEM_mallocN(sizeof(DagNodeQueue),"DAG queue");
103 queue->first = queue->last = NULL;
104 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem3");
107 queue->freenodes->first = queue->freenodes->last = elem;
109 for (i = 1; i <slots;i++) {
110 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem4");
113 queue->freenodes->last->next = elem;
114 queue->freenodes->last = elem;
116 queue->freenodes->count = slots;
120 void queue_raz(DagNodeQueue *queue)
122 DagNodeQueueElem * elem;
125 if (queue->freenodes->last)
126 queue->freenodes->last->next = elem;
128 queue->freenodes->first = queue->freenodes->last = elem;
131 queue->freenodes->count++;
135 queue->freenodes->count++;
137 queue->freenodes->last = elem;
141 void queue_delete(DagNodeQueue *queue)
143 DagNodeQueueElem * elem;
144 DagNodeQueueElem * temp;
153 elem = queue->freenodes->first;
160 MEM_freeN(queue->freenodes);
164 /* insert in queue, remove in front */
165 void push_queue(DagNodeQueue *queue, DagNode *node)
167 DagNodeQueueElem * elem;
171 fprintf(stderr,"pushing null node \n");
174 /*fprintf(stderr,"BFS push : %s %d\n",((ID *) node->ob)->name, queue->count);*/
176 elem = queue->freenodes->first;
178 queue->freenodes->first = elem->next;
179 if ( queue->freenodes->last == elem) {
180 queue->freenodes->last = NULL;
181 queue->freenodes->first = NULL;
183 queue->freenodes->count--;
184 } else { /* alllocating more */
185 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem1");
188 queue->freenodes->first = queue->freenodes->last = elem;
190 for (i = 1; i <DAGQUEUEALLOC;i++) {
191 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem2");
194 queue->freenodes->last->next = elem;
195 queue->freenodes->last = elem;
197 queue->freenodes->count = DAGQUEUEALLOC;
199 elem = queue->freenodes->first;
200 queue->freenodes->first = elem->next;
204 if (queue->last != NULL)
205 queue->last->next = elem;
207 if (queue->first == NULL) {
214 /* insert in front, remove in front */
215 void push_stack(DagNodeQueue *queue, DagNode *node)
217 DagNodeQueueElem * elem;
220 elem = queue->freenodes->first;
222 queue->freenodes->first = elem->next;
223 if ( queue->freenodes->last == elem) {
224 queue->freenodes->last = NULL;
225 queue->freenodes->first = NULL;
227 queue->freenodes->count--;
228 } else { /* alllocating more */
229 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem1");
232 queue->freenodes->first = queue->freenodes->last = elem;
234 for (i = 1; i <DAGQUEUEALLOC;i++) {
235 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem2");
238 queue->freenodes->last->next = elem;
239 queue->freenodes->last = elem;
241 queue->freenodes->count = DAGQUEUEALLOC;
243 elem = queue->freenodes->first;
244 queue->freenodes->first = elem->next;
246 elem->next = queue->first;
249 if (queue->last == NULL)
255 DagNode * pop_queue(DagNodeQueue *queue)
257 DagNodeQueueElem * elem;
262 queue->first = elem->next;
263 if (queue->last == elem) {
268 if (queue->freenodes->last)
269 queue->freenodes->last->next=elem;
270 queue->freenodes->last=elem;
271 if (queue->freenodes->first == NULL)
272 queue->freenodes->first=elem;
276 queue->freenodes->count++;
279 fprintf(stderr,"return null \n");
284 void *pop_ob_queue(struct DagNodeQueue *queue) {
285 return(pop_queue(queue)->ob);
288 DagNode * get_top_node_queue(DagNodeQueue *queue)
290 return queue->first->node;
293 int queue_count(struct DagNodeQueue *queue){
298 DagForest * dag_init()
301 /* use callocN to init all zero */
302 forest = MEM_callocN(sizeof(DagForest),"DAG root");
306 /* isdata = object data... */
307 // XXX this needs to be extended to be more flexible (so that not only objects are evaluated via depsgraph)...
308 static void dag_add_driver_relation(AnimData *adt, DagForest *dag, DagNode *node, int isdata)
313 for (fcu= adt->drivers.first; fcu; fcu= fcu->next) {
314 ChannelDriver *driver= fcu->driver;
317 /* loop over targets, adding relationships as appropriate */
318 for (dtar= driver->targets.first; dtar; dtar= dtar->next) {
320 if (GS(dtar->id->name)==ID_OB) {
321 Object *ob= (Object *)dtar->id;
323 /* normal channel-drives-channel */
324 node1 = dag_get_node(dag, dtar->id);
326 /* check if bone... */
327 if ((ob->type==OB_ARMATURE) && dtar->rna_path && strstr(dtar->rna_path, "pose.pose_channels["))
328 dag_add_relation(dag, node1, node, isdata?DAG_RL_DATA_DATA:DAG_RL_DATA_OB, "Driver");
329 /* check if ob data */
330 else if (dtar->rna_path && strstr(dtar->rna_path, "data."))
331 dag_add_relation(dag, node1, node, isdata?DAG_RL_DATA_DATA:DAG_RL_DATA_OB, "Driver");
334 dag_add_relation(dag, node1, node, isdata?DAG_RL_OB_DATA:DAG_RL_OB_OB, "Driver");
341 static void dag_add_collision_field_relation(DagForest *dag, Scene *scene, Object *ob, DagNode *node)
346 // would be nice to have a list of colliders here
347 // so for now walk all objects in scene check 'same layer rule'
348 for(base = scene->base.first; base; base= base->next) {
349 if((base->lay & ob->lay) && base->object->pd) {
350 Object *ob1= base->object;
351 if((ob1->pd->deflect || ob1->pd->forcefield) && (ob1 != ob)) {
352 node2 = dag_get_node(dag, ob1);
353 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Field Collision");
359 static void build_dag_object(DagForest *dag, DagNode *scenenode, Scene *scene, Object *ob, int mask)
366 ParticleSystem *psys;
369 node = dag_get_node(dag, ob);
371 if ((ob->data) && (mask&DAG_RL_DATA)) {
372 node2 = dag_get_node(dag,ob->data);
373 dag_add_relation(dag,node,node2,DAG_RL_DATA, "Object-Data Relation");
374 node2->first_ancestor = ob;
375 node2->ancestor_count += 1;
378 if (ob->type == OB_ARMATURE) {
383 for (pchan = ob->pose->chanbase.first; pchan; pchan=pchan->next) {
384 for (con = pchan->constraints.first; con; con=con->next) {
385 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
386 ListBase targets = {NULL, NULL};
387 bConstraintTarget *ct;
389 if (cti && cti->get_constraint_targets) {
390 cti->get_constraint_targets(con, &targets);
392 for (ct= targets.first; ct; ct= ct->next) {
393 if (ct->tar && ct->tar != ob) {
394 // fprintf(stderr,"armature %s target :%s \n", ob->id.name, target->id.name);
395 node3 = dag_get_node(dag, ct->tar);
397 if (ct->subtarget[0])
398 dag_add_relation(dag,node3,node, DAG_RL_OB_DATA|DAG_RL_DATA_DATA, cti->name);
399 else if(ELEM(con->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO))
400 dag_add_relation(dag,node3,node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, cti->name);
402 dag_add_relation(dag,node3,node, DAG_RL_OB_DATA, cti->name);
406 if (cti->flush_constraint_targets)
407 cti->flush_constraint_targets(con, &targets, 1);
415 /* driver dependencies, nla modifiers */
416 #if 0 // XXX old animation system
417 if(ob->nlastrips.first) {
419 bActionChannel *chan;
420 for(strip= ob->nlastrips.first; strip; strip= strip->next) {
421 if(strip->modifiers.first) {
422 bActionModifier *amod;
423 for(amod= strip->modifiers.first; amod; amod= amod->next) {
425 node2 = dag_get_node(dag, amod->ob);
426 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "NLA Strip Modifier");
432 #endif // XXX old animation system
434 dag_add_driver_relation(ob->adt, dag, node, (ob->type == OB_ARMATURE)); // XXX isdata arg here doesn't give an accurate picture of situation
438 dag_add_driver_relation(key->adt, dag, node, 1);
440 if (ob->modifiers.first) {
443 for(md=ob->modifiers.first; md; md=md->next) {
444 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
446 if (mti->updateDepgraph) mti->updateDepgraph(md, dag, scene, ob, node);
450 node2 = dag_get_node(dag,ob->parent);
452 switch(ob->partype) {
454 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Parent");
456 case PARVERT1: case PARVERT3: case PARBONE:
457 dag_add_relation(dag,node2,node,DAG_RL_DATA_OB|DAG_RL_OB_OB, "Vertex Parent");
460 if(ob->parent->type==OB_LATTICE)
461 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Lattice Parent");
462 else if(ob->parent->type==OB_CURVE) {
463 Curve *cu= ob->parent->data;
464 if(cu->flag & CU_PATH)
465 dag_add_relation(dag,node2,node,DAG_RL_DATA_OB|DAG_RL_OB_OB, "Curve Parent");
467 dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Curve Parent");
470 dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Parent");
472 /* exception case: parent is duplivert */
473 if(ob->type==OB_MBALL && (ob->parent->transflag & OB_DUPLIVERTS)) {
474 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Duplivert");
480 node2 = dag_get_node(dag,ob->track);
481 dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Track To");
485 node2 = dag_get_node(dag, ob->proxy);
486 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Proxy");
487 /* inverted relation, so addtoroot shouldn't be set to zero */
491 if (ob->type==OB_CAMERA) {
492 Camera *cam = (Camera *)ob->data;
494 dag_add_driver_relation(cam->adt, dag, node, 1);
496 node2 = dag_get_node(dag, cam->dof_ob);
497 dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Camera DoF");
500 if (ob->type==OB_LAMP) {
501 Lamp *la = (Lamp *)ob->data;
503 dag_add_driver_relation(la->adt, dag, node, 1);
506 if (ob->transflag & OB_DUPLI) {
507 if((ob->transflag & OB_DUPLIGROUP) && ob->dup_group) {
509 for(go= ob->dup_group->gobject.first; go; go= go->next) {
511 node2 = dag_get_node(dag, go->ob);
512 /* node2 changes node1, this keeps animations updated in groups?? not logical? */
513 dag_add_relation(dag, node2, node, DAG_RL_OB_OB, "Dupligroup");
519 /* softbody collision */
520 if((ob->type==OB_MESH) || (ob->type==OB_CURVE) || (ob->type==OB_LATTICE))
521 if(modifiers_isSoftbodyEnabled(ob) || modifiers_isClothEnabled(ob))
522 dag_add_collision_field_relation(dag, scene, ob, node);
524 if (ob->type==OB_MBALL) {
525 Object *mom= find_basis_mball(scene, ob);
527 node2 = dag_get_node(dag, mom);
528 dag_add_relation(dag,node,node2,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Metaball"); // mom depends on children!
531 else if (ob->type==OB_CURVE) {
534 node2 = dag_get_node(dag, cu->bevobj);
535 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Curve Bevel");
538 node2 = dag_get_node(dag, cu->taperobj);
539 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Curve Taper");
542 dag_add_driver_relation(cu->adt, dag, node, 1);
544 else if(ob->type==OB_FONT) {
546 if(cu->textoncurve) {
547 node2 = dag_get_node(dag, cu->textoncurve);
548 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Texture On Curve");
552 psys= ob->particlesystem.first;
554 ParticleEffectorCache *nec;
557 for(; psys; psys=psys->next) {
558 ParticleSettings *part= psys->part;
560 dag_add_relation(dag, node, node, DAG_RL_OB_DATA, "Particle-Object Relation");
562 if(psys->flag & PSYS_DISABLED || psys->flag & PSYS_DELETE)
565 if(part->phystype==PART_PHYS_KEYED && psys->keyed_ob &&
566 BLI_findlink(&psys->keyed_ob->particlesystem,psys->keyed_psys-1)) {
567 node2 = dag_get_node(dag, psys->keyed_ob);
568 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA, "Particle Keyed Physics");
571 if(part->draw_as == PART_DRAW_OB && part->dup_ob) {
572 node2 = dag_get_node(dag, part->dup_ob);
573 dag_add_relation(dag, node, node2, DAG_RL_OB_OB, "Particle Object Visualisation");
574 if(part->dup_ob->type == OB_MBALL)
575 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA, "Particle Object Visualisation");
578 if(part->draw_as == PART_DRAW_GR && part->dup_group) {
579 for(go=part->dup_group->gobject.first; go; go=go->next) {
580 node2 = dag_get_node(dag, go->ob);
581 dag_add_relation(dag, node, node2, DAG_RL_OB_OB, "Particle Group Visualisation");
585 if(psys->effectors.first)
586 psys_end_effectors(psys);
587 psys_init_effectors(scene, ob, psys->part->eff_group, psys);
589 if(psys->effectors.first) {
590 for(nec= psys->effectors.first; nec; nec= nec->next) {
591 Object *ob1= nec->ob;
593 if(nec->type & PSYS_EC_EFFECTOR) {
594 node2 = dag_get_node(dag, ob1);
595 if(ob1->pd->forcefield==PFIELD_GUIDE)
596 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Particle Field");
598 dag_add_relation(dag, node2, node, DAG_RL_OB_DATA, "Particle Field");
600 else if(nec->type & PSYS_EC_DEFLECT) {
601 node2 = dag_get_node(dag, ob1);
602 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Particle Collision");
604 else if(nec->type & PSYS_EC_PARTICLE) {
605 node2 = dag_get_node(dag, ob1);
606 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA, "Particle Field");
609 if(nec->type & PSYS_EC_REACTOR) {
610 node2 = dag_get_node(dag, ob1);
611 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA, "Particle Reactor");
618 for (con = ob->constraints.first; con; con=con->next) {
619 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
620 ListBase targets = {NULL, NULL};
621 bConstraintTarget *ct;
623 if (cti && cti->get_constraint_targets) {
624 cti->get_constraint_targets(con, &targets);
626 for (ct= targets.first; ct; ct= ct->next) {
634 node2 = dag_get_node(dag, obt);
635 if (ELEM(con->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO))
636 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB|DAG_RL_OB_OB, cti->name);
638 if (ELEM3(obt->type, OB_ARMATURE, OB_MESH, OB_LATTICE) && (ct->subtarget[0]))
639 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB|DAG_RL_OB_OB, cti->name);
641 dag_add_relation(dag, node2, node, DAG_RL_OB_OB, cti->name);
646 if (cti->flush_constraint_targets)
647 cti->flush_constraint_targets(con, &targets, 1);
652 dag_add_relation(dag,scenenode,node,DAG_RL_SCENE, "Scene Relation");
655 struct DagForest *build_dag(struct Scene *sce, short mask)
675 /* add base node for scene. scene is always the first node in DAG */
676 scenenode = dag_add_node(dag, sce);
678 /* add current scene objects */
679 for(base = sce->base.first; base; base= base->next) {
682 build_dag_object(dag, scenenode, sce, ob, mask);
684 build_dag_object(dag, scenenode, sce, ob->proxy, mask);
686 /* handled in next loop */
688 ob->dup_group->id.flag |= LIB_DOIT;
691 /* add groups used in current scene objects */
692 for(group= G.main->group.first; group; group= group->id.next) {
693 if(group->id.flag & LIB_DOIT) {
694 for(go= group->gobject.first; go; go= go->next) {
695 build_dag_object(dag, scenenode, sce, go->ob, mask);
697 group->id.flag &= ~LIB_DOIT;
701 /* Now all relations were built, but we need to solve 1 exceptional case;
702 When objects have multiple "parents" (for example parent + constraint working on same object)
703 the relation type has to be synced. One of the parents can change, and should give same event to child */
705 /* nodes were callocced, so we can use node->color for temporal storage */
706 for(node = sce->theDag->DagNode.first; node; node= node->next) {
707 if(node->type==ID_OB) {
708 for(itA = node->child; itA; itA= itA->next) {
709 if(itA->node->type==ID_OB) {
710 itA->node->color |= itA->type;
715 /* now set relations equal, so that when only one parent changes, the correct recalcs are found */
716 for(node = sce->theDag->DagNode.first; node; node= node->next) {
717 if(node->type==ID_OB) {
718 for(itA = node->child; itA; itA= itA->next) {
719 if(itA->node->type==ID_OB) {
720 itA->type |= itA->node->color;
726 // cycle detection and solving
727 // solve_cycles(dag);
733 void free_forest(DagForest *Dag)
734 { /* remove all nodes and deps */
738 DagNode *itN = Dag->DagNode.first;
760 BLI_ghash_free(Dag->nodeHash, NULL, NULL);
762 Dag->DagNode.first = NULL;
763 Dag->DagNode.last = NULL;
768 DagNode * dag_find_node (DagForest *forest,void * fob)
771 return BLI_ghash_lookup(forest->nodeHash, fob);
776 static int ugly_hack_sorry= 1; // prevent type check
778 /* no checking of existance, use dag_find_node first or dag_get_node */
779 DagNode * dag_add_node (DagForest *forest, void * fob)
783 node = MEM_callocN(sizeof(DagNode),"DAG node");
786 node->color = DAG_WHITE;
788 if(ugly_hack_sorry) node->type = GS(((ID *) fob)->name); // sorry, done for pose sorting
789 if (forest->numNodes) {
790 ((DagNode *) forest->DagNode.last)->next = node;
791 forest->DagNode.last = node;
794 forest->DagNode.last = node;
795 forest->DagNode.first = node;
796 forest->numNodes = 1;
799 if(!forest->nodeHash)
800 forest->nodeHash= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp);
801 BLI_ghash_insert(forest->nodeHash, fob, node);
807 DagNode * dag_get_node (DagForest *forest,void * fob)
811 node = dag_find_node (forest, fob);
813 node = dag_add_node(forest, fob);
819 DagNode * dag_get_sub_node (DagForest *forest,void * fob)
822 DagAdjList *mainchild, *prev=NULL;
824 mainchild = ((DagNode *) forest->DagNode.first)->child;
825 /* remove from first node (scene) adj list if present */
827 if (mainchild->node == fob) {
829 prev->next = mainchild->next;
830 MEM_freeN(mainchild);
833 ((DagNode *) forest->DagNode.first)->child = mainchild->next;
834 MEM_freeN(mainchild);
839 mainchild = mainchild->next;
841 node = dag_find_node (forest, fob);
843 node = dag_add_node(forest, fob);
847 static void dag_add_parent_relation(DagForest *forest, DagNode *fob1, DagNode *fob2, short rel, char *name)
849 DagAdjList *itA = fob2->parent;
851 while (itA) { /* search if relation exist already */
852 if (itA->node == fob1) {
859 /* create new relation and insert at head. MALLOC alert! */
860 itA = MEM_mallocN(sizeof(DagAdjList),"DAG adj list");
864 itA->next = fob2->parent;
869 void dag_add_relation(DagForest *forest, DagNode *fob1, DagNode *fob2, short rel, char *name)
871 DagAdjList *itA = fob1->child;
873 /* parent relation is for cycle checking */
874 dag_add_parent_relation(forest, fob1, fob2, rel, name);
876 while (itA) { /* search if relation exist already */
877 if (itA->node == fob2) {
884 /* create new relation and insert at head. MALLOC alert! */
885 itA = MEM_mallocN(sizeof(DagAdjList),"DAG adj list");
889 itA->next = fob1->child;
894 static char *dag_node_name(DagNode *node)
898 else if(ugly_hack_sorry)
899 return ((ID*)(node->ob))->name+2;
901 return ((bPoseChannel*)(node->ob))->name;
905 static void dag_node_print_dependencies(DagNode *node)
909 printf("%s depends on:\n", dag_node_name(node));
911 for(itA= node->parent; itA; itA= itA->next)
912 printf(" %s through %s\n", dag_node_name(itA->node), itA->name);
917 static int dag_node_print_dependency_recurs(DagNode *node, DagNode *endnode)
921 if(node->color == DAG_BLACK)
924 node->color= DAG_BLACK;
929 for(itA= node->parent; itA; itA= itA->next) {
930 if(dag_node_print_dependency_recurs(itA->node, endnode)) {
931 printf(" %s depends on %s through %s.\n", dag_node_name(node), dag_node_name(itA->node), itA->name);
939 static void dag_node_print_dependency_cycle(DagForest *dag, DagNode *startnode, DagNode *endnode, char *name)
943 for(node = dag->DagNode.first; node; node= node->next)
944 node->color= DAG_WHITE;
946 printf(" %s depends on %s through %s.\n", dag_node_name(endnode), dag_node_name(startnode), name);
947 dag_node_print_dependency_recurs(startnode, endnode);
951 static int dag_node_recurs_level(DagNode *node, int level)
956 node->color= DAG_BLACK; /* done */
959 for(itA= node->parent; itA; itA= itA->next) {
960 if(itA->node->color==DAG_WHITE) {
961 itA->node->ancestor_count= dag_node_recurs_level(itA->node, level);
962 newlevel= MAX2(newlevel, level+itA->node->ancestor_count);
965 newlevel= MAX2(newlevel, level+itA->node->ancestor_count);
971 static void dag_check_cycle(DagForest *dag)
976 /* tag nodes unchecked */
977 for(node = dag->DagNode.first; node; node= node->next)
978 node->color= DAG_WHITE;
980 for(node = dag->DagNode.first; node; node= node->next) {
981 if(node->color==DAG_WHITE) {
982 node->ancestor_count= dag_node_recurs_level(node, 0);
986 /* check relations, and print errors */
987 for(node = dag->DagNode.first; node; node= node->next) {
988 for(itA= node->parent; itA; itA= itA->next) {
989 if(itA->node->ancestor_count > node->ancestor_count) {
990 if(node->ob && itA->node->ob) {
991 printf("Dependency cycle detected:\n");
992 dag_node_print_dependency_cycle(dag, itA->node, node, itA->name);
998 /* parent relations are only needed for cycle checking, so free now */
999 for(node = dag->DagNode.first; node; node= node->next) {
1000 while (node->parent) {
1001 itA = node->parent->next;
1002 MEM_freeN(node->parent);
1009 * MainDAG is the DAG of all objects in current scene
1010 * used only for drawing there is one also in each scene
1012 static DagForest * MainDag = NULL;
1014 DagForest *getMainDag(void)
1020 void setMainDag(DagForest *dag)
1028 * in theory we should sweep the whole array
1029 * but in our case the first node is the scene
1030 * and is linked to every other object
1032 * for general case we will need to add outer loop
1036 * ToDo : change pos kludge
1039 /* adjust levels for drawing in oops space */
1040 void graph_bfs(void)
1043 DagNodeQueue *nqueue;
1049 /* fprintf(stderr,"starting BFS \n ------------\n"); */
1050 nqueue = queue_create(DAGQUEUEALLOC);
1051 for ( i=0; i<50; i++)
1055 * dagnode.first is alway the root (scene)
1057 node = MainDag->DagNode.first;
1059 node->color = DAG_WHITE;
1060 node->BFS_dist = 9999;
1065 node = MainDag->DagNode.first;
1066 if (node->color == DAG_WHITE) {
1067 node->color = DAG_GRAY;
1069 push_queue(nqueue,node);
1070 while(nqueue->count) {
1071 node = pop_queue(nqueue);
1073 minheight = pos[node->BFS_dist];
1075 while(itA != NULL) {
1076 if((itA->node->color == DAG_WHITE) ) {
1077 itA->node->color = DAG_GRAY;
1078 itA->node->BFS_dist = node->BFS_dist + 1;
1079 itA->node->k = (float) minheight;
1080 push_queue(nqueue,itA->node);
1084 fprintf(stderr,"bfs not dag tree edge color :%i \n",itA->node->color);
1090 if (pos[node->BFS_dist] > node->k ) {
1091 pos[node->BFS_dist] += 1;
1092 node->k = (float) pos[node->BFS_dist];
1094 pos[node->BFS_dist] = (int) node->k +1;
1096 set_node_xy(node, node->BFS_dist*DEPSX*2, pos[node->BFS_dist]*DEPSY*2);
1097 node->color = DAG_BLACK;
1099 fprintf(stderr,"BFS node : %20s %i %5.0f %5.0f\n",((ID *) node->ob)->name,node->BFS_dist, node->x, node->y);
1103 queue_delete(nqueue);
1106 int pre_and_post_BFS(DagForest *dag, short mask, graph_action_func pre_func, graph_action_func post_func, void **data) {
1109 node = dag->DagNode.first;
1110 return pre_and_post_source_BFS(dag, mask, node, pre_func, post_func, data);
1114 int pre_and_post_source_BFS(DagForest *dag, short mask, DagNode *source, graph_action_func pre_func, graph_action_func post_func, void **data)
1117 DagNodeQueue *nqueue;
1120 /* fprintf(stderr,"starting BFS \n ------------\n"); */
1123 * dagnode.first is alway the root (scene)
1125 node = dag->DagNode.first;
1126 nqueue = queue_create(DAGQUEUEALLOC);
1128 node->color = DAG_WHITE;
1129 node->BFS_dist = 9999;
1134 if (node->color == DAG_WHITE) {
1135 node->color = DAG_GRAY;
1137 pre_func(node->ob,data);
1139 while(nqueue->count) {
1140 node = pop_queue(nqueue);
1143 while(itA != NULL) {
1144 if((itA->node->color == DAG_WHITE) && (itA->type & mask)) {
1145 itA->node->color = DAG_GRAY;
1146 itA->node->BFS_dist = node->BFS_dist + 1;
1147 push_queue(nqueue,itA->node);
1148 pre_func(node->ob,data);
1151 else { // back or cross edge
1156 post_func(node->ob,data);
1157 node->color = DAG_BLACK;
1159 fprintf(stderr,"BFS node : %20s %i %5.0f %5.0f\n",((ID *) node->ob)->name,node->BFS_dist, node->x, node->y);
1163 queue_delete(nqueue);
1167 /* non recursive version of DFS, return queue -- outer loop present to catch odd cases (first level cycles)*/
1168 DagNodeQueue * graph_dfs(void)
1171 DagNodeQueue *nqueue;
1172 DagNodeQueue *retqueue;
1182 *fprintf(stderr,"starting DFS \n ------------\n");
1184 nqueue = queue_create(DAGQUEUEALLOC);
1185 retqueue = queue_create(MainDag->numNodes);
1186 for ( i=0; i<50; i++)
1190 * dagnode.first is alway the root (scene)
1192 node = MainDag->DagNode.first;
1194 node->color = DAG_WHITE;
1195 node->DFS_dist = 9999;
1196 node->DFS_dvtm = node->DFS_fntm = 9999;
1203 node = MainDag->DagNode.first;
1206 if (node->color == DAG_WHITE) {
1207 node->color = DAG_GRAY;
1209 node->DFS_dvtm = time;
1211 push_stack(nqueue,node);
1213 while(nqueue->count) {
1214 //graph_print_queue(nqueue);
1217 node = get_top_node_queue(nqueue);
1219 minheight = pos[node->DFS_dist];
1222 while(itA != NULL) {
1223 if((itA->node->color == DAG_WHITE) ) {
1224 itA->node->DFS_dvtm = time;
1225 itA->node->color = DAG_GRAY;
1228 itA->node->DFS_dist = node->DFS_dist + 1;
1229 itA->node->k = (float) minheight;
1230 push_stack(nqueue,itA->node);
1234 if (itA->node->color == DAG_GRAY) { // back edge
1235 fprintf(stderr,"dfs back edge :%15s %15s \n",((ID *) node->ob)->name, ((ID *) itA->node->ob)->name);
1237 } else if (itA->node->color == DAG_BLACK) {
1239 /* already processed node but we may want later to change distance either to shorter to longer.
1240 * DFS_dist is the first encounter
1242 /*if (node->DFS_dist >= itA->node->DFS_dist)
1243 itA->node->DFS_dist = node->DFS_dist + 1;
1245 fprintf(stderr,"dfs forward or cross edge :%15s %i-%i %15s %i-%i \n",
1246 ((ID *) node->ob)->name,
1249 ((ID *) itA->node->ob)->name,
1250 itA->node->DFS_dvtm,
1251 itA->node->DFS_fntm);
1254 fprintf(stderr,"dfs unknown edge \n");
1260 node = pop_queue(nqueue);
1261 node->color = DAG_BLACK;
1263 node->DFS_fntm = time;
1265 if (node->DFS_dist > maxpos)
1266 maxpos = node->DFS_dist;
1267 if (pos[node->DFS_dist] > node->k ) {
1268 pos[node->DFS_dist] += 1;
1269 node->k = (float) pos[node->DFS_dist];
1271 pos[node->DFS_dist] = (int) node->k +1;
1273 set_node_xy(node, node->DFS_dist*DEPSX*2, pos[node->DFS_dist]*DEPSY*2);
1276 fprintf(stderr,"DFS node : %20s %i %i %i %i\n",((ID *) node->ob)->name,node->BFS_dist, node->DFS_dist, node->DFS_dvtm, node->DFS_fntm );
1278 push_stack(retqueue,node);
1285 // fprintf(stderr,"i size : %i \n", maxpos);
1287 queue_delete(nqueue);
1292 int pre_and_post_DFS(DagForest *dag, short mask, graph_action_func pre_func, graph_action_func post_func, void **data) {
1295 node = dag->DagNode.first;
1296 return pre_and_post_source_DFS(dag, mask, node, pre_func, post_func, data);
1299 int pre_and_post_source_DFS(DagForest *dag, short mask, DagNode *source, graph_action_func pre_func, graph_action_func post_func, void **data)
1302 DagNodeQueue *nqueue;
1308 *fprintf(stderr,"starting DFS \n ------------\n");
1310 nqueue = queue_create(DAGQUEUEALLOC);
1313 * dagnode.first is alway the root (scene)
1315 node = dag->DagNode.first;
1317 node->color = DAG_WHITE;
1318 node->DFS_dist = 9999;
1319 node->DFS_dvtm = node->DFS_fntm = 9999;
1328 if (node->color == DAG_WHITE) {
1329 node->color = DAG_GRAY;
1331 node->DFS_dvtm = time;
1333 push_stack(nqueue,node);
1334 pre_func(node->ob,data);
1336 while(nqueue->count) {
1338 node = get_top_node_queue(nqueue);
1341 while(itA != NULL) {
1342 if((itA->node->color == DAG_WHITE) && (itA->type & mask) ) {
1343 itA->node->DFS_dvtm = time;
1344 itA->node->color = DAG_GRAY;
1347 itA->node->DFS_dist = node->DFS_dist + 1;
1348 push_stack(nqueue,itA->node);
1349 pre_func(node->ob,data);
1354 if (itA->node->color == DAG_GRAY) {// back edge
1357 // else if (itA->node->color == DAG_BLACK) { // cross or forward
1364 node = pop_queue(nqueue);
1365 node->color = DAG_BLACK;
1367 node->DFS_fntm = time;
1369 post_func(node->ob,data);
1375 queue_delete(nqueue);
1380 // used to get the obs owning a datablock
1381 struct DagNodeQueue *get_obparents(struct DagForest *dag, void *ob)
1383 DagNode * node, *node1;
1384 DagNodeQueue *nqueue;
1387 node = dag_find_node(dag,ob);
1391 else if (node->ancestor_count == 1) { // simple case
1392 nqueue = queue_create(1);
1393 push_queue(nqueue,node);
1394 } else { // need to go over the whole dag for adj list
1395 nqueue = queue_create(node->ancestor_count);
1397 node1 = dag->DagNode.first;
1399 if (node1->DFS_fntm > node->DFS_fntm) { // a parent is finished after child. must check adj list
1401 while(itA != NULL) {
1402 if ((itA->node == node) && (itA->type == DAG_RL_DATA)) {
1403 push_queue(nqueue,node);
1408 node1 = node1->next;
1414 struct DagNodeQueue *get_first_ancestors(struct DagForest *dag, void *ob)
1416 DagNode * node, *node1;
1417 DagNodeQueue *nqueue;
1420 node = dag_find_node(dag,ob);
1422 // need to go over the whole dag for adj list
1423 nqueue = queue_create(node->ancestor_count);
1425 node1 = dag->DagNode.first;
1427 if (node1->DFS_fntm > node->DFS_fntm) {
1429 while(itA != NULL) {
1430 if (itA->node == node) {
1431 push_queue(nqueue,node);
1436 node1 = node1->next;
1442 // standard DFS list
1443 struct DagNodeQueue *get_all_childs(struct DagForest *dag, void *ob)
1446 DagNodeQueue *nqueue;
1447 DagNodeQueue *retqueue;
1452 nqueue = queue_create(DAGQUEUEALLOC);
1453 retqueue = queue_create(dag->numNodes); // was MainDag... why? (ton)
1455 node = dag->DagNode.first;
1457 node->color = DAG_WHITE;
1463 node = dag_find_node(dag, ob); // could be done in loop above (ton)
1464 if(node) { // can be null for newly added objects
1466 node->color = DAG_GRAY;
1468 push_stack(nqueue,node);
1470 while(nqueue->count) {
1473 node = get_top_node_queue(nqueue);
1476 while(itA != NULL) {
1477 if((itA->node->color == DAG_WHITE) ) {
1478 itA->node->DFS_dvtm = time;
1479 itA->node->color = DAG_GRAY;
1482 push_stack(nqueue,itA->node);
1490 node = pop_queue(nqueue);
1491 node->color = DAG_BLACK;
1494 push_stack(retqueue,node);
1498 queue_delete(nqueue);
1503 short are_obs_related(struct DagForest *dag, void *ob1, void *ob2) {
1507 node = dag_find_node(dag, ob1);
1510 while(itA != NULL) {
1511 if((itA->node->ob == ob2) ) {
1512 return itA->node->type;
1516 return DAG_NO_RELATION;
1519 int is_acyclic( DagForest *dag) {
1520 return dag->is_acyclic;
1523 void set_node_xy(DagNode *node, float x, float y)
1530 /* debug test functions */
1532 void graph_print_queue(DagNodeQueue *nqueue)
1534 DagNodeQueueElem *queueElem;
1536 queueElem = nqueue->first;
1538 fprintf(stderr,"** %s %i %i-%i ",((ID *) queueElem->node->ob)->name,queueElem->node->color,queueElem->node->DFS_dvtm,queueElem->node->DFS_fntm);
1539 queueElem = queueElem->next;
1541 fprintf(stderr,"\n");
1544 void graph_print_queue_dist(DagNodeQueue *nqueue)
1546 DagNodeQueueElem *queueElem;
1549 queueElem = nqueue->first;
1550 max = queueElem->node->DFS_fntm;
1553 fprintf(stderr,"** %25s %2.2i-%2.2i ",((ID *) queueElem->node->ob)->name,queueElem->node->DFS_dvtm,queueElem->node->DFS_fntm);
1554 while (count < queueElem->node->DFS_dvtm-1) { fputc(' ',stderr); count++;}
1556 while (count < queueElem->node->DFS_fntm-2) { fputc('-',stderr); count++;}
1560 queueElem = queueElem->next;
1562 fprintf(stderr,"\n");
1565 void graph_print_adj_list(void)
1570 node = (getMainDag())->DagNode.first;
1572 fprintf(stderr,"node : %s col: %i",((ID *) node->ob)->name, node->color);
1575 fprintf(stderr,"-- %s ",((ID *) itA->node->ob)->name);
1579 fprintf(stderr,"\n");
1584 /* ************************ API *********************** */
1586 /* groups with objects in this scene need to be put in the right order as well */
1587 static void scene_sort_groups(Scene *sce)
1594 /* test; are group objects all in this scene? */
1595 for(ob= G.main->object.first; ob; ob= ob->id.next) {
1596 ob->id.flag &= ~LIB_DOIT;
1597 ob->id.newid= NULL; /* newid abuse for GroupObject */
1599 for(base = sce->base.first; base; base= base->next)
1600 base->object->id.flag |= LIB_DOIT;
1602 for(group= G.main->group.first; group; group= group->id.next) {
1603 for(go= group->gobject.first; go; go= go->next) {
1604 if((go->ob->id.flag & LIB_DOIT)==0)
1607 /* this group is entirely in this scene */
1609 ListBase listb= {NULL, NULL};
1611 for(go= group->gobject.first; go; go= go->next)
1612 go->ob->id.newid= (ID *)go;
1614 /* in order of sorted bases we reinsert group objects */
1615 for(base = sce->base.first; base; base= base->next) {
1617 if(base->object->id.newid) {
1618 go= (GroupObject *)base->object->id.newid;
1619 base->object->id.newid= NULL;
1620 BLI_remlink( &group->gobject, go);
1621 BLI_addtail( &listb, go);
1624 /* copy the newly sorted listbase */
1625 group->gobject= listb;
1630 /* sort the base list on dependency order */
1631 void DAG_scene_sort(struct Scene *sce)
1634 DagNodeQueue *nqueue;
1641 tempbase.first= tempbase.last= NULL;
1643 build_dag(sce, DAG_RL_ALL_BUT_DATA);
1645 dag_check_cycle(sce->theDag);
1647 nqueue = queue_create(DAGQUEUEALLOC);
1649 for(node = sce->theDag->DagNode.first; node; node= node->next) {
1650 node->color = DAG_WHITE;
1655 node = sce->theDag->DagNode.first;
1657 node->color = DAG_GRAY;
1659 push_stack(nqueue,node);
1661 while(nqueue->count) {
1664 node = get_top_node_queue(nqueue);
1667 while(itA != NULL) {
1668 if((itA->node->color == DAG_WHITE) ) {
1669 itA->node->DFS_dvtm = time;
1670 itA->node->color = DAG_GRAY;
1673 push_stack(nqueue,itA->node);
1682 node = pop_queue(nqueue);
1683 if (node->ob == sce) // we are done
1685 node->color = DAG_BLACK;
1688 base = sce->base.first;
1689 while (base && base->object != node->ob)
1692 BLI_remlink(&sce->base,base);
1693 BLI_addhead(&tempbase,base);
1699 // temporal correction for circular dependancies
1700 base = sce->base.first;
1702 BLI_remlink(&sce->base,base);
1703 BLI_addhead(&tempbase,base);
1705 printf("cyclic %s\n", base->object->id.name);
1706 base = sce->base.first;
1709 sce->base = tempbase;
1710 queue_delete(nqueue);
1712 /* all groups with objects in this scene gets resorted too */
1713 scene_sort_groups(sce);
1716 printf("\nordered\n");
1717 for(base = sce->base.first; base; base= base->next) {
1718 printf(" %s\n", base->object->id.name);
1722 sce->recalc |= SCE_PRV_CHANGED; /* test for 3d preview */
1725 /* node was checked to have lasttime != curtime and is if type ID_OB */
1726 static void flush_update_node(DagNode *node, unsigned int layer, int curtime)
1730 int oldflag, changed=0;
1731 unsigned int all_layer;
1733 node->lasttime= curtime;
1736 if(ob && (ob->recalc & OB_RECALC)) {
1739 /* got an object node that changes, now check relations */
1740 for(itA = node->child; itA; itA= itA->next) {
1741 all_layer |= itA->lay;
1742 /* the relationship is visible */
1743 if((itA->lay & layer)) { // XXX || (itA->node->ob == obedit)
1744 if(itA->node->type==ID_OB) {
1746 oldflag= obc->recalc;
1748 /* got a ob->obc relation, now check if flag needs flush */
1749 if(ob->recalc & OB_RECALC_OB) {
1750 if(itA->type & DAG_RL_OB_OB) {
1751 //printf("ob %s changes ob %s\n", ob->id.name, obc->id.name);
1752 obc->recalc |= OB_RECALC_OB;
1754 if(itA->type & DAG_RL_OB_DATA) {
1755 //printf("ob %s changes obdata %s\n", ob->id.name, obc->id.name);
1756 obc->recalc |= OB_RECALC_DATA;
1759 if(ob->recalc & OB_RECALC_DATA) {
1760 if(itA->type & DAG_RL_DATA_OB) {
1761 //printf("obdata %s changes ob %s\n", ob->id.name, obc->id.name);
1762 obc->recalc |= OB_RECALC_OB;
1764 if(itA->type & DAG_RL_DATA_DATA) {
1765 //printf("obdata %s changes obdata %s\n", ob->id.name, obc->id.name);
1766 obc->recalc |= OB_RECALC_DATA;
1769 if(oldflag!=obc->recalc) changed= 1;
1773 /* even nicer, we can clear recalc flags... */
1774 if((all_layer & layer)==0) { // XXX && (ob != obedit)) {
1775 /* but existing displaylists or derivedmesh should be freed */
1776 if(ob->recalc & OB_RECALC_DATA)
1777 object_free_display(ob);
1779 ob->recalc &= ~OB_RECALC;
1783 /* check case where child changes and parent forcing obdata to change */
1784 /* should be done regardless if this ob has recalc set */
1785 /* could merge this in with loop above...? (ton) */
1786 for(itA = node->child; itA; itA= itA->next) {
1787 /* the relationship is visible */
1788 if((itA->lay & layer)) { // XXX || (itA->node->ob == obedit)
1789 if(itA->node->type==ID_OB) {
1792 if((obc->recalc & OB_RECALC)==OB_RECALC_OB) {
1793 /* parent has deforming info */
1794 if(itA->type & (DAG_RL_OB_DATA|DAG_RL_DATA_DATA)) {
1795 // printf("parent %s changes ob %s\n", ob->id.name, obc->id.name);
1796 obc->recalc |= OB_RECALC_DATA;
1803 /* we only go deeper if node not checked or something changed */
1804 for(itA = node->child; itA; itA= itA->next) {
1805 if(changed || itA->node->lasttime!=curtime)
1806 flush_update_node(itA->node, layer, curtime);
1811 /* node was checked to have lasttime != curtime , and is of type ID_OB */
1812 static unsigned int flush_layer_node(Scene *sce, DagNode *node, int curtime)
1816 node->lasttime= curtime;
1817 node->lay= node->scelay;
1819 for(itA = node->child; itA; itA= itA->next) {
1820 if(itA->node->type==ID_OB) {
1821 if(itA->node->lasttime!=curtime) {
1822 itA->lay= flush_layer_node(sce, itA->node, curtime); // lay is only set once for each relation
1824 else itA->lay= itA->node->lay;
1826 node->lay |= itA->lay;
1833 /* node was checked to have lasttime != curtime , and is of type ID_OB */
1834 static void flush_pointcache_reset(Scene *scene, DagNode *node, int curtime, int reset)
1839 node->lasttime= curtime;
1841 for(itA = node->child; itA; itA= itA->next) {
1842 if(itA->node->type==ID_OB) {
1843 if(itA->node->lasttime!=curtime) {
1844 ob= (Object*)(node->ob);
1846 if(reset || (ob->recalc & OB_RECALC)) {
1847 if(BKE_ptcache_object_reset(scene, ob, PTCACHE_RESET_DEPSGRAPH))
1848 ob->recalc |= OB_RECALC_DATA;
1850 flush_pointcache_reset(scene, itA->node, curtime, 1);
1853 flush_pointcache_reset(scene, itA->node, curtime, 0);
1859 /* flushes all recalc flags in objects down the dependency tree */
1860 void DAG_scene_flush_update(Scene *sce, unsigned int lay, int time)
1862 DagNode *firstnode, *node;
1868 if(sce->theDag==NULL) {
1869 printf("DAG zero... not allowed to happen!\n");
1870 DAG_scene_sort(sce);
1873 firstnode= sce->theDag->DagNode.first; // always scene node
1875 for(itA = firstnode->child; itA; itA= itA->next)
1878 /* first we flush the layer flags */
1879 sce->theDag->time++; // so we know which nodes were accessed
1880 lasttime= sce->theDag->time;
1883 for(base= sce->base.first; base; base= base->next) {
1884 node= dag_get_node(sce->theDag, base->object);
1886 node->scelay= base->object->lay;
1891 for(itA = firstnode->child; itA; itA= itA->next)
1892 if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB)
1893 flush_layer_node(sce, itA->node, lasttime);
1895 /* then we use the relationships + layer info to flush update events */
1896 sce->theDag->time++; // so we know which nodes were accessed
1897 lasttime= sce->theDag->time;
1898 for(itA = firstnode->child; itA; itA= itA->next)
1899 if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB)
1900 flush_update_node(itA->node, lay, lasttime);
1902 /* if update is not due to time change, do pointcache clears */
1904 sce->theDag->time++; // so we know which nodes were accessed
1905 lasttime= sce->theDag->time;
1906 for(itA = firstnode->child; itA; itA= itA->next) {
1907 if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB) {
1908 ob= (Object*)(itA->node->ob);
1910 if(ob->recalc & OB_RECALC) {
1911 if(BKE_ptcache_object_reset(sce, ob, PTCACHE_RESET_DEPSGRAPH))
1912 ob->recalc |= OB_RECALC_DATA;
1914 flush_pointcache_reset(sce, itA->node, lasttime, 1);
1917 flush_pointcache_reset(sce, itA->node, lasttime, 0);
1923 static int object_modifiers_use_time(Object *ob)
1927 for (md=ob->modifiers.first; md; md=md->next)
1928 if (modifier_dependsOnTime(md))
1934 static short animdata_use_time(AnimData *adt)
1938 if(adt==NULL) return 0;
1940 /* check action - only if assigned, and it has anim curves */
1941 if (adt->action && adt->action->curves.first)
1944 /* check NLA tracks + strips */
1945 for (nlt= adt->nla_tracks.first; nlt; nlt= nlt->next) {
1946 if (nlt->strips.first)
1953 static void dag_object_time_update_flags(Object *ob)
1955 if(ob->constraints.first) {
1957 for (con = ob->constraints.first; con; con=con->next) {
1958 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
1959 ListBase targets = {NULL, NULL};
1960 bConstraintTarget *ct;
1962 if (cti && cti->get_constraint_targets) {
1963 cti->get_constraint_targets(con, &targets);
1965 for (ct= targets.first; ct; ct= ct->next) {
1967 ob->recalc |= OB_RECALC_OB;
1972 if (cti->flush_constraint_targets)
1973 cti->flush_constraint_targets(con, &targets, 1);
1978 if(ob->scriptlink.totscript) ob->recalc |= OB_RECALC_OB;
1981 /* motion path or bone child */
1982 if(ob->parent->type==OB_CURVE || ob->parent->type==OB_ARMATURE) ob->recalc |= OB_RECALC_OB;
1985 #if 0 // XXX old animation system
1986 if(ob->nlastrips.first) {
1988 bActionStrip *strip;
1989 /* this case is for groups with nla, whilst nla target has no action or nla */
1990 for(strip= ob->nlastrips.first; strip; strip= strip->next) {
1992 strip->object->recalc |= OB_RECALC;
1996 #endif // XXX old animation system
1998 if(animdata_use_time(ob->adt)) {
1999 ob->recalc |= OB_RECALC;
2000 ob->adt->recalc |= ADT_RECALC_ANIM;
2003 if((ob->adt) && (ob->type==OB_ARMATURE)) ob->recalc |= OB_RECALC_DATA;
2005 if(object_modifiers_use_time(ob)) ob->recalc |= OB_RECALC_DATA;
2006 if((ob->pose) && (ob->pose->flag & POSE_CONSTRAINTS_TIMEDEPEND)) ob->recalc |= OB_RECALC_DATA;
2017 if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
2018 ob->recalc |= OB_RECALC_DATA;
2019 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
2022 if(ob->particlesystem.first)
2023 ob->recalc |= OB_RECALC_DATA;
2029 if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
2030 ob->recalc |= OB_RECALC_DATA;
2031 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
2037 if(cu->nurb.first==NULL && cu->str && cu->vfont)
2038 ob->recalc |= OB_RECALC_DATA;
2043 if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
2044 ob->recalc |= OB_RECALC_DATA;
2045 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
2050 if(ob->transflag & OB_DUPLI) ob->recalc |= OB_RECALC_DATA;
2054 if(ob->particlesystem.first) {
2055 ParticleSystem *psys= ob->particlesystem.first;
2057 for(; psys; psys=psys->next) {
2058 if(psys_check_enabled(ob, psys)) {
2059 ob->recalc |= OB_RECALC_DATA;
2067 /* flag all objects that need recalc, for changes in time for example */
2068 void DAG_scene_update_flags(Scene *scene, unsigned int lay)
2076 /* set ob flags where animated systems are */
2077 for(SETLOOPER(scene, base)) {
2080 /* now if DagNode were part of base, the node->lay could be checked... */
2081 /* we do all now, since the scene_flush checks layers and clears recalc flags even */
2082 dag_object_time_update_flags(ob);
2084 /* handled in next loop */
2086 ob->dup_group->id.flag |= LIB_DOIT;
2089 /* we do groups each once */
2090 for(group= G.main->group.first; group; group= group->id.next) {
2091 if(group->id.flag & LIB_DOIT) {
2092 for(go= group->gobject.first; go; go= go->next) {
2093 dag_object_time_update_flags(go->ob);
2098 for(sce= scene; sce; sce= sce->set)
2099 DAG_scene_flush_update(sce, lay, 1);
2101 /* test: set time flag, to disable baked systems to update */
2102 for(SETLOOPER(scene, base)) {
2105 ob->recalc |= OB_RECALC_TIME;
2108 /* hrmf... an exception to look at once, for invisible camera object we do it over */
2110 dag_object_time_update_flags(scene->camera);
2112 /* and store the info in groupobject */
2113 for(group= G.main->group.first; group; group= group->id.next) {
2114 if(group->id.flag & LIB_DOIT) {
2115 for(go= group->gobject.first; go; go= go->next) {
2116 go->recalc= go->ob->recalc;
2117 // printf("ob %s recalc %d\n", go->ob->id.name, go->recalc);
2119 group->id.flag &= ~LIB_DOIT;
2126 /* flag this object and all its relations to recalc */
2127 /* if you need to do more objects, tag object yourself and
2128 use DAG_scene_flush_update() in end */
2129 void DAG_object_flush_update(Scene *sce, Object *ob, short flag)
2132 if(ob==NULL || sce->theDag==NULL) return;
2135 BKE_ptcache_object_reset(sce, ob, PTCACHE_RESET_DEPSGRAPH);
2137 /* all users of this ob->data should be checked */
2138 /* BUT! displists for curves are still only on cu */
2139 if(flag & OB_RECALC_DATA) {
2140 if(ob->type!=OB_CURVE && ob->type!=OB_SURF) {
2142 if(id && id->us>1) {
2143 /* except when there's a key and shapes are locked */
2144 if(ob_get_key(ob) && (ob->shapeflag & (OB_SHAPE_LOCK|OB_SHAPE_TEMPLOCK)));
2147 for (obt=G.main->object.first; obt; obt= obt->id.next) {
2148 if (obt != ob && obt->data==ob->data) {
2149 obt->recalc |= OB_RECALC_DATA;
2150 BKE_ptcache_object_reset(sce, obt, PTCACHE_RESET_DEPSGRAPH);
2158 // XXX if(G.curscreen)
2159 // DAG_scene_flush_update(sce, dag_screen_view3d_layers(), 0);
2161 DAG_scene_flush_update(sce, sce->lay, 0);
2164 /* recursively descends tree, each node only checked once */
2165 /* node is checked to be of type object */
2166 static int parent_check_node(DagNode *node, int curtime)
2170 node->lasttime= curtime;
2172 if(node->color==DAG_GRAY)
2175 for(itA = node->child; itA; itA= itA->next) {
2176 if(itA->node->type==ID_OB) {
2178 if(itA->node->color==DAG_GRAY)
2181 /* descend if not done */
2182 if(itA->node->lasttime!=curtime) {
2183 itA->node->color= parent_check_node(itA->node, curtime);
2185 if(itA->node->color==DAG_GRAY)
2194 /* all nodes that influence this object get tagged, for calculating the exact
2195 position of this object at a given timeframe */
2196 void DAG_object_update_flags(Scene *sce, Object *ob, unsigned int lay)
2201 /* tag nodes unchecked */
2202 for(node = sce->theDag->DagNode.first; node; node= node->next)
2203 node->color = DAG_WHITE;
2205 node= dag_find_node(sce->theDag, ob);
2207 /* object not in scene? then handle group exception. needs to be dagged once too */
2210 while( (group = find_group(ob, group)) ) {
2212 /* primitive; tag all... this call helps building groups for particles */
2213 for(go= group->gobject.first; go; go= go->next)
2214 go->ob->recalc= OB_RECALC;
2219 node->color = DAG_GRAY;
2221 sce->theDag->time++;
2222 node= sce->theDag->DagNode.first;
2223 for(itA = node->child; itA; itA= itA->next) {
2224 if(itA->node->type==ID_OB && itA->node->lasttime!=sce->theDag->time)
2225 itA->node->color= parent_check_node(itA->node, sce->theDag->time);
2228 /* set recalcs and flushes */
2229 DAG_scene_update_flags(sce, lay);
2231 /* now we clear recalcs, unless color is set */
2232 for(node = sce->theDag->DagNode.first; node; node= node->next) {
2233 if(node->type==ID_OB && node->color==DAG_WHITE) {
2234 Object *ob= node->ob;
2241 /* ******************* DAG FOR ARMATURE POSE ***************** */
2243 /* we assume its an armature with pose */
2244 void DAG_pose_sort(Object *ob)
2246 bPose *pose= ob->pose;
2247 bPoseChannel *pchan;
2250 DagNode *node2, *node3;
2253 DagNodeQueue *nqueue;
2259 ugly_hack_sorry= 0; // no ID structs
2261 rootnode = dag_add_node(dag, NULL); // node->ob becomes NULL
2263 /* we add the hierarchy and the constraints */
2264 for(pchan = pose->chanbase.first; pchan; pchan= pchan->next) {
2267 node = dag_get_node(dag, pchan);
2270 node2 = dag_get_node(dag, pchan->parent);
2271 dag_add_relation(dag, node2, node, 0, "Parent Relation");
2274 for (con = pchan->constraints.first; con; con=con->next) {
2275 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
2276 ListBase targets = {NULL, NULL};
2277 bConstraintTarget *ct;
2279 #if 0 // XXX old animation system... driver stuff to watch out for
2282 for(icu= con->ipo->curve.first; icu; icu= icu->next) {
2283 /* icu->driver->ob should actually point to ob->proxy if it
2284 * is a proxy, but since it wasn't set correct it older
2285 * files comparing with ob->proxy makes it work for those */
2286 if(icu->driver && (icu->driver->ob==ob || icu->driver->ob==ob->proxy)) {
2287 bPoseChannel *target= get_pose_channel(ob->pose, icu->driver->name);
2289 node2 = dag_get_node(dag, target);
2290 dag_add_relation(dag, node2, node, 0, "Ipo Driver");
2292 /* uncommented this line, results in dependencies
2293 * not being added properly for this constraint,
2294 * what is the purpose of this? - brecht */
2295 /*cti= NULL;*/ /* trick to get next loop skipped */
2300 #endif // XXX old animation system... driver stuff to watch out for
2302 if (cti && cti->get_constraint_targets) {
2303 cti->get_constraint_targets(con, &targets);
2305 for (ct= targets.first; ct; ct= ct->next) {
2306 if (ct->tar==ob && ct->subtarget[0]) {
2307 bPoseChannel *target= get_pose_channel(ob->pose, ct->subtarget);
2309 node2= dag_get_node(dag, target);
2310 dag_add_relation(dag, node2, node, 0, "IK Constraint");
2312 if (con->type==CONSTRAINT_TYPE_KINEMATIC) {
2313 bKinematicConstraint *data = (bKinematicConstraint *)con->data;
2314 bPoseChannel *parchan;
2317 /* exclude tip from chain? */
2318 if(!(data->flag & CONSTRAINT_IK_TIP))
2319 parchan= pchan->parent;
2323 /* Walk to the chain's root */
2325 node3= dag_get_node(dag, parchan);
2326 dag_add_relation(dag, node2, node3, 0, "IK Constraint");
2329 if (segcount==data->rootbone || segcount>255) break; // 255 is weak
2330 parchan= parchan->parent;
2337 if (cti->flush_constraint_targets)
2338 cti->flush_constraint_targets(con, &targets, 1);
2341 if (addtoroot == 1 ) {
2342 dag_add_relation(dag, rootnode, node, 0, "Root Bone Relation");
2346 dag_check_cycle(dag);
2348 /* now we try to sort... */
2349 tempbase.first= tempbase.last= NULL;
2351 nqueue = queue_create(DAGQUEUEALLOC);
2353 /* tag nodes unchecked */
2354 for(node = dag->DagNode.first; node; node= node->next)
2355 node->color = DAG_WHITE;
2357 node = dag->DagNode.first;
2359 node->color = DAG_GRAY;
2360 push_stack(nqueue, node);
2362 while(nqueue->count) {
2365 node = get_top_node_queue(nqueue);
2368 while(itA != NULL) {
2369 if((itA->node->color == DAG_WHITE) ) {
2370 itA->node->color = DAG_GRAY;
2371 push_stack(nqueue,itA->node);
2380 node = pop_queue(nqueue);
2381 if (node->ob == NULL) // we are done
2383 node->color = DAG_BLACK;
2385 /* put node in new list */
2386 BLI_remlink(&pose->chanbase, node->ob);
2387 BLI_addhead(&tempbase, node->ob);
2392 // temporal correction for circular dependancies
2393 while(pose->chanbase.first) {
2394 pchan= pose->chanbase.first;
2395 BLI_remlink(&pose->chanbase, pchan);
2396 BLI_addhead(&tempbase, pchan);
2398 printf("cyclic %s\n", pchan->name);
2401 pose->chanbase = tempbase;
2402 queue_delete(nqueue);
2404 // printf("\nordered\n");
2405 // for(pchan = pose->chanbase.first; pchan; pchan= pchan->next) {
2406 // printf(" %s\n", pchan->name);