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