Bugfix #8736: Follow Path constraints does not work for Bones
[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                 GroupObject *go;
597
598                 for(; psys; psys=psys->next) {
599                         ParticleSettings *part= psys->part;
600                         
601                         dag_add_relation(dag, node, node, DAG_RL_OB_DATA);
602
603                         if(part->phystype==PART_PHYS_KEYED && psys->keyed_ob &&
604                            BLI_findlink(&psys->keyed_ob->particlesystem,psys->keyed_psys-1)) {
605                                 node2 = dag_get_node(dag, psys->keyed_ob);
606                                 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA);
607                         }
608
609                         if(part->draw_as == PART_DRAW_OB && part->dup_ob) {
610                                 node2 = dag_get_node(dag, part->dup_ob);
611                                 dag_add_relation(dag, node, node2, DAG_RL_OB_OB);
612                         }
613
614                         if(part->draw_as == PART_DRAW_GR && part->dup_group) {
615                                 for(go=part->dup_group->gobject.first; go; go=go->next) {
616                                         node2 = dag_get_node(dag, go->ob);
617                                         dag_add_relation(dag, node, node2, DAG_RL_OB_OB);
618                                 }
619                         }
620
621                         if(psys->effectors.first)
622                                 psys_end_effectors(psys);
623                         psys_init_effectors(ob,psys->part->eff_group,psys);
624
625                         if(psys->effectors.first) {
626                                 for(nec= psys->effectors.first; nec; nec= nec->next) {
627                                         Object *ob1= nec->ob;
628
629                                         if(nec->type & PSYS_EC_EFFECTOR) {
630                                                 node2 = dag_get_node(dag, ob1);
631                                                 if(ob1->pd->forcefield==PFIELD_GUIDE)
632                                                         dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA);
633                                                 else
634                                                         dag_add_relation(dag, node2, node, DAG_RL_OB_DATA);
635                                         }
636                                         else if(nec->type & PSYS_EC_DEFLECT) {
637                                                 node2 = dag_get_node(dag, ob1);
638                                                 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA);
639                                         }
640                                         else if(nec->type & PSYS_EC_PARTICLE) {
641                                                 node2 = dag_get_node(dag, ob1);
642                                                 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA);
643                                         }
644                                         
645                                         if(nec->type & PSYS_EC_REACTOR) {
646                                                 node2 = dag_get_node(dag, ob1);
647                                                 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA);
648                                         }
649                                 }
650                         }
651                 }
652         }
653         
654         for (con = ob->constraints.first; con; con=con->next) {
655                 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
656                 ListBase targets = {NULL, NULL};
657                 bConstraintTarget *ct;
658                 
659                 if (cti && cti->get_constraint_targets) {
660                         cti->get_constraint_targets(con, &targets);
661                         
662                         for (ct= targets.first; ct; ct= ct->next) {
663                                 Object *obt;
664                                 
665                                 if (ct->tar)
666                                         obt= ct->tar;
667                                 else
668                                         continue;
669                                 
670                                 node2 = dag_get_node(dag, obt);
671                                 if (ELEM(con->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO))
672                                         dag_add_relation(dag, node2, node, DAG_RL_DATA_OB|DAG_RL_OB_OB);
673                                 else {
674                                         if (ELEM3(obt->type, OB_ARMATURE, OB_MESH, OB_LATTICE) && (ct->subtarget[0]))
675                                                 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB|DAG_RL_OB_OB);
676                                         else
677                                                 dag_add_relation(dag, node2, node, DAG_RL_OB_OB);
678                                 }
679                                 addtoroot = 0;
680                         }
681                         
682                         if (cti->flush_constraint_targets)
683                                 cti->flush_constraint_targets(con, &targets, 1);
684                 }
685         }
686
687         if (addtoroot == 1 )
688                 dag_add_relation(dag,scenenode,node,DAG_RL_SCENE);
689 }
690
691 struct DagForest *build_dag(struct Scene *sce, short mask) 
692 {
693         Base *base;
694         Object *ob;
695         Group *group;
696         GroupObject *go;
697         DagNode *node;
698         DagNode *scenenode;
699         DagForest *dag;
700         DagAdjList *itA;
701
702         dag = sce->theDag;
703         sce->dagisvalid=1;
704         if ( dag)
705                 free_forest( dag ); 
706         else {
707                 dag = dag_init();
708                 sce->theDag = dag;
709         }
710         
711         /* add base node for scene. scene is always the first node in DAG */
712         scenenode = dag_add_node(dag, sce);     
713         
714         /* add current scene objects */
715         for(base = sce->base.first; base; base= base->next) {
716                 ob= base->object;
717                 
718                 build_dag_object(dag, scenenode, ob, mask);
719                 if(ob->proxy)
720                         build_dag_object(dag, scenenode, ob->proxy, mask);
721                 
722                 /* handled in next loop */
723                 if(ob->dup_group) 
724                         ob->dup_group->id.flag |= LIB_DOIT;
725         }
726         
727         /* add groups used in current scene objects */
728         for(group= G.main->group.first; group; group= group->id.next) {
729                 if(group->id.flag & LIB_DOIT) {
730                         for(go= group->gobject.first; go; go= go->next) {
731                                 build_dag_object(dag, scenenode, go->ob, mask);
732                         }
733                         group->id.flag &= ~LIB_DOIT;
734                 }
735         }
736         
737         /* Now all relations were built, but we need to solve 1 exceptional case;
738            When objects have multiple "parents" (for example parent + constraint working on same object)
739            the relation type has to be synced. One of the parents can change, and should give same event to child */
740         
741         /* nodes were callocced, so we can use node->color for temporal storage */
742         for(node = sce->theDag->DagNode.first; node; node= node->next) {
743                 if(node->type==ID_OB) {
744                         for(itA = node->child; itA; itA= itA->next) {
745                                 if(itA->node->type==ID_OB) {
746                                         itA->node->color |= itA->type;
747                                 }
748                         }
749                 }
750         }
751         /* now set relations equal, so that when only one parent changes, the correct recalcs are found */
752         for(node = sce->theDag->DagNode.first; node; node= node->next) {
753                 if(node->type==ID_OB) {
754                         for(itA = node->child; itA; itA= itA->next) {
755                                 if(itA->node->type==ID_OB) {
756                                         itA->type |= itA->node->color;
757                                 }
758                         }
759                 }
760         }
761         
762         // cycle detection and solving
763         // solve_cycles(dag);   
764         
765         return dag;
766 }
767
768
769 void free_forest(DagForest *Dag) 
770 {  /* remove all nodes and deps */
771         DagNode *tempN;
772         DagAdjList *tempA;      
773         DagAdjList *itA;
774         DagNode *itN = Dag->DagNode.first;
775         
776         while (itN) {
777                 itA = itN->child;       
778                 while (itA) {
779                         tempA = itA;
780                         itA = itA->next;
781                         MEM_freeN(tempA);                       
782                 }
783                 
784                 itA = itN->parent;      
785                 while (itA) {
786                         tempA = itA;
787                         itA = itA->next;
788                         MEM_freeN(tempA);                       
789                 }
790                 
791                 tempN = itN;
792                 itN = itN->next;
793                 MEM_freeN(tempN);
794         }
795         Dag->DagNode.first = NULL;
796         Dag->DagNode.last = NULL;
797         Dag->numNodes = 0;
798
799 }
800
801 DagNode * dag_find_node (DagForest *forest,void * fob)
802 {
803         DagNode *node = forest->DagNode.first;
804         
805         while (node) {
806                 if (node->ob == fob)
807                         return node;
808                 node = node->next;
809         }
810         return NULL;
811 }
812
813 static int ugly_hack_sorry= 1;  // prevent type check
814
815 /* no checking of existance, use dag_find_node first or dag_get_node */
816 DagNode * dag_add_node (DagForest *forest, void * fob)
817 {
818         DagNode *node;
819                 
820         node = MEM_callocN(sizeof(DagNode),"DAG node");
821         if (node) {
822                 node->ob = fob;
823                 node->color = DAG_WHITE;
824
825                 if(ugly_hack_sorry) node->type = GS(((ID *) fob)->name);        // sorry, done for pose sorting
826                 if (forest->numNodes) {
827                         ((DagNode *) forest->DagNode.last)->next = node;
828                         forest->DagNode.last = node;
829                         forest->numNodes++;
830                 } else {
831                         forest->DagNode.last = node;
832                         forest->DagNode.first = node;
833                         forest->numNodes = 1;
834                 }
835         }
836         return node;
837 }
838
839 DagNode * dag_get_node (DagForest *forest,void * fob)
840 {
841         DagNode *node;
842         
843         node = dag_find_node (forest, fob);     
844         if (!node) 
845                 node = dag_add_node(forest, fob);
846         return node;
847 }
848
849
850
851 DagNode * dag_get_sub_node (DagForest *forest,void * fob)
852 {
853         DagNode *node;
854         DagAdjList *mainchild, *prev=NULL;
855         
856         mainchild = ((DagNode *) forest->DagNode.first)->child;
857         /* remove from first node (scene) adj list if present */
858         while (mainchild) {
859                 if (mainchild->node == fob) {
860                         if (prev) {
861                                 prev->next = mainchild->next;
862                                 MEM_freeN(mainchild);
863                                 break;
864                         } else {
865                                 ((DagNode *) forest->DagNode.first)->child = mainchild->next;
866                                 MEM_freeN(mainchild);
867                                 break;
868                         }
869                 }
870                 prev = mainchild;
871                 mainchild = mainchild->next;
872         }
873         node = dag_find_node (forest, fob);     
874         if (!node) 
875                 node = dag_add_node(forest, fob);
876         return node;
877 }
878
879 void dag_add_relation(DagForest *forest, DagNode *fob1, DagNode *fob2, short rel) 
880 {
881         DagAdjList *itA = fob1->child;
882         
883         while (itA) { /* search if relation exist already */
884                 if (itA->node == fob2) {
885                         itA->type |= rel;
886                         itA->count += 1;
887                         return;
888                 }
889                 itA = itA->next;
890         }
891         /* create new relation and insert at head. MALLOC alert! */
892         itA = MEM_mallocN(sizeof(DagAdjList),"DAG adj list");
893         itA->node = fob2;
894         itA->type = rel;
895         itA->count = 1;
896         itA->next = fob1->child;
897         fob1->child = itA;
898 }
899
900 static void dag_add_parent_relation(DagForest *forest, DagNode *fob1, DagNode *fob2, short rel) 
901 {
902         DagAdjList *itA = fob2->parent;
903         
904         while (itA) { /* search if relation exist already */
905                 if (itA->node == fob1) {
906                         itA->type |= rel;
907                         itA->count += 1;
908                         return;
909                 }
910                 itA = itA->next;
911         }
912         /* create new relation and insert at head. MALLOC alert! */
913         itA = MEM_mallocN(sizeof(DagAdjList),"DAG adj list");
914         itA->node = fob1;
915         itA->type = rel;
916         itA->count = 1;
917         itA->next = fob2->parent;
918         fob2->parent = itA;
919 }
920
921
922 /*
923  * MainDAG is the DAG of all objects in current scene
924  * used only for drawing there is one also in each scene
925  */
926 static DagForest * MainDag = NULL;
927
928 DagForest *getMainDag(void)
929 {
930         return MainDag;
931 }
932
933
934 void setMainDag(DagForest *dag)
935 {
936         MainDag = dag;
937 }
938
939
940 /*
941  * note for BFS/DFS
942  * in theory we should sweep the whole array
943  * but in our case the first node is the scene
944  * and is linked to every other object
945  *
946  * for general case we will need to add outer loop
947  */
948
949 /*
950  * ToDo : change pos kludge
951  */
952
953 /* adjust levels for drawing in oops space */
954 void graph_bfs(void)
955 {
956         DagNode *node;
957         DagNodeQueue *nqueue;
958         int pos[50];
959         int i;
960         DagAdjList *itA;
961         int minheight;
962         
963         /* fprintf(stderr,"starting BFS \n ------------\n"); */ 
964         nqueue = queue_create(DAGQUEUEALLOC);
965         for ( i=0; i<50; i++)
966                 pos[i] = 0;
967         
968         /* Init
969          * dagnode.first is alway the root (scene) 
970          */
971         node = MainDag->DagNode.first;
972         while(node) {
973                 node->color = DAG_WHITE;
974                 node->BFS_dist = 9999;
975                 node->k = 0;
976                 node = node->next;
977         }
978         
979         node = MainDag->DagNode.first;
980         if (node->color == DAG_WHITE) {
981                 node->color = DAG_GRAY;
982                 node->BFS_dist = 1;
983                 push_queue(nqueue,node);  
984                 while(nqueue->count) {
985                         node = pop_queue(nqueue);
986                         
987                         minheight = pos[node->BFS_dist];
988                         itA = node->child;
989                         while(itA != NULL) {
990                                 if((itA->node->color == DAG_WHITE) ) {
991                                         itA->node->color = DAG_GRAY;
992                                         itA->node->BFS_dist = node->BFS_dist + 1;
993                                         itA->node->k = (float) minheight;
994                                         push_queue(nqueue,itA->node);
995                                 }
996                                 
997                                  else {
998                                         fprintf(stderr,"bfs not dag tree edge color :%i \n",itA->node->color);
999                                 }
1000                                  
1001                                 
1002                                 itA = itA->next;
1003                         }
1004                         if (pos[node->BFS_dist] > node->k ) {
1005                                 pos[node->BFS_dist] += 1;                               
1006                                 node->k = (float) pos[node->BFS_dist];
1007                         } else {
1008                                 pos[node->BFS_dist] = (int) node->k +1;
1009                         }
1010                         set_node_xy(node, node->BFS_dist*DEPSX*2, pos[node->BFS_dist]*DEPSY*2);
1011                         node->color = DAG_BLACK;
1012                         /*
1013                          fprintf(stderr,"BFS node : %20s %i %5.0f %5.0f\n",((ID *) node->ob)->name,node->BFS_dist, node->x, node->y);   
1014                          */
1015                 }
1016         }
1017         queue_delete(nqueue);
1018 }
1019
1020 int pre_and_post_BFS(DagForest *dag, short mask, graph_action_func pre_func, graph_action_func post_func, void **data) {
1021         DagNode *node;
1022         
1023         node = dag->DagNode.first;
1024         return pre_and_post_source_BFS(dag, mask,  node,  pre_func,  post_func, data);
1025 }
1026
1027
1028 int pre_and_post_source_BFS(DagForest *dag, short mask, DagNode *source, graph_action_func pre_func, graph_action_func post_func, void **data)
1029 {
1030         DagNode *node;
1031         DagNodeQueue *nqueue;
1032         DagAdjList *itA;
1033         int     retval = 0;
1034         /* fprintf(stderr,"starting BFS \n ------------\n"); */ 
1035         
1036         /* Init
1037                 * dagnode.first is alway the root (scene) 
1038                 */
1039         node = dag->DagNode.first;
1040         nqueue = queue_create(DAGQUEUEALLOC);
1041         while(node) {
1042                 node->color = DAG_WHITE;
1043                 node->BFS_dist = 9999;
1044                 node = node->next;
1045         }
1046         
1047         node = source;
1048         if (node->color == DAG_WHITE) {
1049                 node->color = DAG_GRAY;
1050                 node->BFS_dist = 1;
1051                 pre_func(node->ob,data);
1052                 
1053                 while(nqueue->count) {
1054                         node = pop_queue(nqueue);
1055                         
1056                         itA = node->child;
1057                         while(itA != NULL) {
1058                                 if((itA->node->color == DAG_WHITE) && (itA->type & mask)) {
1059                                         itA->node->color = DAG_GRAY;
1060                                         itA->node->BFS_dist = node->BFS_dist + 1;
1061                                         push_queue(nqueue,itA->node);
1062                                         pre_func(node->ob,data);
1063                                 }
1064                                 
1065                                 else { // back or cross edge
1066                                         retval = 1;
1067                                 }
1068                                 itA = itA->next;
1069                         }
1070                         post_func(node->ob,data);
1071                         node->color = DAG_BLACK;
1072                         /*
1073                          fprintf(stderr,"BFS node : %20s %i %5.0f %5.0f\n",((ID *) node->ob)->name,node->BFS_dist, node->x, node->y);   
1074                          */
1075                 }
1076         }
1077         queue_delete(nqueue);
1078         return retval;
1079 }
1080
1081 /* non recursive version of DFS, return queue -- outer loop present to catch odd cases (first level cycles)*/
1082 DagNodeQueue * graph_dfs(void)
1083 {
1084         DagNode *node;
1085         DagNodeQueue *nqueue;
1086         DagNodeQueue *retqueue;
1087         int pos[50];
1088         int i;
1089         DagAdjList *itA;
1090         int time;
1091         int skip = 0;
1092         int minheight;
1093         int maxpos=0;
1094         int     is_cycle = 0;
1095         /*
1096          *fprintf(stderr,"starting DFS \n ------------\n");
1097          */     
1098         nqueue = queue_create(DAGQUEUEALLOC);
1099         retqueue = queue_create(MainDag->numNodes);
1100         for ( i=0; i<50; i++)
1101                 pos[i] = 0;
1102         
1103         /* Init
1104          * dagnode.first is alway the root (scene) 
1105          */
1106         node = MainDag->DagNode.first;
1107         while(node) {
1108                 node->color = DAG_WHITE;
1109                 node->DFS_dist = 9999;
1110                 node->DFS_dvtm = node->DFS_fntm = 9999;
1111                 node->k = 0;
1112                 node =  node->next;
1113         }
1114         
1115         time = 1;
1116         
1117         node = MainDag->DagNode.first;
1118
1119         do {
1120         if (node->color == DAG_WHITE) {
1121                 node->color = DAG_GRAY;
1122                 node->DFS_dist = 1;
1123                 node->DFS_dvtm = time;
1124                 time++;
1125                 push_stack(nqueue,node);  
1126                         
1127                 while(nqueue->count) {
1128                         //graph_print_queue(nqueue);
1129
1130                          skip = 0;
1131                         node = get_top_node_queue(nqueue);
1132                         
1133                         minheight = pos[node->DFS_dist];
1134
1135                         itA = node->child;
1136                         while(itA != NULL) {
1137                                 if((itA->node->color == DAG_WHITE) ) {
1138                                         itA->node->DFS_dvtm = time;
1139                                         itA->node->color = DAG_GRAY;
1140
1141                                         time++;
1142                                         itA->node->DFS_dist = node->DFS_dist + 1;
1143                                         itA->node->k = (float) minheight;
1144                                         push_stack(nqueue,itA->node);
1145                                         skip = 1;
1146                                         break;
1147                                 } else { 
1148                                         if (itA->node->color == DAG_GRAY) { // back edge
1149                                                 fprintf(stderr,"dfs back edge :%15s %15s \n",((ID *) node->ob)->name, ((ID *) itA->node->ob)->name);
1150                                                 is_cycle = 1;
1151                                         } else if (itA->node->color == DAG_BLACK) {
1152                                                 ;
1153                                                 /* already processed node but we may want later to change distance either to shorter to longer.
1154                                                  * DFS_dist is the first encounter  
1155                                                 */
1156                                                 /*if (node->DFS_dist >= itA->node->DFS_dist)
1157                                                         itA->node->DFS_dist = node->DFS_dist + 1;
1158                                                         
1159                                                         fprintf(stderr,"dfs forward or cross edge :%15s %i-%i %15s %i-%i \n",
1160                                                                 ((ID *) node->ob)->name,
1161                                                                 node->DFS_dvtm, 
1162                                                                 node->DFS_fntm, 
1163                                                                 ((ID *) itA->node->ob)->name, 
1164                                                                 itA->node->DFS_dvtm,
1165                                                                 itA->node->DFS_fntm);
1166                                         */
1167                                         } else 
1168                                                 fprintf(stderr,"dfs unknown edge \n");
1169                                 }
1170                                 itA = itA->next;
1171                         }                       
1172
1173                         if (!skip) {
1174                                 node = pop_queue(nqueue);
1175                                 node->color = DAG_BLACK;
1176
1177                                 node->DFS_fntm = time;
1178                                 time++;
1179                                 if (node->DFS_dist > maxpos)
1180                                         maxpos = node->DFS_dist;
1181                                 if (pos[node->DFS_dist] > node->k ) {
1182                                         pos[node->DFS_dist] += 1;                               
1183                                         node->k = (float) pos[node->DFS_dist];
1184                                 } else {
1185                                         pos[node->DFS_dist] = (int) node->k +1;
1186                                 }
1187                                 set_node_xy(node, node->DFS_dist*DEPSX*2, pos[node->DFS_dist]*DEPSY*2);
1188                                 
1189                                 /*
1190                                  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 );       
1191                                 */
1192                                  push_stack(retqueue,node);
1193                                 
1194                         }
1195                 }
1196         }
1197                 node = node->next;
1198         } while (node);
1199 //        fprintf(stderr,"i size : %i \n", maxpos);
1200           
1201         queue_delete(nqueue);
1202           return(retqueue);
1203 }
1204
1205 /* unused */
1206 int pre_and_post_DFS(DagForest *dag, short mask, graph_action_func pre_func, graph_action_func post_func, void **data) {
1207         DagNode *node;
1208
1209         node = dag->DagNode.first;
1210         return pre_and_post_source_DFS(dag, mask,  node,  pre_func,  post_func, data);
1211 }
1212
1213 int pre_and_post_source_DFS(DagForest *dag, short mask, DagNode *source, graph_action_func pre_func, graph_action_func post_func, void **data)
1214 {
1215         DagNode *node;
1216         DagNodeQueue *nqueue;
1217         DagAdjList *itA;
1218         int time;
1219         int skip = 0;
1220         int retval = 0;
1221         /*
1222          *fprintf(stderr,"starting DFS \n ------------\n");
1223          */     
1224         nqueue = queue_create(DAGQUEUEALLOC);
1225         
1226         /* Init
1227                 * dagnode.first is alway the root (scene) 
1228                 */
1229         node = dag->DagNode.first;
1230         while(node) {
1231                 node->color = DAG_WHITE;
1232                 node->DFS_dist = 9999;
1233                 node->DFS_dvtm = node->DFS_fntm = 9999;
1234                 node->k = 0;
1235                 node =  node->next;
1236         }
1237         
1238         time = 1;
1239         
1240         node = source;
1241         do {
1242                 if (node->color == DAG_WHITE) {
1243                         node->color = DAG_GRAY;
1244                         node->DFS_dist = 1;
1245                         node->DFS_dvtm = time;
1246                         time++;
1247                         push_stack(nqueue,node);  
1248                         pre_func(node->ob,data);
1249
1250                         while(nqueue->count) {
1251                                 skip = 0;
1252                                 node = get_top_node_queue(nqueue);
1253                                                                 
1254                                 itA = node->child;
1255                                 while(itA != NULL) {
1256                                         if((itA->node->color == DAG_WHITE) && (itA->type & mask) ) {
1257                                                 itA->node->DFS_dvtm = time;
1258                                                 itA->node->color = DAG_GRAY;
1259                                                 
1260                                                 time++;
1261                                                 itA->node->DFS_dist = node->DFS_dist + 1;
1262                                                 push_stack(nqueue,itA->node);
1263                                                 pre_func(node->ob,data);
1264
1265                                                 skip = 1;
1266                                                 break;
1267                                         } else {
1268                                                 if (itA->node->color == DAG_GRAY) {// back edge
1269                                                         retval = 1;
1270                                                 }
1271 //                                              else if (itA->node->color == DAG_BLACK) { // cross or forward
1272 //                                                      ;
1273                                         }
1274                                         itA = itA->next;
1275                                 }                       
1276                                 
1277                                 if (!skip) {
1278                                         node = pop_queue(nqueue);
1279                                         node->color = DAG_BLACK;
1280                                         
1281                                         node->DFS_fntm = time;
1282                                         time++;
1283                                         post_func(node->ob,data);
1284                                 }
1285                         }
1286                 }
1287                 node = node->next;
1288         } while (node);
1289         queue_delete(nqueue);
1290         return(retval);
1291 }
1292
1293
1294 // used to get the obs owning a datablock
1295 struct DagNodeQueue *get_obparents(struct DagForest     *dag, void *ob) 
1296 {
1297         DagNode * node, *node1;
1298         DagNodeQueue *nqueue;
1299         DagAdjList *itA;
1300
1301         node = dag_find_node(dag,ob);
1302         if(node==NULL) {
1303                 return NULL;
1304         }
1305         else if (node->ancestor_count == 1) { // simple case
1306                 nqueue = queue_create(1);
1307                 push_queue(nqueue,node);
1308         } else {        // need to go over the whole dag for adj list
1309                 nqueue = queue_create(node->ancestor_count);
1310                 
1311                 node1 = dag->DagNode.first;
1312                 do {
1313                         if (node1->DFS_fntm > node->DFS_fntm) { // a parent is finished after child. must check adj list
1314                                 itA = node->child;
1315                                 while(itA != NULL) {
1316                                         if ((itA->node == node) && (itA->type == DAG_RL_DATA)) {
1317                                                 push_queue(nqueue,node);
1318                                         }
1319                                         itA = itA->next;
1320                                 }
1321                         }
1322                         node1 = node1->next;
1323                 } while (node1);
1324         }
1325         return nqueue;
1326 }
1327
1328 struct DagNodeQueue *get_first_ancestors(struct DagForest       *dag, void *ob)
1329 {
1330         DagNode * node, *node1;
1331         DagNodeQueue *nqueue;
1332         DagAdjList *itA;
1333         
1334         node = dag_find_node(dag,ob);
1335         
1336         // need to go over the whole dag for adj list
1337         nqueue = queue_create(node->ancestor_count);
1338         
1339         node1 = dag->DagNode.first;
1340         do {
1341                 if (node1->DFS_fntm > node->DFS_fntm) { 
1342                         itA = node->child;
1343                         while(itA != NULL) {
1344                                 if (itA->node == node) {
1345                                         push_queue(nqueue,node);
1346                                 }
1347                                 itA = itA->next;
1348                         }
1349                 }
1350                 node1 = node1->next;
1351         } while (node1);
1352         
1353         return nqueue;  
1354 }
1355
1356 // standard DFS list
1357 struct DagNodeQueue *get_all_childs(struct DagForest    *dag, void *ob)
1358 {
1359         DagNode *node;
1360         DagNodeQueue *nqueue;
1361         DagNodeQueue *retqueue;
1362         DagAdjList *itA;
1363         int time;
1364         int skip = 0;
1365
1366         nqueue = queue_create(DAGQUEUEALLOC);
1367         retqueue = queue_create(dag->numNodes); // was MainDag... why? (ton)
1368         
1369         node = dag->DagNode.first;
1370         while(node) {
1371                 node->color = DAG_WHITE;
1372                 node =  node->next;
1373         }
1374         
1375         time = 1;
1376         
1377         node = dag_find_node(dag, ob);   // could be done in loop above (ton)
1378         if(node) { // can be null for newly added objects
1379                 
1380                 node->color = DAG_GRAY;
1381                 time++;
1382                 push_stack(nqueue,node);  
1383                 
1384                 while(nqueue->count) {
1385                         
1386                         skip = 0;
1387                         node = get_top_node_queue(nqueue);
1388                                         
1389                         itA = node->child;
1390                         while(itA != NULL) {
1391                                 if((itA->node->color == DAG_WHITE) ) {
1392                                         itA->node->DFS_dvtm = time;
1393                                         itA->node->color = DAG_GRAY;
1394                                         
1395                                         time++;
1396                                         push_stack(nqueue,itA->node);
1397                                         skip = 1;
1398                                         break;
1399                                 } 
1400                                 itA = itA->next;
1401                         }                       
1402                         
1403                         if (!skip) {
1404                                 node = pop_queue(nqueue);
1405                                 node->color = DAG_BLACK;
1406                                 
1407                                 time++;
1408                                 push_stack(retqueue,node);                      
1409                         }
1410                 }
1411         }
1412         queue_delete(nqueue);
1413         return(retqueue);
1414 }
1415
1416 /* unused */
1417 short   are_obs_related(struct DagForest        *dag, void *ob1, void *ob2) {
1418         DagNode * node;
1419         DagAdjList *itA;
1420         
1421         node = dag_find_node(dag, ob1);
1422         
1423         itA = node->child;
1424         while(itA != NULL) {
1425                 if((itA->node->ob == ob2) ) {
1426                         return itA->node->type;
1427                 } 
1428                 itA = itA->next;
1429         }
1430         return DAG_NO_RELATION;
1431 }
1432
1433 int     is_acyclic( DagForest   *dag) {
1434         return dag->is_acyclic;
1435 }
1436
1437 void set_node_xy(DagNode *node, float x, float y)
1438 {
1439         node->x = x;
1440         node->y = y;
1441 }
1442
1443
1444 /* debug test functions */
1445
1446 void graph_print_queue(DagNodeQueue *nqueue)
1447 {       
1448         DagNodeQueueElem *queueElem;
1449         
1450         queueElem = nqueue->first;
1451         while(queueElem) {
1452                 fprintf(stderr,"** %s %i %i-%i ",((ID *) queueElem->node->ob)->name,queueElem->node->color,queueElem->node->DFS_dvtm,queueElem->node->DFS_fntm);
1453                 queueElem = queueElem->next;            
1454         }
1455         fprintf(stderr,"\n");
1456 }
1457
1458 void graph_print_queue_dist(DagNodeQueue *nqueue)
1459 {       
1460         DagNodeQueueElem *queueElem;
1461         int max, count;
1462         
1463         queueElem = nqueue->first;
1464         max = queueElem->node->DFS_fntm;
1465         count = 0;
1466         while(queueElem) {
1467                 fprintf(stderr,"** %25s %2.2i-%2.2i ",((ID *) queueElem->node->ob)->name,queueElem->node->DFS_dvtm,queueElem->node->DFS_fntm);
1468                 while (count < queueElem->node->DFS_dvtm-1) { fputc(' ',stderr); count++;}
1469                 fputc('|',stderr); 
1470                 while (count < queueElem->node->DFS_fntm-2) { fputc('-',stderr); count++;}
1471                 fputc('|',stderr);
1472                 fputc('\n',stderr);
1473                 count = 0;
1474                 queueElem = queueElem->next;            
1475         }
1476         fprintf(stderr,"\n");
1477 }
1478
1479 void graph_print_adj_list(void)
1480 {
1481         DagNode *node;
1482         DagAdjList *itA;
1483         
1484         node = (getMainDag())->DagNode.first;
1485         while(node) {
1486                 fprintf(stderr,"node : %s col: %i",((ID *) node->ob)->name, node->color);               
1487                 itA = node->child;
1488                 while (itA) {
1489                         fprintf(stderr,"-- %s ",((ID *) itA->node->ob)->name);
1490                         
1491                         itA = itA->next;
1492                 }
1493                 fprintf(stderr,"\n");
1494                 node = node->next;
1495         }
1496 }
1497
1498 /* ************************ API *********************** */
1499
1500 /* groups with objects in this scene need to be put in the right order as well */
1501 static void scene_sort_groups(Scene *sce)
1502 {
1503         Base *base;
1504         Group *group;
1505         GroupObject *go;
1506         Object *ob;
1507         
1508         /* test; are group objects all in this scene? */
1509         for(ob= G.main->object.first; ob; ob= ob->id.next) {
1510                 ob->id.flag &= ~LIB_DOIT;
1511                 ob->id.newid= NULL;     /* newid abuse for GroupObject */
1512         }
1513         for(base = sce->base.first; base; base= base->next)
1514                 base->object->id.flag |= LIB_DOIT;
1515         
1516         for(group= G.main->group.first; group; group= group->id.next) {
1517                 for(go= group->gobject.first; go; go= go->next) {
1518                         if((go->ob->id.flag & LIB_DOIT)==0)
1519                                 break;
1520                 }
1521                 /* this group is entirely in this scene */
1522                 if(go==NULL) {
1523                         ListBase listb= {NULL, NULL};
1524                         
1525                         for(go= group->gobject.first; go; go= go->next)
1526                                 go->ob->id.newid= (ID *)go;
1527                         
1528                         /* in order of sorted bases we reinsert group objects */
1529                         for(base = sce->base.first; base; base= base->next) {
1530                                 
1531                                 if(base->object->id.newid) {
1532                                         go= (GroupObject *)base->object->id.newid;
1533                                         base->object->id.newid= NULL;
1534                                         BLI_remlink( &group->gobject, go);
1535                                         BLI_addtail( &listb, go);
1536                                 }
1537                         }
1538                         /* copy the newly sorted listbase */
1539                         group->gobject= listb;
1540                 }
1541         }
1542 }
1543
1544 /* sort the base list on dependency order */
1545 void DAG_scene_sort(struct Scene *sce)
1546 {
1547         DagNode *node;
1548         DagNodeQueue *nqueue;
1549         DagAdjList *itA;
1550         int time;
1551         int skip = 0;
1552         ListBase tempbase;
1553         Base *base;
1554         
1555         tempbase.first= tempbase.last= NULL;
1556         
1557         build_dag(sce, DAG_RL_ALL_BUT_DATA);
1558         
1559         nqueue = queue_create(DAGQUEUEALLOC);
1560         
1561         for(node = sce->theDag->DagNode.first; node; node= node->next) {
1562                 node->color = DAG_WHITE;
1563         }
1564         
1565         time = 1;
1566         
1567         node = sce->theDag->DagNode.first;
1568         
1569         node->color = DAG_GRAY;
1570         time++;
1571         push_stack(nqueue,node);  
1572         
1573         while(nqueue->count) {
1574                 
1575                 skip = 0;
1576                 node = get_top_node_queue(nqueue);
1577                 
1578                 itA = node->child;
1579                 while(itA != NULL) {
1580                         if((itA->node->color == DAG_WHITE) ) {
1581                                 itA->node->DFS_dvtm = time;
1582                                 itA->node->color = DAG_GRAY;
1583                                 
1584                                 time++;
1585                                 push_stack(nqueue,itA->node);
1586                                 skip = 1;
1587                                 break;
1588                         } 
1589                         itA = itA->next;
1590                 }                       
1591                 
1592                 if (!skip) {
1593                         if (node) {
1594                                 node = pop_queue(nqueue);
1595                                 if (node->ob == sce)    // we are done
1596                                         break ;
1597                                 node->color = DAG_BLACK;
1598                                 
1599                                 time++;
1600                                 base = sce->base.first;
1601                                 while (base && base->object != node->ob)
1602                                         base = base->next;
1603                                 if(base) {
1604                                         BLI_remlink(&sce->base,base);
1605                                         BLI_addhead(&tempbase,base);
1606                                 }
1607                         }       
1608                 }
1609         }
1610         
1611         // temporal correction for circular dependancies
1612         base = sce->base.first;
1613         while (base) {
1614                 BLI_remlink(&sce->base,base);
1615                 BLI_addhead(&tempbase,base);
1616                 //if(G.f & G_DEBUG) 
1617                         printf("cyclic %s\n", base->object->id.name);
1618                 base = sce->base.first;
1619         }
1620         
1621         sce->base = tempbase;
1622         queue_delete(nqueue);
1623         
1624         /* all groups with objects in this scene gets resorted too */
1625         scene_sort_groups(sce);
1626         
1627         if(G.f & G_DEBUG) {
1628                 printf("\nordered\n");
1629                 for(base = sce->base.first; base; base= base->next) {
1630                         printf(" %s\n", base->object->id.name);
1631                 }
1632         }
1633         /* temporal...? */
1634         sce->recalc |= SCE_PRV_CHANGED; /* test for 3d preview */
1635 }
1636
1637 /* node was checked to have lasttime != curtime and is if type ID_OB */
1638 static void flush_update_node(DagNode *node, unsigned int layer, int curtime)
1639 {
1640         DagAdjList *itA;
1641         Object *ob, *obc;
1642         int oldflag, changed=0;
1643         unsigned int all_layer;
1644         
1645         node->lasttime= curtime;
1646         
1647         ob= node->ob;
1648         if(ob && (ob->recalc & OB_RECALC)) {
1649                 all_layer= ob->lay;
1650                 /* got an object node that changes, now check relations */
1651                 for(itA = node->child; itA; itA= itA->next) {
1652                         all_layer |= itA->lay;
1653                         /* the relationship is visible */
1654                         if(itA->lay & layer) {
1655                                 if(itA->node->type==ID_OB) {
1656                                         obc= itA->node->ob;
1657                                         oldflag= obc->recalc;
1658                                         
1659                                         /* got a ob->obc relation, now check if flag needs flush */
1660                                         if(ob->recalc & OB_RECALC_OB) {
1661                                                 if(itA->type & DAG_RL_OB_OB) {
1662                                                         //printf("ob %s changes ob %s\n", ob->id.name, obc->id.name);
1663                                                         obc->recalc |= OB_RECALC_OB;
1664                                                 }
1665                                                 if(itA->type & DAG_RL_OB_DATA) {
1666                                                         //printf("ob %s changes obdata %s\n", ob->id.name, obc->id.name);
1667                                                         obc->recalc |= OB_RECALC_DATA;
1668                                                 }
1669                                         }
1670                                         if(ob->recalc & OB_RECALC_DATA) {
1671                                                 if(itA->type & DAG_RL_DATA_OB) {
1672                                                         //printf("obdata %s changes ob %s\n", ob->id.name, obc->id.name);
1673                                                         obc->recalc |= OB_RECALC_OB;
1674                                                 }
1675                                                 if(itA->type & DAG_RL_DATA_DATA) {
1676                                                         //printf("obdata %s changes obdata %s\n", ob->id.name, obc->id.name);
1677                                                         obc->recalc |= OB_RECALC_DATA;
1678                                                 }
1679                                         }
1680                                         if(oldflag!=obc->recalc) changed= 1;
1681                                 }
1682                         }
1683                 }
1684                 /* even nicer, we can clear recalc flags...  */
1685                 if((all_layer & layer)==0) {
1686                         /* but existing displaylists or derivedmesh should be freed */
1687                         if(ob->recalc & OB_RECALC_DATA)
1688                                 object_free_display(ob);
1689                         
1690                         ob->recalc &= ~OB_RECALC;
1691                 }
1692         }
1693         
1694         /* check case where child changes and parent forcing obdata to change */
1695         /* should be done regardless if this ob has recalc set */
1696         /* could merge this in with loop above...? (ton) */
1697         for(itA = node->child; itA; itA= itA->next) {
1698                 /* the relationship is visible */
1699                 if(itA->lay & layer) {
1700                         if(itA->node->type==ID_OB) {
1701                                 obc= itA->node->ob;
1702                                 /* child moves */
1703                                 if((obc->recalc & OB_RECALC)==OB_RECALC_OB) {
1704                                         /* parent has deforming info */
1705                                         if(itA->type & (DAG_RL_OB_DATA|DAG_RL_DATA_DATA)) {
1706                                                 // printf("parent %s changes ob %s\n", ob->id.name, obc->id.name);
1707                                                 obc->recalc |= OB_RECALC_DATA;
1708                                         }
1709                                 }
1710                         }
1711                 }
1712         }
1713         
1714         /* we only go deeper if node not checked or something changed  */
1715         for(itA = node->child; itA; itA= itA->next) {
1716                 if(changed || itA->node->lasttime!=curtime) 
1717                         flush_update_node(itA->node, layer, curtime);
1718         }
1719         
1720 }
1721
1722 /* node was checked to have lasttime != curtime , and is of type ID_OB */
1723 static unsigned int flush_layer_node(DagNode *node, int curtime)
1724 {
1725         DagAdjList *itA;
1726         
1727         node->lasttime= curtime;
1728         node->lay= ((Object *)node->ob)->lay;
1729         
1730         for(itA = node->child; itA; itA= itA->next) {
1731                 if(itA->node->type==ID_OB) {
1732                         if(itA->node->lasttime!=curtime) {
1733                                 itA->lay= flush_layer_node(itA->node, curtime);  // lay is only set once for each relation
1734                                 //printf("layer %d for relation %s to %s\n", itA->lay, ((Object *)node->ob)->id.name, ((Object *)itA->node->ob)->id.name);
1735                         }
1736                         else itA->lay= itA->node->lay;
1737                         
1738                         node->lay |= itA->lay;
1739                 }
1740         }
1741
1742         return node->lay;
1743 }
1744
1745 /* flushes all recalc flags in objects down the dependency tree */
1746 void DAG_scene_flush_update(Scene *sce, unsigned int lay)
1747 {
1748         DagNode *firstnode;
1749         DagAdjList *itA;
1750         int lasttime;
1751         
1752         if(sce->theDag==NULL) {
1753                 printf("DAG zero... not allowed to happen!\n");
1754                 DAG_scene_sort(sce);
1755         }
1756         
1757         firstnode= sce->theDag->DagNode.first;  // always scene node
1758         
1759         /* first we flush the layer flags */
1760         sce->theDag->time++;    // so we know which nodes were accessed
1761         lasttime= sce->theDag->time;
1762         for(itA = firstnode->child; itA; itA= itA->next) {
1763                 if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB) 
1764                         flush_layer_node(itA->node, lasttime);
1765         }
1766         
1767         /* then we use the relationships + layer info to flush update events */
1768         sce->theDag->time++;    // so we know which nodes were accessed
1769         lasttime= sce->theDag->time;
1770         for(itA = firstnode->child; itA; itA= itA->next) {
1771                 if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB) 
1772                         flush_update_node(itA->node, lay, lasttime);
1773         }
1774 }
1775
1776 static int object_modifiers_use_time(Object *ob)
1777 {
1778         ModifierData *md;
1779
1780         for (md=ob->modifiers.first; md; md=md->next)
1781                 if (modifier_dependsOnTime(md))
1782                         return 1;
1783
1784         return 0;
1785 }
1786
1787 static int exists_channel(Object *ob, char *name)
1788 {
1789         bActionStrip *strip;
1790         
1791         if(ob->action)
1792                 if(get_action_channel(ob->action, name))
1793                         return 1;
1794         
1795         for (strip=ob->nlastrips.first; strip; strip=strip->next)
1796                 if(get_action_channel(strip->act, name))
1797                         return 1;
1798         return 0;
1799 }
1800
1801 static void dag_object_time_update_flags(Object *ob)
1802 {
1803         
1804         if(ob->ipo) ob->recalc |= OB_RECALC_OB;
1805         else if(ob->constraints.first) {
1806                 bConstraint *con;
1807                 for (con = ob->constraints.first; con; con=con->next) {
1808                         bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
1809                         ListBase targets = {NULL, NULL};
1810                         bConstraintTarget *ct;
1811                         
1812                         if (cti && cti->get_constraint_targets) {
1813                                 cti->get_constraint_targets(con, &targets);
1814                                 
1815                                 for (ct= targets.first; ct; ct= ct->next) {
1816                                         if (ct->tar) {
1817                                                 ob->recalc |= OB_RECALC_OB;
1818                                                 break;
1819                                         }
1820                                 }
1821                                 
1822                                 if (cti->flush_constraint_targets)
1823                                         cti->flush_constraint_targets(con, &targets, 1);
1824                         }
1825                 }
1826         }
1827         else if(ob->scriptlink.totscript) ob->recalc |= OB_RECALC_OB;
1828         else if(ob->parent) {
1829                 /* motion path or bone child */
1830                 if(ob->parent->type==OB_CURVE || ob->parent->type==OB_ARMATURE) ob->recalc |= OB_RECALC_OB;
1831         }
1832         
1833         if(ob->action || ob->nlastrips.first) {
1834                 /* since actions now are mixed, we set the recalcs on the safe side */
1835                 ob->recalc |= OB_RECALC_OB;
1836                 if(ob->type==OB_ARMATURE)
1837                         ob->recalc |= OB_RECALC_DATA;
1838                 else if(exists_channel(ob, "Shape"))
1839                         ob->recalc |= OB_RECALC_DATA;
1840                 else if(ob->dup_group) {
1841                         bActionStrip *strip;
1842                         /* this case is for groups with nla, whilst nla target has no action or nla */
1843                         for(strip= ob->nlastrips.first; strip; strip= strip->next) {
1844                                 if(strip->object)
1845                                         strip->object->recalc |= OB_RECALC;
1846                         }
1847                 }
1848         }
1849         else if(modifiers_isSoftbodyEnabled(ob)) ob->recalc |= OB_RECALC_DATA;
1850         else if(object_modifiers_use_time(ob)) ob->recalc |= OB_RECALC_DATA;
1851         else if((ob->pose) && (ob->pose->flag & POSE_CONSTRAINTS_TIMEDEPEND)) ob->recalc |= OB_RECALC_DATA;
1852         else {
1853                 Mesh *me;
1854                 Curve *cu;
1855                 Lattice *lt;
1856                 
1857                 switch(ob->type) {
1858                         case OB_MESH:
1859                                 me= ob->data;
1860                                 if(me->key) {
1861                                         if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
1862                                                 ob->recalc |= OB_RECALC_DATA;
1863                                                 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
1864                                         }
1865                                 }
1866                                 else if(ob->effect.first) {
1867                                         Effect *eff= ob->effect.first;
1868                                         PartEff *paf= give_parteff(ob);
1869                                         
1870                                         if(eff->type==EFF_WAVE) 
1871                                                 ob->recalc |= OB_RECALC_DATA;
1872                                         else if(paf && paf->keys==NULL)
1873                                                 ob->recalc |= OB_RECALC_DATA;
1874                                 }
1875                                 if((ob->fluidsimFlag & OB_FLUIDSIM_ENABLE) && (ob->fluidsimSettings)) {
1876                                         // fluidsimSettings might not be initialized during load...
1877                                         if(ob->fluidsimSettings->type & (OB_FLUIDSIM_DOMAIN|OB_FLUIDSIM_PARTICLE)) {
1878                                                 ob->recalc |= OB_RECALC_DATA; // NT FSPARTICLE
1879                                         }
1880                                 }
1881                                 if(ob->particlesystem.first)
1882                                         ob->recalc |= OB_RECALC_DATA;
1883                                 break;
1884                         case OB_CURVE:
1885                         case OB_SURF:
1886                                 cu= ob->data;
1887                                 if(cu->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_FONT:
1895                                 cu= ob->data;
1896                                 if(cu->nurb.first==NULL && cu->str && cu->vfont)
1897                                         ob->recalc |= OB_RECALC_DATA;
1898                                 break;
1899                         case OB_LATTICE:
1900                                 lt= ob->data;
1901                                 if(lt->key) {
1902                                         if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
1903                                                 ob->recalc |= OB_RECALC_DATA;
1904                                                 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
1905                                         }
1906                                 }
1907                                         break;
1908                         case OB_MBALL:
1909                                 if(ob->transflag & OB_DUPLI) ob->recalc |= OB_RECALC_DATA;
1910                                 break;
1911                 }
1912
1913                 if(ob->particlesystem.first) {
1914                         ParticleSystem *psys= ob->particlesystem.first;
1915
1916                         for(; psys; psys=psys->next) {
1917                                 if(psys_check_enabled(ob, psys)) {
1918                                         ob->recalc |= OB_RECALC_DATA;
1919                                         break;
1920                                 }
1921                         }
1922                 }
1923         }               
1924 }
1925
1926 /* flag all objects that need recalc, for changes in time for example */
1927 void DAG_scene_update_flags(Scene *scene, unsigned int lay)
1928 {
1929         Base *base;
1930         Object *ob;
1931         Group *group;
1932         GroupObject *go;
1933         Scene *sce;
1934         
1935         /* set ob flags where animated systems are */
1936         for(SETLOOPER(scene, base)) {
1937                 ob= base->object;
1938                 
1939                 /* now if DagNode were part of base, the node->lay could be checked... */
1940                 /* we do all now, since the scene_flush checks layers and clears recalc flags even */
1941                 dag_object_time_update_flags(ob);
1942                 
1943                 /* handled in next loop */
1944                 if(ob->dup_group) 
1945                         ob->dup_group->id.flag |= LIB_DOIT;
1946         }       
1947         
1948         /* we do groups each once */
1949         for(group= G.main->group.first; group; group= group->id.next) {
1950                 if(group->id.flag & LIB_DOIT) {
1951                         for(go= group->gobject.first; go; go= go->next) {
1952                                 dag_object_time_update_flags(go->ob);
1953                         }
1954                 }
1955         }
1956         
1957         for(sce= scene; sce; sce= sce->set)
1958                 DAG_scene_flush_update(sce, lay);
1959         
1960         /* test: set time flag, to disable baked systems to update */
1961         for(SETLOOPER(scene, base)) {
1962                 ob= base->object;
1963                 if(ob->recalc)
1964                         ob->recalc |= OB_RECALC_TIME;
1965         }
1966         
1967         /* hrmf... an exception to look at once, for invisible camera object we do it over */
1968         if(scene->camera)
1969                 dag_object_time_update_flags(scene->camera);
1970         
1971         /* and store the info in groupobject */
1972         for(group= G.main->group.first; group; group= group->id.next) {
1973                 if(group->id.flag & LIB_DOIT) {
1974                         for(go= group->gobject.first; go; go= go->next) {
1975                                 go->recalc= go->ob->recalc;
1976                                 // printf("ob %s recalc %d\n", go->ob->id.name, go->recalc);
1977                         }
1978                         group->id.flag &= ~LIB_DOIT;
1979                 }
1980         }
1981         
1982 }
1983
1984 /* for depgraph updating, all layers visible in a screen */
1985 /* this is a copy from editscreen.c... I need to think over a more proper solution for this */
1986 /* probably the DAG_object_flush_update() should give layer too? */
1987 /* or some kind of dag context... (DAG_set_layer) */
1988 static unsigned int dag_screen_view3d_layers(void)
1989 {
1990         ScrArea *sa;
1991         int layer= 0;
1992         
1993         for(sa= G.curscreen->areabase.first; sa; sa= sa->next) {
1994                 if(sa->spacetype==SPACE_VIEW3D)
1995                         layer |= ((View3D *)sa->spacedata.first)->lay;
1996         }
1997         return layer;
1998 }
1999
2000
2001 /* flag this object and all its relations to recalc */
2002 /* if you need to do more objects, tag object yourself and
2003    use DAG_scene_flush_update() in end */
2004 void DAG_object_flush_update(Scene *sce, Object *ob, short flag)
2005 {
2006         
2007         if(ob==NULL || sce->theDag==NULL) return;
2008         ob->recalc |= flag;
2009         
2010         /* all users of this ob->data should be checked */
2011         /* BUT! displists for curves are still only on cu */
2012         if(flag & OB_RECALC_DATA) {
2013                 if(ob->type!=OB_CURVE && ob->type!=OB_SURF) {
2014                         ID *id= ob->data;
2015                         if(id && id->us>1) {
2016                                 /* except when there's a key and shapes are locked */
2017                                 if(ob_get_key(ob) && (ob->shapeflag & (OB_SHAPE_LOCK|OB_SHAPE_TEMPLOCK)));
2018                                 else {
2019                                         Object *obt;
2020                                         for (obt=G.main->object.first; obt; obt= obt->id.next) {
2021                                                 if (obt->data==ob->data) {
2022                                                         obt->recalc |= OB_RECALC_DATA;
2023                                                 }
2024                                         }
2025                                 }
2026                         }
2027                 }
2028         }
2029         
2030         if(G.curscreen)
2031                 DAG_scene_flush_update(sce, dag_screen_view3d_layers());
2032         else
2033                 DAG_scene_flush_update(sce, sce->lay);
2034 }
2035
2036 /* recursively descends tree, each node only checked once */
2037 /* node is checked to be of type object */
2038 static int parent_check_node(DagNode *node, int curtime)
2039 {
2040         DagAdjList *itA;
2041         
2042         node->lasttime= curtime;
2043         
2044         if(node->color==DAG_GRAY)
2045                 return DAG_GRAY;
2046         
2047         for(itA = node->child; itA; itA= itA->next) {
2048                 if(itA->node->type==ID_OB) {
2049                         
2050                         if(itA->node->color==DAG_GRAY)
2051                                 return DAG_GRAY;
2052
2053                         /* descend if not done */
2054                         if(itA->node->lasttime!=curtime) {
2055                                 itA->node->color= parent_check_node(itA->node, curtime);
2056                         
2057                                 if(itA->node->color==DAG_GRAY)
2058                                         return DAG_GRAY;
2059                         }
2060                 }
2061         }
2062         
2063         return DAG_WHITE;
2064 }
2065
2066 /* all nodes that influence this object get tagged, for calculating the exact
2067    position of this object at a given timeframe */
2068 void DAG_object_update_flags(Scene *sce, Object *ob, unsigned int lay)
2069 {
2070         DagNode *node;
2071         DagAdjList *itA;
2072         
2073         /* tag nodes unchecked */
2074         for(node = sce->theDag->DagNode.first; node; node= node->next) 
2075                 node->color = DAG_WHITE;
2076         
2077         node= dag_find_node(sce->theDag, ob);
2078         
2079         /* object not in scene? then handle group exception. needs to be dagged once too */
2080         if(node==NULL) {
2081                 Group *group= NULL;
2082                 while( (group = find_group(ob, group)) ) {
2083                         GroupObject *go;
2084                         /* primitive; tag all... this call helps building groups for particles */
2085                         for(go= group->gobject.first; go; go= go->next)
2086                                 go->ob->recalc= OB_RECALC;
2087                 }
2088         }
2089         else {
2090                 
2091                 node->color = DAG_GRAY;
2092                 
2093                 sce->theDag->time++;
2094                 node= sce->theDag->DagNode.first;
2095                 for(itA = node->child; itA; itA= itA->next) {
2096                         if(itA->node->type==ID_OB && itA->node->lasttime!=sce->theDag->time)
2097                                 itA->node->color= parent_check_node(itA->node, sce->theDag->time);
2098                 }
2099                 
2100                 /* set recalcs and flushes */
2101                 DAG_scene_update_flags(sce, lay);
2102                 
2103                 /* now we clear recalcs, unless color is set */
2104                 for(node = sce->theDag->DagNode.first; node; node= node->next) {
2105                         if(node->type==ID_OB && node->color==DAG_WHITE) {
2106                                 Object *ob= node->ob;
2107                                 ob->recalc= 0;
2108                         }
2109                 }
2110         }
2111 }
2112
2113 /* ******************* DAG FOR ARMATURE POSE ***************** */
2114
2115 static int node_recurs_level(DagNode *node, int level)
2116 {
2117         DagAdjList *itA;
2118         int newlevel;
2119
2120         node->color= DAG_BLACK; /* done */
2121         newlevel= ++level;
2122         
2123         for(itA= node->parent; itA; itA= itA->next) {
2124                 if(itA->node->color==DAG_WHITE) {
2125                         itA->node->ancestor_count= node_recurs_level(itA->node, level);
2126                         newlevel= MAX2(newlevel, level+itA->node->ancestor_count);
2127                 }
2128                 else
2129                         newlevel= MAX2(newlevel, level+itA->node->ancestor_count);
2130         }
2131         
2132         return newlevel;
2133 }
2134
2135 static void pose_check_cycle(DagForest *dag)
2136 {
2137         DagNode *node;
2138         DagAdjList *itA;
2139
2140         /* tag nodes unchecked */
2141         for(node = dag->DagNode.first; node; node= node->next)
2142                 node->color= DAG_WHITE;
2143         
2144         for(node = dag->DagNode.first; node; node= node->next) {
2145                 if(node->color==DAG_WHITE) {
2146                         node->ancestor_count= node_recurs_level(node, 0);
2147                 }
2148         }
2149         
2150         /* check relations, and print errors */
2151         for(node = dag->DagNode.first; node; node= node->next) {
2152                 for(itA= node->parent; itA; itA= itA->next) {
2153                         if(itA->node->ancestor_count > node->ancestor_count) {
2154                                 bPoseChannel *pchan= (bPoseChannel *)node->ob;
2155                                 bPoseChannel *parchan= (bPoseChannel *)itA->node->ob;
2156                                 
2157                                 if(pchan && parchan) 
2158                                         if(pchan->parent!=parchan)
2159                                                 printf("Cycle in %s to %s\n", pchan->name, parchan->name);
2160                         }
2161                 }
2162         }
2163 }
2164
2165 /* we assume its an armature with pose */
2166 void DAG_pose_sort(Object *ob)
2167 {
2168         bPose *pose= ob->pose;
2169         bPoseChannel *pchan;
2170         bConstraint *con;
2171         DagNode *node;
2172         DagNode *node2, *node3;
2173         DagNode *rootnode;
2174         DagForest *dag;
2175         DagNodeQueue *nqueue;
2176         DagAdjList *itA;
2177         ListBase tempbase;
2178         int skip = 0;
2179         
2180         dag = dag_init();
2181         ugly_hack_sorry= 0;     // no ID structs
2182
2183         rootnode = dag_add_node(dag, NULL);     // node->ob becomes NULL
2184         
2185         /* we add the hierarchy and the constraints */
2186         for(pchan = pose->chanbase.first; pchan; pchan= pchan->next) {
2187                 int addtoroot = 1;
2188                 
2189                 node = dag_get_node(dag, pchan);
2190                 
2191                 if(pchan->parent) {
2192                         node2 = dag_get_node(dag, pchan->parent);
2193                         dag_add_relation(dag, node2, node, 0);
2194                         dag_add_parent_relation(dag, node2, node, 0);
2195                         addtoroot = 0;
2196                 }
2197                 for (con = pchan->constraints.first; con; con=con->next) {
2198                         bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
2199                         ListBase targets = {NULL, NULL};
2200                         bConstraintTarget *ct;
2201                         
2202                         if(con->ipo) {
2203                                 IpoCurve *icu;
2204                                 for(icu= con->ipo->curve.first; icu; icu= icu->next) {
2205                                         /* icu->driver->ob should actually point to ob->proxy if it
2206                                          * is a proxy, but since it wasn't set correct it older
2207                                          * files comparing with ob->proxy makes it work for those */
2208                                         if(icu->driver && (icu->driver->ob==ob || icu->driver->ob==ob->proxy)) {
2209                                                 bPoseChannel *target= get_pose_channel(ob->pose, icu->driver->name);
2210                                                 if(target) {
2211                                                         node2 = dag_get_node(dag, target);
2212                                                         dag_add_relation(dag, node2, node, 0);
2213                                                         dag_add_parent_relation(dag, node2, node, 0);
2214
2215                                                         /* uncommented this line, results in dependencies
2216                                                          * not being added properly for this constraint,
2217                                                          * what is the purpose of this? - brecht */
2218                                                         /*cti= NULL;*/  /* trick to get next loop skipped */
2219                                                 }
2220                                         }
2221                                 }
2222                         }
2223                         
2224                         if (cti && cti->get_constraint_targets) {
2225                                 cti->get_constraint_targets(con, &targets);
2226                                 
2227                                 for (ct= targets.first; ct; ct= ct->next) {
2228                                         if (ct->tar==ob && ct->subtarget[0]) {
2229                                                 bPoseChannel *target= get_pose_channel(ob->pose, ct->subtarget);
2230                                                 if (target) {
2231                                                         node2= dag_get_node(dag, target);
2232                                                         dag_add_relation(dag, node2, node, 0);
2233                                                         dag_add_parent_relation(dag, node2, node, 0);
2234                                                         
2235                                                         if (con->type==CONSTRAINT_TYPE_KINEMATIC) {
2236                                                                 bKinematicConstraint *data = (bKinematicConstraint *)con->data;
2237                                                                 bPoseChannel *parchan;
2238                                                                 int segcount= 0;
2239                                                                 
2240                                                                 /* exclude tip from chain? */
2241                                                                 if(!(data->flag & CONSTRAINT_IK_TIP))
2242                                                                         parchan= pchan->parent;
2243                                                                 else
2244                                                                         parchan= pchan;
2245                                                                 
2246                                                                 /* Walk to the chain's root */
2247                                                                 while (parchan) {
2248                                                                         node3= dag_get_node(dag, parchan);
2249                                                                         dag_add_relation(dag, node2, node3, 0);
2250                                                                         dag_add_parent_relation(dag, node2, node3, 0);
2251                                                                         
2252                                                                         segcount++;
2253                                                                         if (segcount==data->rootbone || segcount>255) break; // 255 is weak
2254                                                                         parchan= parchan->parent;
2255                                                                 }
2256                                                         }
2257                                                 }
2258                                         }
2259                                 }
2260                                 
2261                                 if (cti->flush_constraint_targets)
2262                                         cti->flush_constraint_targets(con, &targets, 1);
2263                         }
2264                 }
2265                 if (addtoroot == 1 ) {
2266                         dag_add_relation(dag, rootnode, node, 0);
2267                         dag_add_parent_relation(dag, rootnode, node, 0);
2268                 }
2269         }
2270
2271         pose_check_cycle(dag);
2272         
2273         /* now we try to sort... */
2274         tempbase.first= tempbase.last= NULL;
2275
2276         nqueue = queue_create(DAGQUEUEALLOC);
2277         
2278         /* tag nodes unchecked */
2279         for(node = dag->DagNode.first; node; node= node->next) 
2280                 node->color = DAG_WHITE;
2281         
2282         node = dag->DagNode.first;
2283         
2284         node->color = DAG_GRAY;
2285         push_stack(nqueue, node);  
2286         
2287         while(nqueue->count) {
2288                 
2289                 skip = 0;
2290                 node = get_top_node_queue(nqueue);
2291                 
2292                 itA = node->child;
2293                 while(itA != NULL) {
2294                         if((itA->node->color == DAG_WHITE) ) {
2295                                 itA->node->color = DAG_GRAY;
2296                                 push_stack(nqueue,itA->node);
2297                                 skip = 1;
2298                                 break;
2299                         } 
2300                         itA = itA->next;
2301                 }                       
2302                 
2303                 if (!skip) {
2304                         if (node) {
2305                                 node = pop_queue(nqueue);
2306                                 if (node->ob == NULL)   // we are done
2307                                         break ;
2308                                 node->color = DAG_BLACK;
2309                                 
2310                                 /* put node in new list */
2311                                 BLI_remlink(&pose->chanbase, node->ob);
2312                                 BLI_addhead(&tempbase, node->ob);
2313                         }       
2314                 }
2315         }
2316         
2317         // temporal correction for circular dependancies
2318         while(pose->chanbase.first) {
2319                 pchan= pose->chanbase.first;
2320                 BLI_remlink(&pose->chanbase, pchan);
2321                 BLI_addhead(&tempbase, pchan);
2322
2323                 printf("cyclic %s\n", pchan->name);
2324         }
2325         
2326         pose->chanbase = tempbase;
2327         queue_delete(nqueue);
2328         
2329 //      printf("\nordered\n");
2330 //      for(pchan = pose->chanbase.first; pchan; pchan= pchan->next) {
2331 //              printf(" %s\n", pchan->name);
2332 //      }
2333         
2334         free_forest( dag );
2335         MEM_freeN( dag );
2336         
2337         ugly_hack_sorry= 1;
2338 }
2339
2340
2341