code cleanup: function naming, use BKE_*type* prefix.
[blender.git] / source / blender / blenkernel / intern / depsgraph.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2004 Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): none yet.
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/blenkernel/intern/depsgraph.c
27  *  \ingroup bke
28  */
29
30  
31 #include <stdio.h>
32 #include <string.h>
33 #include <math.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #ifdef WIN32
38 #  include "BLI_winstuff.h"
39 #endif
40
41 #include "BLI_utildefines.h"
42 #include "BLI_listbase.h"
43 #include "BLI_ghash.h"
44
45 #include "DNA_anim_types.h"
46 #include "DNA_camera_types.h"
47 #include "DNA_group_types.h"
48 #include "DNA_lattice_types.h"
49 #include "DNA_key_types.h"
50 #include "DNA_material_types.h"
51 #include "DNA_mesh_types.h"
52 #include "DNA_node_types.h"
53 #include "DNA_scene_types.h"
54 #include "DNA_screen_types.h"
55 #include "DNA_windowmanager_types.h"
56 #include "DNA_movieclip_types.h"
57
58 #include "BKE_animsys.h"
59 #include "BKE_action.h"
60 #include "BKE_effect.h"
61 #include "BKE_fcurve.h"
62 #include "BKE_global.h"
63 #include "BKE_group.h"
64 #include "BKE_key.h"
65 #include "BKE_library.h"
66 #include "BKE_main.h"
67 #include "BKE_node.h"
68 #include "BKE_mball.h"
69 #include "BKE_modifier.h"
70 #include "BKE_object.h"
71 #include "BKE_particle.h"
72 #include "BKE_pointcache.h"
73 #include "BKE_scene.h"
74 #include "BKE_screen.h"
75 #include "BKE_tracking.h"
76 #include "BKE_utildefines.h"
77
78 #include "depsgraph_private.h"
79  
80 /* Queue and stack operations for dag traversal 
81  *
82  * the queue store a list of freenodes to avoid successive alloc/dealloc
83  */
84
85 DagNodeQueue * queue_create (int slots) 
86 {
87         DagNodeQueue * queue;
88         DagNodeQueueElem * elem;
89         int i;
90         
91         queue = MEM_mallocN(sizeof(DagNodeQueue), "DAG queue");
92         queue->freenodes = MEM_mallocN(sizeof(DagNodeQueue), "DAG queue");
93         queue->count = 0;
94         queue->maxlevel = 0;
95         queue->first = queue->last = NULL;
96         elem = MEM_mallocN(sizeof(DagNodeQueueElem), "DAG queue elem3");
97         elem->node = NULL;
98         elem->next = NULL;
99         queue->freenodes->first = queue->freenodes->last = elem;
100         
101         for (i = 1; i <slots;i++) {
102                 elem = MEM_mallocN(sizeof(DagNodeQueueElem), "DAG queue elem4");
103                 elem->node = NULL;
104                 elem->next = NULL;
105                 queue->freenodes->last->next = elem;
106                 queue->freenodes->last = elem;
107         }
108         queue->freenodes->count = slots;
109         return queue;
110 }
111
112 void queue_raz(DagNodeQueue *queue)
113 {
114         DagNodeQueueElem * elem;
115         
116         elem = queue->first;
117         if (queue->freenodes->last)
118                 queue->freenodes->last->next = elem;
119         else
120                 queue->freenodes->first = queue->freenodes->last = elem;
121         
122         elem->node = NULL;
123         queue->freenodes->count++;
124         while (elem->next) {
125                 elem = elem->next;
126                 elem->node = NULL;
127                 queue->freenodes->count++;
128         }
129         queue->freenodes->last = elem;
130         queue->count = 0;
131 }
132
133 void queue_delete(DagNodeQueue *queue)
134 {
135         DagNodeQueueElem * elem;
136         DagNodeQueueElem * temp;
137         
138         elem = queue->first;
139         while (elem) {
140                 temp = elem;
141                 elem = elem->next;
142                 MEM_freeN(temp);
143         }
144         
145         elem = queue->freenodes->first;
146         while (elem) {
147                 temp = elem;
148                 elem = elem->next;
149                 MEM_freeN(temp);
150         }
151         
152         MEM_freeN(queue->freenodes);                    
153         MEM_freeN(queue);                       
154 }
155
156 /* insert in queue, remove in front */
157 void push_queue(DagNodeQueue *queue, DagNode *node)
158 {
159         DagNodeQueueElem * elem;
160         int i;
161
162         if (node == NULL) {
163                 fprintf(stderr, "pushing null node\n");
164                 return;
165         }
166         /*fprintf(stderr, "BFS push : %s %d\n", ((ID *) node->ob)->name, queue->count);*/
167
168         elem = queue->freenodes->first;
169         if (elem != NULL) {
170                 queue->freenodes->first = elem->next;
171                 if ( queue->freenodes->last == elem) {
172                         queue->freenodes->last = NULL;
173                         queue->freenodes->first = NULL;
174                 }
175                 queue->freenodes->count--;
176         }
177         else { /* alllocating more */
178                 elem = MEM_mallocN(sizeof(DagNodeQueueElem), "DAG queue elem1");
179                 elem->node = NULL;
180                 elem->next = NULL;
181                 queue->freenodes->first = queue->freenodes->last = elem;
182
183                 for (i = 1; i <DAGQUEUEALLOC;i++) {
184                         elem = MEM_mallocN(sizeof(DagNodeQueueElem), "DAG queue elem2");
185                         elem->node = NULL;
186                         elem->next = NULL;
187                         queue->freenodes->last->next = elem;
188                         queue->freenodes->last = elem;
189                 }
190                 queue->freenodes->count = DAGQUEUEALLOC;
191                         
192                 elem = queue->freenodes->first; 
193                 queue->freenodes->first = elem->next;   
194         }
195         elem->next = NULL;
196         elem->node = node;
197         if (queue->last != NULL)
198                 queue->last->next = elem;
199         queue->last = elem;
200         if (queue->first == NULL) {
201                 queue->first = elem;
202         }
203         queue->count++;
204 }
205
206
207 /* insert in front, remove in front */
208 void push_stack(DagNodeQueue *queue, DagNode *node)
209 {
210         DagNodeQueueElem * elem;
211         int i;
212
213         elem = queue->freenodes->first; 
214         if (elem != NULL) {
215                 queue->freenodes->first = elem->next;
216                 if ( queue->freenodes->last == elem) {
217                         queue->freenodes->last = NULL;
218                         queue->freenodes->first = NULL;
219                 }
220                 queue->freenodes->count--;
221         }
222         else { /* alllocating more */
223                 elem = MEM_mallocN(sizeof(DagNodeQueueElem), "DAG queue elem1");
224                 elem->node = NULL;
225                 elem->next = NULL;
226                 queue->freenodes->first = queue->freenodes->last = elem;
227
228                 for (i = 1; i <DAGQUEUEALLOC;i++) {
229                         elem = MEM_mallocN(sizeof(DagNodeQueueElem), "DAG queue elem2");
230                         elem->node = NULL;
231                         elem->next = NULL;
232                         queue->freenodes->last->next = elem;
233                         queue->freenodes->last = elem;
234                 }
235                 queue->freenodes->count = DAGQUEUEALLOC;
236                         
237                 elem = queue->freenodes->first; 
238                 queue->freenodes->first = elem->next;   
239         }
240         elem->next = queue->first;
241         elem->node = node;
242         queue->first = elem;
243         if (queue->last == NULL)
244                 queue->last = elem;
245         queue->count++;
246 }
247
248
249 DagNode * pop_queue(DagNodeQueue *queue)
250 {
251         DagNodeQueueElem * elem;
252         DagNode *node;
253
254         elem = queue->first;
255         if (elem) {
256                 queue->first = elem->next;
257                 if (queue->last == elem) {
258                         queue->last=NULL;
259                         queue->first=NULL;
260                 }
261                 queue->count--;
262                 if (queue->freenodes->last)
263                         queue->freenodes->last->next=elem;
264                 queue->freenodes->last=elem;
265                 if (queue->freenodes->first == NULL)
266                         queue->freenodes->first=elem;
267                 node = elem->node;
268                 elem->node = NULL;
269                 elem->next = NULL;
270                 queue->freenodes->count++;
271                 return node;
272         }
273         else {
274                 fprintf(stderr, "return null\n");
275                 return NULL;
276         }
277 }
278
279 void    *pop_ob_queue(struct DagNodeQueue *queue)
280 {
281         return(pop_queue(queue)->ob);
282 }
283
284 DagNode * get_top_node_queue(DagNodeQueue *queue) 
285 {
286         return queue->first->node;
287 }
288
289 int             queue_count(struct DagNodeQueue *queue)
290 {
291         return queue->count;
292 }
293
294
295 DagForest *dag_init(void)
296 {
297         DagForest *forest;
298         /* use callocN to init all zero */
299         forest = MEM_callocN(sizeof(DagForest), "DAG root");
300         return forest;
301 }
302
303 /* isdata = object data... */
304 // XXX this needs to be extended to be more flexible (so that not only objects are evaluated via depsgraph)...
305 static void dag_add_driver_relation(AnimData *adt, DagForest *dag, DagNode *node, int isdata)
306 {
307         FCurve *fcu;
308         DagNode *node1;
309         
310         for (fcu= adt->drivers.first; fcu; fcu= fcu->next) {
311                 ChannelDriver *driver= fcu->driver;
312                 DriverVar *dvar;
313                 int isdata_fcu = isdata || (fcu->rna_path && strstr(fcu->rna_path, "modifiers["));
314                 
315                 /* loop over variables to get the target relationships */
316                 for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
317                         /* only used targets */
318                         DRIVER_TARGETS_USED_LOOPER(dvar) 
319                         {
320                                 if (dtar->id) {
321                                         // FIXME: other data types need to be added here so that they can work!
322                                         if (GS(dtar->id->name)==ID_OB) {
323                                                 Object *ob= (Object *)dtar->id;
324                                                 
325                                                 /* normal channel-drives-channel */
326                                                 node1 = dag_get_node(dag, dtar->id);
327                                                 
328                                                 /* check if bone... */
329                                                 if ((ob->type==OB_ARMATURE) && 
330                                                         ( ((dtar->rna_path) && strstr(dtar->rna_path, "pose.bones[")) || 
331                                                           ((dtar->flag & DTAR_FLAG_STRUCT_REF) && (dtar->pchan_name[0])) )) 
332                                                 {
333                                                         dag_add_relation(dag, node1, node, isdata_fcu?DAG_RL_DATA_DATA:DAG_RL_DATA_OB, "Driver");
334                                                 }
335                                                 /* check if ob data */
336                                                 else if (dtar->rna_path && strstr(dtar->rna_path, "data."))
337                                                         dag_add_relation(dag, node1, node, isdata_fcu?DAG_RL_DATA_DATA:DAG_RL_DATA_OB, "Driver");
338                                                 /* normal */
339                                                 else
340                                                         dag_add_relation(dag, node1, node, isdata_fcu?DAG_RL_OB_DATA:DAG_RL_OB_OB, "Driver");
341                                         }
342                                 }
343                         }
344                         DRIVER_TARGETS_LOOPER_END
345                 }
346         }
347 }
348
349 static void dag_add_collision_field_relation(DagForest *dag, Scene *scene, Object *ob, DagNode *node)
350 {
351         Base *base;
352         DagNode *node2;
353
354         // would be nice to have a list of colliders here
355         // so for now walk all objects in scene check 'same layer rule'
356         for (base = scene->base.first; base; base= base->next) {
357                 if ((base->lay & ob->lay) && base->object->pd) {
358                         Object *ob1= base->object;
359                         if ((ob1->pd->deflect || ob1->pd->forcefield) && (ob1 != ob)) {
360                                 node2 = dag_get_node(dag, ob1);                                 
361                                 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Field Collision");
362                         }
363                 }
364         }
365 }
366
367 static void build_dag_object(DagForest *dag, DagNode *scenenode, Scene *scene, Object *ob, int mask)
368 {
369         bConstraint *con;
370         DagNode * node;
371         DagNode * node2;
372         DagNode * node3;
373         Key *key;
374         ParticleSystem *psys;
375         int addtoroot= 1;
376         
377         node = dag_get_node(dag, ob);
378         
379         if ((ob->data) && (mask&DAG_RL_DATA)) {
380                 node2 = dag_get_node(dag, ob->data);
381                 dag_add_relation(dag, node, node2, DAG_RL_DATA, "Object-Data Relation");
382                 node2->first_ancestor = ob;
383                 node2->ancestor_count += 1;
384         }
385
386         /* also build a custom data mask for dependencies that need certain layers */
387         node->customdata_mask= 0;
388         
389         if (ob->type == OB_ARMATURE) {
390                 if (ob->pose) {
391                         bPoseChannel *pchan;
392                         bConstraint *con;
393                         
394                         for (pchan = ob->pose->chanbase.first; pchan; pchan=pchan->next) {
395                                 for (con = pchan->constraints.first; con; con=con->next) {
396                                         bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
397                                         ListBase targets = {NULL, NULL};
398                                         bConstraintTarget *ct;
399                                         
400                                         if (cti && cti->get_constraint_targets) {
401                                                 cti->get_constraint_targets(con, &targets);
402                                                 
403                                                 for (ct= targets.first; ct; ct= ct->next) {
404                                                         if (ct->tar && ct->tar != ob) {
405                                                                 // fprintf(stderr, "armature %s target :%s\n", ob->id.name, target->id.name);
406                                                                 node3 = dag_get_node(dag, ct->tar);
407                                                                 
408                                                                 if (ct->subtarget[0]) {
409                                                                         dag_add_relation(dag, node3, node, DAG_RL_OB_DATA|DAG_RL_DATA_DATA, cti->name);
410                                                                         if (ct->tar->type == OB_MESH)
411                                                                                 node3->customdata_mask |= CD_MASK_MDEFORMVERT;
412                                                                 }
413                                                                 else if (ELEM3(con->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO, CONSTRAINT_TYPE_SPLINEIK))       
414                                                                         dag_add_relation(dag, node3, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, cti->name);
415                                                                 else
416                                                                         dag_add_relation(dag, node3, node, DAG_RL_OB_DATA, cti->name);
417                                                         }
418                                                 }
419                                                 
420                                                 if (cti->flush_constraint_targets)
421                                                         cti->flush_constraint_targets(con, &targets, 1);
422                                         }
423                                         
424                                 }
425                         }
426                 }
427         }
428         
429         /* driver dependencies, nla modifiers */
430 #if 0 // XXX old animation system
431         if (ob->nlastrips.first) {
432                 bActionStrip *strip;
433                 bActionChannel *chan;
434                 for (strip= ob->nlastrips.first; strip; strip= strip->next) {
435                         if (strip->modifiers.first) {
436                                 bActionModifier *amod;
437                                 for (amod= strip->modifiers.first; amod; amod= amod->next) {
438                                         if (amod->ob) {
439                                                 node2 = dag_get_node(dag, amod->ob);
440                                                 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "NLA Strip Modifier");
441                                         }
442                                 }
443                         }
444                 }
445         }
446 #endif // XXX old animation system
447         if (ob->adt)
448                 dag_add_driver_relation(ob->adt, dag, node, (ob->type == OB_ARMATURE)); // XXX isdata arg here doesn't give an accurate picture of situation
449                 
450         key= ob_get_key(ob);
451         if (key && key->adt)
452                 dag_add_driver_relation(key->adt, dag, node, 1);
453
454         if (ob->modifiers.first) {
455                 ModifierData *md;
456                 
457                 for (md=ob->modifiers.first; md; md=md->next) {
458                         ModifierTypeInfo *mti = modifierType_getInfo(md->type);
459                         
460                         if (mti->updateDepgraph) mti->updateDepgraph(md, dag, scene, ob, node);
461                 }
462         }
463         if (ob->parent) {
464                 node2 = dag_get_node(dag, ob->parent);
465                 
466                 switch (ob->partype) {
467                         case PARSKEL:
468                                 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Parent");
469                                 break;
470                         case PARVERT1: case PARVERT3:
471                                 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB|DAG_RL_OB_OB, "Vertex Parent");
472                                 node2->customdata_mask |= CD_MASK_ORIGINDEX;
473                                 break;
474                         case PARBONE:
475                                 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB|DAG_RL_OB_OB, "Bone Parent");
476                                 break;
477                         default:
478                                 if (ob->parent->type==OB_LATTICE) 
479                                         dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Lattice Parent");
480                                 else if (ob->parent->type==OB_CURVE) {
481                                         Curve *cu= ob->parent->data;
482                                         if (cu->flag & CU_PATH) 
483                                                 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB|DAG_RL_OB_OB, "Curve Parent");
484                                         else
485                                                 dag_add_relation(dag, node2, node, DAG_RL_OB_OB, "Curve Parent");
486                                 }
487                                 else
488                                         dag_add_relation(dag, node2, node, DAG_RL_OB_OB, "Parent");
489                 }
490                 /* exception case: parent is duplivert */
491                 if (ob->type==OB_MBALL && (ob->parent->transflag & OB_DUPLIVERTS)) {
492                         dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Duplivert");
493                 }
494                 
495                 addtoroot = 0;
496         }
497         if (ob->proxy) {
498                 node2 = dag_get_node(dag, ob->proxy);
499                 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Proxy");
500                 /* inverted relation, so addtoroot shouldn't be set to zero */
501         }
502         
503         if (ob->transflag & OB_DUPLI) {
504                 if ((ob->transflag & OB_DUPLIGROUP) && ob->dup_group) {
505                         GroupObject *go;
506                         for (go= ob->dup_group->gobject.first; go; go= go->next) {
507                                 if (go->ob) {
508                                         node2 = dag_get_node(dag, go->ob);
509                                         /* node2 changes node1, this keeps animations updated in groups?? not logical? */
510                                         dag_add_relation(dag, node2, node, DAG_RL_OB_OB, "Dupligroup");
511                                 }
512                         }
513                 }
514         }
515
516         /* softbody collision  */
517         if ((ob->type==OB_MESH) || (ob->type==OB_CURVE) || (ob->type==OB_LATTICE)) {
518                 if (modifiers_isSoftbodyEnabled(ob) || modifiers_isClothEnabled(ob) || ob->particlesystem.first)
519                         dag_add_collision_field_relation(dag, scene, ob, node); /* TODO: use effectorweight->group */
520         }
521         
522         /* object data drivers */
523         if (ob->data) {
524                 AnimData *adt= BKE_animdata_from_id((ID *)ob->data);
525                 if (adt)
526                         dag_add_driver_relation(adt, dag, node, 1);
527         }
528         
529         /* object type/data relationships */
530         switch (ob->type) {
531                 case OB_CAMERA:
532                 {
533                         Camera *cam = (Camera *)ob->data;
534                         
535                         if (cam->dof_ob) {
536                                 node2 = dag_get_node(dag, cam->dof_ob);
537                                 dag_add_relation(dag, node2, node, DAG_RL_OB_OB, "Camera DoF");
538                         }
539                 }
540                         break;
541                 case OB_MBALL: 
542                 {
543                         Object *mom= BKE_metaball_basis_find(scene, ob);
544                         
545                         if (mom!=ob) {
546                                 node2 = dag_get_node(dag, mom);
547                                 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Metaball");  // mom depends on children!
548                         }
549                 }
550                         break;
551                 case OB_CURVE:
552                 case OB_FONT:
553                 {
554                         Curve *cu= ob->data;
555                         
556                         if (cu->bevobj) {
557                                 node2 = dag_get_node(dag, cu->bevobj);
558                                 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Curve Bevel");
559                         }
560                         if (cu->taperobj) {
561                                 node2 = dag_get_node(dag, cu->taperobj);
562                                 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Curve Taper");
563                         }
564                         if (ob->type == OB_FONT) {
565                                 if (cu->textoncurve) {
566                                         node2 = dag_get_node(dag, cu->textoncurve);
567                                         dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Texture On Curve");
568                                 }
569                         }
570                 }
571                         break;
572         }
573         
574         /* particles */
575         psys= ob->particlesystem.first;
576         if (psys) {
577                 GroupObject *go;
578
579                 for (; psys; psys=psys->next) {
580                         BoidRule *rule = NULL;
581                         BoidState *state = NULL;
582                         ParticleSettings *part= psys->part;
583                         ListBase *effectors = NULL;
584                         EffectorCache *eff;
585
586                         dag_add_relation(dag, node, node, DAG_RL_OB_DATA, "Particle-Object Relation");
587
588                         if (!psys_check_enabled(ob, psys))
589                                 continue;
590
591                         if (ELEM(part->phystype, PART_PHYS_KEYED, PART_PHYS_BOIDS)) {
592                                 ParticleTarget *pt = psys->targets.first;
593
594                                 for (; pt; pt=pt->next) {
595                                         if (pt->ob && BLI_findlink(&pt->ob->particlesystem, pt->psys-1)) {
596                                                 node2 = dag_get_node(dag, pt->ob);
597                                                 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Particle Targets");
598                                         }
599                            }
600                         }
601
602                         if (part->ren_as == PART_DRAW_OB && part->dup_ob) {
603                                 node2 = dag_get_node(dag, part->dup_ob);
604                                 /* note that this relation actually runs in the wrong direction, the problem
605                                  * is that dupli system all have this (due to parenting), and the render
606                                  * engine instancing assumes particular ordering of objects in list */
607                                 dag_add_relation(dag, node, node2, DAG_RL_OB_OB, "Particle Object Visualisation");
608                                 if (part->dup_ob->type == OB_MBALL)
609                                         dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA, "Particle Object Visualisation");
610                         }
611
612                         if (part->ren_as == PART_DRAW_GR && part->dup_group) {
613                                 for (go=part->dup_group->gobject.first; go; go=go->next) {
614                                         node2 = dag_get_node(dag, go->ob);
615                                         dag_add_relation(dag, node2, node, DAG_RL_OB_OB, "Particle Group Visualisation");
616                                 }
617                         }
618
619                         effectors = pdInitEffectors(scene, ob, psys, part->effector_weights);
620
621                         if (effectors) for (eff = effectors->first; eff; eff=eff->next) {
622                                 if (eff->psys) {
623                                         node2 = dag_get_node(dag, eff->ob);
624                                         dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Particle Field");
625                                 }
626                         }
627
628                         pdEndEffectors(&effectors);
629
630                         if (part->boids) {
631                                 for (state = part->boids->states.first; state; state=state->next) {
632                                         for (rule = state->rules.first; rule; rule=rule->next) {
633                                                 Object *ruleob = NULL;
634                                                 if (rule->type==eBoidRuleType_Avoid)
635                                                         ruleob = ((BoidRuleGoalAvoid*)rule)->ob;
636                                                 else if (rule->type==eBoidRuleType_FollowLeader)
637                                                         ruleob = ((BoidRuleFollowLeader*)rule)->ob;
638
639                                                 if (ruleob) {
640                                                         node2 = dag_get_node(dag, ruleob);
641                                                         dag_add_relation(dag, node2, node, DAG_RL_OB_DATA, "Boid Rule");
642                                                 }
643                                         }
644                                 }
645                         }
646                 }
647         }
648         
649         /* object constraints */
650         for (con = ob->constraints.first; con; con=con->next) {
651                 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
652                 ListBase targets = {NULL, NULL};
653                 bConstraintTarget *ct;
654                 
655                 if (!cti)
656                         continue;
657
658                 /* special case for camera tracking -- it doesn't use targets to define relations */
659                 if (ELEM3(cti->type, CONSTRAINT_TYPE_FOLLOWTRACK, CONSTRAINT_TYPE_CAMERASOLVER, CONSTRAINT_TYPE_OBJECTSOLVER)) {
660                         int depends_on_camera= 0;
661
662                         if (cti->type==CONSTRAINT_TYPE_FOLLOWTRACK) {
663                                 bFollowTrackConstraint *data= (bFollowTrackConstraint *)con->data;
664
665                                 if ((data->clip || data->flag&FOLLOWTRACK_ACTIVECLIP) && data->track[0])
666                                         depends_on_camera= 1;
667
668                                 if (data->depth_ob) {
669                                         node2 = dag_get_node(dag, data->depth_ob);
670                                         dag_add_relation(dag, node2, node, DAG_RL_DATA_OB|DAG_RL_OB_OB, cti->name);
671                                 }
672                         }
673                         else if (cti->type==CONSTRAINT_TYPE_OBJECTSOLVER)
674                                 depends_on_camera= 1;
675
676                         if (depends_on_camera && scene->camera) {
677                                 node2 = dag_get_node(dag, scene->camera);
678                                 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB|DAG_RL_OB_OB, cti->name);
679                         }
680
681                         dag_add_relation(dag, scenenode, node, DAG_RL_SCENE, "Scene Relation");
682                         addtoroot = 0;
683                 }
684                 else if (cti->get_constraint_targets) {
685                         cti->get_constraint_targets(con, &targets);
686                         
687                         for (ct= targets.first; ct; ct= ct->next) {
688                                 Object *obt;
689                                 
690                                 if (ct->tar)
691                                         obt= ct->tar;
692                                 else
693                                         continue;
694                                 
695                                 node2 = dag_get_node(dag, obt);
696                                 if (ELEM(con->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO))
697                                         dag_add_relation(dag, node2, node, DAG_RL_DATA_OB|DAG_RL_OB_OB, cti->name);
698                                 else {
699                                         if (ELEM3(obt->type, OB_ARMATURE, OB_MESH, OB_LATTICE) && (ct->subtarget[0])) {
700                                                 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB|DAG_RL_OB_OB, cti->name);
701                                                 if (obt->type == OB_MESH)
702                                                         node2->customdata_mask |= CD_MASK_MDEFORMVERT;
703                                         }
704                                         else
705                                                 dag_add_relation(dag, node2, node, DAG_RL_OB_OB, cti->name);
706                                 }
707                                 addtoroot = 0;
708                         }
709                         
710                         if (cti->flush_constraint_targets)
711                                 cti->flush_constraint_targets(con, &targets, 1);
712                 }
713         }
714
715         if (addtoroot == 1 )
716                 dag_add_relation(dag, scenenode, node, DAG_RL_SCENE, "Scene Relation");
717 }
718
719 struct DagForest *build_dag(Main *bmain, Scene *sce, short mask) 
720 {
721         Base *base;
722         Object *ob;
723         Group *group;
724         GroupObject *go;
725         DagNode *node;
726         DagNode *scenenode;
727         DagForest *dag;
728         DagAdjList *itA;
729
730         dag = sce->theDag;
731         sce->dagisvalid=1;
732         if ( dag)
733                 free_forest(dag);
734         else {
735                 dag = dag_init();
736                 sce->theDag = dag;
737         }
738         
739         /* add base node for scene. scene is always the first node in DAG */
740         scenenode = dag_add_node(dag, sce);     
741         
742         /* add current scene objects */
743         for (base = sce->base.first; base; base= base->next) {
744                 ob= base->object;
745                 
746                 build_dag_object(dag, scenenode, sce, ob, mask);
747                 if (ob->proxy)
748                         build_dag_object(dag, scenenode, sce, ob->proxy, mask);
749                 
750                 /* handled in next loop */
751                 if (ob->dup_group) 
752                         ob->dup_group->id.flag |= LIB_DOIT;
753         }
754         
755         /* add groups used in current scene objects */
756         for (group= bmain->group.first; group; group= group->id.next) {
757                 if (group->id.flag & LIB_DOIT) {
758                         for (go= group->gobject.first; go; go= go->next) {
759                                 build_dag_object(dag, scenenode, sce, go->ob, mask);
760                         }
761                         group->id.flag &= ~LIB_DOIT;
762                 }
763         }
764         
765         /* Now all relations were built, but we need to solve 1 exceptional case;
766          * When objects have multiple "parents" (for example parent + constraint working on same object)
767          * the relation type has to be synced. One of the parents can change, and should give same event to child */
768         
769         /* nodes were callocced, so we can use node->color for temporal storage */
770         for (node = sce->theDag->DagNode.first; node; node= node->next) {
771                 if (node->type==ID_OB) {
772                         for (itA = node->child; itA; itA= itA->next) {
773                                 if (itA->node->type==ID_OB) {
774                                         itA->node->color |= itA->type;
775                                 }
776                         }
777
778                         /* also flush custom data mask */
779                         ((Object*)node->ob)->customdata_mask= node->customdata_mask;
780                 }
781         }
782         /* now set relations equal, so that when only one parent changes, the correct recalcs are found */
783         for (node = sce->theDag->DagNode.first; node; node= node->next) {
784                 if (node->type==ID_OB) {
785                         for (itA = node->child; itA; itA= itA->next) {
786                                 if (itA->node->type==ID_OB) {
787                                         itA->type |= itA->node->color;
788                                 }
789                         }
790                 }
791         }
792         
793         // cycle detection and solving
794         // solve_cycles(dag);   
795         
796         return dag;
797 }
798
799
800 void free_forest(DagForest *Dag) 
801 {  /* remove all nodes and deps */
802         DagNode *tempN;
803         DagAdjList *tempA;      
804         DagAdjList *itA;
805         DagNode *itN = Dag->DagNode.first;
806         
807         while (itN) {
808                 itA = itN->child;       
809                 while (itA) {
810                         tempA = itA;
811                         itA = itA->next;
812                         MEM_freeN(tempA);                       
813                 }
814                 
815                 itA = itN->parent;      
816                 while (itA) {
817                         tempA = itA;
818                         itA = itA->next;
819                         MEM_freeN(tempA);                       
820                 }
821                 
822                 tempN = itN;
823                 itN = itN->next;
824                 MEM_freeN(tempN);
825         }
826
827         BLI_ghash_free(Dag->nodeHash, NULL, NULL);
828         Dag->nodeHash= NULL;
829         Dag->DagNode.first = NULL;
830         Dag->DagNode.last = NULL;
831         Dag->numNodes = 0;
832
833 }
834
835 DagNode * dag_find_node (DagForest *forest, void * fob)
836 {
837         if (forest->nodeHash)
838                 return BLI_ghash_lookup(forest->nodeHash, fob);
839
840         return NULL;
841 }
842
843 static int ugly_hack_sorry= 1;  // prevent type check
844 static int dag_print_dependencies= 0; // debugging
845
846 /* no checking of existence, use dag_find_node first or dag_get_node */
847 DagNode * dag_add_node (DagForest *forest, void * fob)
848 {
849         DagNode *node;
850                 
851         node = MEM_callocN(sizeof(DagNode), "DAG node");
852         if (node) {
853                 node->ob = fob;
854                 node->color = DAG_WHITE;
855
856                 if (ugly_hack_sorry) node->type = GS(((ID *) fob)->name);       // sorry, done for pose sorting
857                 if (forest->numNodes) {
858                         ((DagNode *) forest->DagNode.last)->next = node;
859                         forest->DagNode.last = node;
860                         forest->numNodes++;
861                 }
862                 else {
863                         forest->DagNode.last = node;
864                         forest->DagNode.first = node;
865                         forest->numNodes = 1;
866                 }
867
868                 if (!forest->nodeHash)
869                         forest->nodeHash= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "dag_add_node gh");
870                 BLI_ghash_insert(forest->nodeHash, fob, node);
871         }
872
873         return node;
874 }
875
876 DagNode * dag_get_node (DagForest *forest, void * fob)
877 {
878         DagNode *node;
879         
880         node = dag_find_node (forest, fob);     
881         if (!node) 
882                 node = dag_add_node(forest, fob);
883         return node;
884 }
885
886
887
888 DagNode * dag_get_sub_node (DagForest *forest, void * fob)
889 {
890         DagNode *node;
891         DagAdjList *mainchild, *prev=NULL;
892         
893         mainchild = ((DagNode *) forest->DagNode.first)->child;
894         /* remove from first node (scene) adj list if present */
895         while (mainchild) {
896                 if (mainchild->node == fob) {
897                         if (prev) {
898                                 prev->next = mainchild->next;
899                                 MEM_freeN(mainchild);
900                                 break;
901                         }
902                         else {
903                                 ((DagNode *) forest->DagNode.first)->child = mainchild->next;
904                                 MEM_freeN(mainchild);
905                                 break;
906                         }
907                 }
908                 prev = mainchild;
909                 mainchild = mainchild->next;
910         }
911         node = dag_find_node (forest, fob);     
912         if (!node) 
913                 node = dag_add_node(forest, fob);
914         return node;
915 }
916
917 static void dag_add_parent_relation(DagForest *UNUSED(forest), DagNode *fob1, DagNode *fob2, short rel, const char *name) 
918 {
919         DagAdjList *itA = fob2->parent;
920         
921         while (itA) { /* search if relation exist already */
922                 if (itA->node == fob1) {
923                         itA->type |= rel;
924                         itA->count += 1;
925                         return;
926                 }
927                 itA = itA->next;
928         }
929         /* create new relation and insert at head. MALLOC alert! */
930         itA = MEM_mallocN(sizeof(DagAdjList), "DAG adj list");
931         itA->node = fob1;
932         itA->type = rel;
933         itA->count = 1;
934         itA->next = fob2->parent;
935         itA->name = name;
936         fob2->parent = itA;
937 }
938
939 void dag_add_relation(DagForest *forest, DagNode *fob1, DagNode *fob2, short rel, const char *name) 
940 {
941         DagAdjList *itA = fob1->child;
942         
943         /* parent relation is for cycle checking */
944         dag_add_parent_relation(forest, fob1, fob2, rel, name);
945
946         while (itA) { /* search if relation exist already */
947                 if (itA->node == fob2) {
948                         itA->type |= rel;
949                         itA->count += 1;
950                         return;
951                 }
952                 itA = itA->next;
953         }
954         /* create new relation and insert at head. MALLOC alert! */
955         itA = MEM_mallocN(sizeof(DagAdjList), "DAG adj list");
956         itA->node = fob2;
957         itA->type = rel;
958         itA->count = 1;
959         itA->next = fob1->child;
960         itA->name = name;
961         fob1->child = itA;
962 }
963
964 static const char *dag_node_name(DagNode *node)
965 {
966         if (node->ob == NULL)
967                 return "null";
968         else if (ugly_hack_sorry)
969                 return ((ID*)(node->ob))->name+2;
970         else
971                 return ((bPoseChannel*)(node->ob))->name;
972 }
973
974 static void dag_node_print_dependencies(DagNode *node)
975 {
976         DagAdjList *itA;
977
978         printf("%s depends on:\n", dag_node_name(node));
979
980         for (itA= node->parent; itA; itA= itA->next)
981                 printf("  %s through %s\n", dag_node_name(itA->node), itA->name);
982         printf("\n");
983 }
984
985 static int dag_node_print_dependency_recurs(DagNode *node, DagNode *endnode)
986 {
987         DagAdjList *itA;
988
989         if (node->color == DAG_BLACK)
990                 return 0;
991
992         node->color= DAG_BLACK;
993
994         if (node == endnode)
995                 return 1;
996
997         for (itA= node->parent; itA; itA= itA->next) {
998                 if (dag_node_print_dependency_recurs(itA->node, endnode)) {
999                         printf("  %s depends on %s through %s.\n", dag_node_name(node), dag_node_name(itA->node), itA->name);
1000                         return 1;
1001                 }
1002         }
1003
1004         return 0;
1005 }
1006
1007 static void dag_node_print_dependency_cycle(DagForest *dag, DagNode *startnode, DagNode *endnode, const char *name)
1008 {
1009         DagNode *node;
1010
1011         for (node = dag->DagNode.first; node; node= node->next)
1012                 node->color= DAG_WHITE;
1013
1014         printf("  %s depends on %s through %s.\n", dag_node_name(endnode), dag_node_name(startnode), name);
1015         dag_node_print_dependency_recurs(startnode, endnode);
1016         printf("\n");
1017 }
1018
1019 static int dag_node_recurs_level(DagNode *node, int level)
1020 {
1021         DagAdjList *itA;
1022         int newlevel;
1023
1024         node->color= DAG_BLACK; /* done */
1025         newlevel= ++level;
1026         
1027         for (itA= node->parent; itA; itA= itA->next) {
1028                 if (itA->node->color==DAG_WHITE) {
1029                         itA->node->ancestor_count= dag_node_recurs_level(itA->node, level);
1030                         newlevel= MAX2(newlevel, level+itA->node->ancestor_count);
1031                 }
1032                 else
1033                         newlevel= MAX2(newlevel, level+itA->node->ancestor_count);
1034         }
1035         
1036         return newlevel;
1037 }
1038
1039 static void dag_check_cycle(DagForest *dag)
1040 {
1041         DagNode *node;
1042         DagAdjList *itA;
1043
1044         /* debugging print */
1045         if (dag_print_dependencies)
1046                 for (node = dag->DagNode.first; node; node= node->next)
1047                         dag_node_print_dependencies(node);
1048
1049         /* tag nodes unchecked */
1050         for (node = dag->DagNode.first; node; node= node->next)
1051                 node->color= DAG_WHITE;
1052         
1053         for (node = dag->DagNode.first; node; node= node->next) {
1054                 if (node->color==DAG_WHITE) {
1055                         node->ancestor_count= dag_node_recurs_level(node, 0);
1056                 }
1057         }
1058         
1059         /* check relations, and print errors */
1060         for (node = dag->DagNode.first; node; node= node->next) {
1061                 for (itA= node->parent; itA; itA= itA->next) {
1062                         if (itA->node->ancestor_count > node->ancestor_count) {
1063                                 if (node->ob && itA->node->ob) {
1064                                         printf("Dependency cycle detected:\n");
1065                                         dag_node_print_dependency_cycle(dag, itA->node, node, itA->name);
1066                                 }
1067                         }
1068                 }
1069         }
1070
1071         /* parent relations are only needed for cycle checking, so free now */
1072         for (node = dag->DagNode.first; node; node= node->next) {
1073                 while (node->parent) {
1074                         itA = node->parent->next;
1075                         MEM_freeN(node->parent);                        
1076                         node->parent = itA;
1077                 }
1078         }
1079 }
1080
1081 /*
1082  * MainDAG is the DAG of all objects in current scene
1083  * used only for drawing there is one also in each scene
1084  */
1085 static DagForest * MainDag = NULL;
1086
1087 DagForest *getMainDag(void)
1088 {
1089         return MainDag;
1090 }
1091
1092
1093 void setMainDag(DagForest *dag)
1094 {
1095         MainDag = dag;
1096 }
1097
1098
1099 /*
1100  * note for BFS/DFS
1101  * in theory we should sweep the whole array
1102  * but in our case the first node is the scene
1103  * and is linked to every other object
1104  *
1105  * for general case we will need to add outer loop
1106  */
1107
1108 /*
1109  * ToDo : change pos kludge
1110  */
1111
1112 /* adjust levels for drawing in oops space */
1113 void graph_bfs(void)
1114 {
1115         DagNode *node;
1116         DagNodeQueue *nqueue;
1117         int pos[50];
1118         int i;
1119         DagAdjList *itA;
1120         int minheight;
1121         
1122         /* fprintf(stderr, "starting BFS\n ------------\n"); */
1123         nqueue = queue_create(DAGQUEUEALLOC);
1124         for ( i=0; i<50; i++)
1125                 pos[i] = 0;
1126         
1127         /* Init
1128          * dagnode.first is always the root (scene)
1129          */
1130         node = MainDag->DagNode.first;
1131         while (node) {
1132                 node->color = DAG_WHITE;
1133                 node->BFS_dist = 9999;
1134                 node->k = 0;
1135                 node = node->next;
1136         }
1137         
1138         node = MainDag->DagNode.first;
1139         if (node->color == DAG_WHITE) {
1140                 node->color = DAG_GRAY;
1141                 node->BFS_dist = 1;
1142                 push_queue(nqueue, node);
1143                 while (nqueue->count) {
1144                         node = pop_queue(nqueue);
1145                         
1146                         minheight = pos[node->BFS_dist];
1147                         itA = node->child;
1148                         while (itA != NULL) {
1149                                 if (itA->node->color == DAG_WHITE) {
1150                                         itA->node->color = DAG_GRAY;
1151                                         itA->node->BFS_dist = node->BFS_dist + 1;
1152                                         itA->node->k = (float) minheight;
1153                                         push_queue(nqueue, itA->node);
1154                                 }
1155                                 
1156                                 else {
1157                                         fprintf(stderr, "bfs not dag tree edge color :%i\n", itA->node->color);
1158                                 }
1159
1160                                 
1161                                 itA = itA->next;
1162                         }
1163                         if (pos[node->BFS_dist] > node->k ) {
1164                                 pos[node->BFS_dist] += 1;                               
1165                                 node->k = (float) pos[node->BFS_dist];
1166                         }
1167                         else {
1168                                 pos[node->BFS_dist] = (int) node->k +1;
1169                         }
1170                         set_node_xy(node, node->BFS_dist*DEPSX*2, pos[node->BFS_dist]*DEPSY*2);
1171                         node->color = DAG_BLACK;
1172
1173                         // fprintf(stderr, "BFS node : %20s %i %5.0f %5.0f\n", ((ID *) node->ob)->name, node->BFS_dist, node->x, node->y);
1174                 }
1175         }
1176         queue_delete(nqueue);
1177 }
1178
1179 int pre_and_post_BFS(DagForest *dag, short mask, graph_action_func pre_func, graph_action_func post_func, void **data)
1180 {
1181         DagNode *node;
1182         
1183         node = dag->DagNode.first;
1184         return pre_and_post_source_BFS(dag, mask,  node,  pre_func,  post_func, data);
1185 }
1186
1187
1188 int pre_and_post_source_BFS(DagForest *dag, short mask, DagNode *source, graph_action_func pre_func, graph_action_func post_func, void **data)
1189 {
1190         DagNode *node;
1191         DagNodeQueue *nqueue;
1192         DagAdjList *itA;
1193         int     retval = 0;
1194         /* fprintf(stderr, "starting BFS\n ------------\n"); */
1195         
1196         /* Init
1197          * dagnode.first is always the root (scene)
1198          */
1199         node = dag->DagNode.first;
1200         nqueue = queue_create(DAGQUEUEALLOC);
1201         while (node) {
1202                 node->color = DAG_WHITE;
1203                 node->BFS_dist = 9999;
1204                 node = node->next;
1205         }
1206         
1207         node = source;
1208         if (node->color == DAG_WHITE) {
1209                 node->color = DAG_GRAY;
1210                 node->BFS_dist = 1;
1211                 pre_func(node->ob, data);
1212                 
1213                 while (nqueue->count) {
1214                         node = pop_queue(nqueue);
1215                         
1216                         itA = node->child;
1217                         while (itA != NULL) {
1218                                 if ((itA->node->color == DAG_WHITE) && (itA->type & mask)) {
1219                                         itA->node->color = DAG_GRAY;
1220                                         itA->node->BFS_dist = node->BFS_dist + 1;
1221                                         push_queue(nqueue, itA->node);
1222                                         pre_func(node->ob, data);
1223                                 }
1224                                 
1225                                 else { // back or cross edge
1226                                         retval = 1;
1227                                 }
1228                                 itA = itA->next;
1229                         }
1230                         post_func(node->ob, data);
1231                         node->color = DAG_BLACK;
1232
1233                         // fprintf(stderr, "BFS node : %20s %i %5.0f %5.0f\n", ((ID *) node->ob)->name, node->BFS_dist, node->x, node->y);
1234                 }
1235         }
1236         queue_delete(nqueue);
1237         return retval;
1238 }
1239
1240 /* non recursive version of DFS, return queue -- outer loop present to catch odd cases (first level cycles)*/
1241 DagNodeQueue * graph_dfs(void)
1242 {
1243         DagNode *node;
1244         DagNodeQueue *nqueue;
1245         DagNodeQueue *retqueue;
1246         int pos[50];
1247         int i;
1248         DagAdjList *itA;
1249         int time;
1250         int skip = 0;
1251         int minheight;
1252         int maxpos=0;
1253         /* int  is_cycle = 0; */ /* UNUSED */
1254         /*
1255          *fprintf(stderr, "starting DFS\n ------------\n");
1256          */     
1257         nqueue = queue_create(DAGQUEUEALLOC);
1258         retqueue = queue_create(MainDag->numNodes);
1259         for ( i=0; i<50; i++)
1260                 pos[i] = 0;
1261         
1262         /* Init
1263          * dagnode.first is always the root (scene)
1264          */
1265         node = MainDag->DagNode.first;
1266         while (node) {
1267                 node->color = DAG_WHITE;
1268                 node->DFS_dist = 9999;
1269                 node->DFS_dvtm = node->DFS_fntm = 9999;
1270                 node->k = 0;
1271                 node =  node->next;
1272         }
1273         
1274         time = 1;
1275         
1276         node = MainDag->DagNode.first;
1277
1278         do {
1279         if (node->color == DAG_WHITE) {
1280                 node->color = DAG_GRAY;
1281                 node->DFS_dist = 1;
1282                 node->DFS_dvtm = time;
1283                 time++;
1284                 push_stack(nqueue, node);
1285                         
1286                 while (nqueue->count) {
1287                         //graph_print_queue(nqueue);
1288
1289                         skip = 0;
1290                         node = get_top_node_queue(nqueue);
1291                         
1292                         minheight = pos[node->DFS_dist];
1293
1294                         itA = node->child;
1295                         while (itA != NULL) {
1296                                 if (itA->node->color == DAG_WHITE) {
1297                                         itA->node->DFS_dvtm = time;
1298                                         itA->node->color = DAG_GRAY;
1299
1300                                         time++;
1301                                         itA->node->DFS_dist = node->DFS_dist + 1;
1302                                         itA->node->k = (float) minheight;
1303                                         push_stack(nqueue, itA->node);
1304                                         skip = 1;
1305                                         break;
1306                                 }
1307                                 else {
1308                                         if (itA->node->color == DAG_GRAY) { // back edge
1309                                                 fprintf(stderr, "dfs back edge :%15s %15s\n", ((ID *) node->ob)->name, ((ID *) itA->node->ob)->name);
1310                                                 /* is_cycle = 1; */ /* UNUSED */
1311                                         }
1312                                         else if (itA->node->color == DAG_BLACK) {
1313                                                 /* already processed node but we may want later to change distance either to shorter to longer.
1314                                                  * DFS_dist is the first encounter  
1315                                                  */
1316 #if 0
1317                                                 if (node->DFS_dist >= itA->node->DFS_dist)
1318                                                         itA->node->DFS_dist = node->DFS_dist + 1;
1319
1320                                                         fprintf(stderr, "dfs forward or cross edge :%15s %i-%i %15s %i-%i\n",
1321                                                                 ((ID *) node->ob)->name,
1322                                                                 node->DFS_dvtm, 
1323                                                                 node->DFS_fntm, 
1324                                                                 ((ID *) itA->node->ob)->name, 
1325                                                                 itA->node->DFS_dvtm,
1326                                                                 itA->node->DFS_fntm);
1327 #endif
1328                                         }
1329                                         else
1330                                                 fprintf(stderr, "dfs unknown edge\n");
1331                                 }
1332                                 itA = itA->next;
1333                         }                       
1334
1335                         if (!skip) {
1336                                 node = pop_queue(nqueue);
1337                                 node->color = DAG_BLACK;
1338
1339                                 node->DFS_fntm = time;
1340                                 time++;
1341                                 if (node->DFS_dist > maxpos)
1342                                         maxpos = node->DFS_dist;
1343                                 if (pos[node->DFS_dist] > node->k ) {
1344                                         pos[node->DFS_dist] += 1;                               
1345                                         node->k = (float) pos[node->DFS_dist];
1346                                 }
1347                                 else {
1348                                         pos[node->DFS_dist] = (int) node->k +1;
1349                                 }
1350                                 set_node_xy(node, node->DFS_dist*DEPSX*2, pos[node->DFS_dist]*DEPSY*2);
1351                                 
1352                                 // 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 );
1353
1354                                 push_stack(retqueue, node);
1355                                 
1356                         }
1357                 }
1358         }
1359                 node = node->next;
1360         } while (node);
1361 //      fprintf(stderr, "i size : %i\n", maxpos);
1362
1363         queue_delete(nqueue);
1364         return(retqueue);
1365 }
1366
1367 /* unused */
1368 int pre_and_post_DFS(DagForest *dag, short mask, graph_action_func pre_func, graph_action_func post_func, void **data)
1369 {
1370         DagNode *node;
1371
1372         node = dag->DagNode.first;
1373         return pre_and_post_source_DFS(dag, mask,  node,  pre_func,  post_func, data);
1374 }
1375
1376 int pre_and_post_source_DFS(DagForest *dag, short mask, DagNode *source, graph_action_func pre_func, graph_action_func post_func, void **data)
1377 {
1378         DagNode *node;
1379         DagNodeQueue *nqueue;
1380         DagAdjList *itA;
1381         int time;
1382         int skip = 0;
1383         int retval = 0;
1384         /*
1385          *fprintf(stderr, "starting DFS\n ------------\n");
1386          */     
1387         nqueue = queue_create(DAGQUEUEALLOC);
1388         
1389         /* Init
1390          * dagnode.first is always the root (scene)
1391          */
1392         node = dag->DagNode.first;
1393         while (node) {
1394                 node->color = DAG_WHITE;
1395                 node->DFS_dist = 9999;
1396                 node->DFS_dvtm = node->DFS_fntm = 9999;
1397                 node->k = 0;
1398                 node =  node->next;
1399         }
1400         
1401         time = 1;
1402         
1403         node = source;
1404         do {
1405                 if (node->color == DAG_WHITE) {
1406                         node->color = DAG_GRAY;
1407                         node->DFS_dist = 1;
1408                         node->DFS_dvtm = time;
1409                         time++;
1410                         push_stack(nqueue, node);
1411                         pre_func(node->ob, data);
1412
1413                         while (nqueue->count) {
1414                                 skip = 0;
1415                                 node = get_top_node_queue(nqueue);
1416                                                                 
1417                                 itA = node->child;
1418                                 while (itA != NULL) {
1419                                         if ((itA->node->color == DAG_WHITE) && (itA->type & mask) ) {
1420                                                 itA->node->DFS_dvtm = time;
1421                                                 itA->node->color = DAG_GRAY;
1422                                                 
1423                                                 time++;
1424                                                 itA->node->DFS_dist = node->DFS_dist + 1;
1425                                                 push_stack(nqueue, itA->node);
1426                                                 pre_func(node->ob, data);
1427
1428                                                 skip = 1;
1429                                                 break;
1430                                         }
1431                                         else {
1432                                                 if (itA->node->color == DAG_GRAY) {// back edge
1433                                                         retval = 1;
1434                                                 }
1435 //                                              else if (itA->node->color == DAG_BLACK) { // cross or forward
1436 //
1437 //                                              }
1438                                         }
1439                                         itA = itA->next;
1440                                 }                       
1441                                 
1442                                 if (!skip) {
1443                                         node = pop_queue(nqueue);
1444                                         node->color = DAG_BLACK;
1445                                         
1446                                         node->DFS_fntm = time;
1447                                         time++;
1448                                         post_func(node->ob, data);
1449                                 }
1450                         }
1451                 }
1452                 node = node->next;
1453         } while (node);
1454         queue_delete(nqueue);
1455         return(retval);
1456 }
1457
1458
1459 // used to get the obs owning a datablock
1460 struct DagNodeQueue *get_obparents(struct DagForest     *dag, void *ob) 
1461 {
1462         DagNode * node, *node1;
1463         DagNodeQueue *nqueue;
1464         DagAdjList *itA;
1465
1466         node = dag_find_node(dag, ob);
1467         if (node==NULL) {
1468                 return NULL;
1469         }
1470         else if (node->ancestor_count == 1) { // simple case
1471                 nqueue = queue_create(1);
1472                 push_queue(nqueue, node);
1473         }
1474         else { /* need to go over the whole dag for adj list */
1475                 nqueue = queue_create(node->ancestor_count);
1476                 
1477                 node1 = dag->DagNode.first;
1478                 do {
1479                         if (node1->DFS_fntm > node->DFS_fntm) { // a parent is finished after child. must check adj list
1480                                 itA = node->child;
1481                                 while (itA != NULL) {
1482                                         if ((itA->node == node) && (itA->type == DAG_RL_DATA)) {
1483                                                 push_queue(nqueue, node);
1484                                         }
1485                                         itA = itA->next;
1486                                 }
1487                         }
1488                         node1 = node1->next;
1489                 } while (node1);
1490         }
1491         return nqueue;
1492 }
1493
1494 struct DagNodeQueue *get_first_ancestors(struct DagForest       *dag, void *ob)
1495 {
1496         DagNode * node, *node1;
1497         DagNodeQueue *nqueue;
1498         DagAdjList *itA;
1499         
1500         node = dag_find_node(dag, ob);
1501         
1502         // need to go over the whole dag for adj list
1503         nqueue = queue_create(node->ancestor_count);
1504         
1505         node1 = dag->DagNode.first;
1506         do {
1507                 if (node1->DFS_fntm > node->DFS_fntm) { 
1508                         itA = node->child;
1509                         while (itA != NULL) {
1510                                 if (itA->node == node) {
1511                                         push_queue(nqueue, node);
1512                                 }
1513                                 itA = itA->next;
1514                         }
1515                 }
1516                 node1 = node1->next;
1517         } while (node1);
1518         
1519         return nqueue;  
1520 }
1521
1522 // standard DFS list
1523 struct DagNodeQueue *get_all_childs(struct DagForest    *dag, void *ob)
1524 {
1525         DagNode *node;
1526         DagNodeQueue *nqueue;
1527         DagNodeQueue *retqueue;
1528         DagAdjList *itA;
1529         int time;
1530         int skip = 0;
1531
1532         nqueue = queue_create(DAGQUEUEALLOC);
1533         retqueue = queue_create(dag->numNodes); // was MainDag... why? (ton)
1534         
1535         node = dag->DagNode.first;
1536         while (node) {
1537                 node->color = DAG_WHITE;
1538                 node =  node->next;
1539         }
1540         
1541         time = 1;
1542         
1543         node = dag_find_node(dag, ob);   // could be done in loop above (ton)
1544         if (node) { // can be null for newly added objects
1545                 
1546                 node->color = DAG_GRAY;
1547                 time++;
1548                 push_stack(nqueue, node);
1549                 
1550                 while (nqueue->count) {
1551                         
1552                         skip = 0;
1553                         node = get_top_node_queue(nqueue);
1554                                         
1555                         itA = node->child;
1556                         while (itA != NULL) {
1557                                 if (itA->node->color == DAG_WHITE) {
1558                                         itA->node->DFS_dvtm = time;
1559                                         itA->node->color = DAG_GRAY;
1560                                         
1561                                         time++;
1562                                         push_stack(nqueue, itA->node);
1563                                         skip = 1;
1564                                         break;
1565                                 } 
1566                                 itA = itA->next;
1567                         }                       
1568                         
1569                         if (!skip) {
1570                                 node = pop_queue(nqueue);
1571                                 node->color = DAG_BLACK;
1572                                 
1573                                 time++;
1574                                 push_stack(retqueue, node);
1575                         }
1576                 }
1577         }
1578         queue_delete(nqueue);
1579         return(retqueue);
1580 }
1581
1582 /* unused */
1583 #if 0
1584 short   are_obs_related(struct DagForest        *dag, void *ob1, void *ob2)
1585 {
1586         DagNode * node;
1587         DagAdjList *itA;
1588         
1589         node = dag_find_node(dag, ob1);
1590         
1591         itA = node->child;
1592         while (itA != NULL) {
1593                 if (itA->node->ob == ob2) {
1594                         return itA->node->type;
1595                 } 
1596                 itA = itA->next;
1597         }
1598         return DAG_NO_RELATION;
1599 }
1600 #endif
1601
1602 int     is_acyclic(DagForest *dag)
1603 {
1604         return dag->is_acyclic;
1605 }
1606
1607 void set_node_xy(DagNode *node, float x, float y)
1608 {
1609         node->x = x;
1610         node->y = y;
1611 }
1612
1613
1614 /* debug test functions */
1615
1616 void graph_print_queue(DagNodeQueue *nqueue)
1617 {       
1618         DagNodeQueueElem *queueElem;
1619         
1620         queueElem = nqueue->first;
1621         while (queueElem) {
1622                 fprintf(stderr, "** %s %i %i-%i ", ((ID *) queueElem->node->ob)->name, queueElem->node->color, queueElem->node->DFS_dvtm, queueElem->node->DFS_fntm);
1623                 queueElem = queueElem->next;            
1624         }
1625         fprintf(stderr, "\n");
1626 }
1627
1628 void graph_print_queue_dist(DagNodeQueue *nqueue)
1629 {       
1630         DagNodeQueueElem *queueElem;
1631         int count;
1632         
1633         queueElem = nqueue->first;
1634         count = 0;
1635         while (queueElem) {
1636                 fprintf(stderr, "** %25s %2.2i-%2.2i ", ((ID *) queueElem->node->ob)->name, queueElem->node->DFS_dvtm, queueElem->node->DFS_fntm);
1637                 while (count < queueElem->node->DFS_dvtm-1) { fputc(' ', stderr); count++;}
1638                 fputc('|', stderr);
1639                 while (count < queueElem->node->DFS_fntm-2) { fputc('-', stderr); count++;}
1640                 fputc('|', stderr);
1641                 fputc('\n', stderr);
1642                 count = 0;
1643                 queueElem = queueElem->next;            
1644         }
1645         fprintf(stderr, "\n");
1646 }
1647
1648 void graph_print_adj_list(void)
1649 {
1650         DagNode *node;
1651         DagAdjList *itA;
1652         
1653         node = (getMainDag())->DagNode.first;
1654         while (node) {
1655                 fprintf(stderr, "node : %s col: %i", ((ID *) node->ob)->name, node->color);
1656                 itA = node->child;
1657                 while (itA) {
1658                         fprintf(stderr, "-- %s ", ((ID *) itA->node->ob)->name);
1659                         
1660                         itA = itA->next;
1661                 }
1662                 fprintf(stderr, "\n");
1663                 node = node->next;
1664         }
1665 }
1666
1667 /* ************************ API *********************** */
1668
1669 /* mechanism to allow editors to be informed of depsgraph updates,
1670  * to do their own updates based on changes... */
1671 static void (*EditorsUpdateIDCb)(Main *bmain, ID *id)= NULL;
1672 static void (*EditorsUpdateSceneCb)(Main *bmain, Scene *scene, int updated)= NULL;
1673
1674 void DAG_editors_update_cb(void (*id_func)(Main *bmain, ID *id), void (*scene_func)(Main *bmain, Scene *scene, int updated))
1675 {
1676         EditorsUpdateIDCb= id_func;
1677         EditorsUpdateSceneCb= scene_func;
1678 }
1679
1680 static void dag_editors_id_update(Main *bmain, ID *id)
1681 {
1682         if (EditorsUpdateIDCb)
1683                 EditorsUpdateIDCb(bmain, id);
1684 }
1685
1686 static void dag_editors_scene_update(Main *bmain, Scene *scene, int updated)
1687 {
1688         if (EditorsUpdateSceneCb)
1689                 EditorsUpdateSceneCb(bmain, scene, updated);
1690 }
1691
1692 /* groups with objects in this scene need to be put in the right order as well */
1693 static void scene_sort_groups(Main *bmain, Scene *sce)
1694 {
1695         Base *base;
1696         Group *group;
1697         GroupObject *go;
1698         Object *ob;
1699         
1700         /* test; are group objects all in this scene? */
1701         for (ob= bmain->object.first; ob; ob= ob->id.next) {
1702                 ob->id.flag &= ~LIB_DOIT;
1703                 ob->id.newid= NULL;     /* newid abuse for GroupObject */
1704         }
1705         for (base = sce->base.first; base; base= base->next)
1706                 base->object->id.flag |= LIB_DOIT;
1707         
1708         for (group= bmain->group.first; group; group= group->id.next) {
1709                 for (go= group->gobject.first; go; go= go->next) {
1710                         if ((go->ob->id.flag & LIB_DOIT)==0)
1711                                 break;
1712                 }
1713                 /* this group is entirely in this scene */
1714                 if (go==NULL) {
1715                         ListBase listb= {NULL, NULL};
1716                         
1717                         for (go= group->gobject.first; go; go= go->next)
1718                                 go->ob->id.newid= (ID *)go;
1719                         
1720                         /* in order of sorted bases we reinsert group objects */
1721                         for (base = sce->base.first; base; base= base->next) {
1722                                 
1723                                 if (base->object->id.newid) {
1724                                         go= (GroupObject *)base->object->id.newid;
1725                                         base->object->id.newid= NULL;
1726                                         BLI_remlink(&group->gobject, go);
1727                                         BLI_addtail(&listb, go);
1728                                 }
1729                         }
1730                         /* copy the newly sorted listbase */
1731                         group->gobject= listb;
1732                 }
1733         }
1734 }
1735
1736 /* sort the base list on dependency order */
1737 void DAG_scene_sort(Main *bmain, Scene *sce)
1738 {
1739         DagNode *node, *rootnode;
1740         DagNodeQueue *nqueue;
1741         DagAdjList *itA;
1742         int time;
1743         int skip = 0;
1744         ListBase tempbase;
1745         Base *base;
1746         
1747         tempbase.first= tempbase.last= NULL;
1748         
1749         build_dag(bmain, sce, DAG_RL_ALL_BUT_DATA);
1750         
1751         dag_check_cycle(sce->theDag);
1752
1753         nqueue = queue_create(DAGQUEUEALLOC);
1754         
1755         for (node = sce->theDag->DagNode.first; node; node= node->next) {
1756                 node->color = DAG_WHITE;
1757         }
1758         
1759         time = 1;
1760         
1761         rootnode = sce->theDag->DagNode.first;
1762         rootnode->color = DAG_GRAY;
1763         time++;
1764         push_stack(nqueue, rootnode);
1765         
1766         while (nqueue->count) {
1767                 
1768                 skip = 0;
1769                 node = get_top_node_queue(nqueue);
1770                 
1771                 itA = node->child;
1772                 while (itA != NULL) {
1773                         if (itA->node->color == DAG_WHITE) {
1774                                 itA->node->DFS_dvtm = time;
1775                                 itA->node->color = DAG_GRAY;
1776                                 
1777                                 time++;
1778                                 push_stack(nqueue, itA->node);
1779                                 skip = 1;
1780                                 break;
1781                         } 
1782                         itA = itA->next;
1783                 }                       
1784                 
1785                 if (!skip) {
1786                         if (node) {
1787                                 node = pop_queue(nqueue);
1788                                 if (node->ob == sce)    // we are done
1789                                         break;
1790                                 node->color = DAG_BLACK;
1791                                 
1792                                 time++;
1793                                 base = sce->base.first;
1794                                 while (base && base->object != node->ob)
1795                                         base = base->next;
1796                                 if (base) {
1797                                         BLI_remlink(&sce->base, base);
1798                                         BLI_addhead(&tempbase, base);
1799                                 }
1800                         }       
1801                 }
1802         }
1803         
1804         /* temporal correction for circular dependencies */
1805         base = sce->base.first;
1806         while (base) {
1807                 BLI_remlink(&sce->base, base);
1808                 BLI_addhead(&tempbase, base);
1809                 //if (G.debug & G_DEBUG)
1810                         printf("cyclic %s\n", base->object->id.name);
1811                 base = sce->base.first;
1812         }
1813         
1814         sce->base = tempbase;
1815         queue_delete(nqueue);
1816         
1817         /* all groups with objects in this scene gets resorted too */
1818         scene_sort_groups(bmain, sce);
1819         
1820         if (G.debug & G_DEBUG) {
1821                 printf("\nordered\n");
1822                 for (base = sce->base.first; base; base= base->next) {
1823                         printf(" %s\n", base->object->id.name);
1824                 }
1825         }
1826         /* temporal...? */
1827         sce->recalc |= SCE_PRV_CHANGED; /* test for 3d preview */
1828 }
1829
1830 static void lib_id_recalc_tag(Main *bmain, ID *id)
1831 {
1832         id->flag |= LIB_ID_RECALC;
1833         bmain->id_tag_update[id->name[0]] = 1;
1834 }
1835
1836 static void lib_id_recalc_data_tag(Main *bmain, ID *id)
1837 {
1838         id->flag |= LIB_ID_RECALC_DATA;
1839         bmain->id_tag_update[id->name[0]] = 1;
1840 }
1841
1842 /* node was checked to have lasttime != curtime and is if type ID_OB */
1843 static void flush_update_node(DagNode *node, unsigned int layer, int curtime)
1844 {
1845         Main *bmain= G.main;
1846         DagAdjList *itA;
1847         Object *ob, *obc;
1848         int oldflag, changed=0;
1849         unsigned int all_layer;
1850         
1851         node->lasttime= curtime;
1852         
1853         ob= node->ob;
1854         if (ob && (ob->recalc & OB_RECALC_ALL)) {
1855                 all_layer= node->scelay;
1856
1857                 /* got an object node that changes, now check relations */
1858                 for (itA = node->child; itA; itA= itA->next) {
1859                         all_layer |= itA->lay;
1860                         /* the relationship is visible */
1861                         if ((itA->lay & layer)) { // XXX || (itA->node->ob == obedit)
1862                                 if (itA->node->type==ID_OB) {
1863                                         obc= itA->node->ob;
1864                                         oldflag= obc->recalc;
1865                                         
1866                                         /* got a ob->obc relation, now check if flag needs flush */
1867                                         if (ob->recalc & OB_RECALC_OB) {
1868                                                 if (itA->type & DAG_RL_OB_OB) {
1869                                                         //printf("ob %s changes ob %s\n", ob->id.name, obc->id.name);
1870                                                         obc->recalc |= OB_RECALC_OB;
1871                                                         lib_id_recalc_tag(bmain, &obc->id);
1872                                                 }
1873                                                 if (itA->type & DAG_RL_OB_DATA) {
1874                                                         //printf("ob %s changes obdata %s\n", ob->id.name, obc->id.name);
1875                                                         obc->recalc |= OB_RECALC_DATA;
1876                                                         lib_id_recalc_data_tag(bmain, &obc->id);
1877                                                 }
1878                                         }
1879                                         if (ob->recalc & OB_RECALC_DATA) {
1880                                                 if (itA->type & DAG_RL_DATA_OB) {
1881                                                         //printf("obdata %s changes ob %s\n", ob->id.name, obc->id.name);
1882                                                         obc->recalc |= OB_RECALC_OB;
1883                                                         lib_id_recalc_tag(bmain, &obc->id);
1884                                                 }
1885                                                 if (itA->type & DAG_RL_DATA_DATA) {
1886                                                         //printf("obdata %s changes obdata %s\n", ob->id.name, obc->id.name);
1887                                                         obc->recalc |= OB_RECALC_DATA;
1888                                                         lib_id_recalc_data_tag(bmain, &obc->id);
1889                                                 }
1890                                         }
1891                                         if (oldflag!=obc->recalc) changed= 1;
1892                                 }
1893                         }
1894                 }
1895                 /* even nicer, we can clear recalc flags...  */
1896                 if ((all_layer & layer)==0) { // XXX && (ob != obedit)) {
1897                         /* but existing displaylists or derivedmesh should be freed */
1898                         if (ob->recalc & OB_RECALC_DATA)
1899                                 BKE_object_free_display(ob);
1900                         
1901                         ob->recalc &= ~OB_RECALC_ALL;
1902                 }
1903         }
1904         
1905         /* check case where child changes and parent forcing obdata to change */
1906         /* should be done regardless if this ob has recalc set */
1907         /* could merge this in with loop above...? (ton) */
1908         for (itA = node->child; itA; itA= itA->next) {
1909                 /* the relationship is visible */
1910                 if ((itA->lay & layer)) {               // XXX  || (itA->node->ob == obedit)
1911                         if (itA->node->type==ID_OB) {
1912                                 obc= itA->node->ob;
1913                                 /* child moves */
1914                                 if ((obc->recalc & OB_RECALC_ALL)==OB_RECALC_OB) {
1915                                         /* parent has deforming info */
1916                                         if (itA->type & (DAG_RL_OB_DATA|DAG_RL_DATA_DATA)) {
1917                                                 // printf("parent %s changes ob %s\n", ob->id.name, obc->id.name);
1918                                                 obc->recalc |= OB_RECALC_DATA;
1919                                                 lib_id_recalc_data_tag(bmain, &obc->id);
1920                                         }
1921                                 }
1922                         }
1923                 }
1924         }
1925         
1926         /* we only go deeper if node not checked or something changed  */
1927         for (itA = node->child; itA; itA= itA->next) {
1928                 if (changed || itA->node->lasttime!=curtime) 
1929                         flush_update_node(itA->node, layer, curtime);
1930         }
1931         
1932 }
1933
1934 /* node was checked to have lasttime != curtime, and is of type ID_OB */
1935 static unsigned int flush_layer_node(Scene *sce, DagNode *node, int curtime)
1936 {
1937         DagAdjList *itA;
1938         
1939         node->lasttime= curtime;
1940         node->lay= node->scelay;
1941         
1942         for (itA = node->child; itA; itA= itA->next) {
1943                 if (itA->node->type==ID_OB) {
1944                         if (itA->node->lasttime!=curtime) {
1945                                 itA->lay= flush_layer_node(sce, itA->node, curtime);  // lay is only set once for each relation
1946                         }
1947                         else itA->lay= itA->node->lay;
1948                         
1949                         node->lay |= itA->lay;
1950                 }
1951         }
1952
1953         return node->lay;
1954 }
1955
1956 /* node was checked to have lasttime != curtime, and is of type ID_OB */
1957 static void flush_pointcache_reset(Scene *scene, DagNode *node, int curtime, int reset)
1958 {
1959         Main *bmain= G.main;
1960         DagAdjList *itA;
1961         Object *ob;
1962         
1963         node->lasttime= curtime;
1964         
1965         for (itA = node->child; itA; itA= itA->next) {
1966                 if (itA->node->type==ID_OB) {
1967                         if (itA->node->lasttime!=curtime) {
1968                                 ob= (Object*)(itA->node->ob);
1969
1970                                 if (reset || (ob->recalc & OB_RECALC_ALL)) {
1971                                         if (BKE_ptcache_object_reset(scene, ob, PTCACHE_RESET_DEPSGRAPH)) {
1972                                                 ob->recalc |= OB_RECALC_DATA;
1973                                                 lib_id_recalc_data_tag(bmain, &ob->id);
1974                                         }
1975
1976                                         flush_pointcache_reset(scene, itA->node, curtime, 1);
1977                                 }
1978                                 else
1979                                         flush_pointcache_reset(scene, itA->node, curtime, 0);
1980                         }
1981                 }
1982         }
1983 }
1984
1985 /* flush layer flags to dependencies */
1986 static void dag_scene_flush_layers(Scene *sce, int lay)
1987 {
1988         DagNode *node, *firstnode;
1989         DagAdjList *itA;
1990         Base *base;
1991         int lasttime;
1992
1993         firstnode= sce->theDag->DagNode.first;  // always scene node
1994
1995         for (itA = firstnode->child; itA; itA= itA->next)
1996                 itA->lay= 0;
1997
1998         sce->theDag->time++;    // so we know which nodes were accessed
1999         lasttime= sce->theDag->time;
2000
2001         /* update layer flags in nodes */
2002         for (base= sce->base.first; base; base= base->next) {
2003                 node= dag_get_node(sce->theDag, base->object);
2004                 node->scelay= base->object->lay;
2005         }
2006
2007         /* ensure cameras are set as if they are on a visible layer, because
2008          * they ared still used for rendering or setting the camera view
2009          *
2010          * XXX, this wont work for local view / unlocked camera's */
2011         if (sce->camera) {
2012                 node= dag_get_node(sce->theDag, sce->camera);
2013                 node->scelay |= lay;
2014         }
2015
2016 #ifdef DURIAN_CAMERA_SWITCH
2017         {
2018                 TimeMarker *m;
2019
2020                 for (m= sce->markers.first; m; m= m->next) {
2021                         if (m->camera) {
2022                                 node= dag_get_node(sce->theDag, m->camera);
2023                                 node->scelay |= lay;
2024                         }
2025                 }
2026         }
2027 #endif
2028
2029         /* flush layer nodes to dependencies */
2030         for (itA = firstnode->child; itA; itA= itA->next)
2031                 if (itA->node->lasttime!=lasttime && itA->node->type==ID_OB) 
2032                         flush_layer_node(sce, itA->node, lasttime);
2033 }
2034
2035 static void dag_tag_renderlayers(Scene *sce, unsigned int lay)
2036 {
2037         if (sce->nodetree) {
2038                 bNode *node;
2039                 Base *base;
2040                 unsigned int lay_changed= 0;
2041                 
2042                 for (base= sce->base.first; base; base= base->next)
2043                         if (base->lay & lay)
2044                                 if (base->object->recalc)
2045                                         lay_changed |= base->lay;
2046                         
2047                 for (node= sce->nodetree->nodes.first; node; node= node->next) {
2048                         if (node->id==(ID *)sce) {
2049                                 SceneRenderLayer *srl= BLI_findlink(&sce->r.layers, node->custom1);
2050                                 if (srl && (srl->lay & lay_changed))
2051                                         nodeUpdate(sce->nodetree, node);
2052                         }
2053                 }
2054         }
2055 }
2056
2057 /* flushes all recalc flags in objects down the dependency tree */
2058 void DAG_scene_flush_update(Main *bmain, Scene *sce, unsigned int lay, const short time)
2059 {
2060         DagNode *firstnode;
2061         DagAdjList *itA;
2062         Object *ob;
2063         int lasttime;
2064         
2065         if (sce->theDag==NULL) {
2066                 printf("DAG zero... not allowed to happen!\n");
2067                 DAG_scene_sort(bmain, sce);
2068         }
2069         
2070         firstnode= sce->theDag->DagNode.first;  // always scene node
2071
2072         /* first we flush the layer flags */
2073         dag_scene_flush_layers(sce, lay);
2074
2075         /* then we use the relationships + layer info to flush update events */
2076         sce->theDag->time++;    // so we know which nodes were accessed
2077         lasttime= sce->theDag->time;
2078         for (itA = firstnode->child; itA; itA= itA->next)
2079                 if (itA->node->lasttime!=lasttime && itA->node->type==ID_OB)
2080                         flush_update_node(itA->node, lay, lasttime);
2081
2082         /* if update is not due to time change, do pointcache clears */
2083         if (!time) {
2084                 sce->theDag->time++;    // so we know which nodes were accessed
2085                 lasttime= sce->theDag->time;
2086                 for (itA = firstnode->child; itA; itA= itA->next) {
2087                         if (itA->node->lasttime!=lasttime && itA->node->type==ID_OB) {
2088                                 ob= (Object*)(itA->node->ob);
2089
2090                                 if (ob->recalc & OB_RECALC_ALL) {
2091                                         if (BKE_ptcache_object_reset(sce, ob, PTCACHE_RESET_DEPSGRAPH)) {
2092                                                 ob->recalc |= OB_RECALC_DATA;
2093                                                 lib_id_recalc_data_tag(bmain, &ob->id);
2094                                         }
2095
2096                                         flush_pointcache_reset(sce, itA->node, lasttime, 1);
2097                                 }
2098                                 else
2099                                         flush_pointcache_reset(sce, itA->node, lasttime, 0);
2100                         }
2101                 }
2102         }
2103         
2104         dag_tag_renderlayers(sce, lay);
2105 }
2106
2107 static int object_modifiers_use_time(Object *ob)
2108 {
2109         ModifierData *md;
2110         
2111         /* check if a modifier in modifier stack needs time input */
2112         for (md=ob->modifiers.first; md; md=md->next)
2113                 if (modifier_dependsOnTime(md))
2114                         return 1;
2115         
2116         /* check whether any modifiers are animated */
2117         if (ob->adt) {
2118                 AnimData *adt = ob->adt;
2119                 FCurve *fcu;
2120                 
2121                 /* action - check for F-Curves with paths containing 'modifiers[' */
2122                 if (adt->action) {
2123                         for (fcu = adt->action->curves.first; fcu; fcu = fcu->next) {
2124                                 if (fcu->rna_path && strstr(fcu->rna_path, "modifiers["))
2125                                         return 1;
2126                         }
2127                 }
2128                 
2129                 /* This here allows modifier properties to get driven and still update properly
2130                  *
2131                  * Workaround to get [#26764] (e.g. subsurf levels not updating when animated/driven)
2132                  * working, without the updating problems ([#28525] [#28690] [#28774] [#28777]) caused
2133                  * by the RNA updates cache introduced in r.38649
2134                  */
2135                 for (fcu = adt->drivers.first; fcu; fcu = fcu->next) {
2136                         if (fcu->rna_path && strstr(fcu->rna_path, "modifiers["))
2137                                 return 1;
2138                 }
2139                 
2140                 // XXX: also, should check NLA strips, though for now assume that nobody uses
2141                 // that and we can omit that for performance reasons...
2142         }
2143         
2144         return 0;
2145 }
2146
2147 static short animdata_use_time(AnimData *adt)
2148 {
2149         NlaTrack *nlt;
2150         
2151         if (adt==NULL) return 0;
2152         
2153         /* check action - only if assigned, and it has anim curves */
2154         if (adt->action && adt->action->curves.first)
2155                 return 1;
2156         
2157         /* check NLA tracks + strips */
2158         for (nlt= adt->nla_tracks.first; nlt; nlt= nlt->next) {
2159                 if (nlt->strips.first)
2160                         return 1;
2161         }
2162         
2163         /* If we have drivers, more likely than not, on a frame change
2164          * they'll need updating because their owner changed
2165          * 
2166          * This is kindof a hack to get around a whole host of problems
2167          * involving drivers using non-object datablock data (which the 
2168          * depsgraph currently has no way of representing let alone correctly
2169          * dependency sort+tagging). By doing this, at least we ensure that 
2170          * some commonly attempted drivers (such as scene -> current frame;
2171          * see "Driver updates fail" thread on Bf-committers dated July 2)
2172          * will work correctly, and that other non-object datablocks will have
2173          * their drivers update at least on frame change.
2174          *
2175          * -- Aligorith, July 4 2011
2176          */
2177         if (adt->drivers.first)
2178                 return 1;
2179         
2180         return 0;
2181 }
2182
2183 static void dag_object_time_update_flags(Object *ob)
2184 {
2185         if (ob->constraints.first) {
2186                 bConstraint *con;
2187                 for (con = ob->constraints.first; con; con=con->next) {
2188                         bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
2189                         ListBase targets = {NULL, NULL};
2190                         bConstraintTarget *ct;
2191                         
2192                         if (cti) {
2193                                 /* special case for camera tracking -- it doesn't use targets to define relations */
2194                                 if (ELEM3(cti->type, CONSTRAINT_TYPE_FOLLOWTRACK, CONSTRAINT_TYPE_CAMERASOLVER, CONSTRAINT_TYPE_OBJECTSOLVER)) {
2195                                         ob->recalc |= OB_RECALC_OB;
2196                                 }
2197                                 else if (cti->get_constraint_targets) {
2198                                         cti->get_constraint_targets(con, &targets);
2199                                         
2200                                         for (ct= targets.first; ct; ct= ct->next) {
2201                                                 if (ct->tar) {
2202                                                         ob->recalc |= OB_RECALC_OB;
2203                                                         break;
2204                                                 }
2205                                         }
2206                                         
2207                                         if (cti->flush_constraint_targets)
2208                                                 cti->flush_constraint_targets(con, &targets, 1);
2209                                 }
2210                                 
2211                         }
2212                 }
2213         }
2214         
2215         if (ob->parent) {
2216                 /* motion path or bone child */
2217                 if (ob->parent->type==OB_CURVE || ob->parent->type==OB_ARMATURE) ob->recalc |= OB_RECALC_OB;
2218         }
2219         
2220 #if 0 // XXX old animation system
2221         if (ob->nlastrips.first) {
2222                 if (ob->dup_group) {
2223                         bActionStrip *strip;
2224                         /* this case is for groups with nla, whilst nla target has no action or nla */
2225                         for (strip= ob->nlastrips.first; strip; strip= strip->next) {
2226                                 if (strip->object)
2227                                         strip->object->recalc |= OB_RECALC_ALL;
2228                         }
2229                 }
2230         }
2231 #endif // XXX old animation system
2232         
2233         if (animdata_use_time(ob->adt)) {
2234                 ob->recalc |= OB_RECALC_OB;
2235                 ob->adt->recalc |= ADT_RECALC_ANIM;
2236         }
2237         
2238         if ((ob->adt) && (ob->type==OB_ARMATURE)) ob->recalc |= OB_RECALC_DATA;
2239         
2240         if (object_modifiers_use_time(ob)) ob->recalc |= OB_RECALC_DATA;
2241         if ((ob->pose) && (ob->pose->flag & POSE_CONSTRAINTS_TIMEDEPEND)) ob->recalc |= OB_RECALC_DATA;
2242         
2243         {
2244                 AnimData *adt= BKE_animdata_from_id((ID *)ob->data);
2245                 Mesh *me;
2246                 Curve *cu;
2247                 Lattice *lt;
2248                 
2249                 switch (ob->type) {
2250                         case OB_MESH:
2251                                 me= ob->data;
2252                                 if (me->key) {
2253                                         if (!(ob->shapeflag & OB_SHAPE_LOCK)) {
2254                                                 ob->recalc |= OB_RECALC_DATA;
2255                                         }
2256                                 }
2257                                 if (ob->particlesystem.first)
2258                                         ob->recalc |= OB_RECALC_DATA;
2259                                 break;
2260                         case OB_CURVE:
2261                         case OB_SURF:
2262                                 cu= ob->data;
2263                                 if (cu->key) {
2264                                         if (!(ob->shapeflag & OB_SHAPE_LOCK)) {
2265                                                 ob->recalc |= OB_RECALC_DATA;
2266                                         }
2267                                 }
2268                                 break;
2269                         case OB_FONT:
2270                                 cu= ob->data;
2271                                 if (cu->nurb.first==NULL && cu->str && cu->vfont)
2272                                         ob->recalc |= OB_RECALC_DATA;
2273                                 break;
2274                         case OB_LATTICE:
2275                                 lt= ob->data;
2276                                 if (lt->key) {
2277                                         if (!(ob->shapeflag & OB_SHAPE_LOCK)) {
2278                                                 ob->recalc |= OB_RECALC_DATA;
2279                                         }
2280                                 }
2281                                         break;
2282                         case OB_MBALL:
2283                                 if (ob->transflag & OB_DUPLI) ob->recalc |= OB_RECALC_DATA;
2284                                 break;
2285                 }
2286                 
2287                 if (animdata_use_time(adt)) {
2288                         ob->recalc |= OB_RECALC_DATA;
2289                         adt->recalc |= ADT_RECALC_ANIM;
2290                 }
2291
2292                 if (ob->particlesystem.first) {
2293                         ParticleSystem *psys= ob->particlesystem.first;
2294
2295                         for (; psys; psys=psys->next) {
2296                                 if (psys_check_enabled(ob, psys)) {
2297                                         ob->recalc |= OB_RECALC_DATA;
2298                                         break;
2299                                 }
2300                         }
2301                 }
2302         }               
2303
2304         if (ob->recalc & OB_RECALC_OB)
2305                 lib_id_recalc_tag(G.main, &ob->id);
2306         if (ob->recalc & OB_RECALC_DATA)
2307                 lib_id_recalc_data_tag(G.main, &ob->id);
2308
2309 }
2310 /* flag all objects that need recalc, for changes in time for example */
2311 /* do_time: make this optional because undo resets objects to their animated locations without this */
2312 void DAG_scene_update_flags(Main *bmain, Scene *scene, unsigned int lay, const short do_time)
2313 {
2314         Base *base;
2315         Object *ob;
2316         Group *group;
2317         GroupObject *go;
2318         Scene *sce_iter;
2319
2320         /* set ob flags where animated systems are */
2321         for (SETLOOPER(scene, sce_iter, base)) {
2322                 ob= base->object;
2323
2324                 if (do_time) {
2325                         /* now if DagNode were part of base, the node->lay could be checked... */
2326                         /* we do all now, since the scene_flush checks layers and clears recalc flags even */
2327                         dag_object_time_update_flags(ob);
2328                 }
2329
2330                 /* handled in next loop */
2331                 if (ob->dup_group)
2332                         ob->dup_group->id.flag |= LIB_DOIT;
2333         }
2334
2335         if (do_time) {
2336                 /* we do groups each once */
2337                 for (group= bmain->group.first; group; group= group->id.next) {
2338                         if (group->id.flag & LIB_DOIT) {
2339                                 for (go= group->gobject.first; go; go= go->next) {
2340                                         dag_object_time_update_flags(go->ob);
2341                                 }
2342                         }
2343                 }
2344         }
2345
2346         for (sce_iter= scene; sce_iter; sce_iter= sce_iter->set)
2347                 DAG_scene_flush_update(bmain, sce_iter, lay, 1);
2348         
2349         if (do_time) {
2350                 /* test: set time flag, to disable baked systems to update */
2351                 for (SETLOOPER(scene, sce_iter, base)) {
2352                         ob= base->object;
2353                         if (ob->recalc)
2354                                 ob->recalc |= OB_RECALC_TIME;
2355                 }
2356
2357                 /* hrmf... an exception to look at once, for invisible camera object we do it over */
2358                 if (scene->camera)
2359                         dag_object_time_update_flags(scene->camera);
2360         }
2361
2362         /* and store the info in groupobject */
2363         for (group= bmain->group.first; group; group= group->id.next) {
2364                 if (group->id.flag & LIB_DOIT) {
2365                         for (go= group->gobject.first; go; go= go->next) {
2366                                 go->recalc= go->ob->recalc;
2367                                 // printf("ob %s recalc %d\n", go->ob->id.name, go->recalc);
2368                         }
2369                         group->id.flag &= ~LIB_DOIT;
2370                 }
2371         }
2372         
2373 }
2374
2375 static void dag_current_scene_layers(Main *bmain, Scene **sce, unsigned int *lay)
2376 {
2377         wmWindowManager *wm;
2378         wmWindow *win;
2379
2380         /* only one scene supported currently, making more scenes work
2381          * correctly requires changes beyond just the dependency graph */
2382
2383         *sce= NULL;
2384         *lay= 0;
2385
2386         if ((wm= bmain->wm.first)) {
2387                 /* if we have a windowmanager, look into windows */
2388                 for (win=wm->windows.first; win; win=win->next) {
2389                         if (win->screen) {
2390                                 if (!*sce) *sce= win->screen->scene;
2391                                 *lay |= BKE_screen_visible_layers(win->screen, win->screen->scene);
2392                         }
2393                 }
2394         }
2395         else {
2396                 /* if not, use the first sce */
2397                 *sce= bmain->scene.first;
2398                 if (*sce) *lay= (*sce)->lay;
2399
2400                 /* XXX for background mode, we should get the scene
2401                  * from somewhere, for the -S option, but it's in
2402                  * the context, how to get it here? */
2403         }
2404 }
2405
2406 void DAG_ids_flush_update(Main *bmain, int time)
2407 {
2408         Scene *sce;
2409         unsigned int lay;
2410
2411         dag_current_scene_layers(bmain, &sce, &lay);
2412
2413         if (sce)
2414                 DAG_scene_flush_update(bmain, sce, lay, time);
2415 }
2416
2417 void DAG_on_visible_update(Main *bmain, const short do_time)
2418 {
2419         Scene *scene;
2420         Base *base;
2421         Object *ob;
2422         Group *group;
2423         GroupObject *go;
2424         DagNode *node;
2425         unsigned int lay, oblay;
2426
2427         dag_current_scene_layers(bmain, &scene, &lay);
2428
2429         if (scene && scene->theDag) {
2430                 Scene *sce_iter;
2431                 /* derivedmeshes and displists are not saved to file so need to be
2432                  * remade, tag them so they get remade in the scene update loop,
2433                  * note armature poses or object matrices are preserved and do not
2434                  * require updates, so we skip those */
2435                 dag_scene_flush_layers(scene, lay);
2436
2437                 for (SETLOOPER(scene, sce_iter, base)) {
2438                         ob= base->object;
2439                         node= (sce_iter->theDag)? dag_get_node(sce_iter->theDag, ob): NULL;
2440                         oblay= (node)? node->lay: ob->lay;
2441
2442                         if ((oblay & lay) & ~scene->lay_updated) {
2443                                 if (ELEM6(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL, OB_LATTICE))
2444                                         ob->recalc |= OB_RECALC_DATA;
2445                                 if (ob->dup_group) 
2446                                         ob->dup_group->id.flag |= LIB_DOIT;
2447                         }
2448                 }
2449
2450                 for (group= bmain->group.first; group; group= group->id.next) {
2451                         if (group->id.flag & LIB_DOIT) {
2452                                 for (go= group->gobject.first; go; go= go->next) {
2453                                         if (ELEM6(go->ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL, OB_LATTICE))
2454                                                 go->ob->recalc |= OB_RECALC_DATA;
2455                                         if (go->ob->proxy_from)
2456                                                 go->ob->recalc |= OB_RECALC_OB;
2457                                 }
2458                                 
2459                                 group->id.flag &= ~LIB_DOIT;
2460                         }
2461                 }
2462
2463                 /* now tag update flags, to ensure deformers get calculated on redraw */
2464                 DAG_scene_update_flags(bmain, scene, lay, do_time);
2465                 scene->lay_updated |= lay;
2466         }
2467
2468         /* hack to get objects updating on layer changes */
2469         DAG_id_type_tag(bmain, ID_OB);
2470 }
2471
2472 static void dag_id_flush_update__isDependentTexture(void *userData, Object *UNUSED(ob), ID **idpoin)
2473 {
2474         struct { ID *id; int is_dependent; } *data = userData;
2475         
2476         if (*idpoin && GS((*idpoin)->name)==ID_TE) {
2477                 if (data->id == (*idpoin))
2478                         data->is_dependent = 1;
2479         }
2480 }
2481
2482 static void dag_id_flush_update(Scene *sce, ID *id)
2483 {
2484         Main *bmain= G.main;
2485         Object *obt, *ob= NULL;
2486         short idtype;
2487
2488         /* here we flush a few things before actual scene wide flush, mostly
2489          * due to only objects and not other datablocks being in the depsgraph */
2490
2491         /* set flags & pointcache for object */
2492         if (GS(id->name) == ID_OB) {
2493                 ob= (Object*)id;
2494                 BKE_ptcache_object_reset(sce, ob, PTCACHE_RESET_DEPSGRAPH);
2495
2496                 if (ob->recalc & OB_RECALC_DATA) {
2497                         /* all users of this ob->data should be checked */
2498                         id= ob->data;
2499
2500                         /* no point in trying in this cases */
2501                         if (id && id->us <= 1) {
2502                                 dag_editors_id_update(bmain, id);
2503                                 id= NULL;
2504                         }
2505                 }
2506         }
2507
2508         /* set flags & pointcache for object data */
2509         if (id) {
2510                 idtype= GS(id->name);
2511
2512                 if (ELEM8(idtype, ID_ME, ID_CU, ID_MB, ID_LA, ID_LT, ID_CA, ID_AR, ID_SPK)) {
2513                         for (obt=bmain->object.first; obt; obt= obt->id.next) {
2514                                 if (!(ob && obt == ob) && obt->data == id) {
2515                                         obt->recalc |= OB_RECALC_DATA;
2516                                         lib_id_recalc_data_tag(bmain, &obt->id);
2517                                         BKE_ptcache_object_reset(sce, obt, PTCACHE_RESET_DEPSGRAPH);
2518                                 }
2519                         }
2520                 }
2521                 
2522                 /* set flags based on textures - can influence depgraph via modifiers */
2523                 if (idtype == ID_TE) {
2524                         for (obt=bmain->object.first; obt; obt= obt->id.next) {
2525                                 struct { ID *id; int is_dependent; } data;
2526                                 data.id= id;
2527                                 data.is_dependent= 0;
2528
2529                                 modifiers_foreachIDLink(obt, dag_id_flush_update__isDependentTexture, &data);
2530                                 if (data.is_dependent) {
2531                                         obt->recalc |= OB_RECALC_DATA;
2532                                         lib_id_recalc_data_tag(bmain, &obt->id);
2533                                 }
2534
2535                                 /* particle settings can use the texture as well */
2536                                 if (obt->particlesystem.first) {
2537                                         ParticleSystem *psys = obt->particlesystem.first;
2538                                         MTex **mtexp, *mtex;
2539                                         int a;
2540                                         for (; psys; psys=psys->next) {
2541                                                 mtexp = psys->part->mtex;
2542                                                 for (a=0; a<MAX_MTEX; a++, mtexp++) {
2543                                                         mtex = *mtexp;
2544                                                         if (mtex && mtex->tex == (Tex*)id) {
2545                                                                 obt->recalc |= OB_RECALC_DATA;
2546                                                                 lib_id_recalc_data_tag(bmain, &obt->id);
2547
2548                                                                 if (mtex->mapto & PAMAP_INIT)
2549                                                                         psys->recalc |= PSYS_RECALC_RESET;
2550                                                                 if (mtex->mapto & PAMAP_CHILD)
2551                                                                         psys->recalc |= PSYS_RECALC_CHILD;
2552
2553                                                                 BKE_ptcache_object_reset(sce, obt, PTCACHE_RESET_DEPSGRAPH);
2554                                                         }
2555                                                 }
2556                                         }
2557                                 }
2558                         }
2559                 }
2560                 
2561                 /* set flags based on ShapeKey */
2562                 if (idtype == ID_KE) {
2563                         for (obt=bmain->object.first; obt; obt= obt->id.next) {
2564                                 Key *key= ob_get_key(obt);
2565                                 if (!(ob && obt == ob) && ((ID *)key == id)) {
2566                                         obt->flag |= (OB_RECALC_OB|OB_RECALC_DATA);
2567                                         lib_id_recalc_tag(bmain, &obt->id);
2568                                         lib_id_recalc_data_tag(bmain, &obt->id);
2569                                         BKE_ptcache_object_reset(sce, obt, PTCACHE_RESET_DEPSGRAPH);
2570                                 }
2571                         }
2572                 }
2573                 
2574                 /* set flags based on particle settings */
2575                 if (idtype == ID_PA) {
2576                         ParticleSystem *psys;
2577                         for (obt=bmain->object.first; obt; obt= obt->id.next)
2578                                 for (psys=obt->particlesystem.first; psys; psys=psys->next)
2579                                         if (&psys->part->id == id)
2580                                                 BKE_ptcache_object_reset(sce, obt, PTCACHE_RESET_DEPSGRAPH);
2581                 }
2582
2583                 if (idtype == ID_MC) {
2584                         MovieClip *clip = (MovieClip *) id;
2585
2586                         BKE_tracking_dopesheet_tag_update(&clip->tracking);
2587
2588                         for (obt=bmain->object.first; obt; obt= obt->id.next) {
2589                                 bConstraint *con;
2590                                 for (con = obt->constraints.first; con; con=con->next) {
2591                                         bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
2592                                         if (ELEM3(cti->type, CONSTRAINT_TYPE_FOLLOWTRACK, CONSTRAINT_TYPE_CAMERASOLVER,
2593                                                  CONSTRAINT_TYPE_OBJECTSOLVER))
2594                                         {
2595                                                 obt->recalc |= OB_RECALC_OB;
2596                                                 break;
2597                                         }
2598                                 }
2599                         }
2600
2601                         if (sce->nodetree) {
2602                                 bNode *node;
2603
2604                                 for (node= sce->nodetree->nodes.first; node; node= node->next) {
2605                                         if (node->id==id) {
2606                                                 nodeUpdate(sce->nodetree, node);
2607                                         }
2608                                 }
2609                         }
2610                 }
2611
2612                 /* camera's matrix is used to orient reconstructed stuff,
2613                  * so it should happen tracking-related constraints recalculation
2614                  * when camera is changing (sergey) */
2615                 if (sce->camera && &sce->camera->id == id) {
2616                         MovieClip *clip = BKE_object_movieclip_get(sce, sce->camera, 1);
2617
2618                         if (clip)
2619                                 dag_id_flush_update(sce, &clip->id);
2620                 }
2621
2622                 /* update editors */
2623                 dag_editors_id_update(bmain, id);
2624         }
2625 }
2626
2627 void DAG_ids_flush_tagged(Main *bmain)
2628 {
2629         ListBase *lbarray[MAX_LIBARRAY];
2630         Scene *sce;
2631         unsigned int lay;
2632         int a, do_flush = 0;
2633
2634         dag_current_scene_layers(bmain, &sce, &lay);
2635
2636         if (!sce || !sce->theDag)
2637                 return;
2638
2639         /* loop over all ID types */
2640         a  = set_listbasepointers(bmain, lbarray);
2641
2642         while (a--) {
2643                 ListBase *lb = lbarray[a];
2644                 ID *id = lb->first;
2645
2646                 /* we tag based on first ID type character to avoid 
2647                  * looping over all ID's in case there are no tags */
2648                 if (id && bmain->id_tag_update[id->name[0]]) {
2649                         for (; id; id=id->next) {
2650                                 if (id->flag & (LIB_ID_RECALC|LIB_ID_RECALC_DATA)) {
2651                                         dag_id_flush_update(sce, id);
2652                                         do_flush = 1;
2653                                 }
2654                         }
2655                 }
2656         }
2657
2658         /* flush changes to other objects */
2659         if (do_flush)
2660                 DAG_scene_flush_update(bmain, sce, lay, 0);
2661 }
2662
2663 void DAG_ids_check_recalc(Main *bmain, Scene *scene, int time)
2664 {
2665         ListBase *lbarray[MAX_LIBARRAY];
2666         int a, updated = 0;
2667
2668         /* loop over all ID types */
2669         a  = set_listbasepointers(bmain, lbarray);
2670
2671         while (a--) {
2672                 ListBase *lb = lbarray[a];
2673                 ID *id = lb->first;
2674
2675                 /* we tag based on first ID type character to avoid 
2676                  * looping over all ID's in case there are no tags */
2677                 if (id && bmain->id_tag_update[id->name[0]]) {
2678                         updated= 1;
2679                         break;
2680                 }
2681         }
2682
2683         dag_editors_scene_update(bmain, scene, (updated || time));
2684 }
2685
2686 void DAG_ids_clear_recalc(Main *bmain)
2687 {
2688         ListBase *lbarray[MAX_LIBARRAY];
2689         int a;
2690
2691         /* loop over all ID types */
2692         a  = set_listbasepointers(bmain, lbarray);
2693
2694         while (a--) {
2695                 ListBase *lb = lbarray[a];
2696                 ID *id = lb->first;
2697
2698                 /* we tag based on first ID type character to avoid 
2699                  * looping over all ID's in case there are no tags */
2700                 if (id && bmain->id_tag_update[id->name[0]]) {
2701                         for (; id; id=id->next)
2702                                 if (id->flag & (LIB_ID_RECALC|LIB_ID_RECALC_DATA))
2703                                         id->flag &= ~(LIB_ID_RECALC|LIB_ID_RECALC_DATA);
2704                 }
2705         }
2706
2707         memset(bmain->id_tag_update, 0, sizeof(bmain->id_tag_update));
2708 }
2709
2710 void DAG_id_tag_update(ID *id, short flag)
2711 {
2712         Main *bmain= G.main;
2713
2714         if (id==NULL) return;
2715         
2716         /* tag ID for update */
2717         if (flag) {
2718                 if (flag & OB_RECALC_OB)
2719                         lib_id_recalc_tag(bmain, id);
2720                 if (flag & (OB_RECALC_DATA|PSYS_RECALC))
2721                         lib_id_recalc_data_tag(bmain, id);
2722         }
2723         else
2724                 lib_id_recalc_tag(bmain, id);
2725
2726         /* flag is for objects and particle systems */
2727         if (flag) {
2728                 Object *ob;
2729                 short idtype = GS(id->name);
2730
2731                 if (idtype == ID_OB) {
2732                         /* only quick tag */
2733                         ob = (Object*)id;
2734                         ob->recalc |= (flag & OB_RECALC_ALL);
2735                 }
2736                 else if (idtype == ID_PA) {
2737                         ParticleSystem *psys;
2738                         /* this is weak still, should be done delayed as well */
2739                         for (ob=bmain->object.first; ob; ob=ob->id.next) {
2740                                 for (psys=ob->particlesystem.first; psys; psys=psys->next) {
2741                                         if (&psys->part->id == id) {
2742                                                 ob->recalc |= (flag & OB_RECALC_ALL);
2743                                                 psys->recalc |= (flag & PSYS_RECALC);
2744                                                 lib_id_recalc_tag(bmain, &ob->id);
2745                                                 lib_id_recalc_data_tag(bmain, &ob->id);
2746                                         }
2747                                 }
2748                         }
2749                 }
2750                 else {
2751                         /* disable because this is called on various ID types automatically.
2752                          * where printing warning is not useful. for now just ignore */
2753                         /* BLI_assert(!"invalid flag for this 'idtype'"); */
2754                 }
2755         }
2756 }
2757
2758 void DAG_id_type_tag(struct Main *bmain, short idtype)
2759 {
2760         bmain->id_tag_update[((char*)&idtype)[0]] = 1;
2761 }
2762
2763 int DAG_id_type_tagged(Main *bmain, short idtype)
2764 {
2765         return bmain->id_tag_update[((char*)&idtype)[0]];
2766 }
2767
2768 #if 0 // UNUSED
2769 /* recursively descends tree, each node only checked once */
2770 /* node is checked to be of type object */
2771 static int parent_check_node(DagNode *node, int curtime)
2772 {
2773         DagAdjList *itA;
2774         
2775         node->lasttime= curtime;
2776         
2777         if (node->color==DAG_GRAY)
2778                 return DAG_GRAY;
2779         
2780         for (itA = node->child; itA; itA= itA->next) {
2781                 if (itA->node->type==ID_OB) {
2782                         
2783                         if (itA->node->color==DAG_GRAY)
2784                                 return DAG_GRAY;
2785
2786                         /* descend if not done */
2787                         if (itA->node->lasttime!=curtime) {
2788                                 itA->node->color= parent_check_node(itA->node, curtime);
2789                         
2790                                 if (itA->node->color==DAG_GRAY)
2791                                         return DAG_GRAY;
2792                         }
2793                 }
2794         }
2795         
2796         return DAG_WHITE;
2797 }
2798 #endif
2799
2800 /* ******************* DAG FOR ARMATURE POSE ***************** */
2801
2802 /* we assume its an armature with pose */
2803 void DAG_pose_sort(Object *ob)
2804 {
2805         bPose *pose= ob->pose;
2806         bPoseChannel *pchan;
2807         bConstraint *con;
2808         DagNode *node;
2809         DagNode *node2, *node3;
2810         DagNode *rootnode;
2811         DagForest *dag;
2812         DagNodeQueue *nqueue;
2813         DagAdjList *itA;
2814         ListBase tempbase;
2815         int skip = 0;
2816         
2817         dag = dag_init();
2818         ugly_hack_sorry= 0;     // no ID structs
2819
2820         rootnode = dag_add_node(dag, NULL);     // node->ob becomes NULL
2821         
2822         /* we add the hierarchy and the constraints */
2823         for (pchan = pose->chanbase.first; pchan; pchan= pchan->next) {
2824                 int addtoroot = 1;
2825                 
2826                 node = dag_get_node(dag, pchan);
2827                 
2828                 if (pchan->parent) {
2829                         node2 = dag_get_node(dag, pchan->parent);
2830                         dag_add_relation(dag, node2, node, 0, "Parent Relation");
2831                         addtoroot = 0;
2832                 }
2833                 for (con = pchan->constraints.first; con; con=con->next) {
2834                         bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
2835                         ListBase targets = {NULL, NULL};
2836                         bConstraintTarget *ct;
2837                         
2838                         if (cti && cti->get_constraint_targets) {
2839                                 cti->get_constraint_targets(con, &targets);
2840                                 
2841                                 for (ct= targets.first; ct; ct= ct->next) {
2842                                         if (ct->tar==ob && ct->subtarget[0]) {
2843                                                 bPoseChannel *target= get_pose_channel(ob->pose, ct->subtarget);
2844                                                 if (target) {
2845                                                         node2= dag_get_node(dag, target);
2846                                                         dag_add_relation(dag, node2, node, 0, "Pose Constraint");
2847                                                         
2848                                                         if (con->type==CONSTRAINT_TYPE_KINEMATIC) {
2849                                                                 bKinematicConstraint *data = (bKinematicConstraint *)con->data;
2850                                                                 bPoseChannel *parchan;
2851                                                                 int segcount= 0;
2852                                                                 
2853                                                                 /* exclude tip from chain? */
2854                                                                 if (!(data->flag & CONSTRAINT_IK_TIP))
2855                                                                         parchan= pchan->parent;
2856                                                                 else
2857                                                                         parchan= pchan;
2858                                                                 
2859                                                                 /* Walk to the chain's root */
2860                                                                 while (parchan) {
2861                                                                         node3= dag_get_node(dag, parchan);
2862                                                                         dag_add_relation(dag, node2, node3, 0, "IK Constraint");
2863                                                                         
2864                                                                         segcount++;
2865                                                                         if (segcount==data->rootbone || segcount>255) break; // 255 is weak
2866                                                                         parchan= parchan->parent;
2867                                                                 }
2868                                                         }
2869                                                 }
2870                                         }
2871                                 }
2872                                 
2873                                 if (cti->flush_constraint_targets)
2874                                         cti->flush_constraint_targets(con, &targets, 1);
2875                         }
2876                 }
2877                 if (addtoroot == 1 ) {
2878                         dag_add_relation(dag, rootnode, node, 0, "Root Bone Relation");
2879                 }
2880         }
2881
2882         dag_check_cycle(dag);
2883         
2884         /* now we try to sort... */
2885         tempbase.first= tempbase.last= NULL;
2886
2887         nqueue = queue_create(DAGQUEUEALLOC);
2888         
2889         /* tag nodes unchecked */
2890         for (node = dag->DagNode.first; node; node= node->next) 
2891                 node->color = DAG_WHITE;
2892         
2893         rootnode->color = DAG_GRAY;
2894         push_stack(nqueue, rootnode);  
2895         
2896         while (nqueue->count) {
2897                 
2898                 skip = 0;
2899                 node = get_top_node_queue(nqueue);
2900                 
2901                 itA = node->child;
2902                 while (itA != NULL) {
2903                         if (itA->node->color == DAG_WHITE) {
2904                                 itA->node->color = DAG_GRAY;
2905                                 push_stack(nqueue, itA->node);
2906                                 skip = 1;
2907                                 break;
2908                         } 
2909                         itA = itA->next;
2910                 }                       
2911                 
2912                 if (!skip) {
2913                         if (node) {
2914                                 node = pop_queue(nqueue);
2915                                 if (node->ob == NULL)   // we are done
2916                                         break;
2917                                 node->color = DAG_BLACK;
2918                                 
2919                                 /* put node in new list */