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