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