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