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