Drivers: "Scripted Expression" drivers (i.e. PyDrivers) work again
[blender.git] / source / blender / blenkernel / intern / depsgraph.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2004 Blender Foundation.
21  * All rights reserved.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27  
28 #include <stdio.h>
29 #include <string.h>
30 #include <math.h>
31
32 #ifdef _WIN32
33 #include "BLI_winstuff.h"
34 #endif
35
36 //#include "BMF_Api.h"
37
38 #include "BLI_blenlib.h"
39 #include "BLI_arithb.h"
40
41 #include "DNA_anim_types.h"
42 #include "DNA_action_types.h"
43 #include "DNA_armature_types.h"
44 #include "DNA_curve_types.h"
45 #include "DNA_camera_types.h"
46 #include "DNA_ID.h"
47 #include "DNA_effect_types.h"
48 #include "DNA_group_types.h"
49 #include "DNA_lattice_types.h"
50 #include "DNA_lamp_types.h"
51 #include "DNA_key_types.h"
52 #include "DNA_mesh_types.h"
53 #include "DNA_modifier_types.h"
54 #include "DNA_nla_types.h"
55 #include "DNA_object_types.h"
56 #include "DNA_object_force.h"
57 #include "DNA_object_fluidsim.h"
58 #include "DNA_outliner_types.h"
59 #include "DNA_particle_types.h"
60 #include "DNA_scene_types.h"
61 #include "DNA_screen_types.h"
62 #include "DNA_space_types.h"
63 #include "DNA_view2d_types.h"
64 #include "DNA_view3d_types.h"
65
66 #include "BKE_action.h"
67 #include "BKE_effect.h"
68 #include "BKE_global.h"
69 #include "BKE_group.h"
70 #include "BKE_key.h"
71 #include "BKE_main.h"
72 #include "BKE_mball.h"
73 #include "BKE_modifier.h"
74 #include "BKE_object.h"
75 #include "BKE_particle.h"
76 #include "BKE_pointcache.h"
77 #include "BKE_utildefines.h"
78 #include "BKE_scene.h"
79
80 #include "MEM_guardedalloc.h"
81
82 #ifndef DISABLE_PYTHON
83 #include "BPY_extern.h"
84 #endif
85
86  #include "depsgraph_private.h"
87  
88 /* Queue and stack operations for dag traversal 
89  *
90  * the queue store a list of freenodes to avoid successives alloc/dealloc
91  */
92
93 DagNodeQueue * queue_create (int slots) 
94 {
95         DagNodeQueue * queue;
96         DagNodeQueueElem * elem;
97         int i;
98         
99         queue = MEM_mallocN(sizeof(DagNodeQueue),"DAG queue");
100         queue->freenodes = MEM_mallocN(sizeof(DagNodeQueue),"DAG queue");
101         queue->count = 0;
102         queue->maxlevel = 0;
103         queue->first = queue->last = NULL;
104         elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem3");
105         elem->node = NULL;
106         elem->next = NULL;
107         queue->freenodes->first = queue->freenodes->last = elem;
108         
109         for (i = 1; i <slots;i++) {
110                 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem4");
111                 elem->node = NULL;
112                 elem->next = NULL;
113                 queue->freenodes->last->next = elem;
114                 queue->freenodes->last = elem;
115         }
116         queue->freenodes->count = slots;
117         return queue;
118 }
119
120 void queue_raz(DagNodeQueue *queue)
121 {
122         DagNodeQueueElem * elem;
123         
124         elem = queue->first;
125         if (queue->freenodes->last)
126                 queue->freenodes->last->next = elem;
127         else
128                 queue->freenodes->first = queue->freenodes->last = elem;
129         
130         elem->node = NULL;
131         queue->freenodes->count++;
132         while (elem->next) {
133                 elem = elem->next;
134                 elem->node = NULL;
135                 queue->freenodes->count++;
136         }
137         queue->freenodes->last = elem;
138         queue->count = 0;
139 }
140
141 void queue_delete(DagNodeQueue *queue)
142 {
143         DagNodeQueueElem * elem;
144         DagNodeQueueElem * temp;
145         
146         elem = queue->first;
147         while (elem) {
148                 temp = elem;
149                 elem = elem->next;
150                 MEM_freeN(temp);
151         }
152         
153         elem = queue->freenodes->first;
154         while (elem) {
155                 temp = elem;
156                 elem = elem->next;
157                 MEM_freeN(temp);
158         }
159         
160         MEM_freeN(queue->freenodes);                    
161         MEM_freeN(queue);                       
162 }
163
164 /* insert in queue, remove in front */
165 void push_queue(DagNodeQueue *queue, DagNode *node)
166 {
167         DagNodeQueueElem * elem;
168         int i;
169
170         if (node == NULL) {
171                 fprintf(stderr,"pushing null node \n");
172                 return;
173         }
174         /*fprintf(stderr,"BFS push : %s %d\n",((ID *) node->ob)->name, queue->count);*/
175
176         elem = queue->freenodes->first;
177         if (elem != NULL) {
178                 queue->freenodes->first = elem->next;
179                 if ( queue->freenodes->last == elem) {
180                         queue->freenodes->last = NULL;
181                         queue->freenodes->first = NULL;
182                 }
183                 queue->freenodes->count--;
184         } else { /* alllocating more */         
185                 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem1");
186                 elem->node = NULL;
187                 elem->next = NULL;
188                 queue->freenodes->first = queue->freenodes->last = elem;
189
190                 for (i = 1; i <DAGQUEUEALLOC;i++) {
191                         elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem2");
192                         elem->node = NULL;
193                         elem->next = NULL;
194                         queue->freenodes->last->next = elem;
195                         queue->freenodes->last = elem;
196                 }
197                 queue->freenodes->count = DAGQUEUEALLOC;
198                         
199                 elem = queue->freenodes->first; 
200                 queue->freenodes->first = elem->next;   
201         }
202         elem->next = NULL;
203         elem->node = node;
204         if (queue->last != NULL)
205                 queue->last->next = elem;
206         queue->last = elem;
207         if (queue->first == NULL) {
208                 queue->first = elem;
209         }
210         queue->count++;
211 }
212
213
214 /* insert in front, remove in front */
215 void push_stack(DagNodeQueue *queue, DagNode *node)
216 {
217         DagNodeQueueElem * elem;
218         int i;
219
220         elem = queue->freenodes->first; 
221         if (elem != NULL) {
222                 queue->freenodes->first = elem->next;
223                 if ( queue->freenodes->last == elem) {
224                         queue->freenodes->last = NULL;
225                         queue->freenodes->first = NULL;
226                 }
227                 queue->freenodes->count--;
228         } else { /* alllocating more */
229                 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem1");
230                 elem->node = NULL;
231                 elem->next = NULL;
232                 queue->freenodes->first = queue->freenodes->last = elem;
233
234                 for (i = 1; i <DAGQUEUEALLOC;i++) {
235                         elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem2");
236                         elem->node = NULL;
237                         elem->next = NULL;
238                         queue->freenodes->last->next = elem;
239                         queue->freenodes->last = elem;
240                 }
241                 queue->freenodes->count = DAGQUEUEALLOC;
242                         
243                 elem = queue->freenodes->first; 
244                 queue->freenodes->first = elem->next;   
245         }
246         elem->next = queue->first;
247         elem->node = node;
248         queue->first = elem;
249         if (queue->last == NULL)
250                 queue->last = elem;
251         queue->count++;
252 }
253
254
255 DagNode * pop_queue(DagNodeQueue *queue)
256 {
257         DagNodeQueueElem * elem;
258         DagNode *node;
259
260         elem = queue->first;
261         if (elem) {
262                 queue->first = elem->next;
263                 if (queue->last == elem) {
264                         queue->last=NULL;
265                         queue->first=NULL;
266                 }
267                 queue->count--;
268                 if (queue->freenodes->last)
269                         queue->freenodes->last->next=elem;
270                 queue->freenodes->last=elem;
271                 if (queue->freenodes->first == NULL)
272                         queue->freenodes->first=elem;
273                 node = elem->node;
274                 elem->node = NULL;
275                 elem->next = NULL;
276                 queue->freenodes->count++;
277                 return node;
278         } else {
279                 fprintf(stderr,"return null \n");
280                 return NULL;
281         }
282 }
283
284 void    *pop_ob_queue(struct DagNodeQueue *queue) {
285         return(pop_queue(queue)->ob);
286 }
287
288 DagNode * get_top_node_queue(DagNodeQueue *queue) 
289 {
290         return queue->first->node;
291 }
292
293 int             queue_count(struct DagNodeQueue *queue){
294         return queue->count;
295 }
296
297
298 DagForest * dag_init()
299 {
300         DagForest *forest;
301         /* use callocN to init all zero */
302         forest = MEM_callocN(sizeof(DagForest),"DAG root");
303         return forest;
304 }
305
306 /* isdata = object data... */
307 // XXX this needs to be extended to be more flexible (so that not only objects are evaluated via depsgraph)...
308 static void dag_add_driver_relation(AnimData *adt, DagForest *dag, DagNode *node, int isdata)
309 {
310         FCurve *fcu;
311         DagNode *node1;
312         
313         for (fcu= adt->drivers.first; fcu; fcu= fcu->next) {
314                 ChannelDriver *driver= fcu->driver;
315                 DriverTarget *dtar;
316                 
317                 /* loop over targets, adding relationships as appropriate */
318                 for (dtar= driver->targets.first; dtar; dtar= dtar->next) {
319                         if (dtar->id) {
320                                 if (GS(dtar->id->name)==ID_OB) {
321                                         Object *ob= (Object *)dtar->id;
322                                         
323                                         /* normal channel-drives-channel */
324                                         node1 = dag_get_node(dag, dtar->id);
325                                         
326                                         /* check if bone... */
327                                         if ((ob->type==OB_ARMATURE) && dtar->rna_path && strstr(dtar->rna_path, "pose.pose_channels["))
328                                                 dag_add_relation(dag, node1, node, isdata?DAG_RL_DATA_DATA:DAG_RL_DATA_OB, "Driver");
329                                         /* check if ob data */
330                                         else if (dtar->rna_path && strstr(dtar->rna_path, "data."))
331                                                 dag_add_relation(dag, node1, node, isdata?DAG_RL_DATA_DATA:DAG_RL_DATA_OB, "Driver");
332                                         /* normal */
333                                         else
334                                                 dag_add_relation(dag, node1, node, isdata?DAG_RL_OB_DATA:DAG_RL_OB_OB, "Driver");
335                                 }
336                         }
337                 }
338         }
339 }
340
341 static void dag_add_collision_field_relation(DagForest *dag, Scene *scene, Object *ob, DagNode *node)
342 {
343         Base *base;
344         DagNode *node2;
345
346         // would be nice to have a list of colliders here
347         // so for now walk all objects in scene check 'same layer rule'
348         for(base = scene->base.first; base; base= base->next) {
349                 if((base->lay & ob->lay) && base->object->pd) {
350                         Object *ob1= base->object;
351                         if((ob1->pd->deflect || ob1->pd->forcefield) && (ob1 != ob))  {
352                                 node2 = dag_get_node(dag, ob1);                                 
353                                 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Field Collision");
354                         }
355                 }
356         }
357 }
358
359 static void build_dag_object(DagForest *dag, DagNode *scenenode, Scene *scene, Object *ob, int mask)
360 {
361         bConstraint *con;
362         DagNode * node;
363         DagNode * node2;
364         DagNode * node3;
365         Key *key;
366         ParticleSystem *psys;
367         int addtoroot= 1;
368         
369         node = dag_get_node(dag, ob);
370         
371         if ((ob->data) && (mask&DAG_RL_DATA)) {
372                 node2 = dag_get_node(dag,ob->data);
373                 dag_add_relation(dag,node,node2,DAG_RL_DATA, "Object-Data Relation");
374                 node2->first_ancestor = ob;
375                 node2->ancestor_count += 1;
376         }
377         
378         if (ob->type == OB_ARMATURE) {
379                 if (ob->pose){
380                         bPoseChannel *pchan;
381                         bConstraint *con;
382                         
383                         for (pchan = ob->pose->chanbase.first; pchan; pchan=pchan->next) {
384                                 for (con = pchan->constraints.first; con; con=con->next) {
385                                         bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
386                                         ListBase targets = {NULL, NULL};
387                                         bConstraintTarget *ct;
388                                         
389                                         if (cti && cti->get_constraint_targets) {
390                                                 cti->get_constraint_targets(con, &targets);
391                                                 
392                                                 for (ct= targets.first; ct; ct= ct->next) {
393                                                         if (ct->tar && ct->tar != ob) {
394                                                                 // fprintf(stderr,"armature %s target :%s \n", ob->id.name, target->id.name);
395                                                                 node3 = dag_get_node(dag, ct->tar);
396                                                                 
397                                                                 if (ct->subtarget[0])
398                                                                         dag_add_relation(dag,node3,node, DAG_RL_OB_DATA|DAG_RL_DATA_DATA, cti->name);
399                                                                 else if(ELEM(con->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO))   
400                                                                         dag_add_relation(dag,node3,node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, cti->name);
401                                                                 else
402                                                                         dag_add_relation(dag,node3,node, DAG_RL_OB_DATA, cti->name);
403                                                         }
404                                                 }
405                                                 
406                                                 if (cti->flush_constraint_targets)
407                                                         cti->flush_constraint_targets(con, &targets, 1);
408                                         }
409                                         
410                                 }
411                         }
412                 }
413         }
414         
415         /* driver dependencies, nla modifiers */
416 #if 0 // XXX old animation system
417         if(ob->nlastrips.first) {
418                 bActionStrip *strip;
419                 bActionChannel *chan;
420                 for(strip= ob->nlastrips.first; strip; strip= strip->next) {
421                         if(strip->modifiers.first) {
422                                 bActionModifier *amod;
423                                 for(amod= strip->modifiers.first; amod; amod= amod->next) {
424                                         if(amod->ob) {
425                                                 node2 = dag_get_node(dag, amod->ob);
426                                                 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "NLA Strip Modifier");
427                                         }
428                                 }
429                         }
430                 }
431         }
432 #endif // XXX old animation system
433         if (ob->adt)
434                 dag_add_driver_relation(ob->adt, dag, node, (ob->type == OB_ARMATURE)); // XXX isdata arg here doesn't give an accurate picture of situation
435                 
436         key= ob_get_key(ob);
437         if (key && key->adt)
438                 dag_add_driver_relation(key->adt, dag, node, 1);
439
440         if (ob->modifiers.first) {
441                 ModifierData *md;
442                 
443                 for(md=ob->modifiers.first; md; md=md->next) {
444                         ModifierTypeInfo *mti = modifierType_getInfo(md->type);
445                         
446                         if (mti->updateDepgraph) mti->updateDepgraph(md, dag, scene, ob, node);
447                 }
448         }
449         if (ob->parent) {
450                 node2 = dag_get_node(dag,ob->parent);
451                 
452                 switch(ob->partype) {
453                         case PARSKEL:
454                                 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Parent");
455                                 break;
456                         case PARVERT1: case PARVERT3: case PARBONE:
457                                 dag_add_relation(dag,node2,node,DAG_RL_DATA_OB|DAG_RL_OB_OB, "Vertex Parent");
458                                 break;
459                         default:
460                                 if(ob->parent->type==OB_LATTICE) 
461                                         dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Lattice Parent");
462                                 else if(ob->parent->type==OB_CURVE) {
463                                         Curve *cu= ob->parent->data;
464                                         if(cu->flag & CU_PATH) 
465                                                 dag_add_relation(dag,node2,node,DAG_RL_DATA_OB|DAG_RL_OB_OB, "Curve Parent");
466                                         else
467                                                 dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Curve Parent");
468                                 }
469                                 else
470                                         dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Parent");
471                 }
472                 /* exception case: parent is duplivert */
473                 if(ob->type==OB_MBALL && (ob->parent->transflag & OB_DUPLIVERTS)) {
474                         dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Duplivert");
475                 }
476                 
477                 addtoroot = 0;
478         }
479         if (ob->track) {
480                 node2 = dag_get_node(dag,ob->track);
481                 dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Track To");
482                 addtoroot = 0;
483         }
484         if (ob->proxy) {
485                 node2 = dag_get_node(dag, ob->proxy);
486                 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Proxy");
487                 /* inverted relation, so addtoroot shouldn't be set to zero */
488         }
489         
490
491         if (ob->type==OB_CAMERA) {
492                 Camera *cam = (Camera *)ob->data;
493                 if (cam->adt)
494                         dag_add_driver_relation(cam->adt, dag, node, 1);
495                 if (cam->dof_ob) {
496                         node2 = dag_get_node(dag, cam->dof_ob);
497                         dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Camera DoF");
498                 }
499         }
500         if (ob->type==OB_LAMP) {
501                 Lamp *la = (Lamp *)ob->data;
502                 if (la->adt)
503                         dag_add_driver_relation(la->adt, dag, node, 1);
504         }
505         
506         if (ob->transflag & OB_DUPLI) {
507                 if((ob->transflag & OB_DUPLIGROUP) && ob->dup_group) {
508                         GroupObject *go;
509                         for(go= ob->dup_group->gobject.first; go; go= go->next) {
510                                 if(go->ob) {
511                                         node2 = dag_get_node(dag, go->ob);
512                                         /* node2 changes node1, this keeps animations updated in groups?? not logical? */
513                                         dag_add_relation(dag, node2, node, DAG_RL_OB_OB, "Dupligroup");
514                                 }
515                         }
516                 }
517         }
518     
519         /* softbody collision  */
520         if((ob->type==OB_MESH) || (ob->type==OB_CURVE) || (ob->type==OB_LATTICE))
521                 if(modifiers_isSoftbodyEnabled(ob) || modifiers_isClothEnabled(ob))
522                         dag_add_collision_field_relation(dag, scene, ob, node);
523                 
524         if (ob->type==OB_MBALL) {
525                 Object *mom= find_basis_mball(scene, ob);
526                 if(mom!=ob) {
527                         node2 = dag_get_node(dag, mom);
528                         dag_add_relation(dag,node,node2,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Metaball");  // mom depends on children!
529                 }
530         }
531         else if (ob->type==OB_CURVE) {
532                 Curve *cu= ob->data;
533                 if(cu->bevobj) {
534                         node2 = dag_get_node(dag, cu->bevobj);
535                         dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Curve Bevel");
536                 }
537                 if(cu->taperobj) {
538                         node2 = dag_get_node(dag, cu->taperobj);
539                         dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Curve Taper");
540                 }
541                 if (cu->adt)
542                         dag_add_driver_relation(cu->adt, dag, node, 1);
543         }
544         else if(ob->type==OB_FONT) {
545                 Curve *cu= ob->data;
546                 if(cu->textoncurve) {
547                         node2 = dag_get_node(dag, cu->textoncurve);
548                         dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Texture On Curve");
549                 }
550         }
551         
552         psys= ob->particlesystem.first;
553         if(psys) {
554                 ParticleEffectorCache *nec;
555                 GroupObject *go;
556
557                 for(; psys; psys=psys->next) {
558                         ParticleSettings *part= psys->part;
559
560                         dag_add_relation(dag, node, node, DAG_RL_OB_DATA, "Particle-Object Relation");
561
562                         if(psys->flag & PSYS_DISABLED || psys->flag & PSYS_DELETE)
563                                 continue;
564
565                         if(part->phystype==PART_PHYS_KEYED && psys->keyed_ob &&
566                            BLI_findlink(&psys->keyed_ob->particlesystem,psys->keyed_psys-1)) {
567                                 node2 = dag_get_node(dag, psys->keyed_ob);
568                                 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA, "Particle Keyed Physics");
569                         }
570
571                         if(part->draw_as == PART_DRAW_OB && part->dup_ob) {
572                                 node2 = dag_get_node(dag, part->dup_ob);
573                                 dag_add_relation(dag, node, node2, DAG_RL_OB_OB, "Particle Object Visualisation");
574                                 if(part->dup_ob->type == OB_MBALL)
575                                         dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA, "Particle Object Visualisation");
576                         }
577
578                         if(part->draw_as == PART_DRAW_GR && part->dup_group) {
579                                 for(go=part->dup_group->gobject.first; go; go=go->next) {
580                                         node2 = dag_get_node(dag, go->ob);
581                                         dag_add_relation(dag, node, node2, DAG_RL_OB_OB, "Particle Group Visualisation");
582                                 }
583                         }
584
585                         if(psys->effectors.first)
586                                 psys_end_effectors(psys);
587                         psys_init_effectors(scene, ob, psys->part->eff_group, psys);
588
589                         if(psys->effectors.first) {
590                                 for(nec= psys->effectors.first; nec; nec= nec->next) {
591                                         Object *ob1= nec->ob;
592
593                                         if(nec->type & PSYS_EC_EFFECTOR) {
594                                                 node2 = dag_get_node(dag, ob1);
595                                                 if(ob1->pd->forcefield==PFIELD_GUIDE)
596                                                         dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Particle Field");
597                                                 else
598                                                         dag_add_relation(dag, node2, node, DAG_RL_OB_DATA, "Particle Field");
599                                         }
600                                         else if(nec->type & PSYS_EC_DEFLECT) {
601                                                 node2 = dag_get_node(dag, ob1);
602                                                 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Particle Collision");
603                                         }
604                                         else if(nec->type & PSYS_EC_PARTICLE) {
605                                                 node2 = dag_get_node(dag, ob1);
606                                                 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA, "Particle Field");
607                                         }
608                                         
609                                         if(nec->type & PSYS_EC_REACTOR) {
610                                                 node2 = dag_get_node(dag, ob1);
611                                                 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA, "Particle Reactor");
612                                         }
613                                 }
614                         }
615                 }
616         }
617         
618         for (con = ob->constraints.first; con; con=con->next) {
619                 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
620                 ListBase targets = {NULL, NULL};
621                 bConstraintTarget *ct;
622                 
623                 if (cti && cti->get_constraint_targets) {
624                         cti->get_constraint_targets(con, &targets);
625                         
626                         for (ct= targets.first; ct; ct= ct->next) {
627                                 Object *obt;
628                                 
629                                 if (ct->tar)
630                                         obt= ct->tar;
631                                 else
632                                         continue;
633                                 
634                                 node2 = dag_get_node(dag, obt);
635                                 if (ELEM(con->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO))
636                                         dag_add_relation(dag, node2, node, DAG_RL_DATA_OB|DAG_RL_OB_OB, cti->name);
637                                 else {
638                                         if (ELEM3(obt->type, OB_ARMATURE, OB_MESH, OB_LATTICE) && (ct->subtarget[0]))
639                                                 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB|DAG_RL_OB_OB, cti->name);
640                                         else
641                                                 dag_add_relation(dag, node2, node, DAG_RL_OB_OB, cti->name);
642                                 }
643                                 addtoroot = 0;
644                         }
645                         
646                         if (cti->flush_constraint_targets)
647                                 cti->flush_constraint_targets(con, &targets, 1);
648                 }
649         }
650
651         if (addtoroot == 1 )
652                 dag_add_relation(dag,scenenode,node,DAG_RL_SCENE, "Scene Relation");
653 }
654
655 struct DagForest *build_dag(struct Scene *sce, short mask) 
656 {
657         Base *base;
658         Object *ob;
659         Group *group;
660         GroupObject *go;
661         DagNode *node;
662         DagNode *scenenode;
663         DagForest *dag;
664         DagAdjList *itA;
665
666         dag = sce->theDag;
667         sce->dagisvalid=1;
668         if ( dag)
669                 free_forest( dag ); 
670         else {
671                 dag = dag_init();
672                 sce->theDag = dag;
673         }
674         
675         /* add base node for scene. scene is always the first node in DAG */
676         scenenode = dag_add_node(dag, sce);     
677         
678         /* add current scene objects */
679         for(base = sce->base.first; base; base= base->next) {
680                 ob= base->object;
681                 
682                 build_dag_object(dag, scenenode, sce, ob, mask);
683                 if(ob->proxy)
684                         build_dag_object(dag, scenenode, sce, ob->proxy, mask);
685                 
686                 /* handled in next loop */
687                 if(ob->dup_group) 
688                         ob->dup_group->id.flag |= LIB_DOIT;
689         }
690         
691         /* add groups used in current scene objects */
692         for(group= G.main->group.first; group; group= group->id.next) {
693                 if(group->id.flag & LIB_DOIT) {
694                         for(go= group->gobject.first; go; go= go->next) {
695                                 build_dag_object(dag, scenenode, sce, go->ob, mask);
696                         }
697                         group->id.flag &= ~LIB_DOIT;
698                 }
699         }
700         
701         /* Now all relations were built, but we need to solve 1 exceptional case;
702            When objects have multiple "parents" (for example parent + constraint working on same object)
703            the relation type has to be synced. One of the parents can change, and should give same event to child */
704         
705         /* nodes were callocced, so we can use node->color for temporal storage */
706         for(node = sce->theDag->DagNode.first; node; node= node->next) {
707                 if(node->type==ID_OB) {
708                         for(itA = node->child; itA; itA= itA->next) {
709                                 if(itA->node->type==ID_OB) {
710                                         itA->node->color |= itA->type;
711                                 }
712                         }
713                 }
714         }
715         /* now set relations equal, so that when only one parent changes, the correct recalcs are found */
716         for(node = sce->theDag->DagNode.first; node; node= node->next) {
717                 if(node->type==ID_OB) {
718                         for(itA = node->child; itA; itA= itA->next) {
719                                 if(itA->node->type==ID_OB) {
720                                         itA->type |= itA->node->color;
721                                 }
722                         }
723                 }
724         }
725         
726         // cycle detection and solving
727         // solve_cycles(dag);   
728         
729         return dag;
730 }
731
732
733 void free_forest(DagForest *Dag) 
734 {  /* remove all nodes and deps */
735         DagNode *tempN;
736         DagAdjList *tempA;      
737         DagAdjList *itA;
738         DagNode *itN = Dag->DagNode.first;
739         
740         while (itN) {
741                 itA = itN->child;       
742                 while (itA) {
743                         tempA = itA;
744                         itA = itA->next;
745                         MEM_freeN(tempA);                       
746                 }
747                 
748                 itA = itN->parent;      
749                 while (itA) {
750                         tempA = itA;
751                         itA = itA->next;
752                         MEM_freeN(tempA);                       
753                 }
754                 
755                 tempN = itN;
756                 itN = itN->next;
757                 MEM_freeN(tempN);
758         }
759         Dag->DagNode.first = NULL;
760         Dag->DagNode.last = NULL;
761         Dag->numNodes = 0;
762
763 }
764
765 DagNode * dag_find_node (DagForest *forest,void * fob)
766 {
767         DagNode *node = forest->DagNode.first;
768         
769         while (node) {
770                 if (node->ob == fob)
771                         return node;
772                 node = node->next;
773         }
774         return NULL;
775 }
776
777 static int ugly_hack_sorry= 1;  // prevent type check
778
779 /* no checking of existance, use dag_find_node first or dag_get_node */
780 DagNode * dag_add_node (DagForest *forest, void * fob)
781 {
782         DagNode *node;
783                 
784         node = MEM_callocN(sizeof(DagNode),"DAG node");
785         if (node) {
786                 node->ob = fob;
787                 node->color = DAG_WHITE;
788
789                 if(ugly_hack_sorry) node->type = GS(((ID *) fob)->name);        // sorry, done for pose sorting
790                 if (forest->numNodes) {
791                         ((DagNode *) forest->DagNode.last)->next = node;
792                         forest->DagNode.last = node;
793                         forest->numNodes++;
794                 } else {
795                         forest->DagNode.last = node;
796                         forest->DagNode.first = node;
797                         forest->numNodes = 1;
798                 }
799         }
800         return node;
801 }
802
803 DagNode * dag_get_node (DagForest *forest,void * fob)
804 {
805         DagNode *node;
806         
807         node = dag_find_node (forest, fob);     
808         if (!node) 
809                 node = dag_add_node(forest, fob);
810         return node;
811 }
812
813
814
815 DagNode * dag_get_sub_node (DagForest *forest,void * fob)
816 {
817         DagNode *node;
818         DagAdjList *mainchild, *prev=NULL;
819         
820         mainchild = ((DagNode *) forest->DagNode.first)->child;
821         /* remove from first node (scene) adj list if present */
822         while (mainchild) {
823                 if (mainchild->node == fob) {
824                         if (prev) {
825                                 prev->next = mainchild->next;
826                                 MEM_freeN(mainchild);
827                                 break;
828                         } else {
829                                 ((DagNode *) forest->DagNode.first)->child = mainchild->next;
830                                 MEM_freeN(mainchild);
831                                 break;
832                         }
833                 }
834                 prev = mainchild;
835                 mainchild = mainchild->next;
836         }
837         node = dag_find_node (forest, fob);     
838         if (!node) 
839                 node = dag_add_node(forest, fob);
840         return node;
841 }
842
843 static void dag_add_parent_relation(DagForest *forest, DagNode *fob1, DagNode *fob2, short rel, char *name) 
844 {
845         DagAdjList *itA = fob2->parent;
846         
847         while (itA) { /* search if relation exist already */
848                 if (itA->node == fob1) {
849                         itA->type |= rel;
850                         itA->count += 1;
851                         return;
852                 }
853                 itA = itA->next;
854         }
855         /* create new relation and insert at head. MALLOC alert! */
856         itA = MEM_mallocN(sizeof(DagAdjList),"DAG adj list");
857         itA->node = fob1;
858         itA->type = rel;
859         itA->count = 1;
860         itA->next = fob2->parent;
861         itA->name = name;
862         fob2->parent = itA;
863 }
864
865 void dag_add_relation(DagForest *forest, DagNode *fob1, DagNode *fob2, short rel, char *name) 
866 {
867         DagAdjList *itA = fob1->child;
868         
869         /* parent relation is for cycle checking */
870         dag_add_parent_relation(forest, fob1, fob2, rel, name);
871
872         while (itA) { /* search if relation exist already */
873                 if (itA->node == fob2) {
874                         itA->type |= rel;
875                         itA->count += 1;
876                         return;
877                 }
878                 itA = itA->next;
879         }
880         /* create new relation and insert at head. MALLOC alert! */
881         itA = MEM_mallocN(sizeof(DagAdjList),"DAG adj list");
882         itA->node = fob2;
883         itA->type = rel;
884         itA->count = 1;
885         itA->next = fob1->child;
886         itA->name = name;
887         fob1->child = itA;
888 }
889
890 static char *dag_node_name(DagNode *node)
891 {
892         if(node->ob == NULL)
893                 return "null";
894         else if(ugly_hack_sorry)
895                 return ((ID*)(node->ob))->name+2;
896         else
897                 return ((bPoseChannel*)(node->ob))->name;
898 }
899
900 #if 0
901 static void dag_node_print_dependencies(DagNode *node)
902 {
903         DagAdjList *itA;
904
905         printf("%s depends on:\n", dag_node_name(node));
906
907         for(itA= node->parent; itA; itA= itA->next)
908                 printf("  %s through %s\n", dag_node_name(itA->node), itA->name);
909         printf("\n");
910 }
911 #endif
912
913 static int dag_node_print_dependency_recurs(DagNode *node, DagNode *endnode)
914 {
915         DagAdjList *itA;
916
917         if(node->color == DAG_BLACK)
918                 return 0;
919
920         node->color= DAG_BLACK;
921
922         if(node == endnode)
923                 return 1;
924
925         for(itA= node->parent; itA; itA= itA->next) {
926                 if(dag_node_print_dependency_recurs(itA->node, endnode)) {
927                         printf("  %s depends on %s through %s.\n", dag_node_name(node), dag_node_name(itA->node), itA->name);
928                         return 1;
929                 }
930         }
931
932         return 0;
933 }
934
935 static void dag_node_print_dependency_cycle(DagForest *dag, DagNode *startnode, DagNode *endnode, char *name)
936 {
937         DagNode *node;
938
939     for(node = dag->DagNode.first; node; node= node->next)
940                 node->color= DAG_WHITE;
941
942         printf("  %s depends on %s through %s.\n", dag_node_name(endnode), dag_node_name(startnode), name);
943         dag_node_print_dependency_recurs(startnode, endnode);
944         printf("\n");
945 }
946
947 static int dag_node_recurs_level(DagNode *node, int level)
948 {
949         DagAdjList *itA;
950         int newlevel;
951
952         node->color= DAG_BLACK; /* done */
953         newlevel= ++level;
954         
955         for(itA= node->parent; itA; itA= itA->next) {
956                 if(itA->node->color==DAG_WHITE) {
957                         itA->node->ancestor_count= dag_node_recurs_level(itA->node, level);
958                         newlevel= MAX2(newlevel, level+itA->node->ancestor_count);
959                 }
960                 else
961                         newlevel= MAX2(newlevel, level+itA->node->ancestor_count);
962         }
963         
964         return newlevel;
965 }
966
967 static void dag_check_cycle(DagForest *dag)
968 {
969         DagNode *node;
970         DagAdjList *itA;
971
972         /* tag nodes unchecked */
973         for(node = dag->DagNode.first; node; node= node->next)
974                 node->color= DAG_WHITE;
975         
976         for(node = dag->DagNode.first; node; node= node->next) {
977                 if(node->color==DAG_WHITE) {
978                         node->ancestor_count= dag_node_recurs_level(node, 0);
979                 }
980         }
981         
982         /* check relations, and print errors */
983         for(node = dag->DagNode.first; node; node= node->next) {
984                 for(itA= node->parent; itA; itA= itA->next) {
985                         if(itA->node->ancestor_count > node->ancestor_count) {
986                                 if(node->ob && itA->node->ob) {
987                                         printf("Dependency cycle detected:\n");
988                                         dag_node_print_dependency_cycle(dag, itA->node, node, itA->name);
989                                 }
990                         }
991                 }
992         }
993
994         /* parent relations are only needed for cycle checking, so free now */
995         for(node = dag->DagNode.first; node; node= node->next) {
996                 while (node->parent) {
997                         itA = node->parent->next;
998                         MEM_freeN(node->parent);                        
999                         node->parent = itA;
1000                 }
1001         }
1002 }
1003
1004 /*
1005  * MainDAG is the DAG of all objects in current scene
1006  * used only for drawing there is one also in each scene
1007  */
1008 static DagForest * MainDag = NULL;
1009
1010 DagForest *getMainDag(void)
1011 {
1012         return MainDag;
1013 }
1014
1015
1016 void setMainDag(DagForest *dag)
1017 {
1018         MainDag = dag;
1019 }
1020
1021
1022 /*
1023  * note for BFS/DFS
1024  * in theory we should sweep the whole array
1025  * but in our case the first node is the scene
1026  * and is linked to every other object
1027  *
1028  * for general case we will need to add outer loop
1029  */
1030
1031 /*
1032  * ToDo : change pos kludge
1033  */
1034
1035 /* adjust levels for drawing in oops space */
1036 void graph_bfs(void)
1037 {
1038         DagNode *node;
1039         DagNodeQueue *nqueue;
1040         int pos[50];
1041         int i;
1042         DagAdjList *itA;
1043         int minheight;
1044         
1045         /* fprintf(stderr,"starting BFS \n ------------\n"); */ 
1046         nqueue = queue_create(DAGQUEUEALLOC);
1047         for ( i=0; i<50; i++)
1048                 pos[i] = 0;
1049         
1050         /* Init
1051          * dagnode.first is alway the root (scene) 
1052          */
1053         node = MainDag->DagNode.first;
1054         while(node) {
1055                 node->color = DAG_WHITE;
1056                 node->BFS_dist = 9999;
1057                 node->k = 0;
1058                 node = node->next;
1059         }
1060         
1061         node = MainDag->DagNode.first;
1062         if (node->color == DAG_WHITE) {
1063                 node->color = DAG_GRAY;
1064                 node->BFS_dist = 1;
1065                 push_queue(nqueue,node);  
1066                 while(nqueue->count) {
1067                         node = pop_queue(nqueue);
1068                         
1069                         minheight = pos[node->BFS_dist];
1070                         itA = node->child;
1071                         while(itA != NULL) {
1072                                 if((itA->node->color == DAG_WHITE) ) {
1073                                         itA->node->color = DAG_GRAY;
1074                                         itA->node->BFS_dist = node->BFS_dist + 1;
1075                                         itA->node->k = (float) minheight;
1076                                         push_queue(nqueue,itA->node);
1077                                 }
1078                                 
1079                                  else {
1080                                         fprintf(stderr,"bfs not dag tree edge color :%i \n",itA->node->color);
1081                                 }
1082                                  
1083                                 
1084                                 itA = itA->next;
1085                         }
1086                         if (pos[node->BFS_dist] > node->k ) {
1087                                 pos[node->BFS_dist] += 1;                               
1088                                 node->k = (float) pos[node->BFS_dist];
1089                         } else {
1090                                 pos[node->BFS_dist] = (int) node->k +1;
1091                         }
1092                         set_node_xy(node, node->BFS_dist*DEPSX*2, pos[node->BFS_dist]*DEPSY*2);
1093                         node->color = DAG_BLACK;
1094                         /*
1095                          fprintf(stderr,"BFS node : %20s %i %5.0f %5.0f\n",((ID *) node->ob)->name,node->BFS_dist, node->x, node->y);   
1096                          */
1097                 }
1098         }
1099         queue_delete(nqueue);
1100 }
1101
1102 int pre_and_post_BFS(DagForest *dag, short mask, graph_action_func pre_func, graph_action_func post_func, void **data) {
1103         DagNode *node;
1104         
1105         node = dag->DagNode.first;
1106         return pre_and_post_source_BFS(dag, mask,  node,  pre_func,  post_func, data);
1107 }
1108
1109
1110 int pre_and_post_source_BFS(DagForest *dag, short mask, DagNode *source, graph_action_func pre_func, graph_action_func post_func, void **data)
1111 {
1112         DagNode *node;
1113         DagNodeQueue *nqueue;
1114         DagAdjList *itA;
1115         int     retval = 0;
1116         /* fprintf(stderr,"starting BFS \n ------------\n"); */ 
1117         
1118         /* Init
1119                 * dagnode.first is alway the root (scene) 
1120                 */
1121         node = dag->DagNode.first;
1122         nqueue = queue_create(DAGQUEUEALLOC);
1123         while(node) {
1124                 node->color = DAG_WHITE;
1125                 node->BFS_dist = 9999;
1126                 node = node->next;
1127         }
1128         
1129         node = source;
1130         if (node->color == DAG_WHITE) {
1131                 node->color = DAG_GRAY;
1132                 node->BFS_dist = 1;
1133                 pre_func(node->ob,data);
1134                 
1135                 while(nqueue->count) {
1136                         node = pop_queue(nqueue);
1137                         
1138                         itA = node->child;
1139                         while(itA != NULL) {
1140                                 if((itA->node->color == DAG_WHITE) && (itA->type & mask)) {
1141                                         itA->node->color = DAG_GRAY;
1142                                         itA->node->BFS_dist = node->BFS_dist + 1;
1143                                         push_queue(nqueue,itA->node);
1144                                         pre_func(node->ob,data);
1145                                 }
1146                                 
1147                                 else { // back or cross edge
1148                                         retval = 1;
1149                                 }
1150                                 itA = itA->next;
1151                         }
1152                         post_func(node->ob,data);
1153                         node->color = DAG_BLACK;
1154                         /*
1155                          fprintf(stderr,"BFS node : %20s %i %5.0f %5.0f\n",((ID *) node->ob)->name,node->BFS_dist, node->x, node->y);   
1156                          */
1157                 }
1158         }
1159         queue_delete(nqueue);
1160         return retval;
1161 }
1162
1163 /* non recursive version of DFS, return queue -- outer loop present to catch odd cases (first level cycles)*/
1164 DagNodeQueue * graph_dfs(void)
1165 {
1166         DagNode *node;
1167         DagNodeQueue *nqueue;
1168         DagNodeQueue *retqueue;
1169         int pos[50];
1170         int i;
1171         DagAdjList *itA;
1172         int time;
1173         int skip = 0;
1174         int minheight;
1175         int maxpos=0;
1176         int     is_cycle = 0;
1177         /*
1178          *fprintf(stderr,"starting DFS \n ------------\n");
1179          */     
1180         nqueue = queue_create(DAGQUEUEALLOC);
1181         retqueue = queue_create(MainDag->numNodes);
1182         for ( i=0; i<50; i++)
1183                 pos[i] = 0;
1184         
1185         /* Init
1186          * dagnode.first is alway the root (scene) 
1187          */
1188         node = MainDag->DagNode.first;
1189         while(node) {
1190                 node->color = DAG_WHITE;
1191                 node->DFS_dist = 9999;
1192                 node->DFS_dvtm = node->DFS_fntm = 9999;
1193                 node->k = 0;
1194                 node =  node->next;
1195         }
1196         
1197         time = 1;
1198         
1199         node = MainDag->DagNode.first;
1200
1201         do {
1202         if (node->color == DAG_WHITE) {
1203                 node->color = DAG_GRAY;
1204                 node->DFS_dist = 1;
1205                 node->DFS_dvtm = time;
1206                 time++;
1207                 push_stack(nqueue,node);  
1208                         
1209                 while(nqueue->count) {
1210                         //graph_print_queue(nqueue);
1211
1212                          skip = 0;
1213                         node = get_top_node_queue(nqueue);
1214                         
1215                         minheight = pos[node->DFS_dist];
1216
1217                         itA = node->child;
1218                         while(itA != NULL) {
1219                                 if((itA->node->color == DAG_WHITE) ) {
1220                                         itA->node->DFS_dvtm = time;
1221                                         itA->node->color = DAG_GRAY;
1222
1223                                         time++;
1224                                         itA->node->DFS_dist = node->DFS_dist + 1;
1225                                         itA->node->k = (float) minheight;
1226                                         push_stack(nqueue,itA->node);
1227                                         skip = 1;
1228                                         break;
1229                                 } else { 
1230                                         if (itA->node->color == DAG_GRAY) { // back edge
1231                                                 fprintf(stderr,"dfs back edge :%15s %15s \n",((ID *) node->ob)->name, ((ID *) itA->node->ob)->name);
1232                                                 is_cycle = 1;
1233                                         } else if (itA->node->color == DAG_BLACK) {
1234                                                 ;
1235                                                 /* already processed node but we may want later to change distance either to shorter to longer.
1236                                                  * DFS_dist is the first encounter  
1237                                                 */
1238                                                 /*if (node->DFS_dist >= itA->node->DFS_dist)
1239                                                         itA->node->DFS_dist = node->DFS_dist + 1;
1240                                                         
1241                                                         fprintf(stderr,"dfs forward or cross edge :%15s %i-%i %15s %i-%i \n",
1242                                                                 ((ID *) node->ob)->name,
1243                                                                 node->DFS_dvtm, 
1244                                                                 node->DFS_fntm, 
1245                                                                 ((ID *) itA->node->ob)->name, 
1246                                                                 itA->node->DFS_dvtm,
1247                                                                 itA->node->DFS_fntm);
1248                                         */
1249                                         } else 
1250                                                 fprintf(stderr,"dfs unknown edge \n");
1251                                 }
1252                                 itA = itA->next;
1253                         }                       
1254
1255                         if (!skip) {
1256                                 node = pop_queue(nqueue);
1257                                 node->color = DAG_BLACK;
1258
1259                                 node->DFS_fntm = time;
1260                                 time++;
1261                                 if (node->DFS_dist > maxpos)
1262                                         maxpos = node->DFS_dist;
1263                                 if (pos[node->DFS_dist] > node->k ) {
1264                                         pos[node->DFS_dist] += 1;                               
1265                                         node->k = (float) pos[node->DFS_dist];
1266                                 } else {
1267                                         pos[node->DFS_dist] = (int) node->k +1;
1268                                 }
1269                                 set_node_xy(node, node->DFS_dist*DEPSX*2, pos[node->DFS_dist]*DEPSY*2);
1270                                 
1271                                 /*
1272                                  fprintf(stderr,"DFS node : %20s %i %i %i %i\n",((ID *) node->ob)->name,node->BFS_dist, node->DFS_dist, node->DFS_dvtm, node->DFS_fntm );       
1273                                 */
1274                                  push_stack(retqueue,node);
1275                                 
1276                         }
1277                 }
1278         }
1279                 node = node->next;
1280         } while (node);
1281 //        fprintf(stderr,"i size : %i \n", maxpos);
1282           
1283         queue_delete(nqueue);
1284           return(retqueue);
1285 }
1286
1287 /* unused */
1288 int pre_and_post_DFS(DagForest *dag, short mask, graph_action_func pre_func, graph_action_func post_func, void **data) {
1289         DagNode *node;
1290
1291         node = dag->DagNode.first;
1292         return pre_and_post_source_DFS(dag, mask,  node,  pre_func,  post_func, data);
1293 }
1294
1295 int pre_and_post_source_DFS(DagForest *dag, short mask, DagNode *source, graph_action_func pre_func, graph_action_func post_func, void **data)
1296 {
1297         DagNode *node;
1298         DagNodeQueue *nqueue;
1299         DagAdjList *itA;
1300         int time;
1301         int skip = 0;
1302         int retval = 0;
1303         /*
1304          *fprintf(stderr,"starting DFS \n ------------\n");
1305          */     
1306         nqueue = queue_create(DAGQUEUEALLOC);
1307         
1308         /* Init
1309                 * dagnode.first is alway the root (scene) 
1310                 */
1311         node = dag->DagNode.first;
1312         while(node) {
1313                 node->color = DAG_WHITE;
1314                 node->DFS_dist = 9999;
1315                 node->DFS_dvtm = node->DFS_fntm = 9999;
1316                 node->k = 0;
1317                 node =  node->next;
1318         }
1319         
1320         time = 1;
1321         
1322         node = source;
1323         do {
1324                 if (node->color == DAG_WHITE) {
1325                         node->color = DAG_GRAY;
1326                         node->DFS_dist = 1;
1327                         node->DFS_dvtm = time;
1328                         time++;
1329                         push_stack(nqueue,node);  
1330                         pre_func(node->ob,data);
1331
1332                         while(nqueue->count) {
1333                                 skip = 0;
1334                                 node = get_top_node_queue(nqueue);
1335                                                                 
1336                                 itA = node->child;
1337                                 while(itA != NULL) {
1338                                         if((itA->node->color == DAG_WHITE) && (itA->type & mask) ) {
1339                                                 itA->node->DFS_dvtm = time;
1340                                                 itA->node->color = DAG_GRAY;
1341                                                 
1342                                                 time++;
1343                                                 itA->node->DFS_dist = node->DFS_dist + 1;
1344                                                 push_stack(nqueue,itA->node);
1345                                                 pre_func(node->ob,data);
1346
1347                                                 skip = 1;
1348                                                 break;
1349                                         } else {
1350                                                 if (itA->node->color == DAG_GRAY) {// back edge
1351                                                         retval = 1;
1352                                                 }
1353 //                                              else if (itA->node->color == DAG_BLACK) { // cross or forward
1354 //                                                      ;
1355                                         }
1356                                         itA = itA->next;
1357                                 }                       
1358                                 
1359                                 if (!skip) {
1360                                         node = pop_queue(nqueue);
1361                                         node->color = DAG_BLACK;
1362                                         
1363                                         node->DFS_fntm = time;
1364                                         time++;
1365                                         post_func(node->ob,data);
1366                                 }
1367                         }
1368                 }
1369                 node = node->next;
1370         } while (node);
1371         queue_delete(nqueue);
1372         return(retval);
1373 }
1374
1375
1376 // used to get the obs owning a datablock
1377 struct DagNodeQueue *get_obparents(struct DagForest     *dag, void *ob) 
1378 {
1379         DagNode * node, *node1;
1380         DagNodeQueue *nqueue;
1381         DagAdjList *itA;
1382
1383         node = dag_find_node(dag,ob);
1384         if(node==NULL) {
1385                 return NULL;
1386         }
1387         else if (node->ancestor_count == 1) { // simple case
1388                 nqueue = queue_create(1);
1389                 push_queue(nqueue,node);
1390         } else {        // need to go over the whole dag for adj list
1391                 nqueue = queue_create(node->ancestor_count);
1392                 
1393                 node1 = dag->DagNode.first;
1394                 do {
1395                         if (node1->DFS_fntm > node->DFS_fntm) { // a parent is finished after child. must check adj list
1396                                 itA = node->child;
1397                                 while(itA != NULL) {
1398                                         if ((itA->node == node) && (itA->type == DAG_RL_DATA)) {
1399                                                 push_queue(nqueue,node);
1400                                         }
1401                                         itA = itA->next;
1402                                 }
1403                         }
1404                         node1 = node1->next;
1405                 } while (node1);
1406         }
1407         return nqueue;
1408 }
1409
1410 struct DagNodeQueue *get_first_ancestors(struct DagForest       *dag, void *ob)
1411 {
1412         DagNode * node, *node1;
1413         DagNodeQueue *nqueue;
1414         DagAdjList *itA;
1415         
1416         node = dag_find_node(dag,ob);
1417         
1418         // need to go over the whole dag for adj list
1419         nqueue = queue_create(node->ancestor_count);
1420         
1421         node1 = dag->DagNode.first;
1422         do {
1423                 if (node1->DFS_fntm > node->DFS_fntm) { 
1424                         itA = node->child;
1425                         while(itA != NULL) {
1426                                 if (itA->node == node) {
1427                                         push_queue(nqueue,node);
1428                                 }
1429                                 itA = itA->next;
1430                         }
1431                 }
1432                 node1 = node1->next;
1433         } while (node1);
1434         
1435         return nqueue;  
1436 }
1437
1438 // standard DFS list
1439 struct DagNodeQueue *get_all_childs(struct DagForest    *dag, void *ob)
1440 {
1441         DagNode *node;
1442         DagNodeQueue *nqueue;
1443         DagNodeQueue *retqueue;
1444         DagAdjList *itA;
1445         int time;
1446         int skip = 0;
1447
1448         nqueue = queue_create(DAGQUEUEALLOC);
1449         retqueue = queue_create(dag->numNodes); // was MainDag... why? (ton)
1450         
1451         node = dag->DagNode.first;
1452         while(node) {
1453                 node->color = DAG_WHITE;
1454                 node =  node->next;
1455         }
1456         
1457         time = 1;
1458         
1459         node = dag_find_node(dag, ob);   // could be done in loop above (ton)
1460         if(node) { // can be null for newly added objects
1461                 
1462                 node->color = DAG_GRAY;
1463                 time++;
1464                 push_stack(nqueue,node);  
1465                 
1466                 while(nqueue->count) {
1467                         
1468                         skip = 0;
1469                         node = get_top_node_queue(nqueue);
1470                                         
1471                         itA = node->child;
1472                         while(itA != NULL) {
1473                                 if((itA->node->color == DAG_WHITE) ) {
1474                                         itA->node->DFS_dvtm = time;
1475                                         itA->node->color = DAG_GRAY;
1476                                         
1477                                         time++;
1478                                         push_stack(nqueue,itA->node);
1479                                         skip = 1;
1480                                         break;
1481                                 } 
1482                                 itA = itA->next;
1483                         }                       
1484                         
1485                         if (!skip) {
1486                                 node = pop_queue(nqueue);
1487                                 node->color = DAG_BLACK;
1488                                 
1489                                 time++;
1490                                 push_stack(retqueue,node);                      
1491                         }
1492                 }
1493         }
1494         queue_delete(nqueue);
1495         return(retqueue);
1496 }
1497
1498 /* unused */
1499 short   are_obs_related(struct DagForest        *dag, void *ob1, void *ob2) {
1500         DagNode * node;
1501         DagAdjList *itA;
1502         
1503         node = dag_find_node(dag, ob1);
1504         
1505         itA = node->child;
1506         while(itA != NULL) {
1507                 if((itA->node->ob == ob2) ) {
1508                         return itA->node->type;
1509                 } 
1510                 itA = itA->next;
1511         }
1512         return DAG_NO_RELATION;
1513 }
1514
1515 int     is_acyclic( DagForest   *dag) {
1516         return dag->is_acyclic;
1517 }
1518
1519 void set_node_xy(DagNode *node, float x, float y)
1520 {
1521         node->x = x;
1522         node->y = y;
1523 }
1524
1525
1526 /* debug test functions */
1527
1528 void graph_print_queue(DagNodeQueue *nqueue)
1529 {       
1530         DagNodeQueueElem *queueElem;
1531         
1532         queueElem = nqueue->first;
1533         while(queueElem) {
1534                 fprintf(stderr,"** %s %i %i-%i ",((ID *) queueElem->node->ob)->name,queueElem->node->color,queueElem->node->DFS_dvtm,queueElem->node->DFS_fntm);
1535                 queueElem = queueElem->next;            
1536         }
1537         fprintf(stderr,"\n");
1538 }
1539
1540 void graph_print_queue_dist(DagNodeQueue *nqueue)
1541 {       
1542         DagNodeQueueElem *queueElem;
1543         int max, count;
1544         
1545         queueElem = nqueue->first;
1546         max = queueElem->node->DFS_fntm;
1547         count = 0;
1548         while(queueElem) {
1549                 fprintf(stderr,"** %25s %2.2i-%2.2i ",((ID *) queueElem->node->ob)->name,queueElem->node->DFS_dvtm,queueElem->node->DFS_fntm);
1550                 while (count < queueElem->node->DFS_dvtm-1) { fputc(' ',stderr); count++;}
1551                 fputc('|',stderr); 
1552                 while (count < queueElem->node->DFS_fntm-2) { fputc('-',stderr); count++;}
1553                 fputc('|',stderr);
1554                 fputc('\n',stderr);
1555                 count = 0;
1556                 queueElem = queueElem->next;            
1557         }
1558         fprintf(stderr,"\n");
1559 }
1560
1561 void graph_print_adj_list(void)
1562 {
1563         DagNode *node;
1564         DagAdjList *itA;
1565         
1566         node = (getMainDag())->DagNode.first;
1567         while(node) {
1568                 fprintf(stderr,"node : %s col: %i",((ID *) node->ob)->name, node->color);               
1569                 itA = node->child;
1570                 while (itA) {
1571                         fprintf(stderr,"-- %s ",((ID *) itA->node->ob)->name);
1572                         
1573                         itA = itA->next;
1574                 }
1575                 fprintf(stderr,"\n");
1576                 node = node->next;
1577         }
1578 }
1579
1580 /* ************************ API *********************** */
1581
1582 /* groups with objects in this scene need to be put in the right order as well */
1583 static void scene_sort_groups(Scene *sce)
1584 {
1585         Base *base;
1586         Group *group;
1587         GroupObject *go;
1588         Object *ob;
1589         
1590         /* test; are group objects all in this scene? */
1591         for(ob= G.main->object.first; ob; ob= ob->id.next) {
1592                 ob->id.flag &= ~LIB_DOIT;
1593                 ob->id.newid= NULL;     /* newid abuse for GroupObject */
1594         }
1595         for(base = sce->base.first; base; base= base->next)
1596                 base->object->id.flag |= LIB_DOIT;
1597         
1598         for(group= G.main->group.first; group; group= group->id.next) {
1599                 for(go= group->gobject.first; go; go= go->next) {
1600                         if((go->ob->id.flag & LIB_DOIT)==0)
1601                                 break;
1602                 }
1603                 /* this group is entirely in this scene */
1604                 if(go==NULL) {
1605                         ListBase listb= {NULL, NULL};
1606                         
1607                         for(go= group->gobject.first; go; go= go->next)
1608                                 go->ob->id.newid= (ID *)go;
1609                         
1610                         /* in order of sorted bases we reinsert group objects */
1611                         for(base = sce->base.first; base; base= base->next) {
1612                                 
1613                                 if(base->object->id.newid) {
1614                                         go= (GroupObject *)base->object->id.newid;
1615                                         base->object->id.newid= NULL;
1616                                         BLI_remlink( &group->gobject, go);
1617                                         BLI_addtail( &listb, go);
1618                                 }
1619                         }
1620                         /* copy the newly sorted listbase */
1621                         group->gobject= listb;
1622                 }
1623         }
1624 }
1625
1626 /* sort the base list on dependency order */
1627 void DAG_scene_sort(struct Scene *sce)
1628 {
1629         DagNode *node;
1630         DagNodeQueue *nqueue;
1631         DagAdjList *itA;
1632         int time;
1633         int skip = 0;
1634         ListBase tempbase;
1635         Base *base;
1636         
1637         tempbase.first= tempbase.last= NULL;
1638         
1639         build_dag(sce, DAG_RL_ALL_BUT_DATA);
1640         
1641         dag_check_cycle(sce->theDag);
1642
1643         nqueue = queue_create(DAGQUEUEALLOC);
1644         
1645         for(node = sce->theDag->DagNode.first; node; node= node->next) {
1646                 node->color = DAG_WHITE;
1647         }
1648         
1649         time = 1;
1650         
1651         node = sce->theDag->DagNode.first;
1652         
1653         node->color = DAG_GRAY;
1654         time++;
1655         push_stack(nqueue,node);  
1656         
1657         while(nqueue->count) {
1658                 
1659                 skip = 0;
1660                 node = get_top_node_queue(nqueue);
1661                 
1662                 itA = node->child;
1663                 while(itA != NULL) {
1664                         if((itA->node->color == DAG_WHITE) ) {
1665                                 itA->node->DFS_dvtm = time;
1666                                 itA->node->color = DAG_GRAY;
1667                                 
1668                                 time++;
1669                                 push_stack(nqueue,itA->node);
1670                                 skip = 1;
1671                                 break;
1672                         } 
1673                         itA = itA->next;
1674                 }                       
1675                 
1676                 if (!skip) {
1677                         if (node) {
1678                                 node = pop_queue(nqueue);
1679                                 if (node->ob == sce)    // we are done
1680                                         break ;
1681                                 node->color = DAG_BLACK;
1682                                 
1683                                 time++;
1684                                 base = sce->base.first;
1685                                 while (base && base->object != node->ob)
1686                                         base = base->next;
1687                                 if(base) {
1688                                         BLI_remlink(&sce->base,base);
1689                                         BLI_addhead(&tempbase,base);
1690                                 }
1691                         }       
1692                 }
1693         }
1694         
1695         // temporal correction for circular dependancies
1696         base = sce->base.first;
1697         while (base) {
1698                 BLI_remlink(&sce->base,base);
1699                 BLI_addhead(&tempbase,base);
1700                 //if(G.f & G_DEBUG) 
1701                         printf("cyclic %s\n", base->object->id.name);
1702                 base = sce->base.first;
1703         }
1704         
1705         sce->base = tempbase;
1706         queue_delete(nqueue);
1707         
1708         /* all groups with objects in this scene gets resorted too */
1709         scene_sort_groups(sce);
1710         
1711         if(G.f & G_DEBUG) {
1712                 printf("\nordered\n");
1713                 for(base = sce->base.first; base; base= base->next) {
1714                         printf(" %s\n", base->object->id.name);
1715                 }
1716         }
1717         /* temporal...? */
1718         sce->recalc |= SCE_PRV_CHANGED; /* test for 3d preview */
1719 }
1720
1721 /* node was checked to have lasttime != curtime and is if type ID_OB */
1722 static void flush_update_node(DagNode *node, unsigned int layer, int curtime)
1723 {
1724         DagAdjList *itA;
1725         Object *ob, *obc;
1726         int oldflag, changed=0;
1727         unsigned int all_layer;
1728         
1729         node->lasttime= curtime;
1730         
1731         ob= node->ob;
1732         if(ob && (ob->recalc & OB_RECALC)) {
1733                 all_layer= ob->lay;
1734
1735                 /* got an object node that changes, now check relations */
1736                 for(itA = node->child; itA; itA= itA->next) {
1737                         all_layer |= itA->lay;
1738                         /* the relationship is visible */
1739                         if((itA->lay & layer)) { // XXX || (itA->node->ob == obedit)
1740                                 if(itA->node->type==ID_OB) {
1741                                         obc= itA->node->ob;
1742                                         oldflag= obc->recalc;
1743                                         
1744                                         /* got a ob->obc relation, now check if flag needs flush */
1745                                         if(ob->recalc & OB_RECALC_OB) {
1746                                                 if(itA->type & DAG_RL_OB_OB) {
1747                                                         //printf("ob %s changes ob %s\n", ob->id.name, obc->id.name);
1748                                                         obc->recalc |= OB_RECALC_OB;
1749                                                 }
1750                                                 if(itA->type & DAG_RL_OB_DATA) {
1751                                                         //printf("ob %s changes obdata %s\n", ob->id.name, obc->id.name);
1752                                                         obc->recalc |= OB_RECALC_DATA;
1753                                                 }
1754                                         }
1755                                         if(ob->recalc & OB_RECALC_DATA) {
1756                                                 if(itA->type & DAG_RL_DATA_OB) {
1757                                                         //printf("obdata %s changes ob %s\n", ob->id.name, obc->id.name);
1758                                                         obc->recalc |= OB_RECALC_OB;
1759                                                 }
1760                                                 if(itA->type & DAG_RL_DATA_DATA) {
1761                                                         //printf("obdata %s changes obdata %s\n", ob->id.name, obc->id.name);
1762                                                         obc->recalc |= OB_RECALC_DATA;
1763                                                 }
1764                                         }
1765                                         if(oldflag!=obc->recalc) changed= 1;
1766                                 }
1767                         }
1768                 }
1769                 /* even nicer, we can clear recalc flags...  */
1770                 if((all_layer & layer)==0) { // XXX && (ob != obedit)) {
1771                         /* but existing displaylists or derivedmesh should be freed */
1772                         if(ob->recalc & OB_RECALC_DATA)
1773                                 object_free_display(ob);
1774                         
1775                         ob->recalc &= ~OB_RECALC;
1776                 }
1777         }
1778         
1779         /* check case where child changes and parent forcing obdata to change */
1780         /* should be done regardless if this ob has recalc set */
1781         /* could merge this in with loop above...? (ton) */
1782         for(itA = node->child; itA; itA= itA->next) {
1783                 /* the relationship is visible */
1784                 if((itA->lay & layer)) {                // XXX  || (itA->node->ob == obedit)
1785                         if(itA->node->type==ID_OB) {
1786                                 obc= itA->node->ob;
1787                                 /* child moves */
1788                                 if((obc->recalc & OB_RECALC)==OB_RECALC_OB) {
1789                                         /* parent has deforming info */
1790                                         if(itA->type & (DAG_RL_OB_DATA|DAG_RL_DATA_DATA)) {
1791                                                 // printf("parent %s changes ob %s\n", ob->id.name, obc->id.name);
1792                                                 obc->recalc |= OB_RECALC_DATA;
1793                                         }
1794                                 }
1795                         }
1796                 }
1797         }
1798         
1799         /* we only go deeper if node not checked or something changed  */
1800         for(itA = node->child; itA; itA= itA->next) {
1801                 if(changed || itA->node->lasttime!=curtime) 
1802                         flush_update_node(itA->node, layer, curtime);
1803         }
1804         
1805 }
1806
1807 /* node was checked to have lasttime != curtime , and is of type ID_OB */
1808 static unsigned int flush_layer_node(Scene *sce, DagNode *node, int curtime)
1809 {
1810         Base *base;
1811         DagAdjList *itA;
1812         
1813         node->lasttime= curtime;
1814         node->lay= 0;
1815         for(base= sce->base.first; base; base= base->next) {
1816                 if(node->ob == base->object) {
1817                         node->lay= ((Object *)node->ob)->lay;
1818                         break;
1819                 }
1820         }
1821         
1822         for(itA = node->child; itA; itA= itA->next) {
1823                 if(itA->node->type==ID_OB) {
1824                         if(itA->node->lasttime!=curtime) {
1825                                 itA->lay= flush_layer_node(sce, itA->node, curtime);  // lay is only set once for each relation
1826                         }
1827                         else itA->lay= itA->node->lay;
1828                         
1829                         node->lay |= itA->lay;
1830                 }
1831         }
1832
1833         return node->lay;
1834 }
1835
1836 /* node was checked to have lasttime != curtime , and is of type ID_OB */
1837 static void flush_pointcache_reset(DagNode *node, int curtime, int reset)
1838 {
1839         DagAdjList *itA;
1840         Object *ob;
1841         
1842         node->lasttime= curtime;
1843         
1844         for(itA = node->child; itA; itA= itA->next) {
1845                 if(itA->node->type==ID_OB) {
1846                         if(itA->node->lasttime!=curtime) {
1847                                 ob= (Object*)(node->ob);
1848
1849                                 if(reset || (ob->recalc & OB_RECALC)) {
1850                                         if(BKE_ptcache_object_reset(ob, PTCACHE_RESET_DEPSGRAPH))
1851                                                 ob->recalc |= OB_RECALC_DATA;
1852
1853                                         flush_pointcache_reset(itA->node, curtime, 1);
1854                                 }
1855                                 else
1856                                         flush_pointcache_reset(itA->node, curtime, 0);
1857                         }
1858                 }
1859         }
1860 }
1861
1862 /* flushes all recalc flags in objects down the dependency tree */
1863 void DAG_scene_flush_update(Scene *sce, unsigned int lay, int time)
1864 {
1865         DagNode *firstnode;
1866         DagAdjList *itA;
1867         Object *ob;
1868         int lasttime;
1869         
1870         if(sce->theDag==NULL) {
1871                 printf("DAG zero... not allowed to happen!\n");
1872                 DAG_scene_sort(sce);
1873         }
1874         
1875         firstnode= sce->theDag->DagNode.first;  // always scene node
1876
1877         for(itA = firstnode->child; itA; itA= itA->next)
1878                 itA->lay= 0;
1879         
1880         /* first we flush the layer flags */
1881         sce->theDag->time++;    // so we know which nodes were accessed
1882         lasttime= sce->theDag->time;
1883
1884         for(itA = firstnode->child; itA; itA= itA->next)
1885                 if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB) 
1886                         flush_layer_node(sce, itA->node, lasttime);
1887         
1888         /* then we use the relationships + layer info to flush update events */
1889         sce->theDag->time++;    // so we know which nodes were accessed
1890         lasttime= sce->theDag->time;
1891         for(itA = firstnode->child; itA; itA= itA->next)
1892                 if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB)
1893                         flush_update_node(itA->node, lay, lasttime);
1894
1895         /* if update is not due to time change, do pointcache clears */
1896         if(!time) {
1897                 sce->theDag->time++;    // so we know which nodes were accessed
1898                 lasttime= sce->theDag->time;
1899                 for(itA = firstnode->child; itA; itA= itA->next) {
1900                         if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB)  {
1901                                 ob= (Object*)(itA->node->ob);
1902
1903                                 if(ob->recalc & OB_RECALC) {
1904                                         if(BKE_ptcache_object_reset(ob, PTCACHE_RESET_DEPSGRAPH))
1905                                                 ob->recalc |= OB_RECALC_DATA;
1906
1907                                         flush_pointcache_reset(itA->node, lasttime, 1);
1908                                 }
1909                                 else
1910                                         flush_pointcache_reset(itA->node, lasttime, 0);
1911                         }
1912                 }
1913         }
1914 }
1915
1916 static int object_modifiers_use_time(Object *ob)
1917 {
1918         ModifierData *md;
1919
1920         for (md=ob->modifiers.first; md; md=md->next)
1921                 if (modifier_dependsOnTime(md))
1922                         return 1;
1923
1924         return 0;
1925 }
1926
1927 static short animdata_use_time(AnimData *adt)
1928 {
1929         NlaTrack *nlt;
1930         
1931         if(adt==NULL) return 0;
1932         
1933         /* check action - only if assigned, and it has anim curves */
1934         if (adt->action && adt->action->curves.first)
1935                 return 1;
1936         
1937         /* check NLA tracks + strips */
1938         for (nlt= adt->nla_tracks.first; nlt; nlt= nlt->next) {
1939                 if (nlt->strips.first)
1940                         return 1;
1941         }
1942         
1943         return 0;
1944 }
1945
1946 static void dag_object_time_update_flags(Object *ob)
1947 {
1948         if(ob->constraints.first) {
1949                 bConstraint *con;
1950                 for (con = ob->constraints.first; con; con=con->next) {
1951                         bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
1952                         ListBase targets = {NULL, NULL};
1953                         bConstraintTarget *ct;
1954                         
1955                         if (cti && cti->get_constraint_targets) {
1956                                 cti->get_constraint_targets(con, &targets);
1957                                 
1958                                 for (ct= targets.first; ct; ct= ct->next) {
1959                                         if (ct->tar) {
1960                                                 ob->recalc |= OB_RECALC_OB;
1961                                                 break;
1962                                         }
1963                                 }
1964                                 
1965                                 if (cti->flush_constraint_targets)
1966                                         cti->flush_constraint_targets(con, &targets, 1);
1967                         }
1968                 }
1969         }
1970         
1971         if(ob->scriptlink.totscript) ob->recalc |= OB_RECALC_OB;
1972         
1973         if(ob->parent) {
1974                 /* motion path or bone child */
1975                 if(ob->parent->type==OB_CURVE || ob->parent->type==OB_ARMATURE) ob->recalc |= OB_RECALC_OB;
1976         }
1977         
1978 #if 0 // XXX old animation system
1979         if(ob->nlastrips.first) {
1980                 if(ob->dup_group) {
1981                         bActionStrip *strip;
1982                         /* this case is for groups with nla, whilst nla target has no action or nla */
1983                         for(strip= ob->nlastrips.first; strip; strip= strip->next) {
1984                                 if(strip->object)
1985                                         strip->object->recalc |= OB_RECALC;
1986                         }
1987                 }
1988         }
1989 #endif // XXX old animation system
1990         
1991         if(animdata_use_time(ob->adt)) {
1992                 ob->recalc |= OB_RECALC;
1993                 ob->adt->recalc |= ADT_RECALC_ANIM;
1994         }
1995         
1996         if((ob->adt) && (ob->type==OB_ARMATURE)) ob->recalc |= OB_RECALC_DATA;
1997         
1998         if(object_modifiers_use_time(ob)) ob->recalc |= OB_RECALC_DATA;
1999         if((ob->pose) && (ob->pose->flag & POSE_CONSTRAINTS_TIMEDEPEND)) ob->recalc |= OB_RECALC_DATA;
2000         
2001         {
2002                 Mesh *me;
2003                 Curve *cu;
2004                 Lattice *lt;
2005                 
2006                 switch(ob->type) {
2007                         case OB_MESH:
2008                                 me= ob->data;
2009                                 if(me->key) {
2010                                         if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
2011                                                 ob->recalc |= OB_RECALC_DATA;
2012                                                 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
2013                                         }
2014                                 }
2015                                 if(ob->particlesystem.first)
2016                                         ob->recalc |= OB_RECALC_DATA;
2017                                 break;
2018                         case OB_CURVE:
2019                         case OB_SURF:
2020                                 cu= ob->data;
2021                                 if(cu->key) {
2022                                         if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
2023                                                 ob->recalc |= OB_RECALC_DATA;
2024                                                 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
2025                                         }
2026                                 }
2027                                 break;
2028                         case OB_FONT:
2029                                 cu= ob->data;
2030                                 if(cu->nurb.first==NULL && cu->str && cu->vfont)
2031                                         ob->recalc |= OB_RECALC_DATA;
2032                                 break;
2033                         case OB_LATTICE:
2034                                 lt= ob->data;
2035                                 if(lt->key) {
2036                                         if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
2037                                                 ob->recalc |= OB_RECALC_DATA;
2038                                                 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
2039                                         }
2040                                 }
2041                                         break;
2042                         case OB_MBALL:
2043                                 if(ob->transflag & OB_DUPLI) ob->recalc |= OB_RECALC_DATA;
2044                                 break;
2045                 }
2046
2047                 if(ob->particlesystem.first) {
2048                         ParticleSystem *psys= ob->particlesystem.first;
2049
2050                         for(; psys; psys=psys->next) {
2051                                 if(psys_check_enabled(ob, psys)) {
2052                                         ob->recalc |= OB_RECALC_DATA;
2053                                         break;
2054                                 }
2055                         }
2056                 }
2057         }               
2058 }
2059
2060 /* flag all objects that need recalc, for changes in time for example */
2061 void DAG_scene_update_flags(Scene *scene, unsigned int lay)
2062 {
2063         Base *base;
2064         Object *ob;
2065         Group *group;
2066         GroupObject *go;
2067         Scene *sce;
2068         
2069         /* set ob flags where animated systems are */
2070         for(SETLOOPER(scene, base)) {
2071                 ob= base->object;
2072                 
2073                 /* now if DagNode were part of base, the node->lay could be checked... */
2074                 /* we do all now, since the scene_flush checks layers and clears recalc flags even */
2075                 dag_object_time_update_flags(ob);
2076                 
2077                 /* handled in next loop */
2078                 if(ob->dup_group) 
2079                         ob->dup_group->id.flag |= LIB_DOIT;
2080         }       
2081         
2082         /* we do groups each once */
2083         for(group= G.main->group.first; group; group= group->id.next) {
2084                 if(group->id.flag & LIB_DOIT) {
2085                         for(go= group->gobject.first; go; go= go->next) {
2086                                 dag_object_time_update_flags(go->ob);
2087                         }
2088                 }
2089         }
2090         
2091         for(sce= scene; sce; sce= sce->set)
2092                 DAG_scene_flush_update(sce, lay, 1);
2093         
2094         /* test: set time flag, to disable baked systems to update */
2095         for(SETLOOPER(scene, base)) {
2096                 ob= base->object;
2097                 if(ob->recalc)
2098                         ob->recalc |= OB_RECALC_TIME;
2099         }
2100         
2101         /* hrmf... an exception to look at once, for invisible camera object we do it over */
2102         if(scene->camera)
2103                 dag_object_time_update_flags(scene->camera);
2104         
2105         /* and store the info in groupobject */
2106         for(group= G.main->group.first; group; group= group->id.next) {
2107                 if(group->id.flag & LIB_DOIT) {
2108                         for(go= group->gobject.first; go; go= go->next) {
2109                                 go->recalc= go->ob->recalc;
2110                                 // printf("ob %s recalc %d\n", go->ob->id.name, go->recalc);
2111                         }
2112                         group->id.flag &= ~LIB_DOIT;
2113                 }
2114         }
2115         
2116 }
2117
2118
2119 /* flag this object and all its relations to recalc */
2120 /* if you need to do more objects, tag object yourself and
2121    use DAG_scene_flush_update() in end */
2122 void DAG_object_flush_update(Scene *sce, Object *ob, short flag)
2123 {
2124         
2125         if(ob==NULL || sce->theDag==NULL) return;
2126
2127         ob->recalc |= flag;
2128         BKE_ptcache_object_reset(ob, PTCACHE_RESET_DEPSGRAPH);
2129         
2130         /* all users of this ob->data should be checked */
2131         /* BUT! displists for curves are still only on cu */
2132         if(flag & OB_RECALC_DATA) {
2133                 if(ob->type!=OB_CURVE && ob->type!=OB_SURF) {
2134                         ID *id= ob->data;
2135                         if(id && id->us>1) {
2136                                 /* except when there's a key and shapes are locked */
2137                                 if(ob_get_key(ob) && (ob->shapeflag & (OB_SHAPE_LOCK|OB_SHAPE_TEMPLOCK)));
2138                                 else {
2139                                         Object *obt;
2140                                         for (obt=G.main->object.first; obt; obt= obt->id.next) {
2141                                                 if (obt != ob && obt->data==ob->data) {
2142                                                         obt->recalc |= OB_RECALC_DATA;
2143                                                         BKE_ptcache_object_reset(obt, PTCACHE_RESET_DEPSGRAPH);
2144                                                 }
2145                                         }
2146                                 }
2147                         }
2148                 }
2149         }
2150         
2151 // XXX  if(G.curscreen)
2152 //              DAG_scene_flush_update(sce, dag_screen_view3d_layers(), 0);
2153 //      else
2154                 DAG_scene_flush_update(sce, sce->lay, 0);
2155 }
2156
2157 /* recursively descends tree, each node only checked once */
2158 /* node is checked to be of type object */
2159 static int parent_check_node(DagNode *node, int curtime)
2160 {
2161         DagAdjList *itA;
2162         
2163         node->lasttime= curtime;
2164         
2165         if(node->color==DAG_GRAY)
2166                 return DAG_GRAY;
2167         
2168         for(itA = node->child; itA; itA= itA->next) {
2169                 if(itA->node->type==ID_OB) {
2170                         
2171                         if(itA->node->color==DAG_GRAY)
2172                                 return DAG_GRAY;
2173
2174                         /* descend if not done */
2175                         if(itA->node->lasttime!=curtime) {
2176                                 itA->node->color= parent_check_node(itA->node, curtime);
2177                         
2178                                 if(itA->node->color==DAG_GRAY)
2179                                         return DAG_GRAY;
2180                         }
2181                 }
2182         }
2183         
2184         return DAG_WHITE;
2185 }
2186
2187 /* all nodes that influence this object get tagged, for calculating the exact
2188    position of this object at a given timeframe */
2189 void DAG_object_update_flags(Scene *sce, Object *ob, unsigned int lay)
2190 {
2191         DagNode *node;
2192         DagAdjList *itA;
2193         
2194         /* tag nodes unchecked */
2195         for(node = sce->theDag->DagNode.first; node; node= node->next) 
2196                 node->color = DAG_WHITE;
2197         
2198         node= dag_find_node(sce->theDag, ob);
2199         
2200         /* object not in scene? then handle group exception. needs to be dagged once too */
2201         if(node==NULL) {
2202                 Group *group= NULL;
2203                 while( (group = find_group(ob, group)) ) {
2204                         GroupObject *go;
2205                         /* primitive; tag all... this call helps building groups for particles */
2206                         for(go= group->gobject.first; go; go= go->next)
2207                                 go->ob->recalc= OB_RECALC;
2208                 }
2209         }
2210         else {
2211                 
2212                 node->color = DAG_GRAY;
2213                 
2214                 sce->theDag->time++;
2215                 node= sce->theDag->DagNode.first;
2216                 for(itA = node->child; itA; itA= itA->next) {
2217                         if(itA->node->type==ID_OB && itA->node->lasttime!=sce->theDag->time)
2218                                 itA->node->color= parent_check_node(itA->node, sce->theDag->time);
2219                 }
2220                 
2221                 /* set recalcs and flushes */
2222                 DAG_scene_update_flags(sce, lay);
2223                 
2224                 /* now we clear recalcs, unless color is set */
2225                 for(node = sce->theDag->DagNode.first; node; node= node->next) {
2226                         if(node->type==ID_OB && node->color==DAG_WHITE) {
2227                                 Object *ob= node->ob;
2228                                 ob->recalc= 0;
2229                         }
2230                 }
2231         }
2232 }
2233
2234 /* ******************* DAG FOR ARMATURE POSE ***************** */
2235
2236 /* we assume its an armature with pose */
2237 void DAG_pose_sort(Object *ob)
2238 {
2239         bPose *pose= ob->pose;
2240         bPoseChannel *pchan;
2241         bConstraint *con;
2242         DagNode *node;
2243         DagNode *node2, *node3;
2244         DagNode *rootnode;
2245         DagForest *dag;
2246         DagNodeQueue *nqueue;
2247         DagAdjList *itA;
2248         ListBase tempbase;
2249         int skip = 0;
2250         
2251         dag = dag_init();
2252         ugly_hack_sorry= 0;     // no ID structs
2253
2254         rootnode = dag_add_node(dag, NULL);     // node->ob becomes NULL
2255         
2256         /* we add the hierarchy and the constraints */
2257         for(pchan = pose->chanbase.first; pchan; pchan= pchan->next) {
2258                 int addtoroot = 1;
2259                 
2260                 node = dag_get_node(dag, pchan);
2261                 
2262                 if(pchan->parent) {
2263                         node2 = dag_get_node(dag, pchan->parent);
2264                         dag_add_relation(dag, node2, node, 0, "Parent Relation");
2265                         addtoroot = 0;
2266                 }
2267                 for (con = pchan->constraints.first; con; con=con->next) {
2268                         bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
2269                         ListBase targets = {NULL, NULL};
2270                         bConstraintTarget *ct;
2271                         
2272 #if 0 // XXX old animation system... driver stuff to watch out for
2273                         if(con->ipo) {
2274                                 IpoCurve *icu;
2275                                 for(icu= con->ipo->curve.first; icu; icu= icu->next) {
2276                                         /* icu->driver->ob should actually point to ob->proxy if it
2277                                          * is a proxy, but since it wasn't set correct it older
2278                                          * files comparing with ob->proxy makes it work for those */
2279                                         if(icu->driver && (icu->driver->ob==ob || icu->driver->ob==ob->proxy)) {
2280                                                 bPoseChannel *target= get_pose_channel(ob->pose, icu->driver->name);
2281                                                 if(target) {
2282                                                         node2 = dag_get_node(dag, target);
2283                                                         dag_add_relation(dag, node2, node, 0, "Ipo Driver");
2284                                                         
2285                                                         /* uncommented this line, results in dependencies
2286                                                          * not being added properly for this constraint,
2287                                                          * what is the purpose of this? - brecht */
2288                                                         /*cti= NULL;*/  /* trick to get next loop skipped */
2289                                                 }
2290                                         }
2291                                 }
2292                         }
2293 #endif // XXX old animation system... driver stuff to watch out for
2294                         
2295                         if (cti && cti->get_constraint_targets) {
2296                                 cti->get_constraint_targets(con, &targets);
2297                                 
2298                                 for (ct= targets.first; ct; ct= ct->next) {
2299                                         if (ct->tar==ob && ct->subtarget[0]) {
2300                                                 bPoseChannel *target= get_pose_channel(ob->pose, ct->subtarget);
2301                                                 if (target) {
2302                                                         node2= dag_get_node(dag, target);
2303                                                         dag_add_relation(dag, node2, node, 0, "IK Constraint");
2304
2305                                                         if (con->type==CONSTRAINT_TYPE_KINEMATIC) {
2306                                                                 bKinematicConstraint *data = (bKinematicConstraint *)con->data;
2307                                                                 bPoseChannel *parchan;
2308                                                                 int segcount= 0;
2309                                                                 
2310                                                                 /* exclude tip from chain? */
2311                                                                 if(!(data->flag & CONSTRAINT_IK_TIP))
2312                                                                         parchan= pchan->parent;
2313                                                                 else
2314                                                                         parchan= pchan;
2315                                                                 
2316                                                                 /* Walk to the chain's root */
2317                                                                 while (parchan) {
2318                                                                         node3= dag_get_node(dag, parchan);
2319                                                                         dag_add_relation(dag, node2, node3, 0, "IK Constraint");
2320                                                                         
2321                                                                         segcount++;
2322                                                                         if (segcount==data->rootbone || segcount>255) break; // 255 is weak
2323                                                                         parchan= parchan->parent;
2324                                                                 }
2325                                                         }
2326                                                 }
2327                                         }
2328                                 }
2329                                 
2330                                 if (cti->flush_constraint_targets)
2331                                         cti->flush_constraint_targets(con, &targets, 1);
2332                         }
2333                 }
2334                 if (addtoroot == 1 ) {
2335                         dag_add_relation(dag, rootnode, node, 0, "Root Bone Relation");
2336                 }
2337         }
2338
2339         dag_check_cycle(dag);
2340         
2341         /* now we try to sort... */
2342         tempbase.first= tempbase.last= NULL;
2343
2344         nqueue = queue_create(DAGQUEUEALLOC);
2345         
2346         /* tag nodes unchecked */
2347         for(node = dag->DagNode.first; node; node= node->next) 
2348                 node->color = DAG_WHITE;
2349         
2350         node = dag->DagNode.first;
2351         
2352         node->color = DAG_GRAY;
2353         push_stack(nqueue, node);  
2354         
2355         while(nqueue->count) {
2356                 
2357                 skip = 0;
2358                 node = get_top_node_queue(nqueue);
2359                 
2360                 itA = node->child;
2361                 while(itA != NULL) {
2362                         if((itA->node->color == DAG_WHITE) ) {
2363                                 itA->node->color = DAG_GRAY;
2364                                 push_stack(nqueue,itA->node);
2365                                 skip = 1;
2366                                 break;
2367                         } 
2368                         itA = itA->next;
2369                 }                       
2370                 
2371                 if (!skip) {
2372                         if (node) {
2373                                 node = pop_queue(nqueue);
2374                                 if (node->ob == NULL)   // we are done
2375                                         break ;
2376                                 node->color = DAG_BLACK;
2377                                 
2378                                 /* put node in new list */
2379                                 BLI_remlink(&pose->chanbase, node->ob);
2380                                 BLI_addhead(&tempbase, node->ob);
2381                         }       
2382                 }
2383         }
2384         
2385         // temporal correction for circular dependancies
2386         while(pose->chanbase.first) {
2387                 pchan= pose->chanbase.first;
2388                 BLI_remlink(&pose->chanbase, pchan);
2389                 BLI_addhead(&tempbase, pchan);
2390
2391                 printf("cyclic %s\n", pchan->name);
2392         }
2393         
2394         pose->chanbase = tempbase;
2395         queue_delete(nqueue);
2396         
2397 //      printf("\nordered\n");
2398 //      for(pchan = pose->chanbase.first; pchan; pchan= pchan->next) {
2399 //              printf(" %s\n", pchan->name);
2400 //      }
2401         
2402         free_forest( dag );
2403         MEM_freeN( dag );
2404         
2405         ugly_hack_sorry= 1;
2406 }
2407
2408
2409