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