svn merge -r 12208:12294 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / blender / blenkernel / intern / depsgraph.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
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
12  * about this.
13  *
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.
18  *
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.
22  *
23  * The Original Code is Copyright (C) 2004 Blender Foundation.
24  * All rights reserved.
25  *
26  * Contributor(s): none yet.
27  *
28  * ***** END GPL/BL DUAL LICENSE BLOCK *****
29  */
30  
31 #include <stdio.h>
32 #include <string.h>
33 #include <math.h>
34
35 #ifdef _WIN32
36 #include "BLI_winstuff.h"
37 #endif
38
39 //#include "BMF_Api.h"
40
41 #include "BLI_blenlib.h"
42 #include "BLI_arithb.h"
43
44 #include "DNA_action_types.h"
45 #include "DNA_armature_types.h"
46 #include "DNA_curve_types.h"
47 #include "DNA_camera_types.h"
48 #include "DNA_ID.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_scene_types.h"
61 #include "DNA_screen_types.h"
62 #include "DNA_space_types.h"
63 #include "DNA_view2d_types.h"
64 #include "DNA_view3d_types.h"
65
66 #include "BKE_action.h"
67 #include "BKE_effect.h"
68 #include "BKE_global.h"
69 #include "BKE_group.h"
70 #include "BKE_key.h"
71 #include "BKE_main.h"
72 #include "BKE_mball.h"
73 #include "BKE_modifier.h"
74 #include "BKE_object.h"
75 #include "BKE_scene.h"
76 #include "BKE_utildefines.h"
77
78 #include "MEM_guardedalloc.h"
79 #include "blendef.h"
80
81 #include "BPY_extern.h"
82
83  #include "depsgraph_private.h"
84  
85 /* Queue and stack operations for dag traversal 
86  *
87  * the queue store a list of freenodes to avoid successives alloc/dealloc
88  */
89
90 DagNodeQueue * queue_create (int slots) 
91 {
92         DagNodeQueue * queue;
93         DagNodeQueueElem * elem;
94         int i;
95         
96         queue = MEM_mallocN(sizeof(DagNodeQueue),"DAG queue");
97         queue->freenodes = MEM_mallocN(sizeof(DagNodeQueue),"DAG queue");
98         queue->count = 0;
99         queue->maxlevel = 0;
100         queue->first = queue->last = NULL;
101         elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem3");
102         elem->node = NULL;
103         elem->next = NULL;
104         queue->freenodes->first = queue->freenodes->last = elem;
105         
106         for (i = 1; i <slots;i++) {
107                 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem4");
108                 elem->node = NULL;
109                 elem->next = NULL;
110                 queue->freenodes->last->next = elem;
111                 queue->freenodes->last = elem;
112         }
113         queue->freenodes->count = slots;
114         return queue;
115 }
116
117 void queue_raz(DagNodeQueue *queue)
118 {
119         DagNodeQueueElem * elem;
120         
121         elem = queue->first;
122         if (queue->freenodes->last)
123                 queue->freenodes->last->next = elem;
124         else
125                 queue->freenodes->first = queue->freenodes->last = elem;
126         
127         elem->node = NULL;
128         queue->freenodes->count++;
129         while (elem->next) {
130                 elem = elem->next;
131                 elem->node = NULL;
132                 queue->freenodes->count++;
133         }
134         queue->freenodes->last = elem;
135         queue->count = 0;
136 }
137
138 void queue_delete(DagNodeQueue *queue)
139 {
140         DagNodeQueueElem * elem;
141         DagNodeQueueElem * temp;
142         
143         elem = queue->first;
144         while (elem) {
145                 temp = elem;
146                 elem = elem->next;
147                 MEM_freeN(temp);
148         }
149         
150         elem = queue->freenodes->first;
151         while (elem) {
152                 temp = elem;
153                 elem = elem->next;
154                 MEM_freeN(temp);
155         }
156         
157         MEM_freeN(queue->freenodes);                    
158         MEM_freeN(queue);                       
159 }
160
161 /* insert in queue, remove in front */
162 void push_queue(DagNodeQueue *queue, DagNode *node)
163 {
164         DagNodeQueueElem * elem;
165         int i;
166
167         if (node == NULL) {
168                 fprintf(stderr,"pushing null node \n");
169                 return;
170         }
171         /*fprintf(stderr,"BFS push : %s %d\n",((ID *) node->ob)->name, queue->count);*/
172
173         elem = queue->freenodes->first;
174         if (elem != NULL) {
175                 queue->freenodes->first = elem->next;
176                 if ( queue->freenodes->last == elem) {
177                         queue->freenodes->last = NULL;
178                         queue->freenodes->first = NULL;
179                 }
180                 queue->freenodes->count--;
181         } else { /* alllocating more */         
182                 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem1");
183                 elem->node = NULL;
184                 elem->next = NULL;
185                 queue->freenodes->first = queue->freenodes->last = elem;
186
187                 for (i = 1; i <DAGQUEUEALLOC;i++) {
188                         elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem2");
189                         elem->node = NULL;
190                         elem->next = NULL;
191                         queue->freenodes->last->next = elem;
192                         queue->freenodes->last = elem;
193                 }
194                 queue->freenodes->count = DAGQUEUEALLOC;
195                         
196                 elem = queue->freenodes->first; 
197                 queue->freenodes->first = elem->next;   
198         }
199         elem->next = NULL;
200         elem->node = node;
201         if (queue->last != NULL)
202                 queue->last->next = elem;
203         queue->last = elem;
204         if (queue->first == NULL) {
205                 queue->first = elem;
206         }
207         queue->count++;
208 }
209
210
211 /* insert in front, remove in front */
212 void push_stack(DagNodeQueue *queue, DagNode *node)
213 {
214         DagNodeQueueElem * elem;
215         int i;
216
217         elem = queue->freenodes->first; 
218         if (elem != NULL) {
219                 queue->freenodes->first = elem->next;
220                 if ( queue->freenodes->last == elem) {
221                         queue->freenodes->last = NULL;
222                         queue->freenodes->first = NULL;
223                 }
224                 queue->freenodes->count--;
225         } else { /* alllocating more */
226                 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem1");
227                 elem->node = NULL;
228                 elem->next = NULL;
229                 queue->freenodes->first = queue->freenodes->last = elem;
230
231                 for (i = 1; i <DAGQUEUEALLOC;i++) {
232                         elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem2");
233                         elem->node = NULL;
234                         elem->next = NULL;
235                         queue->freenodes->last->next = elem;
236                         queue->freenodes->last = elem;
237                 }
238                 queue->freenodes->count = DAGQUEUEALLOC;
239                         
240                 elem = queue->freenodes->first; 
241                 queue->freenodes->first = elem->next;   
242         }
243         elem->next = queue->first;
244         elem->node = node;
245         queue->first = elem;
246         if (queue->last == NULL)
247                 queue->last = elem;
248         queue->count++;
249 }
250
251
252 DagNode * pop_queue(DagNodeQueue *queue)
253 {
254         DagNodeQueueElem * elem;
255         DagNode *node;
256
257         elem = queue->first;
258         if (elem) {
259                 queue->first = elem->next;
260                 if (queue->last == elem) {
261                         queue->last=NULL;
262                         queue->first=NULL;
263                 }
264                 queue->count--;
265                 if (queue->freenodes->last)
266                         queue->freenodes->last->next=elem;
267                 queue->freenodes->last=elem;
268                 if (queue->freenodes->first == NULL)
269                         queue->freenodes->first=elem;
270                 node = elem->node;
271                 elem->node = NULL;
272                 elem->next = NULL;
273                 queue->freenodes->count++;
274                 return node;
275         } else {
276                 fprintf(stderr,"return null \n");
277                 return NULL;
278         }
279 }
280
281 void    *pop_ob_queue(struct DagNodeQueue *queue) {
282         return(pop_queue(queue)->ob);
283 }
284
285 DagNode * get_top_node_queue(DagNodeQueue *queue) 
286 {
287         return queue->first->node;
288 }
289
290 int             queue_count(struct DagNodeQueue *queue){
291         return queue->count;
292 }
293
294
295 DagForest * dag_init()
296 {
297         DagForest *forest;
298         /* use callocN to init all zero */
299         forest = MEM_callocN(sizeof(DagForest),"DAG root");
300         return forest;
301 }
302
303 static void dag_add_driver_relation(Ipo *ipo, DagForest *dag, DagNode *node, int isdata)
304 {
305         IpoCurve *icu;
306         DagNode *node1;
307         
308         for(icu= ipo->curve.first; icu; icu= icu->next) {
309                 if(icu->driver) {
310
311                         if (icu->driver->type == IPO_DRIVER_TYPE_PYTHON) {
312
313                                 if ((icu->driver->flag & IPO_DRIVER_FLAG_INVALID) || (icu->driver->name[0] == '\0'))
314                                         continue; /* empty or invalid expression */
315                                 else {
316                                         /* now we need refs to all objects mentioned in this
317                                          * pydriver expression, to call 'dag_add_relation'
318                                          * for each of them */
319                                         Object **obarray = BPY_pydriver_get_objects(icu->driver);
320                                         if (obarray) {
321                                                 Object *ob, **oba = obarray;
322
323                                                 while (*oba) {
324                                                         ob = *oba;
325                                                         node1 = dag_get_node(dag, ob);
326                                                         if (ob->type == OB_ARMATURE)
327                                                                 dag_add_relation(dag, node1, node, isdata?DAG_RL_DATA_DATA:DAG_RL_DATA_OB);
328                                                         else
329                                                                 dag_add_relation(dag, node1, node, isdata?DAG_RL_OB_DATA:DAG_RL_OB_OB);
330                                                         oba++;
331                                                 }
332
333                                                 MEM_freeN(obarray);
334                                         }
335                                 }
336                         }
337                         else if (icu->driver->ob) {
338                                 node1 = dag_get_node(dag, icu->driver->ob);
339                                 if(icu->driver->blocktype==ID_AR)
340                                         dag_add_relation(dag, node1, node, isdata?DAG_RL_DATA_DATA:DAG_RL_DATA_OB);
341                                 else
342                                         dag_add_relation(dag, node1, node, isdata?DAG_RL_OB_DATA:DAG_RL_OB_OB);
343                         }
344                 }
345         }
346 }
347
348 static void build_dag_object(DagForest *dag, DagNode *scenenode, Object *ob, int mask)
349 {
350         bConstraint *con;
351         bConstraintChannel *conchan;
352         DagNode * node;
353         DagNode * node2;
354         DagNode * node3;
355         Key *key;
356         int addtoroot= 1;
357         
358         node = dag_get_node(dag, ob);
359         
360         if ((ob->data) && (mask&DAG_RL_DATA)) {
361                 node2 = dag_get_node(dag,ob->data);
362                 dag_add_relation(dag,node,node2,DAG_RL_DATA);
363                 node2->first_ancestor = ob;
364                 node2->ancestor_count += 1;
365         }
366         
367         if (ob->type == OB_ARMATURE) {
368                 if (ob->pose){
369                         bPoseChannel *pchan;
370                         bConstraint *con;
371                         Object * target;
372                         char *subtarget;
373                         
374                         for (pchan = ob->pose->chanbase.first; pchan; pchan=pchan->next){
375                                 for (con = pchan->constraints.first; con; con=con->next){
376                                         if (constraint_has_target(con)) {
377                                                 
378                                                 target = get_constraint_target(con, &subtarget);
379                                                 if (target!=ob) {
380                                                         // fprintf(stderr,"armature %s target :%s \n", ob->id.name, target->id.name);
381                                                         node3 = dag_get_node(dag, target);
382                                                         
383                                                         if(subtarget && subtarget[0])
384                                                                 dag_add_relation(dag,node3,node, DAG_RL_OB_DATA|DAG_RL_DATA_DATA);
385                                                         else if(ELEM(con->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO))   
386                                                                 dag_add_relation(dag,node3,node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA);
387                                                         else
388                                                                 dag_add_relation(dag,node3,node, DAG_RL_OB_DATA);
389                                                         
390                                                 }
391                                         }
392                                 }
393                         }
394                 }
395         }
396         
397         /* driver dependencies, nla modifiers */
398         if(ob->ipo) 
399                 dag_add_driver_relation(ob->ipo, dag, node, 0);
400         
401         key= ob_get_key(ob);
402         if(key && key->ipo)
403                 dag_add_driver_relation(key->ipo, dag, node, 1);
404         
405         for (conchan=ob->constraintChannels.first; conchan; conchan=conchan->next)
406                 if(conchan->ipo)
407                         dag_add_driver_relation(conchan->ipo, dag, node, 0);
408
409         if(ob->action) {
410                 bActionChannel *chan;
411                 for (chan = ob->action->chanbase.first; chan; chan=chan->next){
412                         if(chan->ipo)
413                                 dag_add_driver_relation(chan->ipo, dag, node, 1);
414                         for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next)
415                                 if(conchan->ipo)
416                                         dag_add_driver_relation(conchan->ipo, dag, node, 1);
417                 }
418         }
419         if(ob->nlastrips.first) {
420                 bActionStrip *strip;
421                 bActionChannel *chan;
422                 for(strip= ob->nlastrips.first; strip; strip= strip->next) {
423                         if(strip->act && strip->act!=ob->action)
424                                 for (chan = strip->act->chanbase.first; chan; chan=chan->next)
425                                         if(chan->ipo)
426                                                 dag_add_driver_relation(chan->ipo, dag, node, 1);
427                         if(strip->modifiers.first) {
428                                 bActionModifier *amod;
429                                 for(amod= strip->modifiers.first; amod; amod= amod->next) {
430                                         if(amod->ob) {
431                                                 node2 = dag_get_node(dag, amod->ob);
432                                                 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA);
433                                         }
434                                 }
435                         }
436                 }
437         }
438         if (ob->modifiers.first) {
439                 ModifierData *md;
440                 
441                 for(md=ob->modifiers.first; md; md=md->next) {
442                         ModifierTypeInfo *mti = modifierType_getInfo(md->type);
443                         
444                         if (mti->updateDepgraph) mti->updateDepgraph(md, dag, ob, node);
445                 }
446         }
447         if (ob->parent) {
448                 node2 = dag_get_node(dag,ob->parent);
449                 
450                 switch(ob->partype) {
451                         case PARSKEL:
452                                 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_OB);
453                                 break;
454                         case PARVERT1: case PARVERT3: case PARBONE:
455                                 dag_add_relation(dag,node2,node,DAG_RL_DATA_OB|DAG_RL_OB_OB);
456                                 break;
457                         default:
458                                 if(ob->parent->type==OB_LATTICE) 
459                                         dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_OB);
460                                 else if(ob->parent->type==OB_CURVE) {
461                                         Curve *cu= ob->parent->data;
462                                         if(cu->flag & CU_PATH) 
463                                                 dag_add_relation(dag,node2,node,DAG_RL_DATA_OB|DAG_RL_OB_OB);
464                                         else
465                                                 dag_add_relation(dag,node2,node,DAG_RL_OB_OB);
466                                 }
467                                         else
468                                                 dag_add_relation(dag,node2,node,DAG_RL_OB_OB);
469                 }
470                 /* exception case: parent is duplivert */
471                 if(ob->type==OB_MBALL && (ob->parent->transflag & OB_DUPLIVERTS)) {
472                         dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_OB);
473                 }
474                 
475                 addtoroot = 0;
476         }
477         if (ob->track) {
478                 node2 = dag_get_node(dag,ob->track);
479                 dag_add_relation(dag,node2,node,DAG_RL_OB_OB);
480                 addtoroot = 0;
481         }
482         if (ob->proxy) {
483                 node2 = dag_get_node(dag, ob->proxy);
484                 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA|DAG_RL_OB_OB);
485                 /* inverted relation, so addtoroot shouldn't be set to zero */
486         }
487         if (ob->type==OB_CAMERA) {
488                 Camera *cam = (Camera *)ob->data;
489                 if (cam->dof_ob) {
490                         node2 = dag_get_node(dag, cam->dof_ob);
491                         dag_add_relation(dag,node2,node,DAG_RL_OB_OB);
492                 }
493         }
494         if (ob->transflag & OB_DUPLI) {
495                 if((ob->transflag & OB_DUPLIGROUP) && ob->dup_group) {
496                         GroupObject *go;
497                         for(go= ob->dup_group->gobject.first; go; go= go->next) {
498                                 if(go->ob) {
499                                         node2 = dag_get_node(dag, go->ob);
500                                         /* node2 changes node1, this keeps animations updated in groups?? not logical? */
501                                         dag_add_relation(dag, node2, node, DAG_RL_OB_OB);
502                                 }
503                         }
504                 }
505         }
506     
507         /* softbody collision  */
508         if((ob->type==OB_MESH) || (ob->type==OB_CURVE) || (ob->type==OB_LATTICE)) {
509                 Base *base;
510                 if(modifiers_isSoftbodyEnabled(ob)){
511                         // would be nice to have a list of colliders here
512                         // so for now walk all objects in scene check 'same layer rule'
513                         for(base = G.scene->base.first; base; base= base->next) {
514                                 if( (base->lay & ob->lay) && base->object->pd) {
515                                         Object *ob1= base->object;
516                                         if((ob1->pd->deflect) && (ob1 != ob))  {
517                                                 node2 = dag_get_node(dag, ob1);                                 
518                                                 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA);                                            
519                                         }
520                                 }
521                         }
522                 }
523         }
524                 
525         if (ob->type==OB_MBALL) {
526                 Object *mom= find_basis_mball(ob);
527                 if(mom!=ob) {
528                         node2 = dag_get_node(dag, mom);
529                         dag_add_relation(dag,node,node2,DAG_RL_DATA_DATA|DAG_RL_OB_DATA);  // mom depends on children!
530                 }
531         }
532         else if (ob->type==OB_CURVE) {
533                 Curve *cu= ob->data;
534                 if(cu->bevobj) {
535                         node2 = dag_get_node(dag, cu->bevobj);
536                         dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_DATA);
537                 }
538                 if(cu->taperobj) {
539                         node2 = dag_get_node(dag, cu->taperobj);
540                         dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_DATA);
541                 }
542                 if(cu->ipo)
543                         dag_add_driver_relation(cu->ipo, dag, node, 1);
544
545         }
546         else if(ob->type==OB_FONT) {
547                 Curve *cu= ob->data;
548                 if(cu->textoncurve) {
549                         node2 = dag_get_node(dag, cu->textoncurve);
550                         dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_DATA);
551                 }
552         }
553         else if(ob->type==OB_MESH) {
554                 PartEff *paf= give_parteff(ob);
555                 if(paf) {
556                         ListBase *listb;
557                         pEffectorCache *ec;
558                         
559                         /* ob location depends on itself */
560                         if((paf->flag & PAF_STATIC)==0)
561                                 dag_add_relation(dag, node, node, DAG_RL_OB_DATA);
562                         
563                         listb= pdInitEffectors(ob, paf->group);         /* note, makes copy... */
564                         if(listb) {
565                                 for(ec= listb->first; ec; ec= ec->next) {
566                                         Object *ob1= ec->ob;
567                                         PartDeflect *pd= ob1->pd;
568                                                 
569                                         if(pd->forcefield) {
570                                                 node2 = dag_get_node(dag, ob1);
571                                                 if(pd->forcefield==PFIELD_GUIDE)
572                                                         dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA);
573                                                 else
574                                                         dag_add_relation(dag, node2, node, DAG_RL_OB_DATA);
575                                         }
576                                 }
577                                 
578                                 pdEndEffectors(listb);  /* restores copy... */
579                         }
580                 }
581         }
582         
583         for (con = ob->constraints.first; con; con=con->next) {
584                 if (constraint_has_target(con)) {
585                         char *str;
586                         Object *obt= get_constraint_target(con, &str);
587                         
588                         node2 = dag_get_node(dag, obt);
589                         if(ELEM(con->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO))
590                                 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB|DAG_RL_OB_OB);
591                         else {
592                                 if(ELEM3(obt->type, OB_ARMATURE, OB_MESH, OB_LATTICE) && str && str[0])
593                                         dag_add_relation(dag, node2, node, DAG_RL_DATA_OB|DAG_RL_OB_OB);
594                                 else
595                                         dag_add_relation(dag, node2, node, DAG_RL_OB_OB);
596                         }
597                         addtoroot = 0;
598                 }
599         }
600
601         if (addtoroot == 1 )
602                 dag_add_relation(dag,scenenode,node,DAG_RL_SCENE);
603 }
604
605 struct DagForest *build_dag(struct Scene *sce, short mask) 
606 {
607         Base *base;
608         Object *ob;
609         Group *group;
610         GroupObject *go;
611         DagNode *node;
612         DagNode *scenenode;
613         DagForest *dag;
614         DagAdjList *itA;
615
616         dag = sce->theDag;
617         sce->dagisvalid=1;
618         if ( dag)
619                 free_forest( dag ); 
620         else {
621                 dag = dag_init();
622                 sce->theDag = dag;
623         }
624         
625         /* add base node for scene. scene is always the first node in DAG */
626         scenenode = dag_add_node(dag, sce);     
627         
628         /* add current scene objects */
629         for(base = sce->base.first; base; base= base->next) {
630                 ob= base->object;
631                 
632                 build_dag_object(dag, scenenode, ob, mask);
633                 if(ob->proxy)
634                         build_dag_object(dag, scenenode, ob->proxy, mask);
635                 
636                 /* handled in next loop */
637                 if(ob->dup_group) 
638                         ob->dup_group->id.flag |= LIB_DOIT;
639         }
640         
641         /* add groups used in current scene objects */
642         for(group= G.main->group.first; group; group= group->id.next) {
643                 if(group->id.flag & LIB_DOIT) {
644                         for(go= group->gobject.first; go; go= go->next) {
645                                 build_dag_object(dag, scenenode, go->ob, mask);
646                         }
647                         group->id.flag &= ~LIB_DOIT;
648                 }
649         }
650         
651         /* Now all relations were built, but we need to solve 1 exceptional case;
652            When objects have multiple "parents" (for example parent + constraint working on same object)
653            the relation type has to be synced. One of the parents can change, and should give same event to child */
654         
655         /* nodes were callocced, so we can use node->color for temporal storage */
656         for(node = sce->theDag->DagNode.first; node; node= node->next) {
657                 if(node->type==ID_OB) {
658                         for(itA = node->child; itA; itA= itA->next) {
659                                 if(itA->node->type==ID_OB) {
660                                         itA->node->color |= itA->type;
661                                 }
662                         }
663                 }
664         }
665         /* now set relations equal, so that when only one parent changes, the correct recalcs are found */
666         for(node = sce->theDag->DagNode.first; node; node= node->next) {
667                 if(node->type==ID_OB) {
668                         for(itA = node->child; itA; itA= itA->next) {
669                                 if(itA->node->type==ID_OB) {
670                                         itA->type |= itA->node->color;
671                                 }
672                         }
673                 }
674         }
675         
676         // cycle detection and solving
677         // solve_cycles(dag);   
678         
679         return dag;
680 }
681
682
683 void free_forest(DagForest *Dag) 
684 {  /* remove all nodes and deps */
685         DagNode *tempN;
686         DagAdjList *tempA;      
687         DagAdjList *itA;
688         DagNode *itN = Dag->DagNode.first;
689         
690         while (itN) {
691                 itA = itN->child;       
692                 while (itA) {
693                         tempA = itA;
694                         itA = itA->next;
695                         MEM_freeN(tempA);                       
696                 }
697                 
698                 itA = itN->parent;      
699                 while (itA) {
700                         tempA = itA;
701                         itA = itA->next;
702                         MEM_freeN(tempA);                       
703                 }
704                 
705                 tempN = itN;
706                 itN = itN->next;
707                 MEM_freeN(tempN);
708         }
709         Dag->DagNode.first = NULL;
710         Dag->DagNode.last = NULL;
711         Dag->numNodes = 0;
712
713 }
714
715 DagNode * dag_find_node (DagForest *forest,void * fob)
716 {
717         DagNode *node = forest->DagNode.first;
718         
719         while (node) {
720                 if (node->ob == fob)
721                         return node;
722                 node = node->next;
723         }
724         return NULL;
725 }
726
727 static int ugly_hack_sorry= 1;  // prevent type check
728
729 /* no checking of existance, use dag_find_node first or dag_get_node */
730 DagNode * dag_add_node (DagForest *forest, void * fob)
731 {
732         DagNode *node;
733                 
734         node = MEM_callocN(sizeof(DagNode),"DAG node");
735         if (node) {
736                 node->ob = fob;
737                 node->color = DAG_WHITE;
738
739                 if(ugly_hack_sorry) node->type = GS(((ID *) fob)->name);        // sorry, done for pose sorting
740                 if (forest->numNodes) {
741                         ((DagNode *) forest->DagNode.last)->next = node;
742                         forest->DagNode.last = node;
743                         forest->numNodes++;
744                 } else {
745                         forest->DagNode.last = node;
746                         forest->DagNode.first = node;
747                         forest->numNodes = 1;
748                 }
749         }
750         return node;
751 }
752
753 DagNode * dag_get_node (DagForest *forest,void * fob)
754 {
755         DagNode *node;
756         
757         node = dag_find_node (forest, fob);     
758         if (!node) 
759                 node = dag_add_node(forest, fob);
760         return node;
761 }
762
763
764
765 DagNode * dag_get_sub_node (DagForest *forest,void * fob)
766 {
767         DagNode *node;
768         DagAdjList *mainchild, *prev=NULL;
769         
770         mainchild = ((DagNode *) forest->DagNode.first)->child;
771         /* remove from first node (scene) adj list if present */
772         while (mainchild) {
773                 if (mainchild->node == fob) {
774                         if (prev) {
775                                 prev->next = mainchild->next;
776                                 MEM_freeN(mainchild);
777                                 break;
778                         } else {
779                                 ((DagNode *) forest->DagNode.first)->child = mainchild->next;
780                                 MEM_freeN(mainchild);
781                                 break;
782                         }
783                 }
784                 prev = mainchild;
785                 mainchild = mainchild->next;
786         }
787         node = dag_find_node (forest, fob);     
788         if (!node) 
789                 node = dag_add_node(forest, fob);
790         return node;
791 }
792
793 void dag_add_relation(DagForest *forest, DagNode *fob1, DagNode *fob2, short rel) 
794 {
795         DagAdjList *itA = fob1->child;
796         
797         while (itA) { /* search if relation exist already */
798                 if (itA->node == fob2) {
799                         itA->type |= rel;
800                         itA->count += 1;
801                         return;
802                 }
803                 itA = itA->next;
804         }
805         /* create new relation and insert at head. MALLOC alert! */
806         itA = MEM_mallocN(sizeof(DagAdjList),"DAG adj list");
807         itA->node = fob2;
808         itA->type = rel;
809         itA->count = 1;
810         itA->next = fob1->child;
811         fob1->child = itA;
812 }
813
814 static void dag_add_parent_relation(DagForest *forest, DagNode *fob1, DagNode *fob2, short rel) 
815 {
816         DagAdjList *itA = fob2->parent;
817         
818         while (itA) { /* search if relation exist already */
819                 if (itA->node == fob1) {
820                         itA->type |= rel;
821                         itA->count += 1;
822                         return;
823                 }
824                 itA = itA->next;
825         }
826         /* create new relation and insert at head. MALLOC alert! */
827         itA = MEM_mallocN(sizeof(DagAdjList),"DAG adj list");
828         itA->node = fob1;
829         itA->type = rel;
830         itA->count = 1;
831         itA->next = fob2->parent;
832         fob2->parent = itA;
833 }
834
835
836 /*
837  * MainDAG is the DAG of all objects in current scene
838  * used only for drawing there is one also in each scene
839  */
840 static DagForest * MainDag = NULL;
841
842 DagForest *getMainDag(void)
843 {
844         return MainDag;
845 }
846
847
848 void setMainDag(DagForest *dag)
849 {
850         MainDag = dag;
851 }
852
853
854 /*
855  * note for BFS/DFS
856  * in theory we should sweep the whole array
857  * but in our case the first node is the scene
858  * and is linked to every other object
859  *
860  * for general case we will need to add outer loop
861  */
862
863 /*
864  * ToDo : change pos kludge
865  */
866
867 /* adjust levels for drawing in oops space */
868 void graph_bfs(void)
869 {
870         DagNode *node;
871         DagNodeQueue *nqueue;
872         int pos[50];
873         int i;
874         DagAdjList *itA;
875         int minheight;
876         
877         /* fprintf(stderr,"starting BFS \n ------------\n"); */ 
878         nqueue = queue_create(DAGQUEUEALLOC);
879         for ( i=0; i<50; i++)
880                 pos[i] = 0;
881         
882         /* Init
883          * dagnode.first is alway the root (scene) 
884          */
885         node = MainDag->DagNode.first;
886         while(node) {
887                 node->color = DAG_WHITE;
888                 node->BFS_dist = 9999;
889                 node->k = 0;
890                 node = node->next;
891         }
892         
893         node = MainDag->DagNode.first;
894         if (node->color == DAG_WHITE) {
895                 node->color = DAG_GRAY;
896                 node->BFS_dist = 1;
897                 push_queue(nqueue,node);  
898                 while(nqueue->count) {
899                         node = pop_queue(nqueue);
900                         
901                         minheight = pos[node->BFS_dist];
902                         itA = node->child;
903                         while(itA != NULL) {
904                                 if((itA->node->color == DAG_WHITE) ) {
905                                         itA->node->color = DAG_GRAY;
906                                         itA->node->BFS_dist = node->BFS_dist + 1;
907                                         itA->node->k = (float) minheight;
908                                         push_queue(nqueue,itA->node);
909                                 }
910                                 
911                                  else {
912                                         fprintf(stderr,"bfs not dag tree edge color :%i \n",itA->node->color);
913                                 }
914                                  
915                                 
916                                 itA = itA->next;
917                         }
918                         if (pos[node->BFS_dist] > node->k ) {
919                                 pos[node->BFS_dist] += 1;                               
920                                 node->k = (float) pos[node->BFS_dist];
921                         } else {
922                                 pos[node->BFS_dist] = (int) node->k +1;
923                         }
924                         set_node_xy(node, node->BFS_dist*DEPSX*2, pos[node->BFS_dist]*DEPSY*2);
925                         node->color = DAG_BLACK;
926                         /*
927                          fprintf(stderr,"BFS node : %20s %i %5.0f %5.0f\n",((ID *) node->ob)->name,node->BFS_dist, node->x, node->y);   
928                          */
929                 }
930         }
931         queue_delete(nqueue);
932 }
933
934 int pre_and_post_BFS(DagForest *dag, short mask, graph_action_func pre_func, graph_action_func post_func, void **data) {
935         DagNode *node;
936         
937         node = dag->DagNode.first;
938         return pre_and_post_source_BFS(dag, mask,  node,  pre_func,  post_func, data);
939 }
940
941
942 int pre_and_post_source_BFS(DagForest *dag, short mask, DagNode *source, graph_action_func pre_func, graph_action_func post_func, void **data)
943 {
944         DagNode *node;
945         DagNodeQueue *nqueue;
946         DagAdjList *itA;
947         int     retval = 0;
948         /* fprintf(stderr,"starting BFS \n ------------\n"); */ 
949         
950         /* Init
951                 * dagnode.first is alway the root (scene) 
952                 */
953         node = dag->DagNode.first;
954         nqueue = queue_create(DAGQUEUEALLOC);
955         while(node) {
956                 node->color = DAG_WHITE;
957                 node->BFS_dist = 9999;
958                 node = node->next;
959         }
960         
961         node = source;
962         if (node->color == DAG_WHITE) {
963                 node->color = DAG_GRAY;
964                 node->BFS_dist = 1;
965                 pre_func(node->ob,data);
966                 
967                 while(nqueue->count) {
968                         node = pop_queue(nqueue);
969                         
970                         itA = node->child;
971                         while(itA != NULL) {
972                                 if((itA->node->color == DAG_WHITE) && (itA->type & mask)) {
973                                         itA->node->color = DAG_GRAY;
974                                         itA->node->BFS_dist = node->BFS_dist + 1;
975                                         push_queue(nqueue,itA->node);
976                                         pre_func(node->ob,data);
977                                 }
978                                 
979                                 else { // back or cross edge
980                                         retval = 1;
981                                 }
982                                 itA = itA->next;
983                         }
984                         post_func(node->ob,data);
985                         node->color = DAG_BLACK;
986                         /*
987                          fprintf(stderr,"BFS node : %20s %i %5.0f %5.0f\n",((ID *) node->ob)->name,node->BFS_dist, node->x, node->y);   
988                          */
989                 }
990         }
991         queue_delete(nqueue);
992         return retval;
993 }
994
995 /* non recursive version of DFS, return queue -- outer loop present to catch odd cases (first level cycles)*/
996 DagNodeQueue * graph_dfs(void)
997 {
998         DagNode *node;
999         DagNodeQueue *nqueue;
1000         DagNodeQueue *retqueue;
1001         int pos[50];
1002         int i;
1003         DagAdjList *itA;
1004         int time;
1005         int skip = 0;
1006         int minheight;
1007         int maxpos=0;
1008         int     is_cycle = 0;
1009         /*
1010          *fprintf(stderr,"starting DFS \n ------------\n");
1011          */     
1012         nqueue = queue_create(DAGQUEUEALLOC);
1013         retqueue = queue_create(MainDag->numNodes);
1014         for ( i=0; i<50; i++)
1015                 pos[i] = 0;
1016         
1017         /* Init
1018          * dagnode.first is alway the root (scene) 
1019          */
1020         node = MainDag->DagNode.first;
1021         while(node) {
1022                 node->color = DAG_WHITE;
1023                 node->DFS_dist = 9999;
1024                 node->DFS_dvtm = node->DFS_fntm = 9999;
1025                 node->k = 0;
1026                 node =  node->next;
1027         }
1028         
1029         time = 1;
1030         
1031         node = MainDag->DagNode.first;
1032
1033         do {
1034         if (node->color == DAG_WHITE) {
1035                 node->color = DAG_GRAY;
1036                 node->DFS_dist = 1;
1037                 node->DFS_dvtm = time;
1038                 time++;
1039                 push_stack(nqueue,node);  
1040                         
1041                 while(nqueue->count) {
1042                         //graph_print_queue(nqueue);
1043
1044                          skip = 0;
1045                         node = get_top_node_queue(nqueue);
1046                         
1047                         minheight = pos[node->DFS_dist];
1048
1049                         itA = node->child;
1050                         while(itA != NULL) {
1051                                 if((itA->node->color == DAG_WHITE) ) {
1052                                         itA->node->DFS_dvtm = time;
1053                                         itA->node->color = DAG_GRAY;
1054
1055                                         time++;
1056                                         itA->node->DFS_dist = node->DFS_dist + 1;
1057                                         itA->node->k = (float) minheight;
1058                                         push_stack(nqueue,itA->node);
1059                                         skip = 1;
1060                                         break;
1061                                 } else { 
1062                                         if (itA->node->color == DAG_GRAY) { // back edge
1063                                                 fprintf(stderr,"dfs back edge :%15s %15s \n",((ID *) node->ob)->name, ((ID *) itA->node->ob)->name);
1064                                                 is_cycle = 1;
1065                                         } else if (itA->node->color == DAG_BLACK) {
1066                                                 ;
1067                                                 /* already processed node but we may want later to change distance either to shorter to longer.
1068                                                  * DFS_dist is the first encounter  
1069                                                 */
1070                                                 /*if (node->DFS_dist >= itA->node->DFS_dist)
1071                                                         itA->node->DFS_dist = node->DFS_dist + 1;
1072                                                         
1073                                                         fprintf(stderr,"dfs forward or cross edge :%15s %i-%i %15s %i-%i \n",
1074                                                                 ((ID *) node->ob)->name,
1075                                                                 node->DFS_dvtm, 
1076                                                                 node->DFS_fntm, 
1077                                                                 ((ID *) itA->node->ob)->name, 
1078                                                                 itA->node->DFS_dvtm,
1079                                                                 itA->node->DFS_fntm);
1080                                         */
1081                                         } else 
1082                                                 fprintf(stderr,"dfs unknown edge \n");
1083                                 }
1084                                 itA = itA->next;
1085                         }                       
1086
1087                         if (!skip) {
1088                                 node = pop_queue(nqueue);
1089                                 node->color = DAG_BLACK;
1090
1091                                 node->DFS_fntm = time;
1092                                 time++;
1093                                 if (node->DFS_dist > maxpos)
1094                                         maxpos = node->DFS_dist;
1095                                 if (pos[node->DFS_dist] > node->k ) {
1096                                         pos[node->DFS_dist] += 1;                               
1097                                         node->k = (float) pos[node->DFS_dist];
1098                                 } else {
1099                                         pos[node->DFS_dist] = (int) node->k +1;
1100                                 }
1101                                 set_node_xy(node, node->DFS_dist*DEPSX*2, pos[node->DFS_dist]*DEPSY*2);
1102                                 
1103                                 /*
1104                                  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 );       
1105                                 */
1106                                  push_stack(retqueue,node);
1107                                 
1108                         }
1109                 }
1110         }
1111                 node = node->next;
1112         } while (node);
1113 //        fprintf(stderr,"i size : %i \n", maxpos);
1114           
1115         queue_delete(nqueue);
1116           return(retqueue);
1117 }
1118
1119 /* unused */
1120 int pre_and_post_DFS(DagForest *dag, short mask, graph_action_func pre_func, graph_action_func post_func, void **data) {
1121         DagNode *node;
1122
1123         node = dag->DagNode.first;
1124         return pre_and_post_source_DFS(dag, mask,  node,  pre_func,  post_func, data);
1125 }
1126
1127 int pre_and_post_source_DFS(DagForest *dag, short mask, DagNode *source, graph_action_func pre_func, graph_action_func post_func, void **data)
1128 {
1129         DagNode *node;
1130         DagNodeQueue *nqueue;
1131         DagAdjList *itA;
1132         int time;
1133         int skip = 0;
1134         int retval = 0;
1135         /*
1136          *fprintf(stderr,"starting DFS \n ------------\n");
1137          */     
1138         nqueue = queue_create(DAGQUEUEALLOC);
1139         
1140         /* Init
1141                 * dagnode.first is alway the root (scene) 
1142                 */
1143         node = dag->DagNode.first;
1144         while(node) {
1145                 node->color = DAG_WHITE;
1146                 node->DFS_dist = 9999;
1147                 node->DFS_dvtm = node->DFS_fntm = 9999;
1148                 node->k = 0;
1149                 node =  node->next;
1150         }
1151         
1152         time = 1;
1153         
1154         node = source;
1155         do {
1156                 if (node->color == DAG_WHITE) {
1157                         node->color = DAG_GRAY;
1158                         node->DFS_dist = 1;
1159                         node->DFS_dvtm = time;
1160                         time++;
1161                         push_stack(nqueue,node);  
1162                         pre_func(node->ob,data);
1163
1164                         while(nqueue->count) {
1165                                 skip = 0;
1166                                 node = get_top_node_queue(nqueue);
1167                                                                 
1168                                 itA = node->child;
1169                                 while(itA != NULL) {
1170                                         if((itA->node->color == DAG_WHITE) && (itA->type & mask) ) {
1171                                                 itA->node->DFS_dvtm = time;
1172                                                 itA->node->color = DAG_GRAY;
1173                                                 
1174                                                 time++;
1175                                                 itA->node->DFS_dist = node->DFS_dist + 1;
1176                                                 push_stack(nqueue,itA->node);
1177                                                 pre_func(node->ob,data);
1178
1179                                                 skip = 1;
1180                                                 break;
1181                                         } else {
1182                                                 if (itA->node->color == DAG_GRAY) {// back edge
1183                                                         retval = 1;
1184                                                 }
1185 //                                              else if (itA->node->color == DAG_BLACK) { // cross or forward
1186 //                                                      ;
1187                                         }
1188                                         itA = itA->next;
1189                                 }                       
1190                                 
1191                                 if (!skip) {
1192                                         node = pop_queue(nqueue);
1193                                         node->color = DAG_BLACK;
1194                                         
1195                                         node->DFS_fntm = time;
1196                                         time++;
1197                                         post_func(node->ob,data);
1198                                 }
1199                         }
1200                 }
1201                 node = node->next;
1202         } while (node);
1203         queue_delete(nqueue);
1204         return(retval);
1205 }
1206
1207
1208 // used to get the obs owning a datablock
1209 struct DagNodeQueue *get_obparents(struct DagForest     *dag, void *ob) 
1210 {
1211         DagNode * node, *node1;
1212         DagNodeQueue *nqueue;
1213         DagAdjList *itA;
1214
1215         node = dag_find_node(dag,ob);
1216         if(node==NULL) {
1217                 return NULL;
1218         }
1219         else if (node->ancestor_count == 1) { // simple case
1220                 nqueue = queue_create(1);
1221                 push_queue(nqueue,node);
1222         } else {        // need to go over the whole dag for adj list
1223                 nqueue = queue_create(node->ancestor_count);
1224                 
1225                 node1 = dag->DagNode.first;
1226                 do {
1227                         if (node1->DFS_fntm > node->DFS_fntm) { // a parent is finished after child. must check adj list
1228                                 itA = node->child;
1229                                 while(itA != NULL) {
1230                                         if ((itA->node == node) && (itA->type == DAG_RL_DATA)) {
1231                                                 push_queue(nqueue,node);
1232                                         }
1233                                         itA = itA->next;
1234                                 }
1235                         }
1236                         node1 = node1->next;
1237                 } while (node1);
1238         }
1239         return nqueue;
1240 }
1241
1242 struct DagNodeQueue *get_first_ancestors(struct DagForest       *dag, void *ob)
1243 {
1244         DagNode * node, *node1;
1245         DagNodeQueue *nqueue;
1246         DagAdjList *itA;
1247         
1248         node = dag_find_node(dag,ob);
1249         
1250         // need to go over the whole dag for adj list
1251         nqueue = queue_create(node->ancestor_count);
1252         
1253         node1 = dag->DagNode.first;
1254         do {
1255                 if (node1->DFS_fntm > node->DFS_fntm) { 
1256                         itA = node->child;
1257                         while(itA != NULL) {
1258                                 if (itA->node == node) {
1259                                         push_queue(nqueue,node);
1260                                 }
1261                                 itA = itA->next;
1262                         }
1263                 }
1264                 node1 = node1->next;
1265         } while (node1);
1266         
1267         return nqueue;  
1268 }
1269
1270 // standard DFS list
1271 struct DagNodeQueue *get_all_childs(struct DagForest    *dag, void *ob)
1272 {
1273         DagNode *node;
1274         DagNodeQueue *nqueue;
1275         DagNodeQueue *retqueue;
1276         DagAdjList *itA;
1277         int time;
1278         int skip = 0;
1279
1280         nqueue = queue_create(DAGQUEUEALLOC);
1281         retqueue = queue_create(dag->numNodes); // was MainDag... why? (ton)
1282         
1283         node = dag->DagNode.first;
1284         while(node) {
1285                 node->color = DAG_WHITE;
1286                 node =  node->next;
1287         }
1288         
1289         time = 1;
1290         
1291         node = dag_find_node(dag, ob);   // could be done in loop above (ton)
1292         if(node) { // can be null for newly added objects
1293                 
1294                 node->color = DAG_GRAY;
1295                 time++;
1296                 push_stack(nqueue,node);  
1297                 
1298                 while(nqueue->count) {
1299                         
1300                         skip = 0;
1301                         node = get_top_node_queue(nqueue);
1302                                         
1303                         itA = node->child;
1304                         while(itA != NULL) {
1305                                 if((itA->node->color == DAG_WHITE) ) {
1306                                         itA->node->DFS_dvtm = time;
1307                                         itA->node->color = DAG_GRAY;
1308                                         
1309                                         time++;
1310                                         push_stack(nqueue,itA->node);
1311                                         skip = 1;
1312                                         break;
1313                                 } 
1314                                 itA = itA->next;
1315                         }                       
1316                         
1317                         if (!skip) {
1318                                 node = pop_queue(nqueue);
1319                                 node->color = DAG_BLACK;
1320                                 
1321                                 time++;
1322                                 push_stack(retqueue,node);                      
1323                         }
1324                 }
1325         }
1326         queue_delete(nqueue);
1327         return(retqueue);
1328 }
1329
1330 /* unused */
1331 short   are_obs_related(struct DagForest        *dag, void *ob1, void *ob2) {
1332         DagNode * node;
1333         DagAdjList *itA;
1334         
1335         node = dag_find_node(dag, ob1);
1336         
1337         itA = node->child;
1338         while(itA != NULL) {
1339                 if((itA->node->ob == ob2) ) {
1340                         return itA->node->type;
1341                 } 
1342                 itA = itA->next;
1343         }
1344         return DAG_NO_RELATION;
1345 }
1346
1347 int     is_acyclic( DagForest   *dag) {
1348         return dag->is_acyclic;
1349 }
1350
1351 void set_node_xy(DagNode *node, float x, float y)
1352 {
1353         node->x = x;
1354         node->y = y;
1355 }
1356
1357
1358 /* debug test functions */
1359
1360 void graph_print_queue(DagNodeQueue *nqueue)
1361 {       
1362         DagNodeQueueElem *queueElem;
1363         
1364         queueElem = nqueue->first;
1365         while(queueElem) {
1366                 fprintf(stderr,"** %s %i %i-%i ",((ID *) queueElem->node->ob)->name,queueElem->node->color,queueElem->node->DFS_dvtm,queueElem->node->DFS_fntm);
1367                 queueElem = queueElem->next;            
1368         }
1369         fprintf(stderr,"\n");
1370 }
1371
1372 void graph_print_queue_dist(DagNodeQueue *nqueue)
1373 {       
1374         DagNodeQueueElem *queueElem;
1375         int max, count;
1376         
1377         queueElem = nqueue->first;
1378         max = queueElem->node->DFS_fntm;
1379         count = 0;
1380         while(queueElem) {
1381                 fprintf(stderr,"** %25s %2.2i-%2.2i ",((ID *) queueElem->node->ob)->name,queueElem->node->DFS_dvtm,queueElem->node->DFS_fntm);
1382                 while (count < queueElem->node->DFS_dvtm-1) { fputc(' ',stderr); count++;}
1383                 fputc('|',stderr); 
1384                 while (count < queueElem->node->DFS_fntm-2) { fputc('-',stderr); count++;}
1385                 fputc('|',stderr);
1386                 fputc('\n',stderr);
1387                 count = 0;
1388                 queueElem = queueElem->next;            
1389         }
1390         fprintf(stderr,"\n");
1391 }
1392
1393 void graph_print_adj_list(void)
1394 {
1395         DagNode *node;
1396         DagAdjList *itA;
1397         
1398         node = (getMainDag())->DagNode.first;
1399         while(node) {
1400                 fprintf(stderr,"node : %s col: %i",((ID *) node->ob)->name, node->color);               
1401                 itA = node->child;
1402                 while (itA) {
1403                         fprintf(stderr,"-- %s ",((ID *) itA->node->ob)->name);
1404                         
1405                         itA = itA->next;
1406                 }
1407                 fprintf(stderr,"\n");
1408                 node = node->next;
1409         }
1410 }
1411
1412 /* ************************ API *********************** */
1413
1414 /* groups with objects in this scene need to be put in the right order as well */
1415 static void scene_sort_groups(Scene *sce)
1416 {
1417         Base *base;
1418         Group *group;
1419         GroupObject *go;
1420         Object *ob;
1421         
1422         /* test; are group objects all in this scene? */
1423         for(ob= G.main->object.first; ob; ob= ob->id.next) {
1424                 ob->id.flag &= ~LIB_DOIT;
1425                 ob->id.newid= NULL;     /* newid abuse for GroupObject */
1426         }
1427         for(base = sce->base.first; base; base= base->next)
1428                 base->object->id.flag |= LIB_DOIT;
1429         
1430         for(group= G.main->group.first; group; group= group->id.next) {
1431                 for(go= group->gobject.first; go; go= go->next) {
1432                         if((go->ob->id.flag & LIB_DOIT)==0)
1433                                 break;
1434                 }
1435                 /* this group is entirely in this scene */
1436                 if(go==NULL) {
1437                         ListBase listb= {NULL, NULL};
1438                         
1439                         for(go= group->gobject.first; go; go= go->next)
1440                                 go->ob->id.newid= (ID *)go;
1441                         
1442                         /* in order of sorted bases we reinsert group objects */
1443                         for(base = sce->base.first; base; base= base->next) {
1444                                 
1445                                 if(base->object->id.newid) {
1446                                         go= (GroupObject *)base->object->id.newid;
1447                                         base->object->id.newid= NULL;
1448                                         BLI_remlink( &group->gobject, go);
1449                                         BLI_addtail( &listb, go);
1450                                 }
1451                         }
1452                         /* copy the newly sorted listbase */
1453                         group->gobject= listb;
1454                 }
1455         }
1456 }
1457
1458 /* sort the base list on dependency order */
1459 void DAG_scene_sort(struct Scene *sce)
1460 {
1461         DagNode *node;
1462         DagNodeQueue *nqueue;
1463         DagAdjList *itA;
1464         int time;
1465         int skip = 0;
1466         ListBase tempbase;
1467         Base *base;
1468         
1469         tempbase.first= tempbase.last= NULL;
1470         
1471         build_dag(sce, DAG_RL_ALL_BUT_DATA);
1472         
1473         nqueue = queue_create(DAGQUEUEALLOC);
1474         
1475         for(node = sce->theDag->DagNode.first; node; node= node->next) {
1476                 node->color = DAG_WHITE;
1477         }
1478         
1479         time = 1;
1480         
1481         node = sce->theDag->DagNode.first;
1482         
1483         node->color = DAG_GRAY;
1484         time++;
1485         push_stack(nqueue,node);  
1486         
1487         while(nqueue->count) {
1488                 
1489                 skip = 0;
1490                 node = get_top_node_queue(nqueue);
1491                 
1492                 itA = node->child;
1493                 while(itA != NULL) {
1494                         if((itA->node->color == DAG_WHITE) ) {
1495                                 itA->node->DFS_dvtm = time;
1496                                 itA->node->color = DAG_GRAY;
1497                                 
1498                                 time++;
1499                                 push_stack(nqueue,itA->node);
1500                                 skip = 1;
1501                                 break;
1502                         } 
1503                         itA = itA->next;
1504                 }                       
1505                 
1506                 if (!skip) {
1507                         if (node) {
1508                                 node = pop_queue(nqueue);
1509                                 if (node->ob == sce)    // we are done
1510                                         break ;
1511                                 node->color = DAG_BLACK;
1512                                 
1513                                 time++;
1514                                 base = sce->base.first;
1515                                 while (base && base->object != node->ob)
1516                                         base = base->next;
1517                                 if(base) {
1518                                         BLI_remlink(&sce->base,base);
1519                                         BLI_addhead(&tempbase,base);
1520                                 }
1521                         }       
1522                 }
1523         }
1524         
1525         // temporal correction for circular dependancies
1526         base = sce->base.first;
1527         while (base) {
1528                 BLI_remlink(&sce->base,base);
1529                 BLI_addhead(&tempbase,base);
1530                 //if(G.f & G_DEBUG) 
1531                         printf("cyclic %s\n", base->object->id.name);
1532                 base = sce->base.first;
1533         }
1534         
1535         sce->base = tempbase;
1536         queue_delete(nqueue);
1537         
1538         /* all groups with objects in this scene gets resorted too */
1539         scene_sort_groups(sce);
1540         
1541         if(G.f & G_DEBUG) {
1542                 printf("\nordered\n");
1543                 for(base = sce->base.first; base; base= base->next) {
1544                         printf(" %s\n", base->object->id.name);
1545                 }
1546         }
1547         /* temporal...? */
1548         sce->recalc |= SCE_PRV_CHANGED; /* test for 3d preview */
1549 }
1550
1551 /* node was checked to have lasttime != curtime and is if type ID_OB */
1552 static void flush_update_node(DagNode *node, unsigned int layer, int curtime)
1553 {
1554         DagAdjList *itA;
1555         Object *ob, *obc;
1556         int oldflag, changed=0;
1557         unsigned int all_layer;
1558         
1559         node->lasttime= curtime;
1560         
1561         ob= node->ob;
1562         if(ob && (ob->recalc & OB_RECALC)) {
1563                 all_layer= ob->lay;
1564                 /* got an object node that changes, now check relations */
1565                 for(itA = node->child; itA; itA= itA->next) {
1566                         all_layer |= itA->lay;
1567                         /* the relationship is visible */
1568                         if(itA->lay & layer) {
1569                                 if(itA->node->type==ID_OB) {
1570                                         obc= itA->node->ob;
1571                                         oldflag= obc->recalc;
1572                                         
1573                                         /* got a ob->obc relation, now check if flag needs flush */
1574                                         if(ob->recalc & OB_RECALC_OB) {
1575                                                 if(itA->type & DAG_RL_OB_OB) {
1576                                                         //printf("ob %s changes ob %s\n", ob->id.name, obc->id.name);
1577                                                         obc->recalc |= OB_RECALC_OB;
1578                                                 }
1579                                                 if(itA->type & DAG_RL_OB_DATA) {
1580                                                         //printf("ob %s changes obdata %s\n", ob->id.name, obc->id.name);
1581                                                         obc->recalc |= OB_RECALC_DATA;
1582                                                 }
1583                                         }
1584                                         if(ob->recalc & OB_RECALC_DATA) {
1585                                                 if(itA->type & DAG_RL_DATA_OB) {
1586                                                         //printf("obdata %s changes ob %s\n", ob->id.name, obc->id.name);
1587                                                         obc->recalc |= OB_RECALC_OB;
1588                                                 }
1589                                                 if(itA->type & DAG_RL_DATA_DATA) {
1590                                                         //printf("obdata %s changes obdata %s\n", ob->id.name, obc->id.name);
1591                                                         obc->recalc |= OB_RECALC_DATA;
1592                                                 }
1593                                         }
1594                                         if(oldflag!=obc->recalc) changed= 1;
1595                                 }
1596                         }
1597                 }
1598                 /* even nicer, we can clear recalc flags...  */
1599                 if((all_layer & layer)==0) {
1600                         /* but existing displaylists or derivedmesh should be freed */
1601                         if(ob->recalc & OB_RECALC_DATA)
1602                                 object_free_display(ob);
1603                         
1604                         ob->recalc &= ~OB_RECALC;
1605                 }
1606         }
1607         
1608         /* check case where child changes and parent forcing obdata to change */
1609         /* should be done regardless if this ob has recalc set */
1610         /* could merge this in with loop above...? (ton) */
1611         for(itA = node->child; itA; itA= itA->next) {
1612                 /* the relationship is visible */
1613                 if(itA->lay & layer) {
1614                         if(itA->node->type==ID_OB) {
1615                                 obc= itA->node->ob;
1616                                 /* child moves */
1617                                 if((obc->recalc & OB_RECALC)==OB_RECALC_OB) {
1618                                         /* parent has deforming info */
1619                                         if(itA->type & (DAG_RL_OB_DATA|DAG_RL_DATA_DATA)) {
1620                                                 // printf("parent %s changes ob %s\n", ob->id.name, obc->id.name);
1621                                                 obc->recalc |= OB_RECALC_DATA;
1622                                         }
1623                                 }
1624                         }
1625                 }
1626         }
1627         
1628         /* we only go deeper if node not checked or something changed  */
1629         for(itA = node->child; itA; itA= itA->next) {
1630                 if(changed || itA->node->lasttime!=curtime) 
1631                         flush_update_node(itA->node, layer, curtime);
1632         }
1633         
1634 }
1635
1636 /* node was checked to have lasttime != curtime , and is of type ID_OB */
1637 static unsigned int flush_layer_node(DagNode *node, int curtime)
1638 {
1639         DagAdjList *itA;
1640         
1641         node->lasttime= curtime;
1642         node->lay= ((Object *)node->ob)->lay;
1643         
1644         for(itA = node->child; itA; itA= itA->next) {
1645                 if(itA->node->type==ID_OB) {
1646                         if(itA->node->lasttime!=curtime) {
1647                                 itA->lay= flush_layer_node(itA->node, curtime);  // lay is only set once for each relation
1648                                 //printf("layer %d for relation %s to %s\n", itA->lay, ((Object *)node->ob)->id.name, ((Object *)itA->node->ob)->id.name);
1649                         }
1650                         else itA->lay= itA->node->lay;
1651                         
1652                         node->lay |= itA->lay;
1653                 }
1654         }
1655
1656         return node->lay;
1657 }
1658
1659 /* flushes all recalc flags in objects down the dependency tree */
1660 void DAG_scene_flush_update(Scene *sce, unsigned int lay)
1661 {
1662         DagNode *firstnode;
1663         DagAdjList *itA;
1664         int lasttime;
1665         
1666         if(sce->theDag==NULL) {
1667                 printf("DAG zero... not allowed to happen!\n");
1668                 DAG_scene_sort(sce);
1669         }
1670         
1671         firstnode= sce->theDag->DagNode.first;  // always scene node
1672         
1673         /* first we flush the layer flags */
1674         sce->theDag->time++;    // so we know which nodes were accessed
1675         lasttime= sce->theDag->time;
1676         for(itA = firstnode->child; itA; itA= itA->next) {
1677                 if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB) 
1678                         flush_layer_node(itA->node, lasttime);
1679         }
1680         
1681         /* then we use the relationships + layer info to flush update events */
1682         sce->theDag->time++;    // so we know which nodes were accessed
1683         lasttime= sce->theDag->time;
1684         for(itA = firstnode->child; itA; itA= itA->next) {
1685                 if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB) 
1686                         flush_update_node(itA->node, lay, lasttime);
1687         }
1688 }
1689
1690 static int object_modifiers_use_time(Object *ob)
1691 {
1692         ModifierData *md;
1693
1694         for (md=ob->modifiers.first; md; md=md->next)
1695                 if (modifier_dependsOnTime(md))
1696                         return 1;
1697
1698         return 0;
1699 }
1700
1701 static int exists_channel(Object *ob, char *name)
1702 {
1703         bActionStrip *strip;
1704         
1705         if(ob->action)
1706                 if(get_action_channel(ob->action, name))
1707                         return 1;
1708         
1709         for (strip=ob->nlastrips.first; strip; strip=strip->next)
1710                 if(get_action_channel(strip->act, name))
1711                         return 1;
1712         return 0;
1713 }
1714
1715 static void dag_object_time_update_flags(Object *ob)
1716 {
1717         
1718         if(ob->ipo) ob->recalc |= OB_RECALC_OB;
1719         else if(ob->constraints.first) {
1720                 bConstraint *con;
1721                 for (con = ob->constraints.first; con; con=con->next){
1722                         if (constraint_has_target(con)) {
1723                                 ob->recalc |= OB_RECALC_OB;
1724                                 break;
1725                         }
1726                 }
1727         }
1728         else if(ob->scriptlink.totscript) ob->recalc |= OB_RECALC_OB;
1729         else if(ob->parent) {
1730                 /* motion path or bone child */
1731                 if(ob->parent->type==OB_CURVE || ob->parent->type==OB_ARMATURE) ob->recalc |= OB_RECALC_OB;
1732         }
1733         
1734         if(ob->action || ob->nlastrips.first) {
1735                 /* since actions now are mixed, we set the recalcs on the safe side */
1736                 ob->recalc |= OB_RECALC_OB;
1737                 if(ob->type==OB_ARMATURE)
1738                         ob->recalc |= OB_RECALC_DATA;
1739                 else if(exists_channel(ob, "Shape"))
1740                         ob->recalc |= OB_RECALC_DATA;
1741                 else if(ob->dup_group) {
1742                         bActionStrip *strip;
1743                         /* this case is for groups with nla, whilst nla target has no action or nla */
1744                         for(strip= ob->nlastrips.first; strip; strip= strip->next) {
1745                                 if(strip->object)
1746                                         strip->object->recalc |= OB_RECALC;
1747                         }
1748                 }
1749         }
1750         else if(modifiers_isSoftbodyEnabled(ob)) ob->recalc |= OB_RECALC_DATA;
1751         else if(object_modifiers_use_time(ob)) ob->recalc |= OB_RECALC_DATA;
1752         else {
1753                 Mesh *me;
1754                 Curve *cu;
1755                 Lattice *lt;
1756                 
1757                 switch(ob->type) {
1758                         case OB_MESH:
1759                                 me= ob->data;
1760                                 if(me->key) {
1761                                         if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
1762                                                 ob->recalc |= OB_RECALC_DATA;
1763                                                 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
1764                                         }
1765                                 }
1766                                 else if(ob->effect.first) {
1767                                         Effect *eff= ob->effect.first;
1768                                         PartEff *paf= give_parteff(ob);
1769                                         
1770                                         if(eff->type==EFF_WAVE) 
1771                                                 ob->recalc |= OB_RECALC_DATA;
1772                                         else if(paf && paf->keys==NULL)
1773                                                 ob->recalc |= OB_RECALC_DATA;
1774                                 }
1775                                 if((ob->fluidsimFlag & OB_FLUIDSIM_ENABLE) && (ob->fluidsimSettings)) {
1776                                         // fluidsimSettings might not be initialized during load...
1777                                         if(ob->fluidsimSettings->type & (OB_FLUIDSIM_DOMAIN|OB_FLUIDSIM_PARTICLE)) {
1778                                                 ob->recalc |= OB_RECALC_DATA; // NT FSPARTICLE
1779                                         }
1780                                 }
1781                                 break;
1782                         case OB_CURVE:
1783                         case OB_SURF:
1784                                 cu= ob->data;
1785                                 if(cu->key) {
1786                                         if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
1787                                                 ob->recalc |= OB_RECALC_DATA;
1788                                                 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
1789                                         }
1790                                 }
1791                                 break;
1792                         case OB_FONT:
1793                                 cu= ob->data;
1794                                 if(cu->nurb.first==NULL && cu->str && cu->vfont)
1795                                         ob->recalc |= OB_RECALC_DATA;
1796                                 break;
1797                         case OB_LATTICE:
1798                                 lt= ob->data;
1799                                 if(lt->key) {
1800                                         if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
1801                                                 ob->recalc |= OB_RECALC_DATA;
1802                                                 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
1803                                         }
1804                                 }
1805                                         break;
1806                         case OB_MBALL:
1807                                 if(ob->transflag & OB_DUPLI) ob->recalc |= OB_RECALC_DATA;
1808                                 break;
1809                 }
1810         }               
1811 }
1812
1813 /* flag all objects that need recalc, for changes in time for example */
1814 void DAG_scene_update_flags(Scene *scene, unsigned int lay)
1815 {
1816         Base *base;
1817         Object *ob;
1818         Group *group;
1819         GroupObject *go;
1820         Scene *sce;
1821         
1822         /* set ob flags where animated systems are */
1823         for(SETLOOPER(scene, base)) {
1824                 ob= base->object;
1825                 
1826                 /* now if DagNode were part of base, the node->lay could be checked... */
1827                 /* we do all now, since the scene_flush checks layers and clears recalc flags even */
1828                 dag_object_time_update_flags(ob);
1829                 
1830                 /* handled in next loop */
1831                 if(ob->dup_group) 
1832                         ob->dup_group->id.flag |= LIB_DOIT;
1833         }       
1834         
1835         /* we do groups each once */
1836         for(group= G.main->group.first; group; group= group->id.next) {
1837                 if(group->id.flag & LIB_DOIT) {
1838                         for(go= group->gobject.first; go; go= go->next) {
1839                                 dag_object_time_update_flags(go->ob);
1840                         }
1841                 }
1842         }
1843         
1844         for(sce= scene; sce; sce= sce->set)
1845                 DAG_scene_flush_update(sce, lay);
1846         
1847         /* test: set time flag, to disable baked systems to update */
1848         for(SETLOOPER(scene, base)) {
1849                 ob= base->object;
1850                 if(ob->recalc)
1851                         ob->recalc |= OB_RECALC_TIME;
1852         }
1853         
1854         /* hrmf... an exception to look at once, for invisible camera object we do it over */
1855         if(scene->camera)
1856                 dag_object_time_update_flags(scene->camera);
1857         
1858         /* and store the info in groupobject */
1859         for(group= G.main->group.first; group; group= group->id.next) {
1860                 if(group->id.flag & LIB_DOIT) {
1861                         for(go= group->gobject.first; go; go= go->next) {
1862                                 go->recalc= go->ob->recalc;
1863                                 // printf("ob %s recalc %d\n", go->ob->id.name, go->recalc);
1864                         }
1865                         group->id.flag &= ~LIB_DOIT;
1866                 }
1867         }
1868         
1869 }
1870
1871 /* for depgraph updating, all layers visible in a screen */
1872 /* this is a copy from editscreen.c... I need to think over a more proper solution for this */
1873 /* probably the DAG_object_flush_update() should give layer too? */
1874 /* or some kind of dag context... (DAG_set_layer) */
1875 static unsigned int dag_screen_view3d_layers(void)
1876 {
1877         ScrArea *sa;
1878         int layer= 0;
1879         
1880         for(sa= G.curscreen->areabase.first; sa; sa= sa->next) {
1881                 if(sa->spacetype==SPACE_VIEW3D)
1882                         layer |= ((View3D *)sa->spacedata.first)->lay;
1883         }
1884         return layer;
1885 }
1886
1887
1888 /* flag this object and all its relations to recalc */
1889 /* if you need to do more objects, tag object yourself and
1890    use DAG_scene_flush_update() in end */
1891 void DAG_object_flush_update(Scene *sce, Object *ob, short flag)
1892 {
1893         
1894         if(ob==NULL || sce->theDag==NULL) return;
1895         ob->recalc |= flag;
1896         
1897         /* all users of this ob->data should be checked */
1898         /* BUT! displists for curves are still only on cu */
1899         if(flag & OB_RECALC_DATA) {
1900                 if(ob->type!=OB_CURVE && ob->type!=OB_SURF) {
1901                         ID *id= ob->data;
1902                         if(id && id->us>1) {
1903                                 /* except when there's a key and shapes are locked */
1904                                 if(ob_get_key(ob) && (ob->shapeflag & (OB_SHAPE_LOCK|OB_SHAPE_TEMPLOCK)));
1905                                 else {
1906                                         Object *obt;
1907                                         for (obt=G.main->object.first; obt; obt= obt->id.next) {
1908                                                 if (obt->data==ob->data) {
1909                                                         obt->recalc |= OB_RECALC_DATA;
1910                                                 }
1911                                         }
1912                                 }
1913                         }
1914                 }
1915         }
1916         
1917         if(G.curscreen)
1918                 DAG_scene_flush_update(sce, dag_screen_view3d_layers());
1919         else
1920                 DAG_scene_flush_update(sce, sce->lay);
1921 }
1922
1923 /* recursively descends tree, each node only checked once */
1924 /* node is checked to be of type object */
1925 static int parent_check_node(DagNode *node, int curtime)
1926 {
1927         DagAdjList *itA;
1928         
1929         node->lasttime= curtime;
1930         
1931         if(node->color==DAG_GRAY)
1932                 return DAG_GRAY;
1933         
1934         for(itA = node->child; itA; itA= itA->next) {
1935                 if(itA->node->type==ID_OB) {
1936                         
1937                         if(itA->node->color==DAG_GRAY)
1938                                 return DAG_GRAY;
1939
1940                         /* descend if not done */
1941                         if(itA->node->lasttime!=curtime) {
1942                                 itA->node->color= parent_check_node(itA->node, curtime);
1943                         
1944                                 if(itA->node->color==DAG_GRAY)
1945                                         return DAG_GRAY;
1946                         }
1947                 }
1948         }
1949         
1950         return DAG_WHITE;
1951 }
1952
1953 /* all nodes that influence this object get tagged, for calculating the exact
1954    position of this object at a given timeframe */
1955 void DAG_object_update_flags(Scene *sce, Object *ob, unsigned int lay)
1956 {
1957         DagNode *node;
1958         DagAdjList *itA;
1959         
1960         /* tag nodes unchecked */
1961         for(node = sce->theDag->DagNode.first; node; node= node->next) 
1962                 node->color = DAG_WHITE;
1963         
1964         node= dag_find_node(sce->theDag, ob);
1965         
1966         /* object not in scene? then handle group exception. needs to be dagged once too */
1967         if(node==NULL) {
1968                 Group *group= find_group(ob);
1969                 if(group) {
1970                         GroupObject *go;
1971                         /* primitive; tag all... this call helps building groups for particles */
1972                         for(go= group->gobject.first; go; go= go->next)
1973                                 go->ob->recalc= OB_RECALC;
1974                 }
1975         }
1976         else {
1977                 
1978                 node->color = DAG_GRAY;
1979                 
1980                 sce->theDag->time++;
1981                 node= sce->theDag->DagNode.first;
1982                 for(itA = node->child; itA; itA= itA->next) {
1983                         if(itA->node->type==ID_OB && itA->node->lasttime!=sce->theDag->time)
1984                                 itA->node->color= parent_check_node(itA->node, sce->theDag->time);
1985                 }
1986                 
1987                 /* set recalcs and flushes */
1988                 DAG_scene_update_flags(sce, lay);
1989                 
1990                 /* now we clear recalcs, unless color is set */
1991                 for(node = sce->theDag->DagNode.first; node; node= node->next) {
1992                         if(node->type==ID_OB && node->color==DAG_WHITE) {
1993                                 Object *ob= node->ob;
1994                                 ob->recalc= 0;
1995                         }
1996                 }
1997         }
1998 }
1999
2000 /* ******************* DAG FOR ARMATURE POSE ***************** */
2001
2002 static int node_recurs_level(DagNode *node, int level)
2003 {
2004         DagAdjList *itA;
2005         
2006         node->color= DAG_BLACK; /* done */
2007         level++;
2008         
2009         for(itA= node->parent; itA; itA= itA->next) {
2010                 if(itA->node->color==DAG_WHITE)
2011                         itA->node->ancestor_count= node_recurs_level(itA->node, level);
2012         }
2013         
2014         return level;
2015 }
2016
2017 static void pose_check_cycle(DagForest *dag)
2018 {
2019         DagNode *node;
2020         DagAdjList *itA;
2021
2022         /* tag nodes unchecked */
2023         for(node = dag->DagNode.first; node; node= node->next)
2024                 node->color= DAG_WHITE;
2025         
2026         for(node = dag->DagNode.first; node; node= node->next) {
2027                 if(node->color==DAG_WHITE) {
2028                         node->ancestor_count= node_recurs_level(node, 0);
2029                 }
2030         }
2031         
2032         /* check relations, and print errors */
2033         for(node = dag->DagNode.first; node; node= node->next) {
2034                 for(itA= node->parent; itA; itA= itA->next) {
2035                         if(itA->node->ancestor_count > node->ancestor_count) {
2036                                 bPoseChannel *pchan= (bPoseChannel *)node->ob;
2037                                 bPoseChannel *parchan= (bPoseChannel *)itA->node->ob;
2038                                 
2039                                 if(pchan && parchan) 
2040                                         if(pchan->parent!=parchan)
2041                                                 printf("Cycle in %s to %s\n", pchan->name, parchan->name);
2042                         }
2043                 }
2044         }
2045 }
2046
2047 /* we assume its an armature with pose */
2048 void DAG_pose_sort(Object *ob)
2049 {
2050         bPose *pose= ob->pose;
2051         bPoseChannel *pchan;
2052         bConstraint *con;
2053         DagNode *node;
2054         DagNode *node2, *node3;
2055         DagNode *rootnode;
2056         DagForest *dag;
2057         DagNodeQueue *nqueue;
2058         DagAdjList *itA;
2059         ListBase tempbase;
2060         int skip = 0;
2061         
2062         dag = dag_init();
2063         ugly_hack_sorry= 0;     // no ID structs
2064
2065         rootnode = dag_add_node(dag, NULL);     // node->ob becomes NULL
2066         
2067         /* we add the hierarchy and the constraints */
2068         for(pchan = pose->chanbase.first; pchan; pchan= pchan->next) {
2069                 int addtoroot = 1;
2070                 
2071                 node = dag_get_node(dag, pchan);
2072                 
2073                 if(pchan->parent) {
2074                         node2 = dag_get_node(dag, pchan->parent);
2075                         dag_add_relation(dag, node2, node, 0);
2076                         dag_add_parent_relation(dag, node2, node, 0);
2077                         addtoroot = 0;
2078                 }
2079                 for (con = pchan->constraints.first; con; con=con->next){
2080                         if (constraint_has_target(con)) {
2081                                 char *subtarget;
2082                                 Object *target = get_constraint_target(con, &subtarget);
2083                                 
2084                                 if (target==ob && subtarget) {
2085                                         bPoseChannel *target= get_pose_channel(ob->pose, subtarget);
2086                                         if(target) {
2087                                                 node2= dag_get_node(dag, target);
2088                                                 dag_add_relation(dag, node2, node, 0);
2089                                                 dag_add_parent_relation(dag, node2, node, 0);
2090                                                 
2091                                                 if(con->type==CONSTRAINT_TYPE_KINEMATIC) {
2092                                                         bKinematicConstraint *data = (bKinematicConstraint*)con->data;
2093                                                         bPoseChannel *parchan;
2094                                                         int segcount= 0;
2095                                                         
2096                                                         /* exclude tip from chain? */
2097                                                         if(!(data->flag & CONSTRAINT_IK_TIP))
2098                                                                 parchan= pchan->parent;
2099                                                         else
2100                                                                 parchan= pchan;
2101                                                         
2102                                                         /* Walk to the chain's root */
2103                                                         while (parchan){
2104                                                                 node3= dag_get_node(dag, parchan);
2105                                                                 dag_add_relation(dag, node2, node3, 0);
2106                                                                 dag_add_parent_relation(dag, node2, node3, 0);
2107                                                                 
2108                                                                 segcount++;
2109                                                                 if(segcount==data->rootbone || segcount>255) break; // 255 is weak
2110                                                                 parchan= parchan->parent;
2111                                                         }
2112                                                 }
2113                                         }
2114                                 }
2115                         }
2116                 }
2117                 if (addtoroot == 1 ) {
2118                         dag_add_relation(dag, rootnode, node, 0);
2119                         dag_add_parent_relation(dag, rootnode, node, 0);
2120                 }
2121         }
2122
2123         pose_check_cycle(dag);
2124         
2125         /* now we try to sort... */
2126         tempbase.first= tempbase.last= NULL;
2127
2128         nqueue = queue_create(DAGQUEUEALLOC);
2129         
2130         /* tag nodes unchecked */
2131         for(node = dag->DagNode.first; node; node= node->next) 
2132                 node->color = DAG_WHITE;
2133         
2134         node = dag->DagNode.first;
2135         
2136         node->color = DAG_GRAY;
2137         push_stack(nqueue, node);  
2138         
2139         while(nqueue->count) {
2140                 
2141                 skip = 0;
2142                 node = get_top_node_queue(nqueue);
2143                 
2144                 itA = node->child;
2145                 while(itA != NULL) {
2146                         if((itA->node->color == DAG_WHITE) ) {
2147                                 itA->node->color = DAG_GRAY;
2148                                 push_stack(nqueue,itA->node);
2149                                 skip = 1;
2150                                 break;
2151                         } 
2152                         itA = itA->next;
2153                 }                       
2154                 
2155                 if (!skip) {
2156                         if (node) {
2157                                 node = pop_queue(nqueue);
2158                                 if (node->ob == NULL)   // we are done
2159                                         break ;
2160                                 node->color = DAG_BLACK;
2161                                 
2162                                 /* put node in new list */
2163                                 BLI_remlink(&pose->chanbase, node->ob);
2164                                 BLI_addhead(&tempbase, node->ob);
2165                         }       
2166                 }
2167         }
2168         
2169         // temporal correction for circular dependancies
2170         while(pose->chanbase.first) {
2171                 pchan= pose->chanbase.first;
2172                 BLI_remlink(&pose->chanbase, pchan);
2173                 BLI_addhead(&tempbase, pchan);
2174
2175                 printf("cyclic %s\n", pchan->name);
2176         }
2177         
2178         pose->chanbase = tempbase;
2179         queue_delete(nqueue);
2180         
2181 //      printf("\nordered\n");
2182 //      for(pchan = pose->chanbase.first; pchan; pchan= pchan->next) {
2183 //              printf(" %s\n", pchan->name);
2184 //      }
2185         
2186         free_forest( dag );
2187         MEM_freeN( dag );
2188         
2189         ugly_hack_sorry= 1;
2190 }
2191
2192
2193