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