style cleanup: blenkernel
[blender-staging.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) {
622                                 for (eff = effectors->first; eff; eff = eff->next) {
623                                         if (eff->psys) {
624                                                 node2 = dag_get_node(dag, eff->ob);
625                                                 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "Particle Field");
626                                         }
627                                 }
628                         }
629
630                         pdEndEffectors(&effectors);
631
632                         if (part->boids) {
633                                 for (state = part->boids->states.first; state; state = state->next) {
634                                         for (rule = state->rules.first; rule; rule = rule->next) {
635                                                 Object *ruleob = NULL;
636                                                 if (rule->type == eBoidRuleType_Avoid)
637                                                         ruleob = ((BoidRuleGoalAvoid *)rule)->ob;
638                                                 else if (rule->type == eBoidRuleType_FollowLeader)
639                                                         ruleob = ((BoidRuleFollowLeader *)rule)->ob;
640
641                                                 if (ruleob) {
642                                                         node2 = dag_get_node(dag, ruleob);
643                                                         dag_add_relation(dag, node2, node, DAG_RL_OB_DATA, "Boid Rule");
644                                                 }
645                                         }
646                                 }
647                         }
648                 }
649         }
650         
651         /* object constraints */
652         for (con = ob->constraints.first; con; con = con->next) {
653                 bConstraintTypeInfo *cti = constraint_get_typeinfo(con);
654                 ListBase targets = {NULL, NULL};
655                 bConstraintTarget *ct;
656                 
657                 if (!cti)
658                         continue;
659
660                 /* special case for camera tracking -- it doesn't use targets to define relations */
661                 if (ELEM3(cti->type, CONSTRAINT_TYPE_FOLLOWTRACK, CONSTRAINT_TYPE_CAMERASOLVER, CONSTRAINT_TYPE_OBJECTSOLVER)) {
662                         int depends_on_camera = 0;
663
664                         if (cti->type == CONSTRAINT_TYPE_FOLLOWTRACK) {
665                                 bFollowTrackConstraint *data = (bFollowTrackConstraint *)con->data;
666
667                                 if ((data->clip || data->flag & FOLLOWTRACK_ACTIVECLIP) && data->track[0])
668                                         depends_on_camera = 1;
669
670                                 if (data->depth_ob) {
671                                         node2 = dag_get_node(dag, data->depth_ob);
672                                         dag_add_relation(dag, node2, node, DAG_RL_DATA_OB | DAG_RL_OB_OB, cti->name);
673                                 }
674                         }
675                         else if (cti->type == CONSTRAINT_TYPE_OBJECTSOLVER)
676                                 depends_on_camera = 1;
677
678                         if (depends_on_camera && scene->camera) {
679                                 node2 = dag_get_node(dag, scene->camera);
680                                 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB | DAG_RL_OB_OB, cti->name);
681                         }
682
683                         dag_add_relation(dag, scenenode, node, DAG_RL_SCENE, "Scene Relation");
684                         addtoroot = 0;
685                 }
686                 else if (cti->get_constraint_targets) {
687                         cti->get_constraint_targets(con, &targets);
688                         
689                         for (ct = targets.first; ct; ct = ct->next) {
690                                 Object *obt;
691                                 
692                                 if (ct->tar)
693                                         obt = ct->tar;
694                                 else
695                                         continue;
696                                 
697                                 node2 = dag_get_node(dag, obt);
698                                 if (ELEM(con->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO))
699                                         dag_add_relation(dag, node2, node, DAG_RL_DATA_OB | DAG_RL_OB_OB, cti->name);
700                                 else {
701                                         if (ELEM3(obt->type, OB_ARMATURE, OB_MESH, OB_LATTICE) && (ct->subtarget[0])) {
702                                                 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB | DAG_RL_OB_OB, cti->name);
703                                                 if (obt->type == OB_MESH)
704                                                         node2->customdata_mask |= CD_MASK_MDEFORMVERT;
705                                         }
706                                         else
707                                                 dag_add_relation(dag, node2, node, DAG_RL_OB_OB, cti->name);
708                                 }
709                                 addtoroot = 0;
710                         }
711                         
712                         if (cti->flush_constraint_targets)
713                                 cti->flush_constraint_targets(con, &targets, 1);
714                 }
715         }
716
717         if (addtoroot == 1)
718                 dag_add_relation(dag, scenenode, node, DAG_RL_SCENE, "Scene Relation");
719 }
720
721 struct DagForest *build_dag(Main *bmain, Scene *sce, short mask){
722         Base *base;
723         Object *ob;
724         Group *group;
725         GroupObject *go;
726         DagNode *node;
727         DagNode *scenenode;
728         DagForest *dag;
729         DagAdjList *itA;
730
731         dag = sce->theDag;
732         sce->dagisvalid = 1;
733         if (dag)
734                 free_forest(dag);
735         else {
736                 dag = dag_init();
737                 sce->theDag = dag;
738         }
739         
740         /* add base node for scene. scene is always the first node in DAG */
741         scenenode = dag_add_node(dag, sce);     
742         
743         /* add current scene objects */
744         for (base = sce->base.first; base; base = base->next) {
745                 ob = base->object;
746                 
747                 build_dag_object(dag, scenenode, sce, ob, mask);
748                 if (ob->proxy)
749                         build_dag_object(dag, scenenode, sce, ob->proxy, mask);
750                 
751                 /* handled in next loop */
752                 if (ob->dup_group) 
753                         ob->dup_group->id.flag |= LIB_DOIT;
754         }
755         
756         /* add groups used in current scene objects */
757         for (group = bmain->group.first; group; group = group->id.next) {
758                 if (group->id.flag & LIB_DOIT) {
759                         for (go = group->gobject.first; go; go = go->next) {
760                                 build_dag_object(dag, scenenode, sce, go->ob, mask);
761                         }
762                         group->id.flag &= ~LIB_DOIT;
763                 }
764         }
765         
766         /* Now all relations were built, but we need to solve 1 exceptional case;
767          * When objects have multiple "parents" (for example parent + constraint working on same object)
768          * the relation type has to be synced. One of the parents can change, and should give same event to child */
769         
770         /* nodes were callocced, so we can use node->color for temporal storage */
771         for (node = sce->theDag->DagNode.first; node; node = node->next) {
772                 if (node->type == ID_OB) {
773                         for (itA = node->child; itA; itA = itA->next) {
774                                 if (itA->node->type == ID_OB) {
775                                         itA->node->color |= itA->type;
776                                 }
777                         }
778
779                         /* also flush custom data mask */
780                         ((Object *)node->ob)->customdata_mask = node->customdata_mask;
781                 }
782         }
783         /* now set relations equal, so that when only one parent changes, the correct recalcs are found */
784         for (node = sce->theDag->DagNode.first; node; node = node->next) {
785                 if (node->type == ID_OB) {
786                         for (itA = node->child; itA; itA = itA->next) {
787                                 if (itA->node->type == ID_OB) {
788                                         itA->type |= itA->node->color;
789                                 }
790                         }
791                 }
792         }
793         
794         // cycle detection and solving
795         // solve_cycles(dag);   
796         
797         return dag;
798 }
799
800
801 void free_forest(DagForest *Dag) 
802 {  /* remove all nodes and deps */
803         DagNode *tempN;
804         DagAdjList *tempA;      
805         DagAdjList *itA;
806         DagNode *itN = Dag->DagNode.first;
807         
808         while (itN) {
809                 itA = itN->child;       
810                 while (itA) {
811                         tempA = itA;
812                         itA = itA->next;
813                         MEM_freeN(tempA);                       
814                 }
815                 
816                 itA = itN->parent;      
817                 while (itA) {
818                         tempA = itA;
819                         itA = itA->next;
820                         MEM_freeN(tempA);                       
821                 }
822                 
823                 tempN = itN;
824                 itN = itN->next;
825                 MEM_freeN(tempN);
826         }
827
828         BLI_ghash_free(Dag->nodeHash, NULL, NULL);
829         Dag->nodeHash = NULL;
830         Dag->DagNode.first = NULL;
831         Dag->DagNode.last = NULL;
832         Dag->numNodes = 0;
833
834 }
835
836 DagNode *dag_find_node(DagForest *forest, void *fob)
837 {
838         if (forest->nodeHash)
839                 return BLI_ghash_lookup(forest->nodeHash, fob);
840
841         return NULL;
842 }
843
844 static int ugly_hack_sorry = 1;  // prevent type check
845 static int dag_print_dependencies = 0; // debugging
846
847 /* no checking of existence, use dag_find_node first or dag_get_node */
848 DagNode *dag_add_node(DagForest *forest, void *fob)
849 {
850         DagNode *node;
851                 
852         node = MEM_callocN(sizeof(DagNode), "DAG node");
853         if (node) {
854                 node->ob = fob;
855                 node->color = DAG_WHITE;
856
857                 if (ugly_hack_sorry) node->type = GS(((ID *) fob)->name);   // sorry, done for pose sorting
858                 if (forest->numNodes) {
859                         ((DagNode *) forest->DagNode.last)->next = node;
860                         forest->DagNode.last = node;
861                         forest->numNodes++;
862                 }
863                 else {
864                         forest->DagNode.last = node;
865                         forest->DagNode.first = node;
866                         forest->numNodes = 1;
867                 }
868
869                 if (!forest->nodeHash)
870                         forest->nodeHash = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "dag_add_node gh");
871                 BLI_ghash_insert(forest->nodeHash, fob, node);
872         }
873
874         return node;
875 }
876
877 DagNode *dag_get_node(DagForest *forest, void *fob)
878 {
879         DagNode *node;
880         
881         node = dag_find_node(forest, fob);
882         if (!node) 
883                 node = dag_add_node(forest, fob);
884         return node;
885 }
886
887
888
889 DagNode *dag_get_sub_node(DagForest *forest, void *fob)
890 {
891         DagNode *node;
892         DagAdjList *mainchild, *prev = NULL;
893         
894         mainchild = ((DagNode *) forest->DagNode.first)->child;
895         /* remove from first node (scene) adj list if present */
896         while (mainchild) {
897                 if (mainchild->node == fob) {
898                         if (prev) {
899                                 prev->next = mainchild->next;
900                                 MEM_freeN(mainchild);
901                                 break;
902                         }
903                         else {
904                                 ((DagNode *) forest->DagNode.first)->child = mainchild->next;
905                                 MEM_freeN(mainchild);
906                                 break;
907                         }
908                 }
909                 prev = mainchild;
910                 mainchild = mainchild->next;
911         }
912         node = dag_find_node(forest, fob);
913         if (!node) 
914                 node = dag_add_node(forest, fob);
915         return node;
916 }
917
918 static void dag_add_parent_relation(DagForest *UNUSED(forest), DagNode *fob1, DagNode *fob2, short rel, const char *name) 
919 {
920         DagAdjList *itA = fob2->parent;
921         
922         while (itA) { /* search if relation exist already */
923                 if (itA->node == fob1) {
924                         itA->type |= rel;
925                         itA->count += 1;
926                         return;
927                 }
928                 itA = itA->next;
929         }
930         /* create new relation and insert at head. MALLOC alert! */
931         itA = MEM_mallocN(sizeof(DagAdjList), "DAG adj list");
932         itA->node = fob1;
933         itA->type = rel;
934         itA->count = 1;
935         itA->next = fob2->parent;
936         itA->name = name;
937         fob2->parent = itA;
938 }
939
940 void dag_add_relation(DagForest *forest, DagNode *fob1, DagNode *fob2, short rel, const char *name) 
941 {
942         DagAdjList *itA = fob1->child;
943         
944         /* parent relation is for cycle checking */
945         dag_add_parent_relation(forest, fob1, fob2, rel, name);
946
947         while (itA) { /* search if relation exist already */
948                 if (itA->node == fob2) {
949                         itA->type |= rel;
950                         itA->count += 1;
951                         return;
952                 }
953                 itA = itA->next;
954         }
955         /* create new relation and insert at head. MALLOC alert! */
956         itA = MEM_mallocN(sizeof(DagAdjList), "DAG adj list");
957         itA->node = fob2;
958         itA->type = rel;
959         itA->count = 1;
960         itA->next = fob1->child;
961         itA->name = name;
962         fob1->child = itA;
963 }
964
965 static const char *dag_node_name(DagNode *node)
966 {
967         if (node->ob == NULL)
968                 return "null";
969         else if (ugly_hack_sorry)
970                 return ((ID *)(node->ob))->name + 2;
971         else
972                 return ((bPoseChannel *)(node->ob))->name;
973 }
974
975 static void dag_node_print_dependencies(DagNode *node)
976 {
977         DagAdjList *itA;
978
979         printf("%s depends on:\n", dag_node_name(node));
980
981         for (itA = node->parent; itA; itA = itA->next)
982                 printf("  %s through %s\n", dag_node_name(itA->node), itA->name);
983         printf("\n");
984 }
985
986 static int dag_node_print_dependency_recurs(DagNode *node, DagNode *endnode)
987 {
988         DagAdjList *itA;
989
990         if (node->color == DAG_BLACK)
991                 return 0;
992
993         node->color = DAG_BLACK;
994
995         if (node == endnode)
996                 return 1;
997
998         for (itA = node->parent; itA; itA = itA->next) {
999                 if (dag_node_print_dependency_recurs(itA->node, endnode)) {
1000                         printf("  %s depends on %s through %s.\n", dag_node_name(node), dag_node_name(itA->node), itA->name);
1001                         return 1;
1002                 }
1003         }
1004
1005         return 0;
1006 }
1007
1008 static void dag_node_print_dependency_cycle(DagForest *dag, DagNode *startnode, DagNode *endnode, const char *name)
1009 {
1010         DagNode *node;
1011
1012         for (node = dag->DagNode.first; node; node = node->next)
1013                 node->color = DAG_WHITE;
1014
1015         printf("  %s depends on %s through %s.\n", dag_node_name(endnode), dag_node_name(startnode), name);
1016         dag_node_print_dependency_recurs(startnode, endnode);
1017         printf("\n");
1018 }
1019
1020 static int dag_node_recurs_level(DagNode *node, int level)
1021 {
1022         DagAdjList *itA;
1023         int newlevel;
1024
1025         node->color = DAG_BLACK; /* done */
1026         newlevel = ++level;
1027         
1028         for (itA = node->parent; itA; itA = itA->next) {
1029                 if (itA->node->color == DAG_WHITE) {
1030                         itA->node->ancestor_count = dag_node_recurs_level(itA->node, level);
1031                         newlevel = MAX2(newlevel, level + itA->node->ancestor_count);
1032                 }
1033                 else
1034                         newlevel = MAX2(newlevel, level + itA->node->ancestor_count);
1035         }
1036         
1037         return newlevel;
1038 }
1039
1040 static void dag_check_cycle(DagForest *dag)
1041 {
1042         DagNode *node;
1043         DagAdjList *itA;
1044
1045         /* debugging print */
1046         if (dag_print_dependencies)
1047                 for (node = dag->DagNode.first; node; node = node->next)
1048                         dag_node_print_dependencies(node);
1049
1050         /* tag nodes unchecked */
1051         for (node = dag->DagNode.first; node; node = node->next)
1052                 node->color = DAG_WHITE;
1053         
1054         for (node = dag->DagNode.first; node; node = node->next) {
1055                 if (node->color == DAG_WHITE) {
1056                         node->ancestor_count = dag_node_recurs_level(node, 0);
1057                 }
1058         }
1059         
1060         /* check relations, and print errors */
1061         for (node = dag->DagNode.first; node; node = node->next) {
1062                 for (itA = node->parent; itA; itA = itA->next) {
1063                         if (itA->node->ancestor_count > node->ancestor_count) {
1064                                 if (node->ob && itA->node->ob) {
1065                                         printf("Dependency cycle detected:\n");
1066                                         dag_node_print_dependency_cycle(dag, itA->node, node, itA->name);
1067                                 }
1068                         }
1069                 }
1070         }
1071
1072         /* parent relations are only needed for cycle checking, so free now */
1073         for (node = dag->DagNode.first; node; node = node->next) {
1074                 while (node->parent) {
1075                         itA = node->parent->next;
1076                         MEM_freeN(node->parent);                        
1077                         node->parent = itA;
1078                 }
1079         }
1080 }
1081
1082 /*
1083  * MainDAG is the DAG of all objects in current scene
1084  * used only for drawing there is one also in each scene
1085  */
1086 static DagForest *MainDag = NULL;
1087
1088 DagForest *getMainDag(void)
1089 {
1090         return MainDag;
1091 }
1092
1093
1094 void setMainDag(DagForest *dag)
1095 {
1096         MainDag = dag;
1097 }
1098
1099
1100 /*
1101  * note for BFS/DFS
1102  * in theory we should sweep the whole array
1103  * but in our case the first node is the scene
1104  * and is linked to every other object
1105  *
1106  * for general case we will need to add outer loop
1107  */
1108
1109 /*
1110  * ToDo : change pos kludge
1111  */
1112
1113 /* adjust levels for drawing in oops space */
1114 void graph_bfs(void)
1115 {
1116         DagNode *node;
1117         DagNodeQueue *nqueue;
1118         int pos[50];
1119         int i;
1120         DagAdjList *itA;
1121         int minheight;
1122         
1123         /* fprintf(stderr, "starting BFS\n ------------\n"); */
1124         nqueue = queue_create(DAGQUEUEALLOC);
1125         for (i = 0; i < 50; i++)
1126                 pos[i] = 0;
1127         
1128         /* Init
1129          * dagnode.first is always the root (scene)
1130          */
1131         node = MainDag->DagNode.first;
1132         while (node) {
1133                 node->color = DAG_WHITE;
1134                 node->BFS_dist = 9999;
1135                 node->k = 0;
1136                 node = node->next;
1137         }
1138         
1139         node = MainDag->DagNode.first;
1140         if (node->color == DAG_WHITE) {
1141                 node->color = DAG_GRAY;
1142                 node->BFS_dist = 1;
1143                 push_queue(nqueue, node);
1144                 while (nqueue->count) {
1145                         node = pop_queue(nqueue);
1146                         
1147                         minheight = pos[node->BFS_dist];
1148                         itA = node->child;
1149                         while (itA != NULL) {
1150                                 if (itA->node->color == DAG_WHITE) {
1151                                         itA->node->color = DAG_GRAY;
1152                                         itA->node->BFS_dist = node->BFS_dist + 1;
1153                                         itA->node->k = (float) minheight;
1154                                         push_queue(nqueue, itA->node);
1155                                 }
1156                                 
1157                                 else {
1158                                         fprintf(stderr, "bfs not dag tree edge color :%i\n", itA->node->color);
1159                                 }
1160
1161                                 
1162                                 itA = itA->next;
1163                         }
1164                         if (pos[node->BFS_dist] > node->k) {
1165                                 pos[node->BFS_dist] += 1;                               
1166                                 node->k = (float) pos[node->BFS_dist];
1167                         }
1168                         else {
1169                                 pos[node->BFS_dist] = (int) node->k + 1;
1170                         }
1171                         set_node_xy(node, node->BFS_dist * DEPSX * 2, pos[node->BFS_dist] * DEPSY * 2);
1172                         node->color = DAG_BLACK;
1173
1174                         // fprintf(stderr, "BFS node : %20s %i %5.0f %5.0f\n", ((ID *) node->ob)->name, node->BFS_dist, node->x, node->y);
1175                 }
1176         }
1177         queue_delete(nqueue);
1178 }
1179
1180 int pre_and_post_BFS(DagForest *dag, short mask, graph_action_func pre_func, graph_action_func post_func, void **data)
1181 {
1182         DagNode *node;
1183         
1184         node = dag->DagNode.first;
1185         return pre_and_post_source_BFS(dag, mask,  node,  pre_func,  post_func, data);
1186 }
1187
1188
1189 int pre_and_post_source_BFS(DagForest *dag, short mask, DagNode *source, graph_action_func pre_func, graph_action_func post_func, void **data)
1190 {
1191         DagNode *node;
1192         DagNodeQueue *nqueue;
1193         DagAdjList *itA;
1194         int retval = 0;
1195         /* fprintf(stderr, "starting BFS\n ------------\n"); */
1196         
1197         /* Init
1198          * dagnode.first is always the root (scene)
1199          */
1200         node = dag->DagNode.first;
1201         nqueue = queue_create(DAGQUEUEALLOC);
1202         while (node) {
1203                 node->color = DAG_WHITE;
1204                 node->BFS_dist = 9999;
1205                 node = node->next;
1206         }
1207         
1208         node = source;
1209         if (node->color == DAG_WHITE) {
1210                 node->color = DAG_GRAY;
1211                 node->BFS_dist = 1;
1212                 pre_func(node->ob, data);
1213                 
1214                 while (nqueue->count) {
1215                         node = pop_queue(nqueue);
1216                         
1217                         itA = node->child;
1218                         while (itA != NULL) {
1219                                 if ((itA->node->color == DAG_WHITE) && (itA->type & mask)) {
1220                                         itA->node->color = DAG_GRAY;
1221                                         itA->node->BFS_dist = node->BFS_dist + 1;
1222                                         push_queue(nqueue, itA->node);
1223                                         pre_func(node->ob, data);
1224                                 }
1225                                 
1226                                 else { // back or cross edge
1227                                         retval = 1;
1228                                 }
1229                                 itA = itA->next;
1230                         }
1231                         post_func(node->ob, data);
1232                         node->color = DAG_BLACK;
1233
1234                         // fprintf(stderr, "BFS node : %20s %i %5.0f %5.0f\n", ((ID *) node->ob)->name, node->BFS_dist, node->x, node->y);
1235                 }
1236         }
1237         queue_delete(nqueue);
1238         return retval;
1239 }
1240
1241 /* non recursive version of DFS, return queue -- outer loop present to catch odd cases (first level cycles)*/
1242 DagNodeQueue *graph_dfs(void)
1243 {
1244         DagNode *node;
1245         DagNodeQueue *nqueue;
1246         DagNodeQueue *retqueue;
1247         int pos[50];
1248         int i;
1249         DagAdjList *itA;
1250         int time;
1251         int skip = 0;
1252         int minheight;
1253         int maxpos = 0;
1254         /* int  is_cycle = 0; */ /* UNUSED */
1255         /*
1256            *fprintf(stderr, "starting DFS\n ------------\n");
1257          */     
1258         nqueue = queue_create(DAGQUEUEALLOC);
1259         retqueue = queue_create(MainDag->numNodes);
1260         for (i = 0; i < 50; i++)
1261                 pos[i] = 0;
1262         
1263         /* Init
1264          * dagnode.first is always the root (scene)
1265          */
1266         node = MainDag->DagNode.first;
1267         while (node) {
1268                 node->color = DAG_WHITE;
1269                 node->DFS_dist = 9999;
1270                 node->DFS_dvtm = node->DFS_fntm = 9999;
1271                 node->k = 0;
1272                 node =  node->next;
1273         }
1274         
1275         time = 1;
1276         
1277         node = MainDag->DagNode.first;
1278
1279         do {
1280                 if (node->color == DAG_WHITE) {
1281                         node->color = DAG_GRAY;
1282                         node->DFS_dist = 1;
1283                         node->DFS_dvtm = time;
1284                         time++;
1285                         push_stack(nqueue, node);
1286                         
1287                         while (nqueue->count) {
1288                                 //graph_print_queue(nqueue);
1289
1290                                 skip = 0;
1291                                 node = get_top_node_queue(nqueue);
1292                         
1293                                 minheight = pos[node->DFS_dist];
1294
1295                                 itA = node->child;
1296                                 while (itA != NULL) {
1297                                         if (itA->node->color == DAG_WHITE) {
1298                                                 itA->node->DFS_dvtm = time;
1299                                                 itA->node->color = DAG_GRAY;
1300
1301                                                 time++;
1302                                                 itA->node->DFS_dist = node->DFS_dist + 1;
1303                                                 itA->node->k = (float) minheight;
1304                                                 push_stack(nqueue, itA->node);
1305                                                 skip = 1;
1306                                                 break;
1307                                         }
1308                                         else {
1309                                                 if (itA->node->color == DAG_GRAY) { // back edge
1310                                                         fprintf(stderr, "dfs back edge :%15s %15s\n", ((ID *) node->ob)->name, ((ID *) itA->node->ob)->name);
1311                                                         /* is_cycle = 1; */ /* UNUSED */
1312                                                 }
1313                                                 else if (itA->node->color == DAG_BLACK) {
1314                                                         /* already processed node but we may want later to change distance either to shorter to longer.
1315                                                          * DFS_dist is the first encounter
1316                                                          */
1317 #if 0
1318                                                         if (node->DFS_dist >= itA->node->DFS_dist)
1319                                                                 itA->node->DFS_dist = node->DFS_dist + 1;
1320
1321                                                         fprintf(stderr, "dfs forward or cross edge :%15s %i-%i %15s %i-%i\n",
1322                                                                 ((ID *) node->ob)->name,
1323                                                                 node->DFS_dvtm,
1324                                                                 node->DFS_fntm,
1325                                                                 ((ID *) itA->node->ob)->name,
1326                                                                 itA->node->DFS_dvtm,
1327                                                                 itA->node->DFS_fntm);
1328 #endif
1329                                                 }
1330                                                 else
1331                                                         fprintf(stderr, "dfs unknown edge\n");
1332                                         }
1333                                         itA = itA->next;
1334                                 }
1335
1336                                 if (!skip) {
1337                                         node = pop_queue(nqueue);
1338                                         node->color = DAG_BLACK;
1339
1340                                         node->DFS_fntm = time;
1341                                         time++;
1342                                         if (node->DFS_dist > maxpos)
1343                                                 maxpos = node->DFS_dist;
1344                                         if (pos[node->DFS_dist] > node->k) {
1345                                                 pos[node->DFS_dist] += 1;
1346                                                 node->k = (float) pos[node->DFS_dist];
1347                                         }
1348                                         else {
1349                                                 pos[node->DFS_dist] = (int) node->k + 1;
1350                                         }
1351                                         set_node_xy(node, node->DFS_dist * DEPSX * 2, pos[node->DFS_dist] * DEPSY * 2);
1352                                 
1353                                         // 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 );
1354
1355                                         push_stack(retqueue, node);
1356                                 
1357                                 }
1358                         }
1359                 }
1360                 node = node->next;
1361         } while (node);
1362 //      fprintf(stderr, "i size : %i\n", maxpos);
1363
1364         queue_delete(nqueue);
1365         return(retqueue);
1366 }
1367
1368 /* unused */
1369 int pre_and_post_DFS(DagForest *dag, short mask, graph_action_func pre_func, graph_action_func post_func, void **data)
1370 {
1371         DagNode *node;
1372
1373         node = dag->DagNode.first;
1374         return pre_and_post_source_DFS(dag, mask,  node,  pre_func,  post_func, data);
1375 }
1376
1377 int pre_and_post_source_DFS(DagForest *dag, short mask, DagNode *source, graph_action_func pre_func, graph_action_func post_func, void **data)
1378 {
1379         DagNode *node;
1380         DagNodeQueue *nqueue;
1381         DagAdjList *itA;
1382         int time;
1383         int skip = 0;
1384         int retval = 0;
1385         /*
1386          * fprintf(stderr, "starting DFS\n ------------\n");
1387          */     
1388         nqueue = queue_create(DAGQUEUEALLOC);
1389         
1390         /* Init
1391          * dagnode.first is always the root (scene)
1392          */
1393         node = dag->DagNode.first;
1394         while (node) {
1395                 node->color = DAG_WHITE;
1396                 node->DFS_dist = 9999;
1397                 node->DFS_dvtm = node->DFS_fntm = 9999;
1398                 node->k = 0;
1399                 node =  node->next;
1400         }
1401         
1402         time = 1;
1403         
1404         node = source;
1405         do {
1406                 if (node->color == DAG_WHITE) {
1407                         node->color = DAG_GRAY;
1408                         node->DFS_dist = 1;
1409                         node->DFS_dvtm = time;
1410                         time++;
1411                         push_stack(nqueue, node);
1412                         pre_func(node->ob, data);
1413
1414                         while (nqueue->count) {
1415                                 skip = 0;
1416                                 node = get_top_node_queue(nqueue);
1417                                                                 
1418                                 itA = node->child;
1419                                 while (itA != NULL) {
1420                                         if ((itA->node->color == DAG_WHITE) && (itA->type & mask) ) {
1421                                                 itA->node->DFS_dvtm = time;
1422                                                 itA->node->color = DAG_GRAY;
1423                                                 
1424                                                 time++;
1425                                                 itA->node->DFS_dist = node->DFS_dist + 1;
1426                                                 push_stack(nqueue, itA->node);
1427                                                 pre_func(node->ob, data);
1428
1429                                                 skip = 1;
1430                                                 break;
1431                                         }
1432                                         else {
1433                                                 if (itA->node->color == DAG_GRAY) { // back edge
1434                                                         retval = 1;
1435                                                 }
1436 //                                              else if (itA->node->color == DAG_BLACK) { // cross or forward
1437 //
1438 //                                              }
1439                                         }
1440                                         itA = itA->next;
1441                                 }                       
1442                                 
1443                                 if (!skip) {
1444                                         node = pop_queue(nqueue);
1445                                         node->color = DAG_BLACK;
1446                                         
1447                                         node->DFS_fntm = time;
1448                                         time++;
1449                                         post_func(node->ob, data);
1450                                 }
1451                         }
1452                 }
1453                 node = node->next;
1454         } while (node);
1455         queue_delete(nqueue);
1456         return(retval);
1457 }
1458
1459
1460 // used to get the obs owning a datablock
1461 struct DagNodeQueue *get_obparents(struct DagForest *dag, void *ob){
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         DagNode *node, *node1;
1496         DagNodeQueue *nqueue;
1497         DagAdjList *itA;
1498         
1499         node = dag_find_node(dag, ob);
1500         
1501         // need to go over the whole dag for adj list
1502         nqueue = queue_create(node->ancestor_count);
1503         
1504         node1 = dag->DagNode.first;
1505         do {
1506                 if (node1->DFS_fntm > node->DFS_fntm) { 
1507                         itA = node->child;
1508                         while (itA != NULL) {
1509                                 if (itA->node == node) {
1510                                         push_queue(nqueue, node);
1511                                 }
1512                                 itA = itA->next;
1513                         }
1514                 }
1515                 node1 = node1->next;
1516         } while (node1);
1517         
1518         return nqueue;  
1519 }
1520
1521 // standard DFS list
1522 struct DagNodeQueue *get_all_childs(struct DagForest    *dag, void *ob){
1523         DagNode *node;
1524         DagNodeQueue *nqueue;
1525         DagNodeQueue *retqueue;
1526         DagAdjList *itA;
1527         int time;
1528         int skip = 0;
1529
1530         nqueue = queue_create(DAGQUEUEALLOC);
1531         retqueue = queue_create(dag->numNodes); // was MainDag... why? (ton)
1532         
1533         node = dag->DagNode.first;
1534         while (node) {
1535                 node->color = DAG_WHITE;
1536                 node =  node->next;
1537         }
1538         
1539         time = 1;
1540         
1541         node = dag_find_node(dag, ob);   // could be done in loop above (ton)
1542         if (node) { // can be null for newly added objects
1543                 
1544                 node->color = DAG_GRAY;
1545                 time++;
1546                 push_stack(nqueue, node);
1547                 
1548                 while (nqueue->count) {
1549                         
1550                         skip = 0;
1551                         node = get_top_node_queue(nqueue);
1552                                         
1553                         itA = node->child;
1554                         while (itA != NULL) {
1555                                 if (itA->node->color == DAG_WHITE) {
1556                                         itA->node->DFS_dvtm = time;
1557                                         itA->node->color = DAG_GRAY;
1558                                         
1559                                         time++;
1560                                         push_stack(nqueue, itA->node);
1561                                         skip = 1;
1562                                         break;
1563                                 } 
1564                                 itA = itA->next;
1565                         }                       
1566                         
1567                         if (!skip) {
1568                                 node = pop_queue(nqueue);
1569                                 node->color = DAG_BLACK;
1570                                 
1571                                 time++;
1572                                 push_stack(retqueue, node);
1573                         }
1574                 }
1575         }
1576         queue_delete(nqueue);
1577         return(retqueue);
1578 }
1579
1580 /* unused */
1581 #if 0
1582 short   are_obs_related(struct DagForest    *dag, void *ob1, void *ob2)
1583 {
1584         DagNode *node;
1585         DagAdjList *itA;
1586         
1587         node = dag_find_node(dag, ob1);
1588         
1589         itA = node->child;
1590         while (itA != NULL) {
1591                 if (itA->node->ob == ob2) {
1592                         return itA->node->type;
1593                 } 
1594                 itA = itA->next;
1595         }
1596         return DAG_NO_RELATION;
1597 }
1598 #endif
1599
1600 int is_acyclic(DagForest *dag)
1601 {
1602         return dag->is_acyclic;
1603 }
1604
1605 void set_node_xy(DagNode *node, float x, float y)
1606 {
1607         node->x = x;
1608         node->y = y;
1609 }
1610
1611
1612 /* debug test functions */
1613
1614 void graph_print_queue(DagNodeQueue *nqueue)
1615 {       
1616         DagNodeQueueElem *queueElem;
1617         
1618         queueElem = nqueue->first;
1619         while (queueElem) {
1620                 fprintf(stderr, "** %s %i %i-%i ", ((ID *) queueElem->node->ob)->name, queueElem->node->color, queueElem->node->DFS_dvtm, queueElem->node->DFS_fntm);
1621                 queueElem = queueElem->next;            
1622         }
1623         fprintf(stderr, "\n");
1624 }
1625
1626 void graph_print_queue_dist(DagNodeQueue *nqueue)
1627 {       
1628         DagNodeQueueElem *queueElem;
1629         int count;
1630         
1631         queueElem = nqueue->first;
1632         count = 0;
1633         while (queueElem) {
1634                 fprintf(stderr, "** %25s %2.2i-%2.2i ", ((ID *) queueElem->node->ob)->name, queueElem->node->DFS_dvtm, queueElem->node->DFS_fntm);
1635                 while (count < queueElem->node->DFS_dvtm - 1) { fputc(' ', stderr); count++; }
1636                 fputc('|', stderr);
1637                 while (count < queueElem->node->DFS_fntm - 2) { fputc('-', stderr); count++; }
1638                 fputc('|', stderr);
1639                 fputc('\n', stderr);
1640                 count = 0;
1641                 queueElem = queueElem->next;            
1642         }
1643         fprintf(stderr, "\n");
1644 }
1645
1646 void graph_print_adj_list(void)
1647 {
1648         DagNode *node;
1649         DagAdjList *itA;
1650         
1651         node = (getMainDag())->DagNode.first;
1652         while (node) {
1653                 fprintf(stderr, "node : %s col: %i", ((ID *) node->ob)->name, node->color);
1654                 itA = node->child;
1655                 while (itA) {
1656                         fprintf(stderr, "-- %s ", ((ID *) itA->node->ob)->name);
1657                         
1658                         itA = itA->next;
1659                 }
1660                 fprintf(stderr, "\n");
1661                 node = node->next;
1662         }
1663 }
1664
1665 /* ************************ API *********************** */
1666
1667 /* mechanism to allow editors to be informed of depsgraph updates,
1668  * to do their own updates based on changes... */
1669 static void (*EditorsUpdateIDCb)(Main *bmain, ID *id) = NULL;
1670 static void (*EditorsUpdateSceneCb)(Main *bmain, Scene *scene, int updated) = NULL;
1671
1672 void DAG_editors_update_cb(void (*id_func)(Main *bmain, ID *id), void (*scene_func)(Main *bmain, Scene *scene, int updated))
1673 {
1674         EditorsUpdateIDCb = id_func;
1675         EditorsUpdateSceneCb = scene_func;
1676 }
1677
1678 static void dag_editors_id_update(Main *bmain, ID *id)
1679 {
1680         if (EditorsUpdateIDCb)
1681                 EditorsUpdateIDCb(bmain, id);
1682 }
1683
1684 static void dag_editors_scene_update(Main *bmain, Scene *scene, int updated)
1685 {
1686         if (EditorsUpdateSceneCb)
1687                 EditorsUpdateSceneCb(bmain, scene, updated);
1688 }
1689
1690 /* groups with objects in this scene need to be put in the right order as well */
1691 static void scene_sort_groups(Main *bmain, Scene *sce)
1692 {
1693         Base *base;
1694         Group *group;
1695         GroupObject *go;
1696         Object *ob;
1697         
1698         /* test; are group objects all in this scene? */
1699         for (ob = bmain->object.first; ob; ob = ob->id.next) {
1700                 ob->id.flag &= ~LIB_DOIT;
1701                 ob->id.newid = NULL; /* newid abuse for GroupObject */
1702         }
1703         for (base = sce->base.first; base; base = base->next)
1704                 base->object->id.flag |= LIB_DOIT;
1705         
1706         for (group = bmain->group.first; group; group = group->id.next) {
1707                 for (go = group->gobject.first; go; go = go->next) {
1708                         if ((go->ob->id.flag & LIB_DOIT) == 0)
1709                                 break;
1710                 }
1711                 /* this group is entirely in this scene */
1712                 if (go == NULL) {
1713                         ListBase listb = {NULL, NULL};
1714                         
1715                         for (go = group->gobject.first; go; go = go->next)
1716                                 go->ob->id.newid = (ID *)go;
1717                         
1718                         /* in order of sorted bases we reinsert group objects */
1719                         for (base = sce->base.first; base; base = base->next) {
1720                                 
1721                                 if (base->object->id.newid) {
1722                                         go = (GroupObject *)base->object->id.newid;
1723                                         base->object->id.newid = NULL;
1724                                         BLI_remlink(&group->gobject, go);
1725                                         BLI_addtail(&listb, go);
1726                                 }
1727                         }
1728                         /* copy the newly sorted listbase */
1729                         group->gobject = listb;
1730                 }
1731         }
1732 }
1733
1734 /* sort the base list on dependency order */
1735 void DAG_scene_sort(Main *bmain, Scene *sce)
1736 {
1737         DagNode *node, *rootnode;
1738         DagNodeQueue *nqueue;
1739         DagAdjList *itA;
1740         int time;
1741         int skip = 0;
1742         ListBase tempbase;
1743         Base *base;
1744         
1745         tempbase.first = tempbase.last = NULL;
1746         
1747         build_dag(bmain, sce, DAG_RL_ALL_BUT_DATA);
1748         
1749         dag_check_cycle(sce->theDag);
1750
1751         nqueue = queue_create(DAGQUEUEALLOC);
1752         
1753         for (node = sce->theDag->DagNode.first; node; node = node->next) {
1754                 node->color = DAG_WHITE;
1755         }
1756         
1757         time = 1;
1758         
1759         rootnode = sce->theDag->DagNode.first;
1760         rootnode->color = DAG_GRAY;
1761         time++;
1762         push_stack(nqueue, rootnode);
1763         
1764         while (nqueue->count) {
1765                 
1766                 skip = 0;
1767                 node = get_top_node_queue(nqueue);
1768                 
1769                 itA = node->child;
1770                 while (itA != NULL) {
1771                         if (itA->node->color == DAG_WHITE) {
1772                                 itA->node->DFS_dvtm = time;
1773                                 itA->node->color = DAG_GRAY;
1774                                 
1775                                 time++;
1776                                 push_stack(nqueue, itA->node);
1777                                 skip = 1;
1778                                 break;
1779                         } 
1780                         itA = itA->next;
1781                 }                       
1782                 
1783                 if (!skip) {
1784                         if (node) {
1785                                 node = pop_queue(nqueue);
1786                                 if (node->ob == sce)    // we are done
1787                                         break;
1788                                 node->color = DAG_BLACK;
1789                                 
1790                                 time++;
1791                                 base = sce->base.first;
1792                                 while (base && base->object != node->ob)
1793                                         base = base->next;
1794                                 if (base) {
1795                                         BLI_remlink(&sce->base, base);
1796                                         BLI_addhead(&tempbase, base);
1797                                 }
1798                         }       
1799                 }
1800         }
1801         
1802         /* temporal correction for circular dependencies */
1803         base = sce->base.first;
1804         while (base) {
1805                 BLI_remlink(&sce->base, base);
1806                 BLI_addhead(&tempbase, base);
1807                 //if (G.debug & G_DEBUG)
1808                 printf("cyclic %s\n", base->object->id.name);
1809                 base = sce->base.first;
1810         }
1811         
1812         sce->base = tempbase;
1813         queue_delete(nqueue);
1814         
1815         /* all groups with objects in this scene gets resorted too */
1816         scene_sort_groups(bmain, sce);
1817         
1818         if (G.debug & G_DEBUG) {
1819                 printf("\nordered\n");
1820                 for (base = sce->base.first; base; base = base->next) {
1821                         printf(" %s\n", base->object->id.name);
1822                 }
1823         }
1824         /* temporal...? */
1825         sce->recalc |= SCE_PRV_CHANGED; /* test for 3d preview */
1826 }
1827
1828 static void lib_id_recalc_tag(Main *bmain, ID *id)
1829 {
1830         id->flag |= LIB_ID_RECALC;
1831         bmain->id_tag_update[id->name[0]] = 1;
1832 }
1833
1834 static void lib_id_recalc_data_tag(Main *bmain, ID *id)
1835 {
1836         id->flag |= LIB_ID_RECALC_DATA;
1837         bmain->id_tag_update[id->name[0]] = 1;
1838 }
1839
1840 /* node was checked to have lasttime != curtime and is if type ID_OB */
1841 static void flush_update_node(DagNode *node, unsigned int layer, int curtime)
1842 {
1843         Main *bmain = G.main;
1844         DagAdjList *itA;
1845         Object *ob, *obc;
1846         int oldflag, changed = 0;
1847         unsigned int all_layer;
1848         
1849         node->lasttime = curtime;
1850         
1851         ob = node->ob;
1852         if (ob && (ob->recalc & OB_RECALC_ALL)) {
1853                 all_layer = node->scelay;
1854
1855                 /* got an object node that changes, now check relations */
1856                 for (itA = node->child; itA; itA = itA->next) {
1857                         all_layer |= itA->lay;
1858                         /* the relationship is visible */
1859                         if ((itA->lay & layer)) { // XXX || (itA->node->ob == obedit)
1860                                 if (itA->node->type == ID_OB) {
1861                                         obc = itA->node->ob;
1862                                         oldflag = obc->recalc;
1863                                         
1864                                         /* got a ob->obc relation, now check if flag needs flush */
1865                                         if (ob->recalc & OB_RECALC_OB) {
1866                                                 if (itA->type & DAG_RL_OB_OB) {
1867                                                         //printf("ob %s changes ob %s\n", ob->id.name, obc->id.name);
1868                                                         obc->recalc |= OB_RECALC_OB;
1869                                                         lib_id_recalc_tag(bmain, &obc->id);
1870                                                 }
1871                                                 if (itA->type & DAG_RL_OB_DATA) {
1872                                                         //printf("ob %s changes obdata %s\n", ob->id.name, obc->id.name);
1873                                                         obc->recalc |= OB_RECALC_DATA;
1874                                                         lib_id_recalc_data_tag(bmain, &obc->id);
1875                                                 }
1876                                         }
1877                                         if (ob->recalc & OB_RECALC_DATA) {
1878                                                 if (itA->type & DAG_RL_DATA_OB) {
1879                                                         //printf("obdata %s changes ob %s\n", ob->id.name, obc->id.name);
1880                                                         obc->recalc |= OB_RECALC_OB;
1881                                                         lib_id_recalc_tag(bmain, &obc->id);
1882                                                 }
1883                                                 if (itA->type & DAG_RL_DATA_DATA) {
1884                                                         //printf("obdata %s changes obdata %s\n", ob->id.name, obc->id.name);
1885                                                         obc->recalc |= OB_RECALC_DATA;
1886                                                         lib_id_recalc_data_tag(bmain, &obc->id);
1887                                                 }
1888                                         }
1889                                         if (oldflag != obc->recalc) changed = 1;
1890                                 }
1891                         }
1892                 }
1893                 /* even nicer, we can clear recalc flags...  */
1894                 if ((all_layer & layer) == 0) { // XXX && (ob != obedit)) {
1895                         /* but existing displaylists or derivedmesh should be freed */
1896                         if (ob->recalc & OB_RECALC_DATA)
1897                                 BKE_object_free_display(ob);
1898                         
1899                         ob->recalc &= ~OB_RECALC_ALL;
1900                 }
1901         }
1902         
1903         /* check case where child changes and parent forcing obdata to change */
1904         /* should be done regardless if this ob has recalc set */
1905         /* could merge this in with loop above...? (ton) */
1906         for (itA = node->child; itA; itA = itA->next) {
1907                 /* the relationship is visible */
1908                 if ((itA->lay & layer)) {       // XXX  || (itA->node->ob == obedit)
1909                         if (itA->node->type == ID_OB) {
1910                                 obc = itA->node->ob;
1911                                 /* child moves */
1912                                 if ((obc->recalc & OB_RECALC_ALL) == OB_RECALC_OB) {
1913                                         /* parent has deforming info */
1914                                         if (itA->type & (DAG_RL_OB_DATA | DAG_RL_DATA_DATA)) {
1915                                                 // printf("parent %s changes ob %s\n", ob->id.name, obc->id.name);
1916                                                 obc->recalc |= OB_RECALC_DATA;
1917                                                 lib_id_recalc_data_tag(bmain, &obc->id);
1918                                         }
1919                                 }
1920                         }
1921                 }
1922         }
1923         
1924         /* we only go deeper if node not checked or something changed  */
1925         for (itA = node->child; itA; itA = itA->next) {
1926                 if (changed || itA->node->lasttime != curtime)
1927                         flush_update_node(itA->node, layer, curtime);
1928         }
1929         
1930 }
1931
1932 /* node was checked to have lasttime != curtime, and is of type ID_OB */
1933 static unsigned int flush_layer_node(Scene *sce, DagNode *node, int curtime)
1934 {
1935         DagAdjList *itA;
1936         
1937         node->lasttime = curtime;
1938         node->lay = node->scelay;
1939         
1940         for (itA = node->child; itA; itA = itA->next) {
1941                 if (itA->node->type == ID_OB) {
1942                         if (itA->node->lasttime != curtime) {
1943                                 itA->lay = flush_layer_node(sce, itA->node, curtime);  // lay is only set once for each relation
1944                         }
1945                         else itA->lay = itA->node->lay;
1946                         
1947                         node->lay |= itA->lay;
1948                 }
1949         }
1950
1951         return node->lay;
1952 }
1953
1954 /* node was checked to have lasttime != curtime, and is of type ID_OB */
1955 static void flush_pointcache_reset(Scene *scene, DagNode *node, int curtime, int reset)
1956 {
1957         Main *bmain = G.main;
1958         DagAdjList *itA;
1959         Object *ob;
1960         
1961         node->lasttime = curtime;
1962         
1963         for (itA = node->child; itA; itA = itA->next) {
1964                 if (itA->node->type == ID_OB) {
1965                         if (itA->node->lasttime != curtime) {
1966                                 ob = (Object *)(itA->node->ob);
1967
1968                                 if (reset || (ob->recalc & OB_RECALC_ALL)) {
1969                                         if (BKE_ptcache_object_reset(scene, ob, PTCACHE_RESET_DEPSGRAPH)) {
1970                                                 ob->recalc |= OB_RECALC_DATA;
1971                                                 lib_id_recalc_data_tag(bmain, &ob->id);
1972                                         }
1973
1974                                         flush_pointcache_reset(scene, itA->node, curtime, 1);
1975                                 }
1976                                 else
1977                                         flush_pointcache_reset(scene, itA->node, curtime, 0);
1978                         }
1979                 }
1980         }
1981 }
1982
1983 /* flush layer flags to dependencies */
1984 static void dag_scene_flush_layers(Scene *sce, int lay)
1985 {
1986         DagNode *node, *firstnode;
1987         DagAdjList *itA;
1988         Base *base;
1989         int lasttime;
1990
1991         firstnode = sce->theDag->DagNode.first;  // always scene node
1992
1993         for (itA = firstnode->child; itA; itA = itA->next)
1994                 itA->lay = 0;
1995
1996         sce->theDag->time++;    // so we know which nodes were accessed
1997         lasttime = sce->theDag->time;
1998
1999         /* update layer flags in nodes */
2000         for (base = sce->base.first; base; base = base->next) {
2001                 node = dag_get_node(sce->theDag, base->object);
2002                 node->scelay = base->object->lay;
2003         }
2004
2005         /* ensure cameras are set as if they are on a visible layer, because
2006          * they ared still used for rendering or setting the camera view
2007          *
2008          * XXX, this wont work for local view / unlocked camera's */
2009         if (sce->camera) {
2010                 node = dag_get_node(sce->theDag, sce->camera);
2011                 node->scelay |= lay;
2012         }
2013
2014 #ifdef DURIAN_CAMERA_SWITCH
2015         {
2016                 TimeMarker *m;
2017
2018                 for (m = sce->markers.first; m; m = m->next) {
2019                         if (m->camera) {
2020                                 node = dag_get_node(sce->theDag, m->camera);
2021                                 node->scelay |= lay;
2022                         }
2023                 }
2024         }
2025 #endif
2026
2027         /* flush layer nodes to dependencies */
2028         for (itA = firstnode->child; itA; itA = itA->next)
2029                 if (itA->node->lasttime != lasttime && itA->node->type == ID_OB)
2030                         flush_layer_node(sce, itA->node, lasttime);
2031 }
2032
2033 static void dag_tag_renderlayers(Scene *sce, unsigned int lay)
2034 {
2035         if (sce->nodetree) {
2036                 bNode *node;
2037                 Base *base;
2038                 unsigned int lay_changed = 0;
2039                 
2040                 for (base = sce->base.first; base; base = base->next)
2041                         if (base->lay & lay)
2042                                 if (base->object->recalc)
2043                                         lay_changed |= base->lay;
2044                         
2045                 for (node = sce->nodetree->nodes.first; node; node = node->next) {
2046                         if (node->id == (ID *)sce) {
2047                                 SceneRenderLayer *srl = BLI_findlink(&sce->r.layers, node->custom1);
2048                                 if (srl && (srl->lay & lay_changed))
2049                                         nodeUpdate(sce->nodetree, node);
2050                         }
2051                 }
2052         }
2053 }
2054
2055 /* flushes all recalc flags in objects down the dependency tree */
2056 void DAG_scene_flush_update(Main *bmain, Scene *sce, unsigned int lay, const short time)
2057 {
2058         DagNode *firstnode;
2059         DagAdjList *itA;
2060         Object *ob;
2061         int lasttime;
2062         
2063         if (sce->theDag == NULL) {
2064                 printf("DAG zero... not allowed to happen!\n");
2065                 DAG_scene_sort(bmain, sce);
2066         }
2067         
2068         firstnode = sce->theDag->DagNode.first;  // always scene node
2069
2070         /* first we flush the layer flags */
2071         dag_scene_flush_layers(sce, lay);
2072
2073         /* then we use the relationships + layer info to flush update events */
2074         sce->theDag->time++;    // so we know which nodes were accessed
2075         lasttime = sce->theDag->time;
2076         for (itA = firstnode->child; itA; itA = itA->next)
2077                 if (itA->node->lasttime != lasttime && itA->node->type == ID_OB)
2078                         flush_update_node(itA->node, lay, lasttime);
2079
2080         /* if update is not due to time change, do pointcache clears */
2081         if (!time) {
2082                 sce->theDag->time++;    // so we know which nodes were accessed
2083                 lasttime = sce->theDag->time;
2084                 for (itA = firstnode->child; itA; itA = itA->next) {
2085                         if (itA->node->lasttime != lasttime && itA->node->type == ID_OB) {
2086                                 ob = (Object *)(itA->node->ob);
2087
2088                                 if (ob->recalc & OB_RECALC_ALL) {
2089                                         if (BKE_ptcache_object_reset(sce, ob, PTCACHE_RESET_DEPSGRAPH)) {
2090                                                 ob->recalc |= OB_RECALC_DATA;
2091                                                 lib_id_recalc_data_tag(bmain, &ob->id);
2092                                         }
2093
2094                                         flush_pointcache_reset(sce, itA->node, lasttime, 1);
2095                                 }
2096                                 else
2097                                         flush_pointcache_reset(sce, itA->node, lasttime, 0);
2098                         }
2099                 }
2100         }
2101         
2102         dag_tag_renderlayers(sce, lay);
2103 }
2104
2105 static int object_modifiers_use_time(Object *ob)
2106 {
2107         ModifierData *md;
2108         
2109         /* check if a modifier in modifier stack needs time input */
2110         for (md = ob->modifiers.first; md; md = md->next)
2111                 if (modifier_dependsOnTime(md))
2112                         return 1;
2113         
2114         /* check whether any modifiers are animated */
2115         if (ob->adt) {
2116                 AnimData *adt = ob->adt;
2117                 FCurve *fcu;
2118                 
2119                 /* action - check for F-Curves with paths containing 'modifiers[' */
2120                 if (adt->action) {
2121                         for (fcu = adt->action->curves.first; fcu; fcu = fcu->next) {
2122                                 if (fcu->rna_path && strstr(fcu->rna_path, "modifiers["))
2123                                         return 1;
2124                         }
2125                 }
2126                 
2127                 /* This here allows modifier properties to get driven and still update properly
2128                  *
2129                  * Workaround to get [#26764] (e.g. subsurf levels not updating when animated/driven)
2130                  * working, without the updating problems ([#28525] [#28690] [#28774] [#28777]) caused
2131                  * by the RNA updates cache introduced in r.38649
2132                  */
2133                 for (fcu = adt->drivers.first; fcu; fcu = fcu->next) {
2134                         if (fcu->rna_path && strstr(fcu->rna_path, "modifiers["))
2135                                 return 1;
2136                 }
2137                 
2138                 // XXX: also, should check NLA strips, though for now assume that nobody uses
2139                 // that and we can omit that for performance reasons...
2140         }
2141         
2142         return 0;
2143 }
2144
2145 static short animdata_use_time(AnimData *adt)
2146 {
2147         NlaTrack *nlt;
2148         
2149         if (adt == NULL) return 0;
2150         
2151         /* check action - only if assigned, and it has anim curves */
2152         if (adt->action && adt->action->curves.first)
2153                 return 1;
2154         
2155         /* check NLA tracks + strips */
2156         for (nlt = adt->nla_tracks.first; nlt; nlt = nlt->next) {
2157                 if (nlt->strips.first)
2158                         return 1;
2159         }
2160         
2161         /* If we have drivers, more likely than not, on a frame change
2162          * they'll need updating because their owner changed
2163          * 
2164          * This is kindof a hack to get around a whole host of problems
2165          * involving drivers using non-object datablock data (which the 
2166          * depsgraph currently has no way of representing let alone correctly
2167          * dependency sort+tagging). By doing this, at least we ensure that 
2168          * some commonly attempted drivers (such as scene -> current frame;
2169          * see "Driver updates fail" thread on Bf-committers dated July 2)
2170          * will work correctly, and that other non-object datablocks will have
2171          * their drivers update at least on frame change.
2172          *
2173          * -- Aligorith, July 4 2011
2174          */
2175         if (adt->drivers.first)
2176                 return 1;
2177         
2178         return 0;
2179 }
2180
2181 static void dag_object_time_update_flags(Object *ob)
2182 {
2183         if (ob->constraints.first) {
2184                 bConstraint *con;
2185                 for (con = ob->constraints.first; con; con = con->next) {
2186                         bConstraintTypeInfo *cti = constraint_get_typeinfo(con);
2187                         ListBase targets = {NULL, NULL};
2188                         bConstraintTarget *ct;
2189                         
2190                         if (cti) {
2191                                 /* special case for camera tracking -- it doesn't use targets to define relations */
2192                                 if (ELEM3(cti->type, CONSTRAINT_TYPE_FOLLOWTRACK, CONSTRAINT_TYPE_CAMERASOLVER, CONSTRAINT_TYPE_OBJECTSOLVER)) {
2193                                         ob->recalc |= OB_RECALC_OB;
2194                                 }
2195                                 else if (cti->get_constraint_targets) {
2196                                         cti->get_constraint_targets(con, &targets);
2197                                         
2198                                         for (ct = targets.first; ct; ct = ct->next) {
2199                                                 if (ct->tar) {
2200                                                         ob->recalc |= OB_RECALC_OB;
2201                                                         break;
2202                                                 }
2203                                         }
2204                                         
2205                                         if (cti->flush_constraint_targets)
2206                                                 cti->flush_constraint_targets(con, &targets, 1);
2207                                 }
2208                                 
2209                         }
2210                 }
2211         }
2212         
2213         if (ob->parent) {
2214                 /* motion path or bone child */
2215                 if (ob->parent->type == OB_CURVE || ob->parent->type == OB_ARMATURE) ob->recalc |= OB_RECALC_OB;
2216         }
2217         
2218 #if 0 // XXX old animation system
2219         if (ob->nlastrips.first) {
2220                 if (ob->dup_group) {
2221                         bActionStrip *strip;
2222                         /* this case is for groups with nla, whilst nla target has no action or nla */
2223                         for (strip = ob->nlastrips.first; strip; strip = strip->next) {
2224                                 if (strip->object)
2225                                         strip->object->recalc |= OB_RECALC_ALL;
2226                         }
2227                 }
2228         }
2229 #endif // XXX old animation system
2230         
2231         if (animdata_use_time(ob->adt)) {
2232                 ob->recalc |= OB_RECALC_OB;
2233                 ob->adt->recalc |= ADT_RECALC_ANIM;
2234         }
2235         
2236         if ((ob->adt) && (ob->type == OB_ARMATURE)) ob->recalc |= OB_RECALC_DATA;
2237         
2238         if (object_modifiers_use_time(ob)) ob->recalc |= OB_RECALC_DATA;
2239         if ((ob->pose) && (ob->pose->flag & POSE_CONSTRAINTS_TIMEDEPEND)) ob->recalc |= OB_RECALC_DATA;
2240         
2241         {
2242                 AnimData *adt = BKE_animdata_from_id((ID *)ob->data);
2243                 Mesh *me;
2244                 Curve *cu;
2245                 Lattice *lt;
2246                 
2247                 switch (ob->type) {
2248                         case OB_MESH:
2249                                 me = ob->data;
2250                                 if (me->key) {
2251                                         if (!(ob->shapeflag & OB_SHAPE_LOCK)) {
2252                                                 ob->recalc |= OB_RECALC_DATA;
2253                                         }
2254                                 }
2255                                 if (ob->particlesystem.first)
2256                                         ob->recalc |= OB_RECALC_DATA;
2257                                 break;
2258                         case OB_CURVE:
2259                         case OB_SURF:
2260                                 cu = ob->data;
2261                                 if (cu->key) {
2262                                         if (!(ob->shapeflag & OB_SHAPE_LOCK)) {
2263                                                 ob->recalc |= OB_RECALC_DATA;
2264                                         }
2265                                 }
2266                                 break;
2267                         case OB_FONT:
2268                                 cu = ob->data;
2269                                 if (cu->nurb.first == NULL && cu->str && cu->vfont)
2270                                         ob->recalc |= OB_RECALC_DATA;
2271                                 break;
2272                         case OB_LATTICE:
2273                                 lt = ob->data;
2274                                 if (lt->key) {
2275                                         if (!(ob->shapeflag & OB_SHAPE_LOCK)) {
2276                                                 ob->recalc |= OB_RECALC_DATA;
2277                                         }
2278                                 }
2279                                 break;
2280                         case OB_MBALL:
2281                                 if (ob->transflag & OB_DUPLI) ob->recalc |= OB_RECALC_DATA;
2282                                 break;
2283                 }
2284                 
2285                 if (animdata_use_time(adt)) {
2286                         ob->recalc |= OB_RECALC_DATA;
2287                         adt->recalc |= ADT_RECALC_ANIM;
2288                 }
2289
2290                 if (ob->particlesystem.first) {
2291                         ParticleSystem *psys = ob->particlesystem.first;
2292
2293                         for (; psys; psys = psys->next) {
2294                                 if (psys_check_enabled(ob, psys)) {
2295                                         ob->recalc |= OB_RECALC_DATA;
2296                                         break;
2297                                 }
2298                         }
2299                 }
2300         }               
2301
2302         if (ob->recalc & OB_RECALC_OB)
2303                 lib_id_recalc_tag(G.main, &ob->id);
2304         if (ob->recalc & OB_RECALC_DATA)
2305                 lib_id_recalc_data_tag(G.main, &ob->id);
2306
2307 }
2308 /* flag all objects that need recalc, for changes in time for example */
2309 /* do_time: make this optional because undo resets objects to their animated locations without this */
2310 void DAG_scene_update_flags(Main *bmain, Scene *scene, unsigned int lay, const short do_time)
2311 {
2312         Base *base;
2313         Object *ob;
2314         Group *group;
2315         GroupObject *go;
2316         Scene *sce_iter;
2317
2318         /* set ob flags where animated systems are */
2319         for (SETLOOPER(scene, sce_iter, base)) {
2320                 ob = base->object;
2321
2322                 if (do_time) {
2323                         /* now if DagNode were part of base, the node->lay could be checked... */
2324                         /* we do all now, since the scene_flush checks layers and clears recalc flags even */
2325                         dag_object_time_update_flags(ob);
2326                 }
2327
2328                 /* handled in next loop */
2329                 if (ob->dup_group)
2330                         ob->dup_group->id.flag |= LIB_DOIT;
2331         }
2332
2333         if (do_time) {
2334                 /* we do groups each once */
2335                 for (group = bmain->group.first; group; group = group->id.next) {
2336                         if (group->id.flag & LIB_DOIT) {
2337                                 for (go = group->gobject.first; go; go = go->next) {
2338                                         dag_object_time_update_flags(go->ob);
2339                                 }
2340                         }
2341                 }
2342         }
2343
2344         for (sce_iter = scene; sce_iter; sce_iter = sce_iter->set)
2345                 DAG_scene_flush_update(bmain, sce_iter, lay, 1);
2346         
2347         if (do_time) {
2348                 /* test: set time flag, to disable baked systems to update */
2349                 for (SETLOOPER(scene, sce_iter, base)) {
2350                         ob = base->object;
2351                         if (ob->recalc)
2352                                 ob->recalc |= OB_RECALC_TIME;
2353                 }
2354
2355                 /* hrmf... an exception to look at once, for invisible camera object we do it over */
2356                 if (scene->camera)
2357                         dag_object_time_update_flags(scene->camera);
2358         }
2359
2360         /* and store the info in groupobject */
2361         for (group = bmain->group.first; group; group = group->id.next) {
2362                 if (group->id.flag & LIB_DOIT) {
2363                         for (go = group->gobject.first; go; go = go->next) {
2364                                 go->recalc = go->ob->recalc;
2365                                 // printf("ob %s recalc %d\n", go->ob->id.name, go->recalc);
2366                         }
2367                         group->id.flag &= ~LIB_DOIT;
2368                 }
2369         }
2370         
2371 }
2372
2373 static void dag_current_scene_layers(Main *bmain, Scene **sce, unsigned int *lay)
2374 {
2375         wmWindowManager *wm;
2376         wmWindow *win;
2377
2378         /* only one scene supported currently, making more scenes work
2379          * correctly requires changes beyond just the dependency graph */
2380
2381         *sce = NULL;
2382         *lay = 0;
2383
2384         if ((wm = bmain->wm.first)) {
2385                 /* if we have a windowmanager, look into windows */
2386                 for (win = wm->windows.first; win; win = win->next) {
2387                         if (win->screen) {
2388                                 if (!*sce) *sce = win->screen->scene;
2389                                 *lay |= BKE_screen_visible_layers(win->screen, win->screen->scene);
2390                         }
2391                 }
2392         }
2393         else {
2394                 /* if not, use the first sce */
2395                 *sce = bmain->scene.first;
2396                 if (*sce) *lay = (*sce)->lay;
2397
2398                 /* XXX for background mode, we should get the scene
2399                  * from somewhere, for the -S option, but it's in
2400                  * the context, how to get it here? */
2401         }
2402 }
2403
2404 void DAG_ids_flush_update(Main *bmain, int time)
2405 {
2406         Scene *sce;
2407         unsigned int lay;
2408
2409         dag_current_scene_layers(bmain, &sce, &lay);
2410
2411         if (sce)
2412                 DAG_scene_flush_update(bmain, sce, lay, time);
2413 }
2414
2415 void DAG_on_visible_update(Main *bmain, const short do_time)
2416 {
2417         Scene *scene;
2418         Base *base;
2419         Object *ob;
2420         Group *group;
2421         GroupObject *go;
2422         DagNode *node;
2423         unsigned int lay, oblay;
2424
2425         dag_current_scene_layers(bmain, &scene, &lay);
2426
2427         if (scene && scene->theDag) {
2428                 Scene *sce_iter;
2429                 /* derivedmeshes and displists are not saved to file so need to be
2430                  * remade, tag them so they get remade in the scene update loop,
2431                  * note armature poses or object matrices are preserved and do not
2432                  * require updates, so we skip those */
2433                 dag_scene_flush_layers(scene, lay);
2434
2435                 for (SETLOOPER(scene, sce_iter, base)) {
2436                         ob = base->object;
2437                         node = (sce_iter->theDag) ? dag_get_node(sce_iter->theDag, ob) : NULL;
2438                         oblay = (node) ? node->lay : ob->lay;
2439
2440                         if ((oblay & lay) & ~scene->lay_updated) {
2441                                 if (ELEM6(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL, OB_LATTICE))
2442                                         ob->recalc |= OB_RECALC_DATA;
2443                                 if (ob->dup_group) 
2444                                         ob->dup_group->id.flag |= LIB_DOIT;
2445                         }
2446                 }
2447
2448                 for (group = bmain->group.first; group; group = group->id.next) {
2449                         if (group->id.flag & LIB_DOIT) {
2450                                 for (go = group->gobject.first; go; go = go->next) {
2451                                         if (ELEM6(go->ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL, OB_LATTICE))
2452                                                 go->ob->recalc |= OB_RECALC_DATA;
2453                                         if (go->ob->proxy_from)
2454                                                 go->ob->recalc |= OB_RECALC_OB;
2455                                 }
2456                                 
2457                                 group->id.flag &= ~LIB_DOIT;
2458                         }
2459                 }
2460
2461                 /* now tag update flags, to ensure deformers get calculated on redraw */
2462                 DAG_scene_update_flags(bmain, scene, lay, do_time);
2463                 scene->lay_updated |= lay;
2464         }
2465
2466         /* hack to get objects updating on layer changes */
2467         DAG_id_type_tag(bmain, ID_OB);
2468 }
2469
2470 static void dag_id_flush_update__isDependentTexture(void *userData, Object *UNUSED(ob), ID **idpoin)
2471 {
2472         struct { ID *id; int is_dependent; } *data = userData;
2473         
2474         if (*idpoin && GS((*idpoin)->name) == ID_TE) {
2475                 if (data->id == (*idpoin))
2476                         data->is_dependent = 1;
2477         }
2478 }
2479
2480 static void dag_id_flush_update(Scene *sce, ID *id)
2481 {
2482         Main *bmain = G.main;
2483         Object *obt, *ob = NULL;
2484         short idtype;
2485
2486         /* here we flush a few things before actual scene wide flush, mostly
2487          * due to only objects and not other datablocks being in the depsgraph */
2488
2489         /* set flags & pointcache for object */
2490         if (GS(id->name) == ID_OB) {
2491                 ob = (Object *)id;
2492                 BKE_ptcache_object_reset(sce, ob, PTCACHE_RESET_DEPSGRAPH);
2493
2494                 if (ob->recalc & OB_RECALC_DATA) {
2495                         /* all users of this ob->data should be checked */
2496                         id = ob->data;
2497
2498                         /* no point in trying in this cases */
2499                         if (id && id->us <= 1) {
2500                                 dag_editors_id_update(bmain, id);
2501                                 id = NULL;
2502                         }
2503                 }
2504         }
2505
2506         /* set flags & pointcache for object data */
2507         if (id) {
2508                 idtype = GS(id->name);
2509
2510                 if (ELEM8(idtype, ID_ME, ID_CU, ID_MB, ID_LA, ID_LT, ID_CA, ID_AR, ID_SPK)) {
2511                         for (obt = bmain->object.first; obt; obt = obt->id.next) {
2512                                 if (!(ob && obt == ob) && obt->data == id) {
2513                                         obt->recalc |= OB_RECALC_DATA;
2514                                         lib_id_recalc_data_tag(bmain, &obt->id);
2515                                         BKE_ptcache_object_reset(sce, obt, PTCACHE_RESET_DEPSGRAPH);
2516                                 }
2517                         }
2518                 }
2519                 
2520                 /* set flags based on textures - can influence depgraph via modifiers */
2521                 if (idtype == ID_TE) {
2522                         for (obt = bmain->object.first; obt; obt = obt->id.next) {
2523                                 struct { ID *id; int is_dependent; } data;
2524                                 data.id = id;
2525                                 data.is_dependent = 0;
2526
2527                                 modifiers_foreachIDLink(obt, dag_id_flush_update__isDependentTexture, &data);
2528                                 if (data.is_dependent) {
2529                                         obt->recalc |= OB_RECALC_DATA;
2530                                         lib_id_recalc_data_tag(bmain, &obt->id);
2531                                 }
2532
2533                                 /* particle settings can use the texture as well */
2534                                 if (obt->particlesystem.first) {
2535                                         ParticleSystem *psys = obt->particlesystem.first;
2536                                         MTex **mtexp, *mtex;
2537                                         int a;
2538                                         for (; psys; psys = psys->next) {
2539                                                 mtexp = psys->part->mtex;
2540                                                 for (a = 0; a < MAX_MTEX; a++, mtexp++) {
2541                                                         mtex = *mtexp;
2542                                                         if (mtex && mtex->tex == (Tex *)id) {
2543                                                                 obt->recalc |= OB_RECALC_DATA;
2544                                                                 lib_id_recalc_data_tag(bmain, &obt->id);
2545
2546                                                                 if (mtex->mapto & PAMAP_INIT)
2547                                                                         psys->recalc |= PSYS_RECALC_RESET;
2548                                                                 if (mtex->mapto & PAMAP_CHILD)
2549                                                                         psys->recalc |= PSYS_RECALC_CHILD;
2550
2551                                                                 BKE_ptcache_object_reset(sce, obt, PTCACHE_RESET_DEPSGRAPH);
2552                                                         }
2553                                                 }
2554                                         }
2555                                 }
2556                         }
2557                 }
2558                 
2559                 /* set flags based on ShapeKey */
2560                 if (idtype == ID_KE) {
2561                         for (obt = bmain->object.first; obt; obt = obt->id.next) {
2562                                 Key *key = ob_get_key(obt);
2563                                 if (!(ob && obt == ob) && ((ID *)key == id)) {
2564                                         obt->flag |= (OB_RECALC_OB | OB_RECALC_DATA);
2565                                         lib_id_recalc_tag(bmain, &obt->id);
2566                                         lib_id_recalc_data_tag(bmain, &obt->id);
2567                                         BKE_ptcache_object_reset(sce, obt, PTCACHE_RESET_DEPSGRAPH);
2568                                 }
2569                         }
2570                 }
2571                 
2572                 /* set flags based on particle settings */
2573                 if (idtype == ID_PA) {
2574                         ParticleSystem *psys;
2575                         for (obt = bmain->object.first; obt; obt = obt->id.next)
2576                                 for (psys = obt->particlesystem.first; psys; psys = psys->next)
2577                                         if (&psys->part->id == id)
2578                                                 BKE_ptcache_object_reset(sce, obt, PTCACHE_RESET_DEPSGRAPH);
2579                 }
2580
2581                 if (idtype == ID_MC) {
2582                         MovieClip *clip = (MovieClip *) id;
2583
2584                         BKE_tracking_dopesheet_tag_update(&clip->tracking);
2585
2586                         for (obt = bmain->object.first; obt; obt = obt->id.next) {
2587                                 bConstraint *con;
2588                                 for (con = obt->constraints.first; con; con = con->next) {
2589                                         bConstraintTypeInfo *cti = constraint_get_typeinfo(con);
2590                                         if (ELEM3(cti->type, CONSTRAINT_TYPE_FOLLOWTRACK, CONSTRAINT_TYPE_CAMERASOLVER,
2591                                                   CONSTRAINT_TYPE_OBJECTSOLVER))
2592                                         {
2593                                                 obt->recalc |= OB_RECALC_OB;
2594                                                 break;
2595                                         }
2596                                 }
2597                         }
2598
2599                         if (sce->nodetree) {
2600                                 bNode *node;
2601
2602                                 for (node = sce->nodetree->nodes.first; node; node = node->next) {
2603                                         if (node->id == id) {
2604                                                 nodeUpdate(sce->nodetree, node);
2605                                         }
2606                                 }
2607                         }
2608                 }
2609
2610                 /* camera's matrix is used to orient reconstructed stuff,
2611                  * so it should happen tracking-related constraints recalculation
2612                  * when camera is changing (sergey) */
2613                 if (sce->camera && &sce->camera->id == id) {
2614                         MovieClip *clip = BKE_object_movieclip_get(sce, sce->camera, 1);
2615
2616                         if (clip)
2617                                 dag_id_flush_update(sce, &clip->id);
2618                 }
2619
2620                 /* update editors */
2621                 dag_editors_id_update(bmain, id);
2622         }
2623 }
2624
2625 void DAG_ids_flush_tagged(Main *bmain)
2626 {
2627         ListBase *lbarray[MAX_LIBARRAY];
2628         Scene *sce;
2629         unsigned int lay;
2630         int a, do_flush = 0;
2631
2632         dag_current_scene_layers(bmain, &sce, &lay);
2633
2634         if (!sce || !sce->theDag)
2635                 return;
2636
2637         /* loop over all ID types */
2638         a  = set_listbasepointers(bmain, lbarray);
2639
2640         while (a--) {
2641                 ListBase *lb = lbarray[a];
2642                 ID *id = lb->first;
2643
2644                 /* we tag based on first ID type character to avoid 
2645                  * looping over all ID's in case there are no tags */
2646                 if (id && bmain->id_tag_update[id->name[0]]) {
2647                         for (; id; id = id->next) {
2648                                 if (id->flag & (LIB_ID_RECALC | LIB_ID_RECALC_DATA)) {
2649                                         dag_id_flush_update(sce, id);
2650                                         do_flush = 1;
2651                                 }
2652                         }
2653                 }
2654         }
2655
2656         /* flush changes to other objects */
2657         if (do_flush)
2658                 DAG_scene_flush_update(bmain, sce, lay, 0);
2659 }
2660
2661 void DAG_ids_check_recalc(Main *bmain, Scene *scene, int time)
2662 {
2663         ListBase *lbarray[MAX_LIBARRAY];
2664         int a, updated = 0;
2665
2666         /* loop over all ID types */
2667         a  = set_listbasepointers(bmain, lbarray);
2668
2669         while (a--) {
2670                 ListBase *lb = lbarray[a];
2671                 ID *id = lb->first;
2672
2673                 /* we tag based on first ID type character to avoid 
2674                  * looping over all ID's in case there are no tags */
2675                 if (id && bmain->id_tag_update[id->name[0]]) {
2676                         updated = 1;
2677                         break;
2678                 }
2679         }
2680
2681         dag_editors_scene_update(bmain, scene, (updated || time));
2682 }
2683
2684 void DAG_ids_clear_recalc(Main *bmain)
2685 {
2686         ListBase *lbarray[MAX_LIBARRAY];
2687         int a;
2688
2689         /* loop over all ID types */
2690         a  = set_listbasepointers(bmain, lbarray);
2691
2692         while (a--) {
2693                 ListBase *lb = lbarray[a];
2694                 ID *id = lb->first;
2695
2696                 /* we tag based on first ID type character to avoid 
2697                  * looping over all ID's in case there are no tags */
2698                 if (id && bmain->id_tag_update[id->name[0]]) {
2699                         for (; id; id = id->next)
2700                                 if (id->flag & (LIB_ID_RECALC | LIB_ID_RECALC_DATA))
2701                                         id->flag &= ~(LIB_ID_RECALC | LIB_ID_RECALC_DATA);
2702                 }
2703         }
2704
2705         memset(bmain->id_tag_update, 0, sizeof(bmain->id_tag_update));
2706 }
2707
2708 void DAG_id_tag_update(ID *id, short flag)
2709 {
2710         Main *bmain = G.main;
2711
2712         if (id == NULL) return;
2713         
2714         /* tag ID for update */
2715         if (flag) {
2716                 if (flag & OB_RECALC_OB)
2717                         lib_id_recalc_tag(bmain, id);
2718                 if (flag & (OB_RECALC_DATA | PSYS_RECALC))
2719                         lib_id_recalc_data_tag(bmain, id);
2720         }
2721         else
2722                 lib_id_recalc_tag(bmain, id);
2723
2724         /* flag is for objects and particle systems */
2725         if (flag) {
2726                 Object *ob;
2727                 short idtype = GS(id->name);
2728
2729                 if (idtype == ID_OB) {
2730                         /* only quick tag */
2731                         ob = (Object *)id;
2732                         ob->recalc |= (flag & OB_RECALC_ALL);
2733                 }
2734                 else if (idtype == ID_PA) {
2735                         ParticleSystem *psys;
2736                         /* this is weak still, should be done delayed as well */
2737                         for (ob = bmain->object.first; ob; ob = ob->id.next) {
2738                                 for (psys = ob->particlesystem.first; psys; psys = psys->next) {
2739                                         if (&psys->part->id == id) {
2740                                                 ob->recalc |= (flag & OB_RECALC_ALL);
2741                                                 psys->recalc |= (flag & PSYS_RECALC);
2742                                                 lib_id_recalc_tag(bmain, &ob->id);
2743                                                 lib_id_recalc_data_tag(bmain, &ob->id);
2744                                         }
2745                                 }
2746                         }
2747                 }
2748                 else {
2749                         /* disable because this is called on various ID types automatically.
2750                          * where printing warning is not useful. for now just ignore */
2751                         /* BLI_assert(!"invalid flag for this 'idtype'"); */
2752                 }
2753         }
2754 }
2755
2756 void DAG_id_type_tag(struct Main *bmain, short idtype)
2757 {
2758         bmain->id_tag_update[((char *)&idtype)[0]] = 1;
2759 }
2760
2761 int DAG_id_type_tagged(Main *bmain, short idtype)
2762 {
2763         return bmain->id_tag_update[((char *)&idtype)[0]];
2764 }
2765
2766 #if 0 // UNUSED
2767 /* recursively descends tree, each node only checked once */
2768 /* node is checked to be of type object */
2769 static int parent_check_node(DagNode *node, int curtime)
2770 {
2771         DagAdjList *itA;
2772         
2773         node->lasttime = curtime;
2774         
2775         if (node->color == DAG_GRAY)
2776                 return DAG_GRAY;
2777         
2778         for (itA = node->child; itA; itA = itA->next) {
2779                 if (itA->node->type == ID_OB) {
2780                         
2781                         if (itA->node->color == DAG_GRAY)
2782                                 return DAG_GRAY;
2783
2784                         /* descend if not done */
2785                         if (itA->node->lasttime != curtime) {
2786                                 itA->node->color = parent_check_node(itA->node, curtime);
2787                         
2788                                 if (itA->node->color == DAG_GRAY)
2789                                         return DAG_GRAY;
2790                         }
2791                 }
2792         }
2793         
2794         return DAG_WHITE;
2795 }
2796 #endif
2797
2798 /* ******************* DAG FOR ARMATURE POSE ***************** */
2799
2800 /* we assume its an armature with pose */
2801 void DAG_pose_sort(Object *ob)
2802 {
2803         bPose *pose = ob->pose;
2804         bPoseChannel *pchan;
2805         bConstraint *con;
2806         DagNode *node;
2807         DagNode *node2, *node3;
2808         DagNode *rootnode;
2809         DagForest *dag;
2810         DagNodeQueue *nqueue;
2811         DagAdjList *itA;
2812         ListBase tempbase;
2813         int skip = 0;
2814         
2815         dag = dag_init();
2816         ugly_hack_sorry = 0; // no ID structs
2817
2818         rootnode = dag_add_node(dag, NULL); // node->ob becomes NULL
2819         
2820         /* we add the hierarchy and the constraints */
2821         for (pchan = pose->chanbase.first; pchan; pchan = pchan->next) {
2822                 int addtoroot = 1;
2823                 
2824                 node = dag_get_node(dag, pchan);
2825                 
2826                 if (pchan->parent) {
2827                         node2 = dag_get_node(dag, pchan->parent);
2828                         dag_add_relation(dag, node2, node, 0, "Parent Relation");
2829                         addtoroot = 0;
2830                 }
2831                 for (con = pchan->constraints.first; con; con = con->next) {
2832                         bConstraintTypeInfo *cti = constraint_get_typeinfo(con);
2833                         ListBase targets = {NULL, NULL};
2834                         bConstraintTarget *ct;
2835                         
2836                         if (cti && cti->get_constraint_targets) {
2837                                 cti->get_constraint_targets(con, &targets);
2838                                 
2839                                 for (ct = targets.first; ct; ct = ct->next) {
2840                                         if (ct->tar == ob && ct->subtarget[0]) {
2841                                                 bPoseChannel *target = BKE_pose_channel_find_name(ob->pose, ct->subtarget);
2842                                                 if (target) {
2843                                                         node2 = dag_get_node(dag, target);
2844                                                         dag_add_relation(dag, node2, node, 0, "Pose Constraint");
2845                                                         
2846                                                         if (con->type == CONSTRAINT_TYPE_KINEMATIC) {
2847                                                                 bKinematicConstraint *data = (bKinematicConstraint *)con->data;
2848                                                                 bPoseChannel *parchan;
2849                                                                 int segcount = 0;
2850                                                                 
2851                                                                 /* exclude tip from chain? */
2852                                                                 if (!(data->flag & CONSTRAINT_IK_TIP))
2853