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