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