4 * ***** BEGIN GPL/BL DUAL 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. The Blender
10 * Foundation also sells licenses for use in proprietary software under
11 * the Blender License. See http://www.blender.org/BL/ for information
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * The Original Code is Copyright (C) 2004 Blender Foundation.
24 * All rights reserved.
26 * Contributor(s): none yet.
28 * ***** END GPL/BL DUAL LICENSE BLOCK *****
36 #include "BLI_winstuff.h"
39 //#include "BMF_Api.h"
41 #include "BLI_blenlib.h"
42 #include "BLI_arithb.h"
44 #include "DNA_action_types.h"
45 #include "DNA_armature_types.h"
46 #include "DNA_curve_types.h"
47 #include "DNA_camera_types.h"
49 #include "DNA_effect_types.h"
50 #include "DNA_group_types.h"
51 #include "DNA_lattice_types.h"
52 #include "DNA_key_types.h"
53 #include "DNA_mesh_types.h"
54 #include "DNA_modifier_types.h"
55 #include "DNA_nla_types.h"
56 #include "DNA_object_types.h"
57 #include "DNA_object_force.h"
58 #include "DNA_object_fluidsim.h"
59 #include "DNA_oops_types.h"
60 #include "DNA_particle_types.h"
61 #include "DNA_scene_types.h"
62 #include "DNA_screen_types.h"
63 #include "DNA_space_types.h"
64 #include "DNA_view2d_types.h"
65 #include "DNA_view3d_types.h"
67 #include "BKE_action.h"
68 #include "BKE_effect.h"
69 #include "BKE_global.h"
70 #include "BKE_group.h"
73 #include "BKE_mball.h"
74 #include "BKE_modifier.h"
75 #include "BKE_object.h"
76 #include "BKE_particle.h"
77 #include "BKE_utildefines.h"
78 #include "BKE_scene.h"
80 #include "MEM_guardedalloc.h"
83 #include "BPY_extern.h"
85 #include "depsgraph_private.h"
87 /* Queue and stack operations for dag traversal
89 * the queue store a list of freenodes to avoid successives alloc/dealloc
92 DagNodeQueue * queue_create (int slots)
95 DagNodeQueueElem * elem;
98 queue = MEM_mallocN(sizeof(DagNodeQueue),"DAG queue");
99 queue->freenodes = MEM_mallocN(sizeof(DagNodeQueue),"DAG queue");
102 queue->first = queue->last = NULL;
103 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem3");
106 queue->freenodes->first = queue->freenodes->last = elem;
108 for (i = 1; i <slots;i++) {
109 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem4");
112 queue->freenodes->last->next = elem;
113 queue->freenodes->last = elem;
115 queue->freenodes->count = slots;
119 void queue_raz(DagNodeQueue *queue)
121 DagNodeQueueElem * elem;
124 if (queue->freenodes->last)
125 queue->freenodes->last->next = elem;
127 queue->freenodes->first = queue->freenodes->last = elem;
130 queue->freenodes->count++;
134 queue->freenodes->count++;
136 queue->freenodes->last = elem;
140 void queue_delete(DagNodeQueue *queue)
142 DagNodeQueueElem * elem;
143 DagNodeQueueElem * temp;
152 elem = queue->freenodes->first;
159 MEM_freeN(queue->freenodes);
163 /* insert in queue, remove in front */
164 void push_queue(DagNodeQueue *queue, DagNode *node)
166 DagNodeQueueElem * elem;
170 fprintf(stderr,"pushing null node \n");
173 /*fprintf(stderr,"BFS push : %s %d\n",((ID *) node->ob)->name, queue->count);*/
175 elem = queue->freenodes->first;
177 queue->freenodes->first = elem->next;
178 if ( queue->freenodes->last == elem) {
179 queue->freenodes->last = NULL;
180 queue->freenodes->first = NULL;
182 queue->freenodes->count--;
183 } else { /* alllocating more */
184 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem1");
187 queue->freenodes->first = queue->freenodes->last = elem;
189 for (i = 1; i <DAGQUEUEALLOC;i++) {
190 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem2");
193 queue->freenodes->last->next = elem;
194 queue->freenodes->last = elem;
196 queue->freenodes->count = DAGQUEUEALLOC;
198 elem = queue->freenodes->first;
199 queue->freenodes->first = elem->next;
203 if (queue->last != NULL)
204 queue->last->next = elem;
206 if (queue->first == NULL) {
213 /* insert in front, remove in front */
214 void push_stack(DagNodeQueue *queue, DagNode *node)
216 DagNodeQueueElem * elem;
219 elem = queue->freenodes->first;
221 queue->freenodes->first = elem->next;
222 if ( queue->freenodes->last == elem) {
223 queue->freenodes->last = NULL;
224 queue->freenodes->first = NULL;
226 queue->freenodes->count--;
227 } else { /* alllocating more */
228 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem1");
231 queue->freenodes->first = queue->freenodes->last = elem;
233 for (i = 1; i <DAGQUEUEALLOC;i++) {
234 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem2");
237 queue->freenodes->last->next = elem;
238 queue->freenodes->last = elem;
240 queue->freenodes->count = DAGQUEUEALLOC;
242 elem = queue->freenodes->first;
243 queue->freenodes->first = elem->next;
245 elem->next = queue->first;
248 if (queue->last == NULL)
254 DagNode * pop_queue(DagNodeQueue *queue)
256 DagNodeQueueElem * elem;
261 queue->first = elem->next;
262 if (queue->last == elem) {
267 if (queue->freenodes->last)
268 queue->freenodes->last->next=elem;
269 queue->freenodes->last=elem;
270 if (queue->freenodes->first == NULL)
271 queue->freenodes->first=elem;
275 queue->freenodes->count++;
278 fprintf(stderr,"return null \n");
283 void *pop_ob_queue(struct DagNodeQueue *queue) {
284 return(pop_queue(queue)->ob);
287 DagNode * get_top_node_queue(DagNodeQueue *queue)
289 return queue->first->node;
292 int queue_count(struct DagNodeQueue *queue){
297 DagForest * dag_init()
300 /* use callocN to init all zero */
301 forest = MEM_callocN(sizeof(DagForest),"DAG root");
305 static void dag_add_driver_relation(Ipo *ipo, DagForest *dag, DagNode *node, int isdata)
310 for(icu= ipo->curve.first; icu; icu= icu->next) {
313 if (icu->driver->type == IPO_DRIVER_TYPE_PYTHON) {
315 if ((icu->driver->flag & IPO_DRIVER_FLAG_INVALID) || (icu->driver->name[0] == '\0'))
316 continue; /* empty or invalid expression */
318 /* now we need refs to all objects mentioned in this
319 * pydriver expression, to call 'dag_add_relation'
320 * for each of them */
321 Object **obarray = BPY_pydriver_get_objects(icu->driver);
323 Object *ob, **oba = obarray;
327 node1 = dag_get_node(dag, ob);
328 if (ob->type == OB_ARMATURE)
329 dag_add_relation(dag, node1, node, isdata?DAG_RL_DATA_DATA:DAG_RL_DATA_OB);
331 dag_add_relation(dag, node1, node, isdata?DAG_RL_OB_DATA:DAG_RL_OB_OB);
339 else if (icu->driver->ob) {
340 node1 = dag_get_node(dag, icu->driver->ob);
341 if(icu->driver->blocktype==ID_AR)
342 dag_add_relation(dag, node1, node, isdata?DAG_RL_DATA_DATA:DAG_RL_DATA_OB);
344 dag_add_relation(dag, node1, node, isdata?DAG_RL_OB_DATA:DAG_RL_OB_OB);
350 static void build_dag_object(DagForest *dag, DagNode *scenenode, Object *ob, int mask)
353 bConstraintChannel *conchan;
358 ParticleSystem *psys;
361 node = dag_get_node(dag, ob);
363 if ((ob->data) && (mask&DAG_RL_DATA)) {
364 node2 = dag_get_node(dag,ob->data);
365 dag_add_relation(dag,node,node2,DAG_RL_DATA);
366 node2->first_ancestor = ob;
367 node2->ancestor_count += 1;
370 if (ob->type == OB_ARMATURE) {
375 for (pchan = ob->pose->chanbase.first; pchan; pchan=pchan->next) {
376 for (con = pchan->constraints.first; con; con=con->next) {
377 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
378 ListBase targets = {NULL, NULL};
379 bConstraintTarget *ct;
381 if (cti && cti->get_constraint_targets) {
382 cti->get_constraint_targets(con, &targets);
384 for (ct= targets.first; ct; ct= ct->next) {
385 if (ct->tar && ct->tar != ob) {
386 // fprintf(stderr,"armature %s target :%s \n", ob->id.name, target->id.name);
387 node3 = dag_get_node(dag, ct->tar);
389 if (ct->subtarget[0])
390 dag_add_relation(dag,node3,node, DAG_RL_OB_DATA|DAG_RL_DATA_DATA);
391 else if(ELEM(con->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO))
392 dag_add_relation(dag,node3,node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA);
394 dag_add_relation(dag,node3,node, DAG_RL_OB_DATA);
398 if (cti->flush_constraint_targets)
399 cti->flush_constraint_targets(con, &targets, 1);
407 /* driver dependencies, nla modifiers */
409 dag_add_driver_relation(ob->ipo, dag, node, 0);
413 dag_add_driver_relation(key->ipo, dag, node, 1);
415 for (conchan=ob->constraintChannels.first; conchan; conchan=conchan->next)
417 dag_add_driver_relation(conchan->ipo, dag, node, 0);
420 bActionChannel *chan;
421 for (chan = ob->action->chanbase.first; chan; chan=chan->next){
423 dag_add_driver_relation(chan->ipo, dag, node, 1);
424 for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next)
426 dag_add_driver_relation(conchan->ipo, dag, node, 1);
429 if(ob->nlastrips.first) {
431 bActionChannel *chan;
432 for(strip= ob->nlastrips.first; strip; strip= strip->next) {
433 if(strip->act && strip->act!=ob->action)
434 for (chan = strip->act->chanbase.first; chan; chan=chan->next)
436 dag_add_driver_relation(chan->ipo, dag, node, 1);
437 if(strip->modifiers.first) {
438 bActionModifier *amod;
439 for(amod= strip->modifiers.first; amod; amod= amod->next) {
441 node2 = dag_get_node(dag, amod->ob);
442 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA);
448 if (ob->modifiers.first) {
451 for(md=ob->modifiers.first; md; md=md->next) {
452 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
454 if (mti->updateDepgraph) mti->updateDepgraph(md, dag, ob, node);
458 node2 = dag_get_node(dag,ob->parent);
460 switch(ob->partype) {
462 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_OB);
464 case PARVERT1: case PARVERT3: case PARBONE:
465 dag_add_relation(dag,node2,node,DAG_RL_DATA_OB|DAG_RL_OB_OB);
468 if(ob->parent->type==OB_LATTICE)
469 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_OB);
470 else if(ob->parent->type==OB_CURVE) {
471 Curve *cu= ob->parent->data;
472 if(cu->flag & CU_PATH)
473 dag_add_relation(dag,node2,node,DAG_RL_DATA_OB|DAG_RL_OB_OB);
475 dag_add_relation(dag,node2,node,DAG_RL_OB_OB);
478 dag_add_relation(dag,node2,node,DAG_RL_OB_OB);
480 /* exception case: parent is duplivert */
481 if(ob->type==OB_MBALL && (ob->parent->transflag & OB_DUPLIVERTS)) {
482 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_OB);
488 node2 = dag_get_node(dag,ob->track);
489 dag_add_relation(dag,node2,node,DAG_RL_OB_OB);
493 node2 = dag_get_node(dag, ob->proxy);
494 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA|DAG_RL_OB_OB);
495 /* inverted relation, so addtoroot shouldn't be set to zero */
497 if (ob->type==OB_CAMERA) {
498 Camera *cam = (Camera *)ob->data;
500 node2 = dag_get_node(dag, cam->dof_ob);
501 dag_add_relation(dag,node2,node,DAG_RL_OB_OB);
504 if (ob->transflag & OB_DUPLI) {
505 if((ob->transflag & OB_DUPLIGROUP) && ob->dup_group) {
507 for(go= ob->dup_group->gobject.first; go; go= go->next) {
509 node2 = dag_get_node(dag, go->ob);
510 /* node2 changes node1, this keeps animations updated in groups?? not logical? */
511 dag_add_relation(dag, node2, node, DAG_RL_OB_OB);
517 /* softbody collision */
518 if((ob->type==OB_MESH) || (ob->type==OB_CURVE) || (ob->type==OB_LATTICE)) {
520 if(modifiers_isSoftbodyEnabled(ob)){
521 // would be nice to have a list of colliders here
522 // so for now walk all objects in scene check 'same layer rule'
523 for(base = G.scene->base.first; base; base= base->next) {
524 if( (base->lay & ob->lay) && base->object->pd) {
525 Object *ob1= base->object;
526 if((ob1->pd->deflect) && (ob1 != ob)) {
527 node2 = dag_get_node(dag, ob1);
528 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA);
535 if (ob->type==OB_MBALL) {
536 Object *mom= find_basis_mball(ob);
538 node2 = dag_get_node(dag, mom);
539 dag_add_relation(dag,node,node2,DAG_RL_DATA_DATA|DAG_RL_OB_DATA); // mom depends on children!
542 else if (ob->type==OB_CURVE) {
545 node2 = dag_get_node(dag, cu->bevobj);
546 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_DATA);
549 node2 = dag_get_node(dag, cu->taperobj);
550 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_DATA);
553 dag_add_driver_relation(cu->ipo, dag, node, 1);
556 else if(ob->type==OB_FONT) {
558 if(cu->textoncurve) {
559 node2 = dag_get_node(dag, cu->textoncurve);
560 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_DATA);
563 else if(ob->type==OB_MESH) {
564 PartEff *paf= give_parteff(ob);
569 /* ob location depends on itself */
570 if((paf->flag & PAF_STATIC)==0)
571 dag_add_relation(dag, node, node, DAG_RL_OB_DATA);
573 listb= pdInitEffectors(ob, paf->group); /* note, makes copy... */
575 for(ec= listb->first; ec; ec= ec->next) {
577 PartDeflect *pd= ob1->pd;
580 node2 = dag_get_node(dag, ob1);
581 if(pd->forcefield==PFIELD_GUIDE)
582 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA);
584 dag_add_relation(dag, node2, node, DAG_RL_OB_DATA);
588 pdEndEffectors(listb); /* restores copy... */
593 psys= ob->particlesystem.first;
595 ParticleEffectorCache *nec;
597 for(; psys; psys=psys->next) {
598 ParticleSettings *part= psys->part;
600 dag_add_relation(dag, node, node, DAG_RL_OB_DATA);
602 if(part->phystype==PART_PHYS_KEYED && psys->keyed_ob &&
603 BLI_findlink(&psys->keyed_ob->particlesystem,psys->keyed_psys-1)) {
604 node2 = dag_get_node(dag, psys->keyed_ob);
605 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA);
608 if(psys->effectors.first)
609 psys_end_effectors(psys);
610 psys_init_effectors(ob,psys->part->eff_group,psys);
612 if(psys->effectors.first) {
613 for(nec= psys->effectors.first; nec; nec= nec->next) {
614 Object *ob1= nec->ob;
616 if(nec->type & PSYS_EC_EFFECTOR) {
617 node2 = dag_get_node(dag, ob1);
618 if(ob1->pd->forcefield==PFIELD_GUIDE)
619 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA);
621 dag_add_relation(dag, node2, node, DAG_RL_OB_DATA);
623 else if(nec->type & PSYS_EC_DEFLECT) {
624 node2 = dag_get_node(dag, ob1);
625 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA);
627 else if(nec->type & PSYS_EC_PARTICLE) {
628 node2 = dag_get_node(dag, ob1);
629 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA);
632 if(nec->type & PSYS_EC_REACTOR) {
633 node2 = dag_get_node(dag, ob1);
634 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA);
641 for (con = ob->constraints.first; con; con=con->next) {
642 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
643 ListBase targets = {NULL, NULL};
644 bConstraintTarget *ct;
646 if (cti && cti->get_constraint_targets) {
647 cti->get_constraint_targets(con, &targets);
649 for (ct= targets.first; ct; ct= ct->next) {
657 node2 = dag_get_node(dag, obt);
658 if (ELEM(con->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO))
659 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB|DAG_RL_OB_OB);
661 if (ELEM3(obt->type, OB_ARMATURE, OB_MESH, OB_LATTICE) && (ct->subtarget[0]))
662 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB|DAG_RL_OB_OB);
664 dag_add_relation(dag, node2, node, DAG_RL_OB_OB);
669 if (cti->flush_constraint_targets)
670 cti->flush_constraint_targets(con, &targets, 1);
675 dag_add_relation(dag,scenenode,node,DAG_RL_SCENE);
678 struct DagForest *build_dag(struct Scene *sce, short mask)
698 /* add base node for scene. scene is always the first node in DAG */
699 scenenode = dag_add_node(dag, sce);
701 /* add current scene objects */
702 for(base = sce->base.first; base; base= base->next) {
705 build_dag_object(dag, scenenode, ob, mask);
707 build_dag_object(dag, scenenode, ob->proxy, mask);
709 /* handled in next loop */
711 ob->dup_group->id.flag |= LIB_DOIT;
714 /* add groups used in current scene objects */
715 for(group= G.main->group.first; group; group= group->id.next) {
716 if(group->id.flag & LIB_DOIT) {
717 for(go= group->gobject.first; go; go= go->next) {
718 build_dag_object(dag, scenenode, go->ob, mask);
720 group->id.flag &= ~LIB_DOIT;
724 /* Now all relations were built, but we need to solve 1 exceptional case;
725 When objects have multiple "parents" (for example parent + constraint working on same object)
726 the relation type has to be synced. One of the parents can change, and should give same event to child */
728 /* nodes were callocced, so we can use node->color for temporal storage */
729 for(node = sce->theDag->DagNode.first; node; node= node->next) {
730 if(node->type==ID_OB) {
731 for(itA = node->child; itA; itA= itA->next) {
732 if(itA->node->type==ID_OB) {
733 itA->node->color |= itA->type;
738 /* now set relations equal, so that when only one parent changes, the correct recalcs are found */
739 for(node = sce->theDag->DagNode.first; node; node= node->next) {
740 if(node->type==ID_OB) {
741 for(itA = node->child; itA; itA= itA->next) {
742 if(itA->node->type==ID_OB) {
743 itA->type |= itA->node->color;
749 // cycle detection and solving
750 // solve_cycles(dag);
756 void free_forest(DagForest *Dag)
757 { /* remove all nodes and deps */
761 DagNode *itN = Dag->DagNode.first;
782 Dag->DagNode.first = NULL;
783 Dag->DagNode.last = NULL;
788 DagNode * dag_find_node (DagForest *forest,void * fob)
790 DagNode *node = forest->DagNode.first;
800 static int ugly_hack_sorry= 1; // prevent type check
802 /* no checking of existance, use dag_find_node first or dag_get_node */
803 DagNode * dag_add_node (DagForest *forest, void * fob)
807 node = MEM_callocN(sizeof(DagNode),"DAG node");
810 node->color = DAG_WHITE;
812 if(ugly_hack_sorry) node->type = GS(((ID *) fob)->name); // sorry, done for pose sorting
813 if (forest->numNodes) {
814 ((DagNode *) forest->DagNode.last)->next = node;
815 forest->DagNode.last = node;
818 forest->DagNode.last = node;
819 forest->DagNode.first = node;
820 forest->numNodes = 1;
826 DagNode * dag_get_node (DagForest *forest,void * fob)
830 node = dag_find_node (forest, fob);
832 node = dag_add_node(forest, fob);
838 DagNode * dag_get_sub_node (DagForest *forest,void * fob)
841 DagAdjList *mainchild, *prev=NULL;
843 mainchild = ((DagNode *) forest->DagNode.first)->child;
844 /* remove from first node (scene) adj list if present */
846 if (mainchild->node == fob) {
848 prev->next = mainchild->next;
849 MEM_freeN(mainchild);
852 ((DagNode *) forest->DagNode.first)->child = mainchild->next;
853 MEM_freeN(mainchild);
858 mainchild = mainchild->next;
860 node = dag_find_node (forest, fob);
862 node = dag_add_node(forest, fob);
866 void dag_add_relation(DagForest *forest, DagNode *fob1, DagNode *fob2, short rel)
868 DagAdjList *itA = fob1->child;
870 while (itA) { /* search if relation exist already */
871 if (itA->node == fob2) {
878 /* create new relation and insert at head. MALLOC alert! */
879 itA = MEM_mallocN(sizeof(DagAdjList),"DAG adj list");
883 itA->next = fob1->child;
887 static void dag_add_parent_relation(DagForest *forest, DagNode *fob1, DagNode *fob2, short rel)
889 DagAdjList *itA = fob2->parent;
891 while (itA) { /* search if relation exist already */
892 if (itA->node == fob1) {
899 /* create new relation and insert at head. MALLOC alert! */
900 itA = MEM_mallocN(sizeof(DagAdjList),"DAG adj list");
904 itA->next = fob2->parent;
910 * MainDAG is the DAG of all objects in current scene
911 * used only for drawing there is one also in each scene
913 static DagForest * MainDag = NULL;
915 DagForest *getMainDag(void)
921 void setMainDag(DagForest *dag)
929 * in theory we should sweep the whole array
930 * but in our case the first node is the scene
931 * and is linked to every other object
933 * for general case we will need to add outer loop
937 * ToDo : change pos kludge
940 /* adjust levels for drawing in oops space */
944 DagNodeQueue *nqueue;
950 /* fprintf(stderr,"starting BFS \n ------------\n"); */
951 nqueue = queue_create(DAGQUEUEALLOC);
952 for ( i=0; i<50; i++)
956 * dagnode.first is alway the root (scene)
958 node = MainDag->DagNode.first;
960 node->color = DAG_WHITE;
961 node->BFS_dist = 9999;
966 node = MainDag->DagNode.first;
967 if (node->color == DAG_WHITE) {
968 node->color = DAG_GRAY;
970 push_queue(nqueue,node);
971 while(nqueue->count) {
972 node = pop_queue(nqueue);
974 minheight = pos[node->BFS_dist];
977 if((itA->node->color == DAG_WHITE) ) {
978 itA->node->color = DAG_GRAY;
979 itA->node->BFS_dist = node->BFS_dist + 1;
980 itA->node->k = (float) minheight;
981 push_queue(nqueue,itA->node);
985 fprintf(stderr,"bfs not dag tree edge color :%i \n",itA->node->color);
991 if (pos[node->BFS_dist] > node->k ) {
992 pos[node->BFS_dist] += 1;
993 node->k = (float) pos[node->BFS_dist];
995 pos[node->BFS_dist] = (int) node->k +1;
997 set_node_xy(node, node->BFS_dist*DEPSX*2, pos[node->BFS_dist]*DEPSY*2);
998 node->color = DAG_BLACK;
1000 fprintf(stderr,"BFS node : %20s %i %5.0f %5.0f\n",((ID *) node->ob)->name,node->BFS_dist, node->x, node->y);
1004 queue_delete(nqueue);
1007 int pre_and_post_BFS(DagForest *dag, short mask, graph_action_func pre_func, graph_action_func post_func, void **data) {
1010 node = dag->DagNode.first;
1011 return pre_and_post_source_BFS(dag, mask, node, pre_func, post_func, data);
1015 int pre_and_post_source_BFS(DagForest *dag, short mask, DagNode *source, graph_action_func pre_func, graph_action_func post_func, void **data)
1018 DagNodeQueue *nqueue;
1021 /* fprintf(stderr,"starting BFS \n ------------\n"); */
1024 * dagnode.first is alway the root (scene)
1026 node = dag->DagNode.first;
1027 nqueue = queue_create(DAGQUEUEALLOC);
1029 node->color = DAG_WHITE;
1030 node->BFS_dist = 9999;
1035 if (node->color == DAG_WHITE) {
1036 node->color = DAG_GRAY;
1038 pre_func(node->ob,data);
1040 while(nqueue->count) {
1041 node = pop_queue(nqueue);
1044 while(itA != NULL) {
1045 if((itA->node->color == DAG_WHITE) && (itA->type & mask)) {
1046 itA->node->color = DAG_GRAY;
1047 itA->node->BFS_dist = node->BFS_dist + 1;
1048 push_queue(nqueue,itA->node);
1049 pre_func(node->ob,data);
1052 else { // back or cross edge
1057 post_func(node->ob,data);
1058 node->color = DAG_BLACK;
1060 fprintf(stderr,"BFS node : %20s %i %5.0f %5.0f\n",((ID *) node->ob)->name,node->BFS_dist, node->x, node->y);
1064 queue_delete(nqueue);
1068 /* non recursive version of DFS, return queue -- outer loop present to catch odd cases (first level cycles)*/
1069 DagNodeQueue * graph_dfs(void)
1072 DagNodeQueue *nqueue;
1073 DagNodeQueue *retqueue;
1083 *fprintf(stderr,"starting DFS \n ------------\n");
1085 nqueue = queue_create(DAGQUEUEALLOC);
1086 retqueue = queue_create(MainDag->numNodes);
1087 for ( i=0; i<50; i++)
1091 * dagnode.first is alway the root (scene)
1093 node = MainDag->DagNode.first;
1095 node->color = DAG_WHITE;
1096 node->DFS_dist = 9999;
1097 node->DFS_dvtm = node->DFS_fntm = 9999;
1104 node = MainDag->DagNode.first;
1107 if (node->color == DAG_WHITE) {
1108 node->color = DAG_GRAY;
1110 node->DFS_dvtm = time;
1112 push_stack(nqueue,node);
1114 while(nqueue->count) {
1115 //graph_print_queue(nqueue);
1118 node = get_top_node_queue(nqueue);
1120 minheight = pos[node->DFS_dist];
1123 while(itA != NULL) {
1124 if((itA->node->color == DAG_WHITE) ) {
1125 itA->node->DFS_dvtm = time;
1126 itA->node->color = DAG_GRAY;
1129 itA->node->DFS_dist = node->DFS_dist + 1;
1130 itA->node->k = (float) minheight;
1131 push_stack(nqueue,itA->node);
1135 if (itA->node->color == DAG_GRAY) { // back edge
1136 fprintf(stderr,"dfs back edge :%15s %15s \n",((ID *) node->ob)->name, ((ID *) itA->node->ob)->name);
1138 } else if (itA->node->color == DAG_BLACK) {
1140 /* already processed node but we may want later to change distance either to shorter to longer.
1141 * DFS_dist is the first encounter
1143 /*if (node->DFS_dist >= itA->node->DFS_dist)
1144 itA->node->DFS_dist = node->DFS_dist + 1;
1146 fprintf(stderr,"dfs forward or cross edge :%15s %i-%i %15s %i-%i \n",
1147 ((ID *) node->ob)->name,
1150 ((ID *) itA->node->ob)->name,
1151 itA->node->DFS_dvtm,
1152 itA->node->DFS_fntm);
1155 fprintf(stderr,"dfs unknown edge \n");
1161 node = pop_queue(nqueue);
1162 node->color = DAG_BLACK;
1164 node->DFS_fntm = time;
1166 if (node->DFS_dist > maxpos)
1167 maxpos = node->DFS_dist;
1168 if (pos[node->DFS_dist] > node->k ) {
1169 pos[node->DFS_dist] += 1;
1170 node->k = (float) pos[node->DFS_dist];
1172 pos[node->DFS_dist] = (int) node->k +1;
1174 set_node_xy(node, node->DFS_dist*DEPSX*2, pos[node->DFS_dist]*DEPSY*2);
1177 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 );
1179 push_stack(retqueue,node);
1186 // fprintf(stderr,"i size : %i \n", maxpos);
1188 queue_delete(nqueue);
1193 int pre_and_post_DFS(DagForest *dag, short mask, graph_action_func pre_func, graph_action_func post_func, void **data) {
1196 node = dag->DagNode.first;
1197 return pre_and_post_source_DFS(dag, mask, node, pre_func, post_func, data);
1200 int pre_and_post_source_DFS(DagForest *dag, short mask, DagNode *source, graph_action_func pre_func, graph_action_func post_func, void **data)
1203 DagNodeQueue *nqueue;
1209 *fprintf(stderr,"starting DFS \n ------------\n");
1211 nqueue = queue_create(DAGQUEUEALLOC);
1214 * dagnode.first is alway the root (scene)
1216 node = dag->DagNode.first;
1218 node->color = DAG_WHITE;
1219 node->DFS_dist = 9999;
1220 node->DFS_dvtm = node->DFS_fntm = 9999;
1229 if (node->color == DAG_WHITE) {
1230 node->color = DAG_GRAY;
1232 node->DFS_dvtm = time;
1234 push_stack(nqueue,node);
1235 pre_func(node->ob,data);
1237 while(nqueue->count) {
1239 node = get_top_node_queue(nqueue);
1242 while(itA != NULL) {
1243 if((itA->node->color == DAG_WHITE) && (itA->type & mask) ) {
1244 itA->node->DFS_dvtm = time;
1245 itA->node->color = DAG_GRAY;
1248 itA->node->DFS_dist = node->DFS_dist + 1;
1249 push_stack(nqueue,itA->node);
1250 pre_func(node->ob,data);
1255 if (itA->node->color == DAG_GRAY) {// back edge
1258 // else if (itA->node->color == DAG_BLACK) { // cross or forward
1265 node = pop_queue(nqueue);
1266 node->color = DAG_BLACK;
1268 node->DFS_fntm = time;
1270 post_func(node->ob,data);
1276 queue_delete(nqueue);
1281 // used to get the obs owning a datablock
1282 struct DagNodeQueue *get_obparents(struct DagForest *dag, void *ob)
1284 DagNode * node, *node1;
1285 DagNodeQueue *nqueue;
1288 node = dag_find_node(dag,ob);
1292 else if (node->ancestor_count == 1) { // simple case
1293 nqueue = queue_create(1);
1294 push_queue(nqueue,node);
1295 } else { // need to go over the whole dag for adj list
1296 nqueue = queue_create(node->ancestor_count);
1298 node1 = dag->DagNode.first;
1300 if (node1->DFS_fntm > node->DFS_fntm) { // a parent is finished after child. must check adj list
1302 while(itA != NULL) {
1303 if ((itA->node == node) && (itA->type == DAG_RL_DATA)) {
1304 push_queue(nqueue,node);
1309 node1 = node1->next;
1315 struct DagNodeQueue *get_first_ancestors(struct DagForest *dag, void *ob)
1317 DagNode * node, *node1;
1318 DagNodeQueue *nqueue;
1321 node = dag_find_node(dag,ob);
1323 // need to go over the whole dag for adj list
1324 nqueue = queue_create(node->ancestor_count);
1326 node1 = dag->DagNode.first;
1328 if (node1->DFS_fntm > node->DFS_fntm) {
1330 while(itA != NULL) {
1331 if (itA->node == node) {
1332 push_queue(nqueue,node);
1337 node1 = node1->next;
1343 // standard DFS list
1344 struct DagNodeQueue *get_all_childs(struct DagForest *dag, void *ob)
1347 DagNodeQueue *nqueue;
1348 DagNodeQueue *retqueue;
1353 nqueue = queue_create(DAGQUEUEALLOC);
1354 retqueue = queue_create(dag->numNodes); // was MainDag... why? (ton)
1356 node = dag->DagNode.first;
1358 node->color = DAG_WHITE;
1364 node = dag_find_node(dag, ob); // could be done in loop above (ton)
1365 if(node) { // can be null for newly added objects
1367 node->color = DAG_GRAY;
1369 push_stack(nqueue,node);
1371 while(nqueue->count) {
1374 node = get_top_node_queue(nqueue);
1377 while(itA != NULL) {
1378 if((itA->node->color == DAG_WHITE) ) {
1379 itA->node->DFS_dvtm = time;
1380 itA->node->color = DAG_GRAY;
1383 push_stack(nqueue,itA->node);
1391 node = pop_queue(nqueue);
1392 node->color = DAG_BLACK;
1395 push_stack(retqueue,node);
1399 queue_delete(nqueue);
1404 short are_obs_related(struct DagForest *dag, void *ob1, void *ob2) {
1408 node = dag_find_node(dag, ob1);
1411 while(itA != NULL) {
1412 if((itA->node->ob == ob2) ) {
1413 return itA->node->type;
1417 return DAG_NO_RELATION;
1420 int is_acyclic( DagForest *dag) {
1421 return dag->is_acyclic;
1424 void set_node_xy(DagNode *node, float x, float y)
1431 /* debug test functions */
1433 void graph_print_queue(DagNodeQueue *nqueue)
1435 DagNodeQueueElem *queueElem;
1437 queueElem = nqueue->first;
1439 fprintf(stderr,"** %s %i %i-%i ",((ID *) queueElem->node->ob)->name,queueElem->node->color,queueElem->node->DFS_dvtm,queueElem->node->DFS_fntm);
1440 queueElem = queueElem->next;
1442 fprintf(stderr,"\n");
1445 void graph_print_queue_dist(DagNodeQueue *nqueue)
1447 DagNodeQueueElem *queueElem;
1450 queueElem = nqueue->first;
1451 max = queueElem->node->DFS_fntm;
1454 fprintf(stderr,"** %25s %2.2i-%2.2i ",((ID *) queueElem->node->ob)->name,queueElem->node->DFS_dvtm,queueElem->node->DFS_fntm);
1455 while (count < queueElem->node->DFS_dvtm-1) { fputc(' ',stderr); count++;}
1457 while (count < queueElem->node->DFS_fntm-2) { fputc('-',stderr); count++;}
1461 queueElem = queueElem->next;
1463 fprintf(stderr,"\n");
1466 void graph_print_adj_list(void)
1471 node = (getMainDag())->DagNode.first;
1473 fprintf(stderr,"node : %s col: %i",((ID *) node->ob)->name, node->color);
1476 fprintf(stderr,"-- %s ",((ID *) itA->node->ob)->name);
1480 fprintf(stderr,"\n");
1485 /* ************************ API *********************** */
1487 /* groups with objects in this scene need to be put in the right order as well */
1488 static void scene_sort_groups(Scene *sce)
1495 /* test; are group objects all in this scene? */
1496 for(ob= G.main->object.first; ob; ob= ob->id.next) {
1497 ob->id.flag &= ~LIB_DOIT;
1498 ob->id.newid= NULL; /* newid abuse for GroupObject */
1500 for(base = sce->base.first; base; base= base->next)
1501 base->object->id.flag |= LIB_DOIT;
1503 for(group= G.main->group.first; group; group= group->id.next) {
1504 for(go= group->gobject.first; go; go= go->next) {
1505 if((go->ob->id.flag & LIB_DOIT)==0)
1508 /* this group is entirely in this scene */
1510 ListBase listb= {NULL, NULL};
1512 for(go= group->gobject.first; go; go= go->next)
1513 go->ob->id.newid= (ID *)go;
1515 /* in order of sorted bases we reinsert group objects */
1516 for(base = sce->base.first; base; base= base->next) {
1518 if(base->object->id.newid) {
1519 go= (GroupObject *)base->object->id.newid;
1520 base->object->id.newid= NULL;
1521 BLI_remlink( &group->gobject, go);
1522 BLI_addtail( &listb, go);
1525 /* copy the newly sorted listbase */
1526 group->gobject= listb;
1531 /* sort the base list on dependency order */
1532 void DAG_scene_sort(struct Scene *sce)
1535 DagNodeQueue *nqueue;
1542 tempbase.first= tempbase.last= NULL;
1544 build_dag(sce, DAG_RL_ALL_BUT_DATA);
1546 nqueue = queue_create(DAGQUEUEALLOC);
1548 for(node = sce->theDag->DagNode.first; node; node= node->next) {
1549 node->color = DAG_WHITE;
1554 node = sce->theDag->DagNode.first;
1556 node->color = DAG_GRAY;
1558 push_stack(nqueue,node);
1560 while(nqueue->count) {
1563 node = get_top_node_queue(nqueue);
1566 while(itA != NULL) {
1567 if((itA->node->color == DAG_WHITE) ) {
1568 itA->node->DFS_dvtm = time;
1569 itA->node->color = DAG_GRAY;
1572 push_stack(nqueue,itA->node);
1581 node = pop_queue(nqueue);
1582 if (node->ob == sce) // we are done
1584 node->color = DAG_BLACK;
1587 base = sce->base.first;
1588 while (base && base->object != node->ob)
1591 BLI_remlink(&sce->base,base);
1592 BLI_addhead(&tempbase,base);
1598 // temporal correction for circular dependancies
1599 base = sce->base.first;
1601 BLI_remlink(&sce->base,base);
1602 BLI_addhead(&tempbase,base);
1604 printf("cyclic %s\n", base->object->id.name);
1605 base = sce->base.first;
1608 sce->base = tempbase;
1609 queue_delete(nqueue);
1611 /* all groups with objects in this scene gets resorted too */
1612 scene_sort_groups(sce);
1615 printf("\nordered\n");
1616 for(base = sce->base.first; base; base= base->next) {
1617 printf(" %s\n", base->object->id.name);
1621 sce->recalc |= SCE_PRV_CHANGED; /* test for 3d preview */
1624 /* node was checked to have lasttime != curtime and is if type ID_OB */
1625 static void flush_update_node(DagNode *node, unsigned int layer, int curtime)
1629 int oldflag, changed=0;
1630 unsigned int all_layer;
1632 node->lasttime= curtime;
1635 if(ob && (ob->recalc & OB_RECALC)) {
1637 /* got an object node that changes, now check relations */
1638 for(itA = node->child; itA; itA= itA->next) {
1639 all_layer |= itA->lay;
1640 /* the relationship is visible */
1641 if(itA->lay & layer) {
1642 if(itA->node->type==ID_OB) {
1644 oldflag= obc->recalc;
1646 /* got a ob->obc relation, now check if flag needs flush */
1647 if(ob->recalc & OB_RECALC_OB) {
1648 if(itA->type & DAG_RL_OB_OB) {
1649 //printf("ob %s changes ob %s\n", ob->id.name, obc->id.name);
1650 obc->recalc |= OB_RECALC_OB;
1652 if(itA->type & DAG_RL_OB_DATA) {
1653 //printf("ob %s changes obdata %s\n", ob->id.name, obc->id.name);
1654 obc->recalc |= OB_RECALC_DATA;
1657 if(ob->recalc & OB_RECALC_DATA) {
1658 if(itA->type & DAG_RL_DATA_OB) {
1659 //printf("obdata %s changes ob %s\n", ob->id.name, obc->id.name);
1660 obc->recalc |= OB_RECALC_OB;
1662 if(itA->type & DAG_RL_DATA_DATA) {
1663 //printf("obdata %s changes obdata %s\n", ob->id.name, obc->id.name);
1664 obc->recalc |= OB_RECALC_DATA;
1667 if(oldflag!=obc->recalc) changed= 1;
1671 /* even nicer, we can clear recalc flags... */
1672 if((all_layer & layer)==0) {
1673 /* but existing displaylists or derivedmesh should be freed */
1674 if(ob->recalc & OB_RECALC_DATA)
1675 object_free_display(ob);
1677 ob->recalc &= ~OB_RECALC;
1681 /* check case where child changes and parent forcing obdata to change */
1682 /* should be done regardless if this ob has recalc set */
1683 /* could merge this in with loop above...? (ton) */
1684 for(itA = node->child; itA; itA= itA->next) {
1685 /* the relationship is visible */
1686 if(itA->lay & layer) {
1687 if(itA->node->type==ID_OB) {
1690 if((obc->recalc & OB_RECALC)==OB_RECALC_OB) {
1691 /* parent has deforming info */
1692 if(itA->type & (DAG_RL_OB_DATA|DAG_RL_DATA_DATA)) {
1693 // printf("parent %s changes ob %s\n", ob->id.name, obc->id.name);
1694 obc->recalc |= OB_RECALC_DATA;
1701 /* we only go deeper if node not checked or something changed */
1702 for(itA = node->child; itA; itA= itA->next) {
1703 if(changed || itA->node->lasttime!=curtime)
1704 flush_update_node(itA->node, layer, curtime);
1709 /* node was checked to have lasttime != curtime , and is of type ID_OB */
1710 static unsigned int flush_layer_node(DagNode *node, int curtime)
1714 node->lasttime= curtime;
1715 node->lay= ((Object *)node->ob)->lay;
1717 for(itA = node->child; itA; itA= itA->next) {
1718 if(itA->node->type==ID_OB) {
1719 if(itA->node->lasttime!=curtime) {
1720 itA->lay= flush_layer_node(itA->node, curtime); // lay is only set once for each relation
1721 //printf("layer %d for relation %s to %s\n", itA->lay, ((Object *)node->ob)->id.name, ((Object *)itA->node->ob)->id.name);
1723 else itA->lay= itA->node->lay;
1725 node->lay |= itA->lay;
1732 /* flushes all recalc flags in objects down the dependency tree */
1733 void DAG_scene_flush_update(Scene *sce, unsigned int lay)
1739 if(sce->theDag==NULL) {
1740 printf("DAG zero... not allowed to happen!\n");
1741 DAG_scene_sort(sce);
1744 firstnode= sce->theDag->DagNode.first; // always scene node
1746 /* first we flush the layer flags */
1747 sce->theDag->time++; // so we know which nodes were accessed
1748 lasttime= sce->theDag->time;
1749 for(itA = firstnode->child; itA; itA= itA->next) {
1750 if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB)
1751 flush_layer_node(itA->node, lasttime);
1754 /* then we use the relationships + layer info to flush update events */
1755 sce->theDag->time++; // so we know which nodes were accessed
1756 lasttime= sce->theDag->time;
1757 for(itA = firstnode->child; itA; itA= itA->next) {
1758 if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB)
1759 flush_update_node(itA->node, lay, lasttime);
1763 static int object_modifiers_use_time(Object *ob)
1767 for (md=ob->modifiers.first; md; md=md->next)
1768 if (modifier_dependsOnTime(md))
1774 static int exists_channel(Object *ob, char *name)
1776 bActionStrip *strip;
1779 if(get_action_channel(ob->action, name))
1782 for (strip=ob->nlastrips.first; strip; strip=strip->next)
1783 if(get_action_channel(strip->act, name))
1788 static void dag_object_time_update_flags(Object *ob)
1791 if(ob->ipo) ob->recalc |= OB_RECALC_OB;
1792 else if(ob->constraints.first) {
1794 for (con = ob->constraints.first; con; con=con->next) {
1795 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
1796 ListBase targets = {NULL, NULL};
1797 bConstraintTarget *ct;
1799 if (cti && cti->get_constraint_targets) {
1800 cti->get_constraint_targets(con, &targets);
1802 for (ct= targets.first; ct; ct= ct->next) {
1804 ob->recalc |= OB_RECALC_OB;
1809 if (cti->flush_constraint_targets)
1810 cti->flush_constraint_targets(con, &targets, 1);
1814 else if(ob->scriptlink.totscript) ob->recalc |= OB_RECALC_OB;
1815 else if(ob->parent) {
1816 /* motion path or bone child */
1817 if(ob->parent->type==OB_CURVE || ob->parent->type==OB_ARMATURE) ob->recalc |= OB_RECALC_OB;
1820 if(ob->action || ob->nlastrips.first) {
1821 /* since actions now are mixed, we set the recalcs on the safe side */
1822 ob->recalc |= OB_RECALC_OB;
1823 if(ob->type==OB_ARMATURE)
1824 ob->recalc |= OB_RECALC_DATA;
1825 else if(exists_channel(ob, "Shape"))
1826 ob->recalc |= OB_RECALC_DATA;
1827 else if(ob->dup_group) {
1828 bActionStrip *strip;
1829 /* this case is for groups with nla, whilst nla target has no action or nla */
1830 for(strip= ob->nlastrips.first; strip; strip= strip->next) {
1832 strip->object->recalc |= OB_RECALC;
1836 else if(modifiers_isSoftbodyEnabled(ob)) ob->recalc |= OB_RECALC_DATA;
1837 else if(object_modifiers_use_time(ob)) ob->recalc |= OB_RECALC_DATA;
1847 if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
1848 ob->recalc |= OB_RECALC_DATA;
1849 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
1852 else if(ob->effect.first) {
1853 Effect *eff= ob->effect.first;
1854 PartEff *paf= give_parteff(ob);
1856 if(eff->type==EFF_WAVE)
1857 ob->recalc |= OB_RECALC_DATA;
1858 else if(paf && paf->keys==NULL)
1859 ob->recalc |= OB_RECALC_DATA;
1861 if((ob->fluidsimFlag & OB_FLUIDSIM_ENABLE) && (ob->fluidsimSettings)) {
1862 // fluidsimSettings might not be initialized during load...
1863 if(ob->fluidsimSettings->type & (OB_FLUIDSIM_DOMAIN|OB_FLUIDSIM_PARTICLE)) {
1864 ob->recalc |= OB_RECALC_DATA; // NT FSPARTICLE
1867 if(ob->particlesystem.first)
1868 ob->recalc |= OB_RECALC_DATA;
1874 if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
1875 ob->recalc |= OB_RECALC_DATA;
1876 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
1882 if(cu->nurb.first==NULL && cu->str && cu->vfont)
1883 ob->recalc |= OB_RECALC_DATA;
1888 if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
1889 ob->recalc |= OB_RECALC_DATA;
1890 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
1895 if(ob->transflag & OB_DUPLI) ob->recalc |= OB_RECALC_DATA;
1899 if(ob->particlesystem.first) {
1900 ParticleSystem *psys= ob->particlesystem.first;
1902 for(; psys; psys=psys->next) {
1903 if(psys->flag & PSYS_ENABLED) {
1904 ob->recalc |= OB_RECALC_DATA;
1912 /* flag all objects that need recalc, for changes in time for example */
1913 void DAG_scene_update_flags(Scene *scene, unsigned int lay)
1921 /* set ob flags where animated systems are */
1922 for(SETLOOPER(scene, base)) {
1925 /* now if DagNode were part of base, the node->lay could be checked... */
1926 /* we do all now, since the scene_flush checks layers and clears recalc flags even */
1927 dag_object_time_update_flags(ob);
1929 /* handled in next loop */
1931 ob->dup_group->id.flag |= LIB_DOIT;
1934 /* we do groups each once */
1935 for(group= G.main->group.first; group; group= group->id.next) {
1936 if(group->id.flag & LIB_DOIT) {
1937 for(go= group->gobject.first; go; go= go->next) {
1938 dag_object_time_update_flags(go->ob);
1943 for(sce= scene; sce; sce= sce->set)
1944 DAG_scene_flush_update(sce, lay);
1946 /* test: set time flag, to disable baked systems to update */
1947 for(SETLOOPER(scene, base)) {
1950 ob->recalc |= OB_RECALC_TIME;
1953 /* hrmf... an exception to look at once, for invisible camera object we do it over */
1955 dag_object_time_update_flags(scene->camera);
1957 /* and store the info in groupobject */
1958 for(group= G.main->group.first; group; group= group->id.next) {
1959 if(group->id.flag & LIB_DOIT) {
1960 for(go= group->gobject.first; go; go= go->next) {
1961 go->recalc= go->ob->recalc;
1962 // printf("ob %s recalc %d\n", go->ob->id.name, go->recalc);
1964 group->id.flag &= ~LIB_DOIT;
1970 /* for depgraph updating, all layers visible in a screen */
1971 /* this is a copy from editscreen.c... I need to think over a more proper solution for this */
1972 /* probably the DAG_object_flush_update() should give layer too? */
1973 /* or some kind of dag context... (DAG_set_layer) */
1974 static unsigned int dag_screen_view3d_layers(void)
1979 for(sa= G.curscreen->areabase.first; sa; sa= sa->next) {
1980 if(sa->spacetype==SPACE_VIEW3D)
1981 layer |= ((View3D *)sa->spacedata.first)->lay;
1987 /* flag this object and all its relations to recalc */
1988 /* if you need to do more objects, tag object yourself and
1989 use DAG_scene_flush_update() in end */
1990 void DAG_object_flush_update(Scene *sce, Object *ob, short flag)
1993 if(ob==NULL || sce->theDag==NULL) return;
1996 /* all users of this ob->data should be checked */
1997 /* BUT! displists for curves are still only on cu */
1998 if(flag & OB_RECALC_DATA) {
1999 if(ob->type!=OB_CURVE && ob->type!=OB_SURF) {
2001 if(id && id->us>1) {
2002 /* except when there's a key and shapes are locked */
2003 if(ob_get_key(ob) && (ob->shapeflag & (OB_SHAPE_LOCK|OB_SHAPE_TEMPLOCK)));
2006 for (obt=G.main->object.first; obt; obt= obt->id.next) {
2007 if (obt->data==ob->data) {
2008 obt->recalc |= OB_RECALC_DATA;
2017 DAG_scene_flush_update(sce, dag_screen_view3d_layers());
2019 DAG_scene_flush_update(sce, sce->lay);
2022 /* recursively descends tree, each node only checked once */
2023 /* node is checked to be of type object */
2024 static int parent_check_node(DagNode *node, int curtime)
2028 node->lasttime= curtime;
2030 if(node->color==DAG_GRAY)
2033 for(itA = node->child; itA; itA= itA->next) {
2034 if(itA->node->type==ID_OB) {
2036 if(itA->node->color==DAG_GRAY)
2039 /* descend if not done */
2040 if(itA->node->lasttime!=curtime) {
2041 itA->node->color= parent_check_node(itA->node, curtime);
2043 if(itA->node->color==DAG_GRAY)
2052 /* all nodes that influence this object get tagged, for calculating the exact
2053 position of this object at a given timeframe */
2054 void DAG_object_update_flags(Scene *sce, Object *ob, unsigned int lay)
2059 /* tag nodes unchecked */
2060 for(node = sce->theDag->DagNode.first; node; node= node->next)
2061 node->color = DAG_WHITE;
2063 node= dag_find_node(sce->theDag, ob);
2065 /* object not in scene? then handle group exception. needs to be dagged once too */
2067 Group *group= find_group(ob);
2070 /* primitive; tag all... this call helps building groups for particles */
2071 for(go= group->gobject.first; go; go= go->next)
2072 go->ob->recalc= OB_RECALC;
2077 node->color = DAG_GRAY;
2079 sce->theDag->time++;
2080 node= sce->theDag->DagNode.first;
2081 for(itA = node->child; itA; itA= itA->next) {
2082 if(itA->node->type==ID_OB && itA->node->lasttime!=sce->theDag->time)
2083 itA->node->color= parent_check_node(itA->node, sce->theDag->time);
2086 /* set recalcs and flushes */
2087 DAG_scene_update_flags(sce, lay);
2089 /* now we clear recalcs, unless color is set */
2090 for(node = sce->theDag->DagNode.first; node; node= node->next) {
2091 if(node->type==ID_OB && node->color==DAG_WHITE) {
2092 Object *ob= node->ob;
2099 /* ******************* DAG FOR ARMATURE POSE ***************** */
2101 static int node_recurs_level(DagNode *node, int level)
2105 node->color= DAG_BLACK; /* done */
2108 for(itA= node->parent; itA; itA= itA->next) {
2109 if(itA->node->color==DAG_WHITE)
2110 itA->node->ancestor_count= node_recurs_level(itA->node, level);
2116 static void pose_check_cycle(DagForest *dag)
2121 /* tag nodes unchecked */
2122 for(node = dag->DagNode.first; node; node= node->next)
2123 node->color= DAG_WHITE;
2125 for(node = dag->DagNode.first; node; node= node->next) {
2126 if(node->color==DAG_WHITE) {
2127 node->ancestor_count= node_recurs_level(node, 0);
2131 /* check relations, and print errors */
2132 for(node = dag->DagNode.first; node; node= node->next) {
2133 for(itA= node->parent; itA; itA= itA->next) {
2134 if(itA->node->ancestor_count > node->ancestor_count) {
2135 bPoseChannel *pchan= (bPoseChannel *)node->ob;
2136 bPoseChannel *parchan= (bPoseChannel *)itA->node->ob;
2138 if(pchan && parchan)
2139 if(pchan->parent!=parchan)
2140 printf("Cycle in %s to %s\n", pchan->name, parchan->name);
2146 /* we assume its an armature with pose */
2147 void DAG_pose_sort(Object *ob)
2149 bPose *pose= ob->pose;
2150 bPoseChannel *pchan;
2153 DagNode *node2, *node3;
2156 DagNodeQueue *nqueue;
2162 ugly_hack_sorry= 0; // no ID structs
2164 rootnode = dag_add_node(dag, NULL); // node->ob becomes NULL
2166 /* we add the hierarchy and the constraints */
2167 for(pchan = pose->chanbase.first; pchan; pchan= pchan->next) {
2170 node = dag_get_node(dag, pchan);
2173 node2 = dag_get_node(dag, pchan->parent);
2174 dag_add_relation(dag, node2, node, 0);
2175 dag_add_parent_relation(dag, node2, node, 0);
2178 for (con = pchan->constraints.first; con; con=con->next) {
2179 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
2180 ListBase targets = {NULL, NULL};
2181 bConstraintTarget *ct;
2185 for(icu= con->ipo->curve.first; icu; icu= icu->next) {
2186 if(icu->driver && icu->driver->ob==ob) {
2187 bPoseChannel *target= get_pose_channel(ob->pose, icu->driver->name);
2189 node2 = dag_get_node(dag, target);
2190 dag_add_relation(dag, node2, node, 0);
2191 dag_add_parent_relation(dag, node2, node, 0);
2193 /* uncommented this line, results in dependencies
2194 * not being added properly for this constraint,
2195 * what is the purpose of this? - brecht */
2196 /*cti= NULL;*/ /* trick to get next loop skipped */
2202 if (cti && cti->get_constraint_targets) {
2203 cti->get_constraint_targets(con, &targets);
2205 for (ct= targets.first; ct; ct= ct->next) {
2206 if (ct->tar==ob && ct->subtarget[0]) {
2207 bPoseChannel *target= get_pose_channel(ob->pose, ct->subtarget);
2209 node2= dag_get_node(dag, target);
2210 dag_add_relation(dag, node2, node, 0);
2211 dag_add_parent_relation(dag, node2, node, 0);
2213 if (con->type==CONSTRAINT_TYPE_KINEMATIC) {
2214 bKinematicConstraint *data = (bKinematicConstraint *)con->data;
2215 bPoseChannel *parchan;
2218 /* exclude tip from chain? */
2219 if(!(data->flag & CONSTRAINT_IK_TIP))
2220 parchan= pchan->parent;
2224 /* Walk to the chain's root */
2226 node3= dag_get_node(dag, parchan);
2227 dag_add_relation(dag, node2, node3, 0);
2228 dag_add_parent_relation(dag, node2, node3, 0);
2231 if (segcount==data->rootbone || segcount>255) break; // 255 is weak
2232 parchan= parchan->parent;
2239 if (cti->flush_constraint_targets)
2240 cti->flush_constraint_targets(con, &targets, 1);
2243 if (addtoroot == 1 ) {
2244 dag_add_relation(dag, rootnode, node, 0);
2245 dag_add_parent_relation(dag, rootnode, node, 0);
2249 pose_check_cycle(dag);
2251 /* now we try to sort... */
2252 tempbase.first= tempbase.last= NULL;
2254 nqueue = queue_create(DAGQUEUEALLOC);
2256 /* tag nodes unchecked */
2257 for(node = dag->DagNode.first; node; node= node->next)
2258 node->color = DAG_WHITE;
2260 node = dag->DagNode.first;
2262 node->color = DAG_GRAY;
2263 push_stack(nqueue, node);
2265 while(nqueue->count) {
2268 node = get_top_node_queue(nqueue);
2271 while(itA != NULL) {
2272 if((itA->node->color == DAG_WHITE) ) {
2273 itA->node->color = DAG_GRAY;
2274 push_stack(nqueue,itA->node);
2283 node = pop_queue(nqueue);
2284 if (node->ob == NULL) // we are done
2286 node->color = DAG_BLACK;
2288 /* put node in new list */
2289 BLI_remlink(&pose->chanbase, node->ob);
2290 BLI_addhead(&tempbase, node->ob);
2295 // temporal correction for circular dependancies
2296 while(pose->chanbase.first) {
2297 pchan= pose->chanbase.first;
2298 BLI_remlink(&pose->chanbase, pchan);
2299 BLI_addhead(&tempbase, pchan);
2301 printf("cyclic %s\n", pchan->name);
2304 pose->chanbase = tempbase;
2305 queue_delete(nqueue);
2307 // printf("\nordered\n");
2308 // for(pchan = pose->chanbase.first; pchan; pchan= pchan->next) {
2309 // printf(" %s\n", pchan->name);