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 "BMF_Api.h"
38 #include "BLI_blenlib.h"
39 #include "BLI_arithb.h"
41 #include "DNA_action_types.h"
42 #include "DNA_armature_types.h"
43 #include "DNA_curve_types.h"
44 #include "DNA_camera_types.h"
46 #include "DNA_effect_types.h"
47 #include "DNA_group_types.h"
48 #include "DNA_lattice_types.h"
49 #include "DNA_lamp_types.h"
50 #include "DNA_key_types.h"
51 #include "DNA_mesh_types.h"
52 #include "DNA_modifier_types.h"
53 #include "DNA_nla_types.h"
54 #include "DNA_object_types.h"
55 #include "DNA_object_force.h"
56 #include "DNA_object_fluidsim.h"
57 #include "DNA_oops_types.h"
58 #include "DNA_particle_types.h"
59 #include "DNA_scene_types.h"
60 #include "DNA_screen_types.h"
61 #include "DNA_space_types.h"
62 #include "DNA_view2d_types.h"
63 #include "DNA_view3d_types.h"
65 #include "BKE_action.h"
66 #include "BKE_effect.h"
67 #include "BKE_global.h"
68 #include "BKE_group.h"
71 #include "BKE_mball.h"
72 #include "BKE_modifier.h"
73 #include "BKE_object.h"
74 #include "BKE_particle.h"
75 #include "BKE_pointcache.h"
76 #include "BKE_utildefines.h"
77 #include "BKE_scene.h"
79 #include "MEM_guardedalloc.h"
82 #include "BPY_extern.h"
84 #include "depsgraph_private.h"
86 /* Queue and stack operations for dag traversal
88 * the queue store a list of freenodes to avoid successives alloc/dealloc
91 DagNodeQueue * queue_create (int slots)
94 DagNodeQueueElem * elem;
97 queue = MEM_mallocN(sizeof(DagNodeQueue),"DAG queue");
98 queue->freenodes = MEM_mallocN(sizeof(DagNodeQueue),"DAG queue");
101 queue->first = queue->last = NULL;
102 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem3");
105 queue->freenodes->first = queue->freenodes->last = elem;
107 for (i = 1; i <slots;i++) {
108 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem4");
111 queue->freenodes->last->next = elem;
112 queue->freenodes->last = elem;
114 queue->freenodes->count = slots;
118 void queue_raz(DagNodeQueue *queue)
120 DagNodeQueueElem * elem;
123 if (queue->freenodes->last)
124 queue->freenodes->last->next = elem;
126 queue->freenodes->first = queue->freenodes->last = elem;
129 queue->freenodes->count++;
133 queue->freenodes->count++;
135 queue->freenodes->last = elem;
139 void queue_delete(DagNodeQueue *queue)
141 DagNodeQueueElem * elem;
142 DagNodeQueueElem * temp;
151 elem = queue->freenodes->first;
158 MEM_freeN(queue->freenodes);
162 /* insert in queue, remove in front */
163 void push_queue(DagNodeQueue *queue, DagNode *node)
165 DagNodeQueueElem * elem;
169 fprintf(stderr,"pushing null node \n");
172 /*fprintf(stderr,"BFS push : %s %d\n",((ID *) node->ob)->name, queue->count);*/
174 elem = queue->freenodes->first;
176 queue->freenodes->first = elem->next;
177 if ( queue->freenodes->last == elem) {
178 queue->freenodes->last = NULL;
179 queue->freenodes->first = NULL;
181 queue->freenodes->count--;
182 } else { /* alllocating more */
183 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem1");
186 queue->freenodes->first = queue->freenodes->last = elem;
188 for (i = 1; i <DAGQUEUEALLOC;i++) {
189 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem2");
192 queue->freenodes->last->next = elem;
193 queue->freenodes->last = elem;
195 queue->freenodes->count = DAGQUEUEALLOC;
197 elem = queue->freenodes->first;
198 queue->freenodes->first = elem->next;
202 if (queue->last != NULL)
203 queue->last->next = elem;
205 if (queue->first == NULL) {
212 /* insert in front, remove in front */
213 void push_stack(DagNodeQueue *queue, DagNode *node)
215 DagNodeQueueElem * elem;
218 elem = queue->freenodes->first;
220 queue->freenodes->first = elem->next;
221 if ( queue->freenodes->last == elem) {
222 queue->freenodes->last = NULL;
223 queue->freenodes->first = NULL;
225 queue->freenodes->count--;
226 } else { /* alllocating more */
227 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem1");
230 queue->freenodes->first = queue->freenodes->last = elem;
232 for (i = 1; i <DAGQUEUEALLOC;i++) {
233 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem2");
236 queue->freenodes->last->next = elem;
237 queue->freenodes->last = elem;
239 queue->freenodes->count = DAGQUEUEALLOC;
241 elem = queue->freenodes->first;
242 queue->freenodes->first = elem->next;
244 elem->next = queue->first;
247 if (queue->last == NULL)
253 DagNode * pop_queue(DagNodeQueue *queue)
255 DagNodeQueueElem * elem;
260 queue->first = elem->next;
261 if (queue->last == elem) {
266 if (queue->freenodes->last)
267 queue->freenodes->last->next=elem;
268 queue->freenodes->last=elem;
269 if (queue->freenodes->first == NULL)
270 queue->freenodes->first=elem;
274 queue->freenodes->count++;
277 fprintf(stderr,"return null \n");
282 void *pop_ob_queue(struct DagNodeQueue *queue) {
283 return(pop_queue(queue)->ob);
286 DagNode * get_top_node_queue(DagNodeQueue *queue)
288 return queue->first->node;
291 int queue_count(struct DagNodeQueue *queue){
296 DagForest * dag_init()
299 /* use callocN to init all zero */
300 forest = MEM_callocN(sizeof(DagForest),"DAG root");
304 static void dag_add_driver_relation(Ipo *ipo, DagForest *dag, DagNode *node, int isdata)
309 for(icu= ipo->curve.first; icu; icu= icu->next) {
312 if (icu->driver->type == IPO_DRIVER_TYPE_PYTHON) {
314 if ((icu->driver->flag & IPO_DRIVER_FLAG_INVALID) || (icu->driver->name[0] == '\0'))
315 continue; /* empty or invalid expression */
317 /* now we need refs to all objects mentioned in this
318 * pydriver expression, to call 'dag_add_relation'
319 * for each of them */
320 Object **obarray = BPY_pydriver_get_objects(icu->driver);
322 Object *ob, **oba = obarray;
326 node1 = dag_get_node(dag, ob);
327 if (ob->type == OB_ARMATURE)
328 dag_add_relation(dag, node1, node, isdata?DAG_RL_DATA_DATA:DAG_RL_DATA_OB, "Python Ipo Driver");
330 dag_add_relation(dag, node1, node, isdata?DAG_RL_OB_DATA:DAG_RL_OB_OB, "Python Ipo Driver");
338 else if (icu->driver->ob) {
339 node1 = dag_get_node(dag, icu->driver->ob);
340 if(icu->driver->blocktype==ID_AR)
341 dag_add_relation(dag, node1, node, isdata?DAG_RL_DATA_DATA:DAG_RL_DATA_OB, "Ipo Driver");
343 dag_add_relation(dag, node1, node, isdata?DAG_RL_OB_DATA:DAG_RL_OB_OB, "Ipo Driver");
349 static void dag_add_collision_field_relation(DagForest *dag, Object *ob, DagNode *node)
354 // would be nice to have a list of colliders here
355 // so for now walk all objects in scene check 'same layer rule'
356 for(base = G.scene->base.first; base; base= base->next) {
357 if((base->lay & ob->lay) && base->object->pd) {
358 Object *ob1= base->object;
359 if((ob1->pd->deflect || ob1->pd->forcefield) && (ob1 != ob)) {
360 node2 = dag_get_node(dag, ob1);
361 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Field Collision");
367 static void build_dag_object(DagForest *dag, DagNode *scenenode, Object *ob, int mask)
370 bConstraintChannel *conchan;
375 ParticleSystem *psys;
378 node = dag_get_node(dag, ob);
380 if ((ob->data) && (mask&DAG_RL_DATA)) {
381 node2 = dag_get_node(dag,ob->data);
382 dag_add_relation(dag,node,node2,DAG_RL_DATA, "Object-Data Relation");
383 node2->first_ancestor = ob;
384 node2->ancestor_count += 1;
387 if (ob->type == OB_ARMATURE) {
392 for (pchan = ob->pose->chanbase.first; pchan; pchan=pchan->next) {
393 for (con = pchan->constraints.first; con; con=con->next) {
394 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
395 ListBase targets = {NULL, NULL};
396 bConstraintTarget *ct;
398 if (cti && cti->get_constraint_targets) {
399 cti->get_constraint_targets(con, &targets);
401 for (ct= targets.first; ct; ct= ct->next) {
402 if (ct->tar && ct->tar != ob) {
403 // fprintf(stderr,"armature %s target :%s \n", ob->id.name, target->id.name);
404 node3 = dag_get_node(dag, ct->tar);
406 if (ct->subtarget[0])
407 dag_add_relation(dag,node3,node, DAG_RL_OB_DATA|DAG_RL_DATA_DATA, cti->name);
408 else if(ELEM(con->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO))
409 dag_add_relation(dag,node3,node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, cti->name);
411 dag_add_relation(dag,node3,node, DAG_RL_OB_DATA, cti->name);
415 if (cti->flush_constraint_targets)
416 cti->flush_constraint_targets(con, &targets, 1);
424 /* driver dependencies, nla modifiers */
426 dag_add_driver_relation(ob->ipo, dag, node, 0);
430 dag_add_driver_relation(key->ipo, dag, node, 1);
432 for (conchan=ob->constraintChannels.first; conchan; conchan=conchan->next)
434 dag_add_driver_relation(conchan->ipo, dag, node, 0);
437 bActionChannel *chan;
438 for (chan = ob->action->chanbase.first; chan; chan=chan->next){
440 dag_add_driver_relation(chan->ipo, dag, node, 1);
441 for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next)
443 dag_add_driver_relation(conchan->ipo, dag, node, 1);
446 if(ob->nlastrips.first) {
448 bActionChannel *chan;
449 for(strip= ob->nlastrips.first; strip; strip= strip->next) {
450 if(strip->act && strip->act!=ob->action)
451 for (chan = strip->act->chanbase.first; chan; chan=chan->next)
453 dag_add_driver_relation(chan->ipo, dag, node, 1);
454 if(strip->modifiers.first) {
455 bActionModifier *amod;
456 for(amod= strip->modifiers.first; amod; amod= amod->next) {
458 node2 = dag_get_node(dag, amod->ob);
459 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "NLA Strip Modifier");
465 if (ob->modifiers.first) {
468 for(md=ob->modifiers.first; md; md=md->next) {
469 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
471 if (mti->updateDepgraph) mti->updateDepgraph(md, dag, ob, node);
475 node2 = dag_get_node(dag,ob->parent);
477 switch(ob->partype) {
479 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Parent");
481 case PARVERT1: case PARVERT3: case PARBONE:
482 dag_add_relation(dag,node2,node,DAG_RL_DATA_OB|DAG_RL_OB_OB, "Vertex Parent");
485 if(ob->parent->type==OB_LATTICE)
486 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Lattice Parent");
487 else if(ob->parent->type==OB_CURVE) {
488 Curve *cu= ob->parent->data;
489 if(cu->flag & CU_PATH)
490 dag_add_relation(dag,node2,node,DAG_RL_DATA_OB|DAG_RL_OB_OB, "Curve Parent");
492 dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Curve Parent");
495 dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Curve Parent");
497 /* exception case: parent is duplivert */
498 if(ob->type==OB_MBALL && (ob->parent->transflag & OB_DUPLIVERTS)) {
499 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Duplivert");
505 node2 = dag_get_node(dag,ob->track);
506 dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Track To");
510 node2 = dag_get_node(dag, ob->proxy);
511 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Proxy");
512 /* inverted relation, so addtoroot shouldn't be set to zero */
514 if (ob->type==OB_CAMERA) {
515 Camera *cam = (Camera *)ob->data;
517 dag_add_driver_relation(cam->ipo, dag, node, 1);
520 node2 = dag_get_node(dag, cam->dof_ob);
521 dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Camera DoF");
524 if (ob->type==OB_LAMP) {
525 Lamp *la = (Lamp *)ob->data;
527 dag_add_driver_relation(la->ipo, dag, node, 1);
530 if (ob->transflag & OB_DUPLI) {
531 if((ob->transflag & OB_DUPLIGROUP) && ob->dup_group) {
533 for(go= ob->dup_group->gobject.first; go; go= go->next) {
535 node2 = dag_get_node(dag, go->ob);
536 /* node2 changes node1, this keeps animations updated in groups?? not logical? */
537 dag_add_relation(dag, node2, node, DAG_RL_OB_OB, "Dupligroup");
543 /* softbody collision */
544 if((ob->type==OB_MESH) || (ob->type==OB_CURVE) || (ob->type==OB_LATTICE))
545 if(modifiers_isSoftbodyEnabled(ob) || modifiers_isClothEnabled(ob))
546 dag_add_collision_field_relation(dag, ob, node);
548 if (ob->type==OB_MBALL) {
549 Object *mom= find_basis_mball(ob);
551 node2 = dag_get_node(dag, mom);
552 dag_add_relation(dag,node,node2,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Metaball"); // mom depends on children!
555 else if (ob->type==OB_CURVE) {
558 node2 = dag_get_node(dag, cu->bevobj);
559 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Curve Bevel");
562 node2 = dag_get_node(dag, cu->taperobj);
563 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Curve Taper");
566 dag_add_driver_relation(cu->ipo, dag, node, 1);
569 else if(ob->type==OB_FONT) {
571 if(cu->textoncurve) {
572 node2 = dag_get_node(dag, cu->textoncurve);
573 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Texture On Curve");
577 psys= ob->particlesystem.first;
579 ParticleEffectorCache *nec;
582 for(; psys; psys=psys->next) {
583 ParticleSettings *part= psys->part;
585 dag_add_relation(dag, node, node, DAG_RL_OB_DATA, "Particle-Object Relation");
587 if(psys->flag & PSYS_DISABLED || psys->flag & PSYS_DELETE)
590 if(part->phystype==PART_PHYS_KEYED && psys->keyed_ob &&
591 BLI_findlink(&psys->keyed_ob->particlesystem,psys->keyed_psys-1)) {
592 node2 = dag_get_node(dag, psys->keyed_ob);
593 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA, "Particle Keyed Physics");
596 if(part->draw_as == PART_DRAW_OB && part->dup_ob) {
597 node2 = dag_get_node(dag, part->dup_ob);
598 dag_add_relation(dag, node, node2, DAG_RL_OB_OB, "Particle Object Visualisation");
599 if(part->dup_ob->type == OB_MBALL)
600 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA, "Particle Object Visualisation");
603 if(part->draw_as == PART_DRAW_GR && part->dup_group) {
604 for(go=part->dup_group->gobject.first; go; go=go->next) {
605 node2 = dag_get_node(dag, go->ob);
606 dag_add_relation(dag, node, node2, DAG_RL_OB_OB, "Particle Group Visualisation");
610 if(psys->effectors.first)
611 psys_end_effectors(psys);
612 psys_init_effectors(ob,psys->part->eff_group,psys);
614 if(psys->effectors.first) {
615 for(nec= psys->effectors.first; nec; nec= nec->next) {
616 Object *ob1= nec->ob;
618 if(nec->type & PSYS_EC_EFFECTOR) {
619 node2 = dag_get_node(dag, ob1);
620 if(ob1->pd->forcefield==PFIELD_GUIDE)
621 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Particle Field");
623 dag_add_relation(dag, node2, node, DAG_RL_OB_DATA, "Particle Field");
625 else if(nec->type & PSYS_EC_DEFLECT) {
626 node2 = dag_get_node(dag, ob1);
627 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Particle Collision");
629 else if(nec->type & PSYS_EC_PARTICLE) {
630 node2 = dag_get_node(dag, ob1);
631 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA, "Particle Field");
634 if(nec->type & PSYS_EC_REACTOR) {
635 node2 = dag_get_node(dag, ob1);
636 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA, "Particle Reactor");
643 for (con = ob->constraints.first; con; con=con->next) {
644 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
645 ListBase targets = {NULL, NULL};
646 bConstraintTarget *ct;
648 if (cti && cti->get_constraint_targets) {
649 cti->get_constraint_targets(con, &targets);
651 for (ct= targets.first; ct; ct= ct->next) {
659 node2 = dag_get_node(dag, obt);
660 if (ELEM(con->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO))
661 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB|DAG_RL_OB_OB, cti->name);
663 if (ELEM3(obt->type, OB_ARMATURE, OB_MESH, OB_LATTICE) && (ct->subtarget[0]))
664 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB|DAG_RL_OB_OB, cti->name);
666 dag_add_relation(dag, node2, node, DAG_RL_OB_OB, cti->name);
671 if (cti->flush_constraint_targets)
672 cti->flush_constraint_targets(con, &targets, 1);
677 dag_add_relation(dag,scenenode,node,DAG_RL_SCENE, "Scene Relation");
680 struct DagForest *build_dag(struct Scene *sce, short mask)
700 /* add base node for scene. scene is always the first node in DAG */
701 scenenode = dag_add_node(dag, sce);
703 /* add current scene objects */
704 for(base = sce->base.first; base; base= base->next) {
707 build_dag_object(dag, scenenode, ob, mask);
709 build_dag_object(dag, scenenode, ob->proxy, mask);
711 /* handled in next loop */
713 ob->dup_group->id.flag |= LIB_DOIT;
716 /* add groups used in current scene objects */
717 for(group= G.main->group.first; group; group= group->id.next) {
718 if(group->id.flag & LIB_DOIT) {
719 for(go= group->gobject.first; go; go= go->next) {
720 build_dag_object(dag, scenenode, go->ob, mask);
722 group->id.flag &= ~LIB_DOIT;
726 /* Now all relations were built, but we need to solve 1 exceptional case;
727 When objects have multiple "parents" (for example parent + constraint working on same object)
728 the relation type has to be synced. One of the parents can change, and should give same event to child */
730 /* nodes were callocced, so we can use node->color for temporal storage */
731 for(node = sce->theDag->DagNode.first; node; node= node->next) {
732 if(node->type==ID_OB) {
733 for(itA = node->child; itA; itA= itA->next) {
734 if(itA->node->type==ID_OB) {
735 itA->node->color |= itA->type;
740 /* now set relations equal, so that when only one parent changes, the correct recalcs are found */
741 for(node = sce->theDag->DagNode.first; node; node= node->next) {
742 if(node->type==ID_OB) {
743 for(itA = node->child; itA; itA= itA->next) {
744 if(itA->node->type==ID_OB) {
745 itA->type |= itA->node->color;
751 // cycle detection and solving
752 // solve_cycles(dag);
758 void free_forest(DagForest *Dag)
759 { /* remove all nodes and deps */
763 DagNode *itN = Dag->DagNode.first;
784 Dag->DagNode.first = NULL;
785 Dag->DagNode.last = NULL;
790 DagNode * dag_find_node (DagForest *forest,void * fob)
792 DagNode *node = forest->DagNode.first;
802 static int ugly_hack_sorry= 1; // prevent type check
804 /* no checking of existance, use dag_find_node first or dag_get_node */
805 DagNode * dag_add_node (DagForest *forest, void * fob)
809 node = MEM_callocN(sizeof(DagNode),"DAG node");
812 node->color = DAG_WHITE;
814 if(ugly_hack_sorry) node->type = GS(((ID *) fob)->name); // sorry, done for pose sorting
815 if (forest->numNodes) {
816 ((DagNode *) forest->DagNode.last)->next = node;
817 forest->DagNode.last = node;
820 forest->DagNode.last = node;
821 forest->DagNode.first = node;
822 forest->numNodes = 1;
828 DagNode * dag_get_node (DagForest *forest,void * fob)
832 node = dag_find_node (forest, fob);
834 node = dag_add_node(forest, fob);
840 DagNode * dag_get_sub_node (DagForest *forest,void * fob)
843 DagAdjList *mainchild, *prev=NULL;
845 mainchild = ((DagNode *) forest->DagNode.first)->child;
846 /* remove from first node (scene) adj list if present */
848 if (mainchild->node == fob) {
850 prev->next = mainchild->next;
851 MEM_freeN(mainchild);
854 ((DagNode *) forest->DagNode.first)->child = mainchild->next;
855 MEM_freeN(mainchild);
860 mainchild = mainchild->next;
862 node = dag_find_node (forest, fob);
864 node = dag_add_node(forest, fob);
868 static void dag_add_parent_relation(DagForest *forest, DagNode *fob1, DagNode *fob2, short rel, char *name)
870 DagAdjList *itA = fob2->parent;
872 while (itA) { /* search if relation exist already */
873 if (itA->node == fob1) {
880 /* create new relation and insert at head. MALLOC alert! */
881 itA = MEM_mallocN(sizeof(DagAdjList),"DAG adj list");
885 itA->next = fob2->parent;
890 void dag_add_relation(DagForest *forest, DagNode *fob1, DagNode *fob2, short rel, char *name)
892 DagAdjList *itA = fob1->child;
894 /* parent relation is for cycle checking */
895 dag_add_parent_relation(forest, fob1, fob2, rel, name);
897 while (itA) { /* search if relation exist already */
898 if (itA->node == fob2) {
905 /* create new relation and insert at head. MALLOC alert! */
906 itA = MEM_mallocN(sizeof(DagAdjList),"DAG adj list");
910 itA->next = fob1->child;
915 static char *dag_node_name(DagNode *node)
919 else if(ugly_hack_sorry)
920 return ((ID*)(node->ob))->name+2;
922 return ((bPoseChannel*)(node->ob))->name;
926 static void dag_node_print_dependencies(DagNode *node)
930 printf("%s depends on:\n", dag_node_name(node));
932 for(itA= node->parent; itA; itA= itA->next)
933 printf(" %s through %s\n", dag_node_name(itA->node), itA->name);
938 static int dag_node_print_dependency_recurs(DagNode *node, DagNode *endnode)
942 if(node->color == DAG_BLACK)
945 node->color= DAG_BLACK;
950 for(itA= node->parent; itA; itA= itA->next) {
951 if(dag_node_print_dependency_recurs(itA->node, endnode)) {
952 printf(" %s depends on %s through %s.\n", dag_node_name(node), dag_node_name(itA->node), itA->name);
960 static void dag_node_print_dependency_cycle(DagForest *dag, DagNode *startnode, DagNode *endnode, char *name)
964 for(node = dag->DagNode.first; node; node= node->next)
965 node->color= DAG_WHITE;
967 printf(" %s depends on %s through %s.\n", dag_node_name(endnode), dag_node_name(startnode), name);
968 dag_node_print_dependency_recurs(startnode, endnode);
972 static int dag_node_recurs_level(DagNode *node, int level)
977 node->color= DAG_BLACK; /* done */
980 for(itA= node->parent; itA; itA= itA->next) {
981 if(itA->node->color==DAG_WHITE) {
982 itA->node->ancestor_count= dag_node_recurs_level(itA->node, level);
983 newlevel= MAX2(newlevel, level+itA->node->ancestor_count);
986 newlevel= MAX2(newlevel, level+itA->node->ancestor_count);
992 static void dag_check_cycle(DagForest *dag)
997 /* tag nodes unchecked */
998 for(node = dag->DagNode.first; node; node= node->next)
999 node->color= DAG_WHITE;
1001 for(node = dag->DagNode.first; node; node= node->next) {
1002 if(node->color==DAG_WHITE) {
1003 node->ancestor_count= dag_node_recurs_level(node, 0);
1007 /* check relations, and print errors */
1008 for(node = dag->DagNode.first; node; node= node->next) {
1009 for(itA= node->parent; itA; itA= itA->next) {
1010 if(itA->node->ancestor_count > node->ancestor_count) {
1011 if(node->ob && itA->node->ob) {
1012 printf("Dependency cycle detected:\n");
1013 dag_node_print_dependency_cycle(dag, itA->node, node, itA->name);
1019 /* parent relations are only needed for cycle checking, so free now */
1020 for(node = dag->DagNode.first; node; node= node->next) {
1021 while (node->parent) {
1022 itA = node->parent->next;
1023 MEM_freeN(node->parent);
1030 * MainDAG is the DAG of all objects in current scene
1031 * used only for drawing there is one also in each scene
1033 static DagForest * MainDag = NULL;
1035 DagForest *getMainDag(void)
1041 void setMainDag(DagForest *dag)
1049 * in theory we should sweep the whole array
1050 * but in our case the first node is the scene
1051 * and is linked to every other object
1053 * for general case we will need to add outer loop
1057 * ToDo : change pos kludge
1060 /* adjust levels for drawing in oops space */
1061 void graph_bfs(void)
1064 DagNodeQueue *nqueue;
1070 /* fprintf(stderr,"starting BFS \n ------------\n"); */
1071 nqueue = queue_create(DAGQUEUEALLOC);
1072 for ( i=0; i<50; i++)
1076 * dagnode.first is alway the root (scene)
1078 node = MainDag->DagNode.first;
1080 node->color = DAG_WHITE;
1081 node->BFS_dist = 9999;
1086 node = MainDag->DagNode.first;
1087 if (node->color == DAG_WHITE) {
1088 node->color = DAG_GRAY;
1090 push_queue(nqueue,node);
1091 while(nqueue->count) {
1092 node = pop_queue(nqueue);
1094 minheight = pos[node->BFS_dist];
1096 while(itA != NULL) {
1097 if((itA->node->color == DAG_WHITE) ) {
1098 itA->node->color = DAG_GRAY;
1099 itA->node->BFS_dist = node->BFS_dist + 1;
1100 itA->node->k = (float) minheight;
1101 push_queue(nqueue,itA->node);
1105 fprintf(stderr,"bfs not dag tree edge color :%i \n",itA->node->color);
1111 if (pos[node->BFS_dist] > node->k ) {
1112 pos[node->BFS_dist] += 1;
1113 node->k = (float) pos[node->BFS_dist];
1115 pos[node->BFS_dist] = (int) node->k +1;
1117 set_node_xy(node, node->BFS_dist*DEPSX*2, pos[node->BFS_dist]*DEPSY*2);
1118 node->color = DAG_BLACK;
1120 fprintf(stderr,"BFS node : %20s %i %5.0f %5.0f\n",((ID *) node->ob)->name,node->BFS_dist, node->x, node->y);
1124 queue_delete(nqueue);
1127 int pre_and_post_BFS(DagForest *dag, short mask, graph_action_func pre_func, graph_action_func post_func, void **data) {
1130 node = dag->DagNode.first;
1131 return pre_and_post_source_BFS(dag, mask, node, pre_func, post_func, data);
1135 int pre_and_post_source_BFS(DagForest *dag, short mask, DagNode *source, graph_action_func pre_func, graph_action_func post_func, void **data)
1138 DagNodeQueue *nqueue;
1141 /* fprintf(stderr,"starting BFS \n ------------\n"); */
1144 * dagnode.first is alway the root (scene)
1146 node = dag->DagNode.first;
1147 nqueue = queue_create(DAGQUEUEALLOC);
1149 node->color = DAG_WHITE;
1150 node->BFS_dist = 9999;
1155 if (node->color == DAG_WHITE) {
1156 node->color = DAG_GRAY;
1158 pre_func(node->ob,data);
1160 while(nqueue->count) {
1161 node = pop_queue(nqueue);
1164 while(itA != NULL) {
1165 if((itA->node->color == DAG_WHITE) && (itA->type & mask)) {
1166 itA->node->color = DAG_GRAY;
1167 itA->node->BFS_dist = node->BFS_dist + 1;
1168 push_queue(nqueue,itA->node);
1169 pre_func(node->ob,data);
1172 else { // back or cross edge
1177 post_func(node->ob,data);
1178 node->color = DAG_BLACK;
1180 fprintf(stderr,"BFS node : %20s %i %5.0f %5.0f\n",((ID *) node->ob)->name,node->BFS_dist, node->x, node->y);
1184 queue_delete(nqueue);
1188 /* non recursive version of DFS, return queue -- outer loop present to catch odd cases (first level cycles)*/
1189 DagNodeQueue * graph_dfs(void)
1192 DagNodeQueue *nqueue;
1193 DagNodeQueue *retqueue;
1203 *fprintf(stderr,"starting DFS \n ------------\n");
1205 nqueue = queue_create(DAGQUEUEALLOC);
1206 retqueue = queue_create(MainDag->numNodes);
1207 for ( i=0; i<50; i++)
1211 * dagnode.first is alway the root (scene)
1213 node = MainDag->DagNode.first;
1215 node->color = DAG_WHITE;
1216 node->DFS_dist = 9999;
1217 node->DFS_dvtm = node->DFS_fntm = 9999;
1224 node = MainDag->DagNode.first;
1227 if (node->color == DAG_WHITE) {
1228 node->color = DAG_GRAY;
1230 node->DFS_dvtm = time;
1232 push_stack(nqueue,node);
1234 while(nqueue->count) {
1235 //graph_print_queue(nqueue);
1238 node = get_top_node_queue(nqueue);
1240 minheight = pos[node->DFS_dist];
1243 while(itA != NULL) {
1244 if((itA->node->color == DAG_WHITE) ) {
1245 itA->node->DFS_dvtm = time;
1246 itA->node->color = DAG_GRAY;
1249 itA->node->DFS_dist = node->DFS_dist + 1;
1250 itA->node->k = (float) minheight;
1251 push_stack(nqueue,itA->node);
1255 if (itA->node->color == DAG_GRAY) { // back edge
1256 fprintf(stderr,"dfs back edge :%15s %15s \n",((ID *) node->ob)->name, ((ID *) itA->node->ob)->name);
1258 } else if (itA->node->color == DAG_BLACK) {
1260 /* already processed node but we may want later to change distance either to shorter to longer.
1261 * DFS_dist is the first encounter
1263 /*if (node->DFS_dist >= itA->node->DFS_dist)
1264 itA->node->DFS_dist = node->DFS_dist + 1;
1266 fprintf(stderr,"dfs forward or cross edge :%15s %i-%i %15s %i-%i \n",
1267 ((ID *) node->ob)->name,
1270 ((ID *) itA->node->ob)->name,
1271 itA->node->DFS_dvtm,
1272 itA->node->DFS_fntm);
1275 fprintf(stderr,"dfs unknown edge \n");
1281 node = pop_queue(nqueue);
1282 node->color = DAG_BLACK;
1284 node->DFS_fntm = time;
1286 if (node->DFS_dist > maxpos)
1287 maxpos = node->DFS_dist;
1288 if (pos[node->DFS_dist] > node->k ) {
1289 pos[node->DFS_dist] += 1;
1290 node->k = (float) pos[node->DFS_dist];
1292 pos[node->DFS_dist] = (int) node->k +1;
1294 set_node_xy(node, node->DFS_dist*DEPSX*2, pos[node->DFS_dist]*DEPSY*2);
1297 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 );
1299 push_stack(retqueue,node);
1306 // fprintf(stderr,"i size : %i \n", maxpos);
1308 queue_delete(nqueue);
1313 int pre_and_post_DFS(DagForest *dag, short mask, graph_action_func pre_func, graph_action_func post_func, void **data) {
1316 node = dag->DagNode.first;
1317 return pre_and_post_source_DFS(dag, mask, node, pre_func, post_func, data);
1320 int pre_and_post_source_DFS(DagForest *dag, short mask, DagNode *source, graph_action_func pre_func, graph_action_func post_func, void **data)
1323 DagNodeQueue *nqueue;
1329 *fprintf(stderr,"starting DFS \n ------------\n");
1331 nqueue = queue_create(DAGQUEUEALLOC);
1334 * dagnode.first is alway the root (scene)
1336 node = dag->DagNode.first;
1338 node->color = DAG_WHITE;
1339 node->DFS_dist = 9999;
1340 node->DFS_dvtm = node->DFS_fntm = 9999;
1349 if (node->color == DAG_WHITE) {
1350 node->color = DAG_GRAY;
1352 node->DFS_dvtm = time;
1354 push_stack(nqueue,node);
1355 pre_func(node->ob,data);
1357 while(nqueue->count) {
1359 node = get_top_node_queue(nqueue);
1362 while(itA != NULL) {
1363 if((itA->node->color == DAG_WHITE) && (itA->type & mask) ) {
1364 itA->node->DFS_dvtm = time;
1365 itA->node->color = DAG_GRAY;
1368 itA->node->DFS_dist = node->DFS_dist + 1;
1369 push_stack(nqueue,itA->node);
1370 pre_func(node->ob,data);
1375 if (itA->node->color == DAG_GRAY) {// back edge
1378 // else if (itA->node->color == DAG_BLACK) { // cross or forward
1385 node = pop_queue(nqueue);
1386 node->color = DAG_BLACK;
1388 node->DFS_fntm = time;
1390 post_func(node->ob,data);
1396 queue_delete(nqueue);
1401 // used to get the obs owning a datablock
1402 struct DagNodeQueue *get_obparents(struct DagForest *dag, void *ob)
1404 DagNode * node, *node1;
1405 DagNodeQueue *nqueue;
1408 node = dag_find_node(dag,ob);
1412 else if (node->ancestor_count == 1) { // simple case
1413 nqueue = queue_create(1);
1414 push_queue(nqueue,node);
1415 } else { // need to go over the whole dag for adj list
1416 nqueue = queue_create(node->ancestor_count);
1418 node1 = dag->DagNode.first;
1420 if (node1->DFS_fntm > node->DFS_fntm) { // a parent is finished after child. must check adj list
1422 while(itA != NULL) {
1423 if ((itA->node == node) && (itA->type == DAG_RL_DATA)) {
1424 push_queue(nqueue,node);
1429 node1 = node1->next;
1435 struct DagNodeQueue *get_first_ancestors(struct DagForest *dag, void *ob)
1437 DagNode * node, *node1;
1438 DagNodeQueue *nqueue;
1441 node = dag_find_node(dag,ob);
1443 // need to go over the whole dag for adj list
1444 nqueue = queue_create(node->ancestor_count);
1446 node1 = dag->DagNode.first;
1448 if (node1->DFS_fntm > node->DFS_fntm) {
1450 while(itA != NULL) {
1451 if (itA->node == node) {
1452 push_queue(nqueue,node);
1457 node1 = node1->next;
1463 // standard DFS list
1464 struct DagNodeQueue *get_all_childs(struct DagForest *dag, void *ob)
1467 DagNodeQueue *nqueue;
1468 DagNodeQueue *retqueue;
1473 nqueue = queue_create(DAGQUEUEALLOC);
1474 retqueue = queue_create(dag->numNodes); // was MainDag... why? (ton)
1476 node = dag->DagNode.first;
1478 node->color = DAG_WHITE;
1484 node = dag_find_node(dag, ob); // could be done in loop above (ton)
1485 if(node) { // can be null for newly added objects
1487 node->color = DAG_GRAY;
1489 push_stack(nqueue,node);
1491 while(nqueue->count) {
1494 node = get_top_node_queue(nqueue);
1497 while(itA != NULL) {
1498 if((itA->node->color == DAG_WHITE) ) {
1499 itA->node->DFS_dvtm = time;
1500 itA->node->color = DAG_GRAY;
1503 push_stack(nqueue,itA->node);
1511 node = pop_queue(nqueue);
1512 node->color = DAG_BLACK;
1515 push_stack(retqueue,node);
1519 queue_delete(nqueue);
1524 short are_obs_related(struct DagForest *dag, void *ob1, void *ob2) {
1528 node = dag_find_node(dag, ob1);
1531 while(itA != NULL) {
1532 if((itA->node->ob == ob2) ) {
1533 return itA->node->type;
1537 return DAG_NO_RELATION;
1540 int is_acyclic( DagForest *dag) {
1541 return dag->is_acyclic;
1544 void set_node_xy(DagNode *node, float x, float y)
1551 /* debug test functions */
1553 void graph_print_queue(DagNodeQueue *nqueue)
1555 DagNodeQueueElem *queueElem;
1557 queueElem = nqueue->first;
1559 fprintf(stderr,"** %s %i %i-%i ",((ID *) queueElem->node->ob)->name,queueElem->node->color,queueElem->node->DFS_dvtm,queueElem->node->DFS_fntm);
1560 queueElem = queueElem->next;
1562 fprintf(stderr,"\n");
1565 void graph_print_queue_dist(DagNodeQueue *nqueue)
1567 DagNodeQueueElem *queueElem;
1570 queueElem = nqueue->first;
1571 max = queueElem->node->DFS_fntm;
1574 fprintf(stderr,"** %25s %2.2i-%2.2i ",((ID *) queueElem->node->ob)->name,queueElem->node->DFS_dvtm,queueElem->node->DFS_fntm);
1575 while (count < queueElem->node->DFS_dvtm-1) { fputc(' ',stderr); count++;}
1577 while (count < queueElem->node->DFS_fntm-2) { fputc('-',stderr); count++;}
1581 queueElem = queueElem->next;
1583 fprintf(stderr,"\n");
1586 void graph_print_adj_list(void)
1591 node = (getMainDag())->DagNode.first;
1593 fprintf(stderr,"node : %s col: %i",((ID *) node->ob)->name, node->color);
1596 fprintf(stderr,"-- %s ",((ID *) itA->node->ob)->name);
1600 fprintf(stderr,"\n");
1605 /* ************************ API *********************** */
1607 /* groups with objects in this scene need to be put in the right order as well */
1608 static void scene_sort_groups(Scene *sce)
1615 /* test; are group objects all in this scene? */
1616 for(ob= G.main->object.first; ob; ob= ob->id.next) {
1617 ob->id.flag &= ~LIB_DOIT;
1618 ob->id.newid= NULL; /* newid abuse for GroupObject */
1620 for(base = sce->base.first; base; base= base->next)
1621 base->object->id.flag |= LIB_DOIT;
1623 for(group= G.main->group.first; group; group= group->id.next) {
1624 for(go= group->gobject.first; go; go= go->next) {
1625 if((go->ob->id.flag & LIB_DOIT)==0)
1628 /* this group is entirely in this scene */
1630 ListBase listb= {NULL, NULL};
1632 for(go= group->gobject.first; go; go= go->next)
1633 go->ob->id.newid= (ID *)go;
1635 /* in order of sorted bases we reinsert group objects */
1636 for(base = sce->base.first; base; base= base->next) {
1638 if(base->object->id.newid) {
1639 go= (GroupObject *)base->object->id.newid;
1640 base->object->id.newid= NULL;
1641 BLI_remlink( &group->gobject, go);
1642 BLI_addtail( &listb, go);
1645 /* copy the newly sorted listbase */
1646 group->gobject= listb;
1651 /* sort the base list on dependency order */
1652 void DAG_scene_sort(struct Scene *sce)
1655 DagNodeQueue *nqueue;
1662 tempbase.first= tempbase.last= NULL;
1664 build_dag(sce, DAG_RL_ALL_BUT_DATA);
1666 dag_check_cycle(sce->theDag);
1668 nqueue = queue_create(DAGQUEUEALLOC);
1670 for(node = sce->theDag->DagNode.first; node; node= node->next) {
1671 node->color = DAG_WHITE;
1676 node = sce->theDag->DagNode.first;
1678 node->color = DAG_GRAY;
1680 push_stack(nqueue,node);
1682 while(nqueue->count) {
1685 node = get_top_node_queue(nqueue);
1688 while(itA != NULL) {
1689 if((itA->node->color == DAG_WHITE) ) {
1690 itA->node->DFS_dvtm = time;
1691 itA->node->color = DAG_GRAY;
1694 push_stack(nqueue,itA->node);
1703 node = pop_queue(nqueue);
1704 if (node->ob == sce) // we are done
1706 node->color = DAG_BLACK;
1709 base = sce->base.first;
1710 while (base && base->object != node->ob)
1713 BLI_remlink(&sce->base,base);
1714 BLI_addhead(&tempbase,base);
1720 // temporal correction for circular dependancies
1721 base = sce->base.first;
1723 BLI_remlink(&sce->base,base);
1724 BLI_addhead(&tempbase,base);
1726 printf("cyclic %s\n", base->object->id.name);
1727 base = sce->base.first;
1730 sce->base = tempbase;
1731 queue_delete(nqueue);
1733 /* all groups with objects in this scene gets resorted too */
1734 scene_sort_groups(sce);
1737 printf("\nordered\n");
1738 for(base = sce->base.first; base; base= base->next) {
1739 printf(" %s\n", base->object->id.name);
1743 sce->recalc |= SCE_PRV_CHANGED; /* test for 3d preview */
1746 /* node was checked to have lasttime != curtime and is if type ID_OB */
1747 static void flush_update_node(DagNode *node, unsigned int layer, int curtime)
1751 int oldflag, changed=0;
1752 unsigned int all_layer;
1754 node->lasttime= curtime;
1757 if(ob && (ob->recalc & OB_RECALC)) {
1760 /* got an object node that changes, now check relations */
1761 for(itA = node->child; itA; itA= itA->next) {
1762 all_layer |= itA->lay;
1763 /* the relationship is visible */
1764 if((itA->lay & layer) || (itA->node->ob == G.obedit)) {
1765 if(itA->node->type==ID_OB) {
1767 oldflag= obc->recalc;
1769 /* got a ob->obc relation, now check if flag needs flush */
1770 if(ob->recalc & OB_RECALC_OB) {
1771 if(itA->type & DAG_RL_OB_OB) {
1772 //printf("ob %s changes ob %s\n", ob->id.name, obc->id.name);
1773 obc->recalc |= OB_RECALC_OB;
1775 if(itA->type & DAG_RL_OB_DATA) {
1776 //printf("ob %s changes obdata %s\n", ob->id.name, obc->id.name);
1777 obc->recalc |= OB_RECALC_DATA;
1780 if(ob->recalc & OB_RECALC_DATA) {
1781 if(itA->type & DAG_RL_DATA_OB) {
1782 //printf("obdata %s changes ob %s\n", ob->id.name, obc->id.name);
1783 obc->recalc |= OB_RECALC_OB;
1785 if(itA->type & DAG_RL_DATA_DATA) {
1786 //printf("obdata %s changes obdata %s\n", ob->id.name, obc->id.name);
1787 obc->recalc |= OB_RECALC_DATA;
1790 if(oldflag!=obc->recalc) changed= 1;
1794 /* even nicer, we can clear recalc flags... */
1795 if((all_layer & layer)==0 && (ob != G.obedit)) {
1796 /* but existing displaylists or derivedmesh should be freed */
1797 if(ob->recalc & OB_RECALC_DATA)
1798 object_free_display(ob);
1800 ob->recalc &= ~OB_RECALC;
1804 /* check case where child changes and parent forcing obdata to change */
1805 /* should be done regardless if this ob has recalc set */
1806 /* could merge this in with loop above...? (ton) */
1807 for(itA = node->child; itA; itA= itA->next) {
1808 /* the relationship is visible */
1809 if((itA->lay & layer) || (itA->node->ob == G.obedit)) {
1810 if(itA->node->type==ID_OB) {
1813 if((obc->recalc & OB_RECALC)==OB_RECALC_OB) {
1814 /* parent has deforming info */
1815 if(itA->type & (DAG_RL_OB_DATA|DAG_RL_DATA_DATA)) {
1816 // printf("parent %s changes ob %s\n", ob->id.name, obc->id.name);
1817 obc->recalc |= OB_RECALC_DATA;
1824 /* we only go deeper if node not checked or something changed */
1825 for(itA = node->child; itA; itA= itA->next) {
1826 if(changed || itA->node->lasttime!=curtime)
1827 flush_update_node(itA->node, layer, curtime);
1832 /* node was checked to have lasttime != curtime , and is of type ID_OB */
1833 static unsigned int flush_layer_node(Scene *sce, DagNode *node, int curtime)
1838 node->lasttime= curtime;
1840 for(base= sce->base.first; base; base= base->next) {
1841 if(node->ob == base->object) {
1842 node->lay= ((Object *)node->ob)->lay;
1847 for(itA = node->child; itA; itA= itA->next) {
1848 if(itA->node->type==ID_OB) {
1849 if(itA->node->lasttime!=curtime) {
1850 itA->lay= flush_layer_node(sce, itA->node, curtime); // lay is only set once for each relation
1852 else itA->lay= itA->node->lay;
1854 node->lay |= itA->lay;
1861 /* node was checked to have lasttime != curtime , and is of type ID_OB */
1862 static void flush_pointcache_reset(DagNode *node, int curtime, int reset)
1867 node->lasttime= curtime;
1869 for(itA = node->child; itA; itA= itA->next) {
1870 if(itA->node->type==ID_OB) {
1871 if(itA->node->lasttime!=curtime) {
1872 ob= (Object*)(node->ob);
1874 if(reset || (ob->recalc & OB_RECALC)) {
1875 if(BKE_ptcache_object_reset(ob, PTCACHE_RESET_DEPSGRAPH))
1876 ob->recalc |= OB_RECALC_DATA;
1878 flush_pointcache_reset(itA->node, curtime, 1);
1881 flush_pointcache_reset(itA->node, curtime, 0);
1887 /* flushes all recalc flags in objects down the dependency tree */
1888 void DAG_scene_flush_update(Scene *sce, unsigned int lay, int time)
1895 if(sce->theDag==NULL) {
1896 printf("DAG zero... not allowed to happen!\n");
1897 DAG_scene_sort(sce);
1900 firstnode= sce->theDag->DagNode.first; // always scene node
1902 for(itA = firstnode->child; itA; itA= itA->next)
1905 /* first we flush the layer flags */
1906 sce->theDag->time++; // so we know which nodes were accessed
1907 lasttime= sce->theDag->time;
1909 for(itA = firstnode->child; itA; itA= itA->next)
1910 if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB)
1911 flush_layer_node(sce, itA->node, lasttime);
1913 /* then we use the relationships + layer info to flush update events */
1914 sce->theDag->time++; // so we know which nodes were accessed
1915 lasttime= sce->theDag->time;
1916 for(itA = firstnode->child; itA; itA= itA->next)
1917 if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB)
1918 flush_update_node(itA->node, lay, lasttime);
1920 /* if update is not due to time change, do pointcache clears */
1922 sce->theDag->time++; // so we know which nodes were accessed
1923 lasttime= sce->theDag->time;
1924 for(itA = firstnode->child; itA; itA= itA->next) {
1925 if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB) {
1926 ob= (Object*)(itA->node->ob);
1928 if(ob->recalc & OB_RECALC) {
1929 if(BKE_ptcache_object_reset(ob, PTCACHE_RESET_DEPSGRAPH))
1930 ob->recalc |= OB_RECALC_DATA;
1932 flush_pointcache_reset(itA->node, lasttime, 1);
1935 flush_pointcache_reset(itA->node, lasttime, 0);
1941 static int object_modifiers_use_time(Object *ob)
1945 for (md=ob->modifiers.first; md; md=md->next)
1946 if (modifier_dependsOnTime(md))
1952 static int exists_channel(Object *ob, char *name)
1954 bActionStrip *strip;
1957 if(get_action_channel(ob->action, name))
1960 for (strip=ob->nlastrips.first; strip; strip=strip->next)
1961 if(get_action_channel(strip->act, name))
1966 static void dag_object_time_update_flags(Object *ob)
1969 if(ob->ipo) ob->recalc |= OB_RECALC_OB;
1970 else if(ob->constraints.first) {
1972 for (con = ob->constraints.first; con; con=con->next) {
1973 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
1974 ListBase targets = {NULL, NULL};
1975 bConstraintTarget *ct;
1977 if (cti && cti->get_constraint_targets) {
1978 cti->get_constraint_targets(con, &targets);
1980 for (ct= targets.first; ct; ct= ct->next) {
1982 ob->recalc |= OB_RECALC_OB;
1987 if (cti->flush_constraint_targets)
1988 cti->flush_constraint_targets(con, &targets, 1);
1993 if(ob->scriptlink.totscript) ob->recalc |= OB_RECALC_OB;
1996 /* motion path or bone child */
1997 if(ob->parent->type==OB_CURVE || ob->parent->type==OB_ARMATURE) ob->recalc |= OB_RECALC_OB;
2000 if(ob->action || ob->nlastrips.first) {
2001 /* since actions now are mixed, we set the recalcs on the safe side */
2002 ob->recalc |= OB_RECALC_OB;
2003 if(ob->type==OB_ARMATURE)
2004 ob->recalc |= OB_RECALC_DATA;
2005 else if(exists_channel(ob, "Shape"))
2006 ob->recalc |= OB_RECALC_DATA;
2007 else if(ob->dup_group) {
2008 bActionStrip *strip;
2009 /* this case is for groups with nla, whilst nla target has no action or nla */
2010 for(strip= ob->nlastrips.first; strip; strip= strip->next) {
2012 strip->object->recalc |= OB_RECALC;
2017 if(object_modifiers_use_time(ob)) ob->recalc |= OB_RECALC_DATA;
2018 if((ob->pose) && (ob->pose->flag & POSE_CONSTRAINTS_TIMEDEPEND)) ob->recalc |= OB_RECALC_DATA;
2029 if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
2030 ob->recalc |= OB_RECALC_DATA;
2031 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
2034 if((ob->fluidsimFlag & OB_FLUIDSIM_ENABLE) && (ob->fluidsimSettings)) {
2035 // fluidsimSettings might not be initialized during load...
2036 if(ob->fluidsimSettings->type & (OB_FLUIDSIM_DOMAIN|OB_FLUIDSIM_PARTICLE)) {
2037 ob->recalc |= OB_RECALC_DATA; // NT FSPARTICLE
2040 if(ob->particlesystem.first)
2041 ob->recalc |= OB_RECALC_DATA;
2047 if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
2048 ob->recalc |= OB_RECALC_DATA;
2049 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
2055 if(cu->nurb.first==NULL && cu->str && cu->vfont)
2056 ob->recalc |= OB_RECALC_DATA;
2061 if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
2062 ob->recalc |= OB_RECALC_DATA;
2063 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
2068 if(ob->transflag & OB_DUPLI) ob->recalc |= OB_RECALC_DATA;
2072 if(ob->particlesystem.first) {
2073 ParticleSystem *psys= ob->particlesystem.first;
2075 for(; psys; psys=psys->next) {
2076 if(psys_check_enabled(ob, psys)) {
2077 ob->recalc |= OB_RECALC_DATA;
2085 /* flag all objects that need recalc, for changes in time for example */
2086 void DAG_scene_update_flags(Scene *scene, unsigned int lay)
2094 /* set ob flags where animated systems are */
2095 for(SETLOOPER(scene, base)) {
2098 /* now if DagNode were part of base, the node->lay could be checked... */
2099 /* we do all now, since the scene_flush checks layers and clears recalc flags even */
2100 dag_object_time_update_flags(ob);
2102 /* handled in next loop */
2104 ob->dup_group->id.flag |= LIB_DOIT;
2107 /* we do groups each once */
2108 for(group= G.main->group.first; group; group= group->id.next) {
2109 if(group->id.flag & LIB_DOIT) {
2110 for(go= group->gobject.first; go; go= go->next) {
2111 dag_object_time_update_flags(go->ob);
2116 for(sce= scene; sce; sce= sce->set)
2117 DAG_scene_flush_update(sce, lay, 1);
2119 /* test: set time flag, to disable baked systems to update */
2120 for(SETLOOPER(scene, base)) {
2123 ob->recalc |= OB_RECALC_TIME;
2126 /* hrmf... an exception to look at once, for invisible camera object we do it over */
2128 dag_object_time_update_flags(scene->camera);
2130 /* and store the info in groupobject */
2131 for(group= G.main->group.first; group; group= group->id.next) {
2132 if(group->id.flag & LIB_DOIT) {
2133 for(go= group->gobject.first; go; go= go->next) {
2134 go->recalc= go->ob->recalc;
2135 // printf("ob %s recalc %d\n", go->ob->id.name, go->recalc);
2137 group->id.flag &= ~LIB_DOIT;
2143 /* for depgraph updating, all layers visible in a screen */
2144 /* this is a copy from editscreen.c... I need to think over a more proper solution for this */
2145 /* probably the DAG_object_flush_update() should give layer too? */
2146 /* or some kind of dag context... (DAG_set_layer) */
2147 static unsigned int dag_screen_view3d_layers(void)
2152 for(sa= G.curscreen->areabase.first; sa; sa= sa->next) {
2153 if(sa->spacetype==SPACE_VIEW3D)
2154 layer |= ((View3D *)sa->spacedata.first)->lay;
2160 /* flag this object and all its relations to recalc */
2161 /* if you need to do more objects, tag object yourself and
2162 use DAG_scene_flush_update() in end */
2163 void DAG_object_flush_update(Scene *sce, Object *ob, short flag)
2166 if(ob==NULL || sce->theDag==NULL) return;
2169 BKE_ptcache_object_reset(ob, PTCACHE_RESET_DEPSGRAPH);
2171 /* all users of this ob->data should be checked */
2172 /* BUT! displists for curves are still only on cu */
2173 if(flag & OB_RECALC_DATA) {
2174 if(ob->type!=OB_CURVE && ob->type!=OB_SURF) {
2176 if(id && id->us>1) {
2177 /* except when there's a key and shapes are locked */
2178 if(ob_get_key(ob) && (ob->shapeflag & (OB_SHAPE_LOCK|OB_SHAPE_TEMPLOCK)));
2181 for (obt=G.main->object.first; obt; obt= obt->id.next) {
2182 if (obt != ob && obt->data==ob->data) {
2183 obt->recalc |= OB_RECALC_DATA;
2184 BKE_ptcache_object_reset(obt, PTCACHE_RESET_DEPSGRAPH);
2193 DAG_scene_flush_update(sce, dag_screen_view3d_layers(), 0);
2195 DAG_scene_flush_update(sce, sce->lay, 0);
2198 /* recursively descends tree, each node only checked once */
2199 /* node is checked to be of type object */
2200 static int parent_check_node(DagNode *node, int curtime)
2204 node->lasttime= curtime;
2206 if(node->color==DAG_GRAY)
2209 for(itA = node->child; itA; itA= itA->next) {
2210 if(itA->node->type==ID_OB) {
2212 if(itA->node->color==DAG_GRAY)
2215 /* descend if not done */
2216 if(itA->node->lasttime!=curtime) {
2217 itA->node->color= parent_check_node(itA->node, curtime);
2219 if(itA->node->color==DAG_GRAY)
2228 /* all nodes that influence this object get tagged, for calculating the exact
2229 position of this object at a given timeframe */
2230 void DAG_object_update_flags(Scene *sce, Object *ob, unsigned int lay)
2235 /* tag nodes unchecked */
2236 for(node = sce->theDag->DagNode.first; node; node= node->next)
2237 node->color = DAG_WHITE;
2239 node= dag_find_node(sce->theDag, ob);
2241 /* object not in scene? then handle group exception. needs to be dagged once too */
2244 while( (group = find_group(ob, group)) ) {
2246 /* primitive; tag all... this call helps building groups for particles */
2247 for(go= group->gobject.first; go; go= go->next)
2248 go->ob->recalc= OB_RECALC;
2253 node->color = DAG_GRAY;
2255 sce->theDag->time++;
2256 node= sce->theDag->DagNode.first;
2257 for(itA = node->child; itA; itA= itA->next) {
2258 if(itA->node->type==ID_OB && itA->node->lasttime!=sce->theDag->time)
2259 itA->node->color= parent_check_node(itA->node, sce->theDag->time);
2262 /* set recalcs and flushes */
2263 DAG_scene_update_flags(sce, lay);
2265 /* now we clear recalcs, unless color is set */
2266 for(node = sce->theDag->DagNode.first; node; node= node->next) {
2267 if(node->type==ID_OB && node->color==DAG_WHITE) {
2268 Object *ob= node->ob;
2275 /* ******************* DAG FOR ARMATURE POSE ***************** */
2277 /* we assume its an armature with pose */
2278 void DAG_pose_sort(Object *ob)
2280 bPose *pose= ob->pose;
2281 bPoseChannel *pchan;
2284 DagNode *node2, *node3;
2287 DagNodeQueue *nqueue;
2293 ugly_hack_sorry= 0; // no ID structs
2295 rootnode = dag_add_node(dag, NULL); // node->ob becomes NULL
2297 /* we add the hierarchy and the constraints */
2298 for(pchan = pose->chanbase.first; pchan; pchan= pchan->next) {
2301 node = dag_get_node(dag, pchan);
2304 node2 = dag_get_node(dag, pchan->parent);
2305 dag_add_relation(dag, node2, node, 0, "Parent Relation");
2308 for (con = pchan->constraints.first; con; con=con->next) {
2309 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
2310 ListBase targets = {NULL, NULL};
2311 bConstraintTarget *ct;
2315 for(icu= con->ipo->curve.first; icu; icu= icu->next) {
2316 /* icu->driver->ob should actually point to ob->proxy if it
2317 * is a proxy, but since it wasn't set correct it older
2318 * files comparing with ob->proxy makes it work for those */
2319 if(icu->driver && (icu->driver->ob==ob || icu->driver->ob==ob->proxy)) {
2320 bPoseChannel *target= get_pose_channel(ob->pose, icu->driver->name);
2322 node2 = dag_get_node(dag, target);
2323 dag_add_relation(dag, node2, node, 0, "Ipo Driver");
2325 /* uncommented this line, results in dependencies
2326 * not being added properly for this constraint,
2327 * what is the purpose of this? - brecht */
2328 /*cti= NULL;*/ /* trick to get next loop skipped */
2334 if (cti && cti->get_constraint_targets) {
2335 cti->get_constraint_targets(con, &targets);
2337 for (ct= targets.first; ct; ct= ct->next) {
2338 if (ct->tar==ob && ct->subtarget[0]) {
2339 bPoseChannel *target= get_pose_channel(ob->pose, ct->subtarget);
2341 node2= dag_get_node(dag, target);
2342 dag_add_relation(dag, node2, node, 0, "IK Constraint");
2344 if (con->type==CONSTRAINT_TYPE_KINEMATIC) {
2345 bKinematicConstraint *data = (bKinematicConstraint *)con->data;
2346 bPoseChannel *parchan;
2349 /* exclude tip from chain? */
2350 if(!(data->flag & CONSTRAINT_IK_TIP))
2351 parchan= pchan->parent;
2355 /* Walk to the chain's root */
2357 node3= dag_get_node(dag, parchan);
2358 dag_add_relation(dag, node2, node3, 0, "IK Constraint");
2361 if (segcount==data->rootbone || segcount>255) break; // 255 is weak
2362 parchan= parchan->parent;
2369 if (cti->flush_constraint_targets)
2370 cti->flush_constraint_targets(con, &targets, 1);
2373 if (addtoroot == 1 ) {
2374 dag_add_relation(dag, rootnode, node, 0, "Root Bone Relation");
2378 dag_check_cycle(dag);
2380 /* now we try to sort... */
2381 tempbase.first= tempbase.last= NULL;
2383 nqueue = queue_create(DAGQUEUEALLOC);
2385 /* tag nodes unchecked */
2386 for(node = dag->DagNode.first; node; node= node->next)
2387 node->color = DAG_WHITE;
2389 node = dag->DagNode.first;
2391 node->color = DAG_GRAY;
2392 push_stack(nqueue, node);
2394 while(nqueue->count) {
2397 node = get_top_node_queue(nqueue);
2400 while(itA != NULL) {
2401 if((itA->node->color == DAG_WHITE) ) {
2402 itA->node->color = DAG_GRAY;
2403 push_stack(nqueue,itA->node);
2412 node = pop_queue(nqueue);
2413 if (node->ob == NULL) // we are done
2415 node->color = DAG_BLACK;
2417 /* put node in new list */
2418 BLI_remlink(&pose->chanbase, node->ob);
2419 BLI_addhead(&tempbase, node->ob);
2424 // temporal correction for circular dependancies
2425 while(pose->chanbase.first) {
2426 pchan= pose->chanbase.first;
2427 BLI_remlink(&pose->chanbase, pchan);
2428 BLI_addhead(&tempbase, pchan);
2430 printf("cyclic %s\n", pchan->name);
2433 pose->chanbase = tempbase;
2434 queue_delete(nqueue);
2436 // printf("\nordered\n");
2437 // for(pchan = pose->chanbase.first; pchan; pchan= pchan->next) {
2438 // printf(" %s\n", pchan->name);