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