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