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