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