fix for [#8236] Lens Change by driver won't update in 3d View
[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)
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                                 if(BKE_ptcache_object_reset(ob, PTCACHE_RESET_DEPSGRAPH))
1833                                         ob->recalc |= OB_RECALC_DATA;
1834                                 flush_pointcache_reset(itA->node, curtime);
1835                         }
1836                 }
1837         }
1838 }
1839
1840 /* flushes all recalc flags in objects down the dependency tree */
1841 void DAG_scene_flush_update(Scene *sce, unsigned int lay, int time)
1842 {
1843         DagNode *firstnode;
1844         DagAdjList *itA;
1845         Object *ob;
1846         int lasttime;
1847         
1848         if(sce->theDag==NULL) {
1849                 printf("DAG zero... not allowed to happen!\n");
1850                 DAG_scene_sort(sce);
1851         }
1852         
1853         firstnode= sce->theDag->DagNode.first;  // always scene node
1854
1855         for(itA = firstnode->child; itA; itA= itA->next)
1856                 itA->lay= 0;
1857         
1858         /* first we flush the layer flags */
1859         sce->theDag->time++;    // so we know which nodes were accessed
1860         lasttime= sce->theDag->time;
1861
1862         for(itA = firstnode->child; itA; itA= itA->next)
1863                 if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB) 
1864                         flush_layer_node(sce, itA->node, lasttime);
1865         
1866         /* then we use the relationships + layer info to flush update events */
1867         sce->theDag->time++;    // so we know which nodes were accessed
1868         lasttime= sce->theDag->time;
1869         for(itA = firstnode->child; itA; itA= itA->next)
1870                 if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB)
1871                         flush_update_node(itA->node, lay, lasttime);
1872
1873         /* if update is not due to time change, do pointcache clears */
1874         if(!time) {
1875                 sce->theDag->time++;    // so we know which nodes were accessed
1876                 lasttime= sce->theDag->time;
1877                 for(itA = firstnode->child; itA; itA= itA->next) {
1878                         if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB)  {
1879                                 ob= (Object*)(itA->node->ob);
1880                                 if(BKE_ptcache_object_reset(ob, PTCACHE_RESET_DEPSGRAPH))
1881                                         ob->recalc |= OB_RECALC_DATA;
1882                                 flush_pointcache_reset(itA->node, lasttime);
1883                         }
1884                 }
1885         }
1886 }
1887
1888 static int object_modifiers_use_time(Object *ob)
1889 {
1890         ModifierData *md;
1891
1892         for (md=ob->modifiers.first; md; md=md->next)
1893                 if (modifier_dependsOnTime(md))
1894                         return 1;
1895
1896         return 0;
1897 }
1898
1899 static int exists_channel(Object *ob, char *name)
1900 {
1901         bActionStrip *strip;
1902         
1903         if(ob->action)
1904                 if(get_action_channel(ob->action, name))
1905                         return 1;
1906         
1907         for (strip=ob->nlastrips.first; strip; strip=strip->next)
1908                 if(get_action_channel(strip->act, name))
1909                         return 1;
1910         return 0;
1911 }
1912
1913 static void dag_object_time_update_flags(Object *ob)
1914 {
1915         
1916         if(ob->ipo) ob->recalc |= OB_RECALC_OB;
1917         else if(ob->constraints.first) {
1918                 bConstraint *con;
1919                 for (con = ob->constraints.first; con; con=con->next) {
1920                         bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
1921                         ListBase targets = {NULL, NULL};
1922                         bConstraintTarget *ct;
1923                         
1924                         if (cti && cti->get_constraint_targets) {
1925                                 cti->get_constraint_targets(con, &targets);
1926                                 
1927                                 for (ct= targets.first; ct; ct= ct->next) {
1928                                         if (ct->tar) {
1929                                                 ob->recalc |= OB_RECALC_OB;
1930                                                 break;
1931                                         }
1932                                 }
1933                                 
1934                                 if (cti->flush_constraint_targets)
1935                                         cti->flush_constraint_targets(con, &targets, 1);
1936                         }
1937                 }
1938         }
1939         else if(ob->scriptlink.totscript) ob->recalc |= OB_RECALC_OB;
1940         else if(ob->parent) {
1941                 /* motion path or bone child */
1942                 if(ob->parent->type==OB_CURVE || ob->parent->type==OB_ARMATURE) ob->recalc |= OB_RECALC_OB;
1943         }
1944         
1945         if(ob->action || ob->nlastrips.first) {
1946                 /* since actions now are mixed, we set the recalcs on the safe side */
1947                 ob->recalc |= OB_RECALC_OB;
1948                 if(ob->type==OB_ARMATURE)
1949                         ob->recalc |= OB_RECALC_DATA;
1950                 else if(exists_channel(ob, "Shape"))
1951                         ob->recalc |= OB_RECALC_DATA;
1952                 else if(ob->dup_group) {
1953                         bActionStrip *strip;
1954                         /* this case is for groups with nla, whilst nla target has no action or nla */
1955                         for(strip= ob->nlastrips.first; strip; strip= strip->next) {
1956                                 if(strip->object)
1957                                         strip->object->recalc |= OB_RECALC;
1958                         }
1959                 }
1960         }
1961         else if(modifiers_isSoftbodyEnabled(ob)) ob->recalc |= OB_RECALC_DATA;
1962         else if(object_modifiers_use_time(ob)) ob->recalc |= OB_RECALC_DATA;
1963         else if((ob->pose) && (ob->pose->flag & POSE_CONSTRAINTS_TIMEDEPEND)) ob->recalc |= OB_RECALC_DATA;
1964         else {
1965                 Mesh *me;
1966                 Curve *cu;
1967                 Lattice *lt;
1968                 
1969                 switch(ob->type) {
1970                         case OB_MESH:
1971                                 me= ob->data;
1972                                 if(me->key) {
1973                                         if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
1974                                                 ob->recalc |= OB_RECALC_DATA;
1975                                                 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
1976                                         }
1977                                 }
1978                                 else if(ob->effect.first) {
1979                                         Effect *eff= ob->effect.first;
1980                                         PartEff *paf= give_parteff(ob);
1981                                         
1982                                         if(eff->type==EFF_WAVE) 
1983                                                 ob->recalc |= OB_RECALC_DATA;
1984                                         else if(paf && paf->keys==NULL)
1985                                                 ob->recalc |= OB_RECALC_DATA;
1986                                 }
1987                                 if((ob->fluidsimFlag & OB_FLUIDSIM_ENABLE) && (ob->fluidsimSettings)) {
1988                                         // fluidsimSettings might not be initialized during load...
1989                                         if(ob->fluidsimSettings->type & (OB_FLUIDSIM_DOMAIN|OB_FLUIDSIM_PARTICLE)) {
1990                                                 ob->recalc |= OB_RECALC_DATA; // NT FSPARTICLE
1991                                         }
1992                                 }
1993                                 if(ob->particlesystem.first)
1994                                         ob->recalc |= OB_RECALC_DATA;
1995                                 break;
1996                         case OB_CURVE:
1997                         case OB_SURF:
1998                                 cu= ob->data;
1999                                 if(cu->key) {
2000                                         if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
2001                                                 ob->recalc |= OB_RECALC_DATA;
2002                                                 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
2003                                         }
2004                                 }
2005                                 break;
2006                         case OB_FONT:
2007                                 cu= ob->data;
2008                                 if(cu->nurb.first==NULL && cu->str && cu->vfont)
2009                                         ob->recalc |= OB_RECALC_DATA;
2010                                 break;
2011                         case OB_LATTICE:
2012                                 lt= ob->data;
2013                                 if(lt->key) {
2014                                         if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
2015                                                 ob->recalc |= OB_RECALC_DATA;
2016                                                 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
2017                                         }
2018                                 }
2019                                         break;
2020                         case OB_MBALL:
2021                                 if(ob->transflag & OB_DUPLI) ob->recalc |= OB_RECALC_DATA;
2022                                 break;
2023                 }
2024
2025                 if(ob->particlesystem.first) {
2026                         ParticleSystem *psys= ob->particlesystem.first;
2027
2028                         for(; psys; psys=psys->next) {
2029                                 if(psys_check_enabled(ob, psys)) {
2030                                         ob->recalc |= OB_RECALC_DATA;
2031                                         break;
2032                                 }
2033                         }
2034                 }
2035         }               
2036 }
2037
2038 /* flag all objects that need recalc, for changes in time for example */
2039 void DAG_scene_update_flags(Scene *scene, unsigned int lay)
2040 {
2041         Base *base;
2042         Object *ob;
2043         Group *group;
2044         GroupObject *go;
2045         Scene *sce;
2046         
2047         /* set ob flags where animated systems are */
2048         for(SETLOOPER(scene, base)) {
2049                 ob= base->object;
2050                 
2051                 /* now if DagNode were part of base, the node->lay could be checked... */
2052                 /* we do all now, since the scene_flush checks layers and clears recalc flags even */
2053                 dag_object_time_update_flags(ob);
2054                 
2055                 /* handled in next loop */
2056                 if(ob->dup_group) 
2057                         ob->dup_group->id.flag |= LIB_DOIT;
2058         }       
2059         
2060         /* we do groups each once */
2061         for(group= G.main->group.first; group; group= group->id.next) {
2062                 if(group->id.flag & LIB_DOIT) {
2063                         for(go= group->gobject.first; go; go= go->next) {
2064                                 dag_object_time_update_flags(go->ob);
2065                         }
2066                 }
2067         }
2068         
2069         for(sce= scene; sce; sce= sce->set)
2070                 DAG_scene_flush_update(sce, lay, 1);
2071         
2072         /* test: set time flag, to disable baked systems to update */
2073         for(SETLOOPER(scene, base)) {
2074                 ob= base->object;
2075                 if(ob->recalc)
2076                         ob->recalc |= OB_RECALC_TIME;
2077         }
2078         
2079         /* hrmf... an exception to look at once, for invisible camera object we do it over */
2080         if(scene->camera)
2081                 dag_object_time_update_flags(scene->camera);
2082         
2083         /* and store the info in groupobject */
2084         for(group= G.main->group.first; group; group= group->id.next) {
2085                 if(group->id.flag & LIB_DOIT) {
2086                         for(go= group->gobject.first; go; go= go->next) {
2087                                 go->recalc= go->ob->recalc;
2088                                 // printf("ob %s recalc %d\n", go->ob->id.name, go->recalc);
2089                         }
2090                         group->id.flag &= ~LIB_DOIT;
2091                 }
2092         }
2093         
2094 }
2095
2096 /* for depgraph updating, all layers visible in a screen */
2097 /* this is a copy from editscreen.c... I need to think over a more proper solution for this */
2098 /* probably the DAG_object_flush_update() should give layer too? */
2099 /* or some kind of dag context... (DAG_set_layer) */
2100 static unsigned int dag_screen_view3d_layers(void)
2101 {
2102         ScrArea *sa;
2103         int layer= 0;
2104         
2105         for(sa= G.curscreen->areabase.first; sa; sa= sa->next) {
2106                 if(sa->spacetype==SPACE_VIEW3D)
2107                         layer |= ((View3D *)sa->spacedata.first)->lay;
2108         }
2109         return layer;
2110 }
2111
2112
2113 /* flag this object and all its relations to recalc */
2114 /* if you need to do more objects, tag object yourself and
2115    use DAG_scene_flush_update() in end */
2116 void DAG_object_flush_update(Scene *sce, Object *ob, short flag)
2117 {
2118         
2119         if(ob==NULL || sce->theDag==NULL) return;
2120
2121         ob->recalc |= flag;
2122         BKE_ptcache_object_reset(ob, PTCACHE_RESET_DEPSGRAPH);
2123         
2124         /* all users of this ob->data should be checked */
2125         /* BUT! displists for curves are still only on cu */
2126         if(flag & OB_RECALC_DATA) {
2127                 if(ob->type!=OB_CURVE && ob->type!=OB_SURF) {
2128                         ID *id= ob->data;
2129                         if(id && id->us>1) {
2130                                 /* except when there's a key and shapes are locked */
2131                                 if(ob_get_key(ob) && (ob->shapeflag & (OB_SHAPE_LOCK|OB_SHAPE_TEMPLOCK)));
2132                                 else {
2133                                         Object *obt;
2134                                         for (obt=G.main->object.first; obt; obt= obt->id.next) {
2135                                                 if (obt != ob && obt->data==ob->data) {
2136                                                         obt->recalc |= OB_RECALC_DATA;
2137                                                         BKE_ptcache_object_reset(obt, PTCACHE_RESET_DEPSGRAPH);
2138                                                 }
2139                                         }
2140                                 }
2141                         }
2142                 }
2143         }
2144         
2145         if(G.curscreen)
2146                 DAG_scene_flush_update(sce, dag_screen_view3d_layers(), 0);
2147         else
2148                 DAG_scene_flush_update(sce, sce->lay, 0);
2149 }
2150
2151 /* recursively descends tree, each node only checked once */
2152 /* node is checked to be of type object */
2153 static int parent_check_node(DagNode *node, int curtime)
2154 {
2155         DagAdjList *itA;
2156         
2157         node->lasttime= curtime;
2158         
2159         if(node->color==DAG_GRAY)
2160                 return DAG_GRAY;
2161         
2162         for(itA = node->child; itA; itA= itA->next) {
2163                 if(itA->node->type==ID_OB) {
2164                         
2165                         if(itA->node->color==DAG_GRAY)
2166                                 return DAG_GRAY;
2167
2168                         /* descend if not done */
2169                         if(itA->node->lasttime!=curtime) {
2170                                 itA->node->color= parent_check_node(itA->node, curtime);
2171                         
2172                                 if(itA->node->color==DAG_GRAY)
2173                                         return DAG_GRAY;
2174                         }
2175                 }
2176         }
2177         
2178         return DAG_WHITE;
2179 }
2180
2181 /* all nodes that influence this object get tagged, for calculating the exact
2182    position of this object at a given timeframe */
2183 void DAG_object_update_flags(Scene *sce, Object *ob, unsigned int lay)
2184 {
2185         DagNode *node;
2186         DagAdjList *itA;
2187         
2188         /* tag nodes unchecked */
2189         for(node = sce->theDag->DagNode.first; node; node= node->next) 
2190                 node->color = DAG_WHITE;
2191         
2192         node= dag_find_node(sce->theDag, ob);
2193         
2194         /* object not in scene? then handle group exception. needs to be dagged once too */
2195         if(node==NULL) {
2196                 Group *group= NULL;
2197                 while( (group = find_group(ob, group)) ) {
2198                         GroupObject *go;
2199                         /* primitive; tag all... this call helps building groups for particles */
2200                         for(go= group->gobject.first; go; go= go->next)
2201                                 go->ob->recalc= OB_RECALC;
2202                 }
2203         }
2204         else {
2205                 
2206                 node->color = DAG_GRAY;
2207                 
2208                 sce->theDag->time++;
2209                 node= sce->theDag->DagNode.first;
2210                 for(itA = node->child; itA; itA= itA->next) {
2211                         if(itA->node->type==ID_OB && itA->node->lasttime!=sce->theDag->time)
2212                                 itA->node->color= parent_check_node(itA->node, sce->theDag->time);
2213                 }
2214                 
2215                 /* set recalcs and flushes */
2216                 DAG_scene_update_flags(sce, lay);
2217                 
2218                 /* now we clear recalcs, unless color is set */
2219                 for(node = sce->theDag->DagNode.first; node; node= node->next) {
2220                         if(node->type==ID_OB && node->color==DAG_WHITE) {
2221                                 Object *ob= node->ob;
2222                                 ob->recalc= 0;
2223                         }
2224                 }
2225         }
2226 }
2227
2228 /* ******************* DAG FOR ARMATURE POSE ***************** */
2229
2230 static int node_recurs_level(DagNode *node, int level)
2231 {
2232         DagAdjList *itA;
2233         int newlevel;
2234
2235         node->color= DAG_BLACK; /* done */
2236         newlevel= ++level;
2237         
2238         for(itA= node->parent; itA; itA= itA->next) {
2239                 if(itA->node->color==DAG_WHITE) {
2240                         itA->node->ancestor_count= node_recurs_level(itA->node, level);
2241                         newlevel= MAX2(newlevel, level+itA->node->ancestor_count);
2242                 }
2243                 else
2244                         newlevel= MAX2(newlevel, level+itA->node->ancestor_count);
2245         }
2246         
2247         return newlevel;
2248 }
2249
2250 static void pose_check_cycle(DagForest *dag)
2251 {
2252         DagNode *node;
2253         DagAdjList *itA;
2254
2255         /* tag nodes unchecked */
2256         for(node = dag->DagNode.first; node; node= node->next)
2257                 node->color= DAG_WHITE;
2258         
2259         for(node = dag->DagNode.first; node; node= node->next) {
2260                 if(node->color==DAG_WHITE) {
2261                         node->ancestor_count= node_recurs_level(node, 0);
2262                 }
2263         }
2264         
2265         /* check relations, and print errors */
2266         for(node = dag->DagNode.first; node; node= node->next) {
2267                 for(itA= node->parent; itA; itA= itA->next) {
2268                         if(itA->node->ancestor_count > node->ancestor_count) {
2269                                 bPoseChannel *pchan= (bPoseChannel *)node->ob;
2270                                 bPoseChannel *parchan= (bPoseChannel *)itA->node->ob;
2271                                 
2272                                 if(pchan && parchan)  {
2273                                         printf("Cycle detected:\n");
2274                                         dag_node_print_dependency_cycle(dag, itA->node, node, itA->name);
2275                                 }
2276                         }
2277                 }
2278         }
2279 }
2280
2281 /* we assume its an armature with pose */
2282 void DAG_pose_sort(Object *ob)
2283 {
2284         bPose *pose= ob->pose;
2285         bPoseChannel *pchan;
2286         bConstraint *con;
2287         DagNode *node;
2288         DagNode *node2, *node3;
2289         DagNode *rootnode;
2290         DagForest *dag;
2291         DagNodeQueue *nqueue;
2292         DagAdjList *itA;
2293         ListBase tempbase;
2294         int skip = 0;
2295         
2296         dag = dag_init();
2297         ugly_hack_sorry= 0;     // no ID structs
2298
2299         rootnode = dag_add_node(dag, NULL);     // node->ob becomes NULL
2300         
2301         /* we add the hierarchy and the constraints */
2302         for(pchan = pose->chanbase.first; pchan; pchan= pchan->next) {
2303                 int addtoroot = 1;
2304                 
2305                 node = dag_get_node(dag, pchan);
2306                 
2307                 if(pchan->parent) {
2308                         node2 = dag_get_node(dag, pchan->parent);
2309                         dag_add_relation(dag, node2, node, 0, "Parent Relation");
2310                         dag_add_parent_relation(dag, node2, node, 0, "Parent Relation");
2311                         addtoroot = 0;
2312                 }
2313                 for (con = pchan->constraints.first; con; con=con->next) {
2314                         bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
2315                         ListBase targets = {NULL, NULL};
2316                         bConstraintTarget *ct;
2317                         
2318                         if(con->ipo) {
2319                                 IpoCurve *icu;
2320                                 for(icu= con->ipo->curve.first; icu; icu= icu->next) {
2321                                         /* icu->driver->ob should actually point to ob->proxy if it
2322                                          * is a proxy, but since it wasn't set correct it older
2323                                          * files comparing with ob->proxy makes it work for those */
2324                                         if(icu->driver && (icu->driver->ob==ob || icu->driver->ob==ob->proxy)) {
2325                                                 bPoseChannel *target= get_pose_channel(ob->pose, icu->driver->name);
2326                                                 if(target) {
2327                                                         node2 = dag_get_node(dag, target);
2328                                                         dag_add_relation(dag, node2, node, 0, "Ipo Driver");
2329                                                         dag_add_parent_relation(dag, node2, node, 0, "Ipo Driver");
2330
2331                                                         /* uncommented this line, results in dependencies
2332                                                          * not being added properly for this constraint,
2333                                                          * what is the purpose of this? - brecht */
2334                                                         /*cti= NULL;*/  /* trick to get next loop skipped */
2335                                                 }
2336                                         }
2337                                 }
2338                         }
2339                         
2340                         if (cti && cti->get_constraint_targets) {
2341                                 cti->get_constraint_targets(con, &targets);
2342                                 
2343                                 for (ct= targets.first; ct; ct= ct->next) {
2344                                         if (ct->tar==ob && ct->subtarget[0]) {
2345                                                 bPoseChannel *target= get_pose_channel(ob->pose, ct->subtarget);
2346                                                 if (target) {
2347                                                         node2= dag_get_node(dag, target);
2348                                                         dag_add_relation(dag, node2, node, 0, "IK Constraint");
2349                                                         dag_add_parent_relation(dag, node2, node, 0, "IK Constraint");
2350
2351                                                         if (con->type==CONSTRAINT_TYPE_KINEMATIC) {
2352                                                                 bKinematicConstraint *data = (bKinematicConstraint *)con->data;
2353                                                                 bPoseChannel *parchan;
2354                                                                 int segcount= 0;
2355                                                                 
2356                                                                 /* exclude tip from chain? */
2357                                                                 if(!(data->flag & CONSTRAINT_IK_TIP))
2358                                                                         parchan= pchan->parent;
2359                                                                 else
2360                                                                         parchan= pchan;
2361                                                                 
2362                                                                 /* Walk to the chain's root */
2363                                                                 while (parchan) {
2364                                                                         node3= dag_get_node(dag, parchan);
2365                                                                         dag_add_relation(dag, node2, node3, 0, "IK Constraint");
2366                                                                         dag_add_parent_relation(dag, node2, node3, 0, "IK Constraint");
2367                                                                         
2368                                                                         segcount++;
2369                                                                         if (segcount==data->rootbone || segcount>255) break; // 255 is weak
2370                                                                         parchan= parchan->parent;
2371                                                                 }
2372                                                         }
2373                                                 }
2374                                         }
2375                                 }
2376                                 
2377                                 if (cti->flush_constraint_targets)
2378                                         cti->flush_constraint_targets(con, &targets, 1);
2379                         }
2380                 }
2381                 if (addtoroot == 1 ) {
2382                         dag_add_relation(dag, rootnode, node, 0, "Root Bone Relation");
2383                         dag_add_parent_relation(dag, rootnode, node, 0, "Root Bone Relation");
2384                 }
2385         }
2386
2387         pose_check_cycle(dag);
2388         
2389         /* now we try to sort... */
2390         tempbase.first= tempbase.last= NULL;
2391
2392         nqueue = queue_create(DAGQUEUEALLOC);
2393         
2394         /* tag nodes unchecked */
2395         for(node = dag->DagNode.first; node; node= node->next) 
2396                 node->color = DAG_WHITE;
2397         
2398         node = dag->DagNode.first;
2399         
2400         node->color = DAG_GRAY;
2401         push_stack(nqueue, node);  
2402         
2403         while(nqueue->count) {
2404                 
2405                 skip = 0;
2406                 node = get_top_node_queue(nqueue);
2407                 
2408                 itA = node->child;
2409                 while(itA != NULL) {
2410                         if((itA->node->color == DAG_WHITE) ) {
2411                                 itA->node->color = DAG_GRAY;
2412                                 push_stack(nqueue,itA->node);
2413                                 skip = 1;
2414                                 break;
2415                         } 
2416                         itA = itA->next;
2417                 }                       
2418                 
2419                 if (!skip) {
2420                         if (node) {
2421                                 node = pop_queue(nqueue);
2422                                 if (node->ob == NULL)   // we are done
2423                                         break ;
2424                                 node->color = DAG_BLACK;
2425                                 
2426                                 /* put node in new list */
2427                                 BLI_remlink(&pose->chanbase, node->ob);
2428                                 BLI_addhead(&tempbase, node->ob);
2429                         }       
2430                 }
2431         }
2432         
2433         // temporal correction for circular dependancies
2434         while(pose->chanbase.first) {
2435                 pchan= pose->chanbase.first;
2436                 BLI_remlink(&pose->chanbase, pchan);
2437                 BLI_addhead(&tempbase, pchan);
2438
2439                 printf("cyclic %s\n", pchan->name);
2440         }
2441         
2442         pose->chanbase = tempbase;
2443         queue_delete(nqueue);
2444         
2445 //      printf("\nordered\n");
2446 //      for(pchan = pose->chanbase.first; pchan; pchan= pchan->next) {
2447 //              printf(" %s\n", pchan->name);
2448 //      }
2449         
2450         free_forest( dag );
2451         MEM_freeN( dag );
2452         
2453         ugly_hack_sorry= 1;
2454 }
2455
2456
2457