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