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