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