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