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