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