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