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