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