4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2004 Blender Foundation.
21 * All rights reserved.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
33 #include "BLI_winstuff.h"
36 #include "BLI_blenlib.h"
37 #include "BLI_arithb.h"
39 #include "DNA_anim_types.h"
40 #include "DNA_action_types.h"
41 #include "DNA_armature_types.h"
42 #include "DNA_curve_types.h"
43 #include "DNA_camera_types.h"
45 #include "DNA_effect_types.h"
46 #include "DNA_group_types.h"
47 #include "DNA_lattice_types.h"
48 #include "DNA_lamp_types.h"
49 #include "DNA_key_types.h"
50 #include "DNA_mesh_types.h"
51 #include "DNA_modifier_types.h"
52 #include "DNA_nla_types.h"
53 #include "DNA_object_types.h"
54 #include "DNA_object_force.h"
55 #include "DNA_object_fluidsim.h"
56 #include "DNA_outliner_types.h"
57 #include "DNA_particle_types.h"
58 #include "DNA_scene_types.h"
59 #include "DNA_screen_types.h"
60 #include "DNA_space_types.h"
61 #include "DNA_view2d_types.h"
62 #include "DNA_view3d_types.h"
64 #include "BKE_action.h"
65 #include "BKE_effect.h"
66 #include "BKE_global.h"
67 #include "BKE_group.h"
70 #include "BKE_mball.h"
71 #include "BKE_modifier.h"
72 #include "BKE_object.h"
73 #include "BKE_particle.h"
74 #include "BKE_pointcache.h"
75 #include "BKE_utildefines.h"
76 #include "BKE_scene.h"
78 #include "MEM_guardedalloc.h"
80 #ifndef DISABLE_PYTHON
81 #include "BPY_extern.h"
84 #include "depsgraph_private.h"
86 /* Queue and stack operations for dag traversal
88 * the queue store a list of freenodes to avoid successives alloc/dealloc
91 DagNodeQueue * queue_create (int slots)
94 DagNodeQueueElem * elem;
97 queue = MEM_mallocN(sizeof(DagNodeQueue),"DAG queue");
98 queue->freenodes = MEM_mallocN(sizeof(DagNodeQueue),"DAG queue");
101 queue->first = queue->last = NULL;
102 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem3");
105 queue->freenodes->first = queue->freenodes->last = elem;
107 for (i = 1; i <slots;i++) {
108 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem4");
111 queue->freenodes->last->next = elem;
112 queue->freenodes->last = elem;
114 queue->freenodes->count = slots;
118 void queue_raz(DagNodeQueue *queue)
120 DagNodeQueueElem * elem;
123 if (queue->freenodes->last)
124 queue->freenodes->last->next = elem;
126 queue->freenodes->first = queue->freenodes->last = elem;
129 queue->freenodes->count++;
133 queue->freenodes->count++;
135 queue->freenodes->last = elem;
139 void queue_delete(DagNodeQueue *queue)
141 DagNodeQueueElem * elem;
142 DagNodeQueueElem * temp;
151 elem = queue->freenodes->first;
158 MEM_freeN(queue->freenodes);
162 /* insert in queue, remove in front */
163 void push_queue(DagNodeQueue *queue, DagNode *node)
165 DagNodeQueueElem * elem;
169 fprintf(stderr,"pushing null node \n");
172 /*fprintf(stderr,"BFS push : %s %d\n",((ID *) node->ob)->name, queue->count);*/
174 elem = queue->freenodes->first;
176 queue->freenodes->first = elem->next;
177 if ( queue->freenodes->last == elem) {
178 queue->freenodes->last = NULL;
179 queue->freenodes->first = NULL;
181 queue->freenodes->count--;
182 } else { /* alllocating more */
183 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem1");
186 queue->freenodes->first = queue->freenodes->last = elem;
188 for (i = 1; i <DAGQUEUEALLOC;i++) {
189 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem2");
192 queue->freenodes->last->next = elem;
193 queue->freenodes->last = elem;
195 queue->freenodes->count = DAGQUEUEALLOC;
197 elem = queue->freenodes->first;
198 queue->freenodes->first = elem->next;
202 if (queue->last != NULL)
203 queue->last->next = elem;
205 if (queue->first == NULL) {
212 /* insert in front, remove in front */
213 void push_stack(DagNodeQueue *queue, DagNode *node)
215 DagNodeQueueElem * elem;
218 elem = queue->freenodes->first;
220 queue->freenodes->first = elem->next;
221 if ( queue->freenodes->last == elem) {
222 queue->freenodes->last = NULL;
223 queue->freenodes->first = NULL;
225 queue->freenodes->count--;
226 } else { /* alllocating more */
227 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem1");
230 queue->freenodes->first = queue->freenodes->last = elem;
232 for (i = 1; i <DAGQUEUEALLOC;i++) {
233 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem2");
236 queue->freenodes->last->next = elem;
237 queue->freenodes->last = elem;
239 queue->freenodes->count = DAGQUEUEALLOC;
241 elem = queue->freenodes->first;
242 queue->freenodes->first = elem->next;
244 elem->next = queue->first;
247 if (queue->last == NULL)
253 DagNode * pop_queue(DagNodeQueue *queue)
255 DagNodeQueueElem * elem;
260 queue->first = elem->next;
261 if (queue->last == elem) {
266 if (queue->freenodes->last)
267 queue->freenodes->last->next=elem;
268 queue->freenodes->last=elem;
269 if (queue->freenodes->first == NULL)
270 queue->freenodes->first=elem;
274 queue->freenodes->count++;
277 fprintf(stderr,"return null \n");
282 void *pop_ob_queue(struct DagNodeQueue *queue) {
283 return(pop_queue(queue)->ob);
286 DagNode * get_top_node_queue(DagNodeQueue *queue)
288 return queue->first->node;
291 int queue_count(struct DagNodeQueue *queue){
296 DagForest * dag_init()
299 /* use callocN to init all zero */
300 forest = MEM_callocN(sizeof(DagForest),"DAG root");
304 /* isdata = object data... */
305 // XXX this needs to be extended to be more flexible (so that not only objects are evaluated via depsgraph)...
306 static void dag_add_driver_relation(AnimData *adt, DagForest *dag, DagNode *node, int isdata)
311 for (fcu= adt->drivers.first; fcu; fcu= fcu->next) {
312 ChannelDriver *driver= fcu->driver;
315 /* loop over targets, adding relationships as appropriate */
316 for (dtar= driver->targets.first; dtar; dtar= dtar->next) {
318 if (GS(dtar->id->name)==ID_OB) {
319 Object *ob= (Object *)dtar->id;
321 /* normal channel-drives-channel */
322 node1 = dag_get_node(dag, dtar->id);
324 /* check if bone... */
325 if ((ob->type==OB_ARMATURE) && dtar->rna_path && strstr(dtar->rna_path, "pose.pose_channels["))
326 dag_add_relation(dag, node1, node, isdata?DAG_RL_DATA_DATA:DAG_RL_DATA_OB, "Driver");
327 /* check if ob data */
328 else if (dtar->rna_path && strstr(dtar->rna_path, "data."))
329 dag_add_relation(dag, node1, node, isdata?DAG_RL_DATA_DATA:DAG_RL_DATA_OB, "Driver");
332 dag_add_relation(dag, node1, node, isdata?DAG_RL_OB_DATA:DAG_RL_OB_OB, "Driver");
339 static void dag_add_collision_field_relation(DagForest *dag, Scene *scene, Object *ob, DagNode *node)
344 // would be nice to have a list of colliders here
345 // so for now walk all objects in scene check 'same layer rule'
346 for(base = scene->base.first; base; base= base->next) {
347 if((base->lay & ob->lay) && base->object->pd) {
348 Object *ob1= base->object;
349 if((ob1->pd->deflect || ob1->pd->forcefield) && (ob1 != ob)) {
350 node2 = dag_get_node(dag, ob1);
351 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Field Collision");
357 static void build_dag_object(DagForest *dag, DagNode *scenenode, Scene *scene, Object *ob, int mask)
364 ParticleSystem *psys;
367 node = dag_get_node(dag, ob);
369 if ((ob->data) && (mask&DAG_RL_DATA)) {
370 node2 = dag_get_node(dag,ob->data);
371 dag_add_relation(dag,node,node2,DAG_RL_DATA, "Object-Data Relation");
372 node2->first_ancestor = ob;
373 node2->ancestor_count += 1;
376 if (ob->type == OB_ARMATURE) {
381 for (pchan = ob->pose->chanbase.first; pchan; pchan=pchan->next) {
382 for (con = pchan->constraints.first; con; con=con->next) {
383 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
384 ListBase targets = {NULL, NULL};
385 bConstraintTarget *ct;
387 if (cti && cti->get_constraint_targets) {
388 cti->get_constraint_targets(con, &targets);
390 for (ct= targets.first; ct; ct= ct->next) {
391 if (ct->tar && ct->tar != ob) {
392 // fprintf(stderr,"armature %s target :%s \n", ob->id.name, target->id.name);
393 node3 = dag_get_node(dag, ct->tar);
395 if (ct->subtarget[0])
396 dag_add_relation(dag,node3,node, DAG_RL_OB_DATA|DAG_RL_DATA_DATA, cti->name);
397 else if(ELEM(con->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO))
398 dag_add_relation(dag,node3,node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, cti->name);
400 dag_add_relation(dag,node3,node, DAG_RL_OB_DATA, cti->name);
404 if (cti->flush_constraint_targets)
405 cti->flush_constraint_targets(con, &targets, 1);
413 /* driver dependencies, nla modifiers */
414 #if 0 // XXX old animation system
415 if(ob->nlastrips.first) {
417 bActionChannel *chan;
418 for(strip= ob->nlastrips.first; strip; strip= strip->next) {
419 if(strip->modifiers.first) {
420 bActionModifier *amod;
421 for(amod= strip->modifiers.first; amod; amod= amod->next) {
423 node2 = dag_get_node(dag, amod->ob);
424 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "NLA Strip Modifier");
430 #endif // XXX old animation system
432 dag_add_driver_relation(ob->adt, dag, node, (ob->type == OB_ARMATURE)); // XXX isdata arg here doesn't give an accurate picture of situation
436 dag_add_driver_relation(key->adt, dag, node, 1);
438 if (ob->modifiers.first) {
441 for(md=ob->modifiers.first; md; md=md->next) {
442 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
444 if (mti->updateDepgraph) mti->updateDepgraph(md, dag, scene, ob, node);
448 node2 = dag_get_node(dag,ob->parent);
450 switch(ob->partype) {
452 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Parent");
454 case PARVERT1: case PARVERT3: case PARBONE:
455 dag_add_relation(dag,node2,node,DAG_RL_DATA_OB|DAG_RL_OB_OB, "Vertex Parent");
458 if(ob->parent->type==OB_LATTICE)
459 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Lattice Parent");
460 else if(ob->parent->type==OB_CURVE) {
461 Curve *cu= ob->parent->data;
462 if(cu->flag & CU_PATH)
463 dag_add_relation(dag,node2,node,DAG_RL_DATA_OB|DAG_RL_OB_OB, "Curve Parent");
465 dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Curve Parent");
468 dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Parent");
470 /* exception case: parent is duplivert */
471 if(ob->type==OB_MBALL && (ob->parent->transflag & OB_DUPLIVERTS)) {
472 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Duplivert");
478 node2 = dag_get_node(dag,ob->track);
479 dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Track To");
483 node2 = dag_get_node(dag, ob->proxy);
484 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Proxy");
485 /* inverted relation, so addtoroot shouldn't be set to zero */
489 if (ob->type==OB_CAMERA) {
490 Camera *cam = (Camera *)ob->data;
492 dag_add_driver_relation(cam->adt, dag, node, 1);
494 node2 = dag_get_node(dag, cam->dof_ob);
495 dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Camera DoF");
498 if (ob->type==OB_LAMP) {
499 Lamp *la = (Lamp *)ob->data;
501 dag_add_driver_relation(la->adt, dag, node, 1);
504 if (ob->transflag & OB_DUPLI) {
505 if((ob->transflag & OB_DUPLIGROUP) && ob->dup_group) {
507 for(go= ob->dup_group->gobject.first; go; go= go->next) {
509 node2 = dag_get_node(dag, go->ob);
510 /* node2 changes node1, this keeps animations updated in groups?? not logical? */
511 dag_add_relation(dag, node2, node, DAG_RL_OB_OB, "Dupligroup");
517 /* softbody collision */
518 if((ob->type==OB_MESH) || (ob->type==OB_CURVE) || (ob->type==OB_LATTICE))
519 if(modifiers_isSoftbodyEnabled(ob) || modifiers_isClothEnabled(ob))
520 dag_add_collision_field_relation(dag, scene, ob, node);
522 if (ob->type==OB_MBALL) {
523 Object *mom= find_basis_mball(scene, ob);
525 node2 = dag_get_node(dag, mom);
526 dag_add_relation(dag,node,node2,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Metaball"); // mom depends on children!
529 else if (ob->type==OB_CURVE) {
532 node2 = dag_get_node(dag, cu->bevobj);
533 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Curve Bevel");
536 node2 = dag_get_node(dag, cu->taperobj);
537 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Curve Taper");
540 dag_add_driver_relation(cu->adt, dag, node, 1);
542 else if(ob->type==OB_FONT) {
544 if(cu->textoncurve) {
545 node2 = dag_get_node(dag, cu->textoncurve);
546 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Texture On Curve");
550 psys= ob->particlesystem.first;
552 ParticleEffectorCache *nec;
555 for(; psys; psys=psys->next) {
556 ParticleSettings *part= psys->part;
558 dag_add_relation(dag, node, node, DAG_RL_OB_DATA, "Particle-Object Relation");
560 if(psys->flag & PSYS_DISABLED || psys->flag & PSYS_DELETE)
563 if(part->phystype==PART_PHYS_KEYED && psys->keyed_ob &&
564 BLI_findlink(&psys->keyed_ob->particlesystem,psys->keyed_psys-1)) {
565 node2 = dag_get_node(dag, psys->keyed_ob);
566 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA, "Particle Keyed Physics");
569 if(part->draw_as == PART_DRAW_OB && part->dup_ob) {
570 node2 = dag_get_node(dag, part->dup_ob);
571 dag_add_relation(dag, node, node2, DAG_RL_OB_OB, "Particle Object Visualisation");
572 if(part->dup_ob->type == OB_MBALL)
573 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA, "Particle Object Visualisation");
576 if(part->draw_as == PART_DRAW_GR && part->dup_group) {
577 for(go=part->dup_group->gobject.first; go; go=go->next) {
578 node2 = dag_get_node(dag, go->ob);
579 dag_add_relation(dag, node, node2, DAG_RL_OB_OB, "Particle Group Visualisation");
583 if(psys->effectors.first)
584 psys_end_effectors(psys);
585 psys_init_effectors(scene, ob, psys->part->eff_group, psys);
587 if(psys->effectors.first) {
588 for(nec= psys->effectors.first; nec; nec= nec->next) {
589 Object *ob1= nec->ob;
591 if(nec->type & PSYS_EC_EFFECTOR) {
592 node2 = dag_get_node(dag, ob1);
593 if(ob1->pd->forcefield==PFIELD_GUIDE)
594 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Particle Field");
596 dag_add_relation(dag, node2, node, DAG_RL_OB_DATA, "Particle Field");
598 else if(nec->type & PSYS_EC_DEFLECT) {
599 node2 = dag_get_node(dag, ob1);
600 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Particle Collision");
602 else if(nec->type & PSYS_EC_PARTICLE) {
603 node2 = dag_get_node(dag, ob1);
604 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA, "Particle Field");
607 if(nec->type & PSYS_EC_REACTOR) {
608 node2 = dag_get_node(dag, ob1);
609 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA, "Particle Reactor");
616 for (con = ob->constraints.first; con; con=con->next) {
617 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
618 ListBase targets = {NULL, NULL};
619 bConstraintTarget *ct;
621 if (cti && cti->get_constraint_targets) {
622 cti->get_constraint_targets(con, &targets);
624 for (ct= targets.first; ct; ct= ct->next) {
632 node2 = dag_get_node(dag, obt);
633 if (ELEM(con->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO))
634 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB|DAG_RL_OB_OB, cti->name);
636 if (ELEM3(obt->type, OB_ARMATURE, OB_MESH, OB_LATTICE) && (ct->subtarget[0]))
637 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB|DAG_RL_OB_OB, cti->name);
639 dag_add_relation(dag, node2, node, DAG_RL_OB_OB, cti->name);
644 if (cti->flush_constraint_targets)
645 cti->flush_constraint_targets(con, &targets, 1);
650 dag_add_relation(dag,scenenode,node,DAG_RL_SCENE, "Scene Relation");
653 struct DagForest *build_dag(struct Scene *sce, short mask)
673 /* add base node for scene. scene is always the first node in DAG */
674 scenenode = dag_add_node(dag, sce);
676 /* add current scene objects */
677 for(base = sce->base.first; base; base= base->next) {
680 build_dag_object(dag, scenenode, sce, ob, mask);
682 build_dag_object(dag, scenenode, sce, ob->proxy, mask);
684 /* handled in next loop */
686 ob->dup_group->id.flag |= LIB_DOIT;
689 /* add groups used in current scene objects */
690 for(group= G.main->group.first; group; group= group->id.next) {
691 if(group->id.flag & LIB_DOIT) {
692 for(go= group->gobject.first; go; go= go->next) {
693 build_dag_object(dag, scenenode, sce, go->ob, mask);
695 group->id.flag &= ~LIB_DOIT;
699 /* Now all relations were built, but we need to solve 1 exceptional case;
700 When objects have multiple "parents" (for example parent + constraint working on same object)
701 the relation type has to be synced. One of the parents can change, and should give same event to child */
703 /* nodes were callocced, so we can use node->color for temporal storage */
704 for(node = sce->theDag->DagNode.first; node; node= node->next) {
705 if(node->type==ID_OB) {
706 for(itA = node->child; itA; itA= itA->next) {
707 if(itA->node->type==ID_OB) {
708 itA->node->color |= itA->type;
713 /* now set relations equal, so that when only one parent changes, the correct recalcs are found */
714 for(node = sce->theDag->DagNode.first; node; node= node->next) {
715 if(node->type==ID_OB) {
716 for(itA = node->child; itA; itA= itA->next) {
717 if(itA->node->type==ID_OB) {
718 itA->type |= itA->node->color;
724 // cycle detection and solving
725 // solve_cycles(dag);
731 void free_forest(DagForest *Dag)
732 { /* remove all nodes and deps */
736 DagNode *itN = Dag->DagNode.first;
757 Dag->DagNode.first = NULL;
758 Dag->DagNode.last = NULL;
763 DagNode * dag_find_node (DagForest *forest,void * fob)
765 DagNode *node = forest->DagNode.first;
775 static int ugly_hack_sorry= 1; // prevent type check
777 /* no checking of existance, use dag_find_node first or dag_get_node */
778 DagNode * dag_add_node (DagForest *forest, void * fob)
782 node = MEM_callocN(sizeof(DagNode),"DAG node");
785 node->color = DAG_WHITE;
787 if(ugly_hack_sorry) node->type = GS(((ID *) fob)->name); // sorry, done for pose sorting
788 if (forest->numNodes) {
789 ((DagNode *) forest->DagNode.last)->next = node;
790 forest->DagNode.last = node;
793 forest->DagNode.last = node;
794 forest->DagNode.first = node;
795 forest->numNodes = 1;
801 DagNode * dag_get_node (DagForest *forest,void * fob)
805 node = dag_find_node (forest, fob);
807 node = dag_add_node(forest, fob);
813 DagNode * dag_get_sub_node (DagForest *forest,void * fob)
816 DagAdjList *mainchild, *prev=NULL;
818 mainchild = ((DagNode *) forest->DagNode.first)->child;
819 /* remove from first node (scene) adj list if present */
821 if (mainchild->node == fob) {
823 prev->next = mainchild->next;
824 MEM_freeN(mainchild);
827 ((DagNode *) forest->DagNode.first)->child = mainchild->next;
828 MEM_freeN(mainchild);
833 mainchild = mainchild->next;
835 node = dag_find_node (forest, fob);
837 node = dag_add_node(forest, fob);
841 static void dag_add_parent_relation(DagForest *forest, DagNode *fob1, DagNode *fob2, short rel, char *name)
843 DagAdjList *itA = fob2->parent;
845 while (itA) { /* search if relation exist already */
846 if (itA->node == fob1) {
853 /* create new relation and insert at head. MALLOC alert! */
854 itA = MEM_mallocN(sizeof(DagAdjList),"DAG adj list");
858 itA->next = fob2->parent;
863 void dag_add_relation(DagForest *forest, DagNode *fob1, DagNode *fob2, short rel, char *name)
865 DagAdjList *itA = fob1->child;
867 /* parent relation is for cycle checking */
868 dag_add_parent_relation(forest, fob1, fob2, rel, name);
870 while (itA) { /* search if relation exist already */
871 if (itA->node == fob2) {
878 /* create new relation and insert at head. MALLOC alert! */
879 itA = MEM_mallocN(sizeof(DagAdjList),"DAG adj list");
883 itA->next = fob1->child;
888 static char *dag_node_name(DagNode *node)
892 else if(ugly_hack_sorry)
893 return ((ID*)(node->ob))->name+2;
895 return ((bPoseChannel*)(node->ob))->name;
899 static void dag_node_print_dependencies(DagNode *node)
903 printf("%s depends on:\n", dag_node_name(node));
905 for(itA= node->parent; itA; itA= itA->next)
906 printf(" %s through %s\n", dag_node_name(itA->node), itA->name);
911 static int dag_node_print_dependency_recurs(DagNode *node, DagNode *endnode)
915 if(node->color == DAG_BLACK)
918 node->color= DAG_BLACK;
923 for(itA= node->parent; itA; itA= itA->next) {
924 if(dag_node_print_dependency_recurs(itA->node, endnode)) {
925 printf(" %s depends on %s through %s.\n", dag_node_name(node), dag_node_name(itA->node), itA->name);
933 static void dag_node_print_dependency_cycle(DagForest *dag, DagNode *startnode, DagNode *endnode, char *name)
937 for(node = dag->DagNode.first; node; node= node->next)
938 node->color= DAG_WHITE;
940 printf(" %s depends on %s through %s.\n", dag_node_name(endnode), dag_node_name(startnode), name);
941 dag_node_print_dependency_recurs(startnode, endnode);
945 static int dag_node_recurs_level(DagNode *node, int level)
950 node->color= DAG_BLACK; /* done */
953 for(itA= node->parent; itA; itA= itA->next) {
954 if(itA->node->color==DAG_WHITE) {
955 itA->node->ancestor_count= dag_node_recurs_level(itA->node, level);
956 newlevel= MAX2(newlevel, level+itA->node->ancestor_count);
959 newlevel= MAX2(newlevel, level+itA->node->ancestor_count);
965 static void dag_check_cycle(DagForest *dag)
970 /* tag nodes unchecked */
971 for(node = dag->DagNode.first; node; node= node->next)
972 node->color= DAG_WHITE;
974 for(node = dag->DagNode.first; node; node= node->next) {
975 if(node->color==DAG_WHITE) {
976 node->ancestor_count= dag_node_recurs_level(node, 0);
980 /* check relations, and print errors */
981 for(node = dag->DagNode.first; node; node= node->next) {
982 for(itA= node->parent; itA; itA= itA->next) {
983 if(itA->node->ancestor_count > node->ancestor_count) {
984 if(node->ob && itA->node->ob) {
985 printf("Dependency cycle detected:\n");
986 dag_node_print_dependency_cycle(dag, itA->node, node, itA->name);
992 /* parent relations are only needed for cycle checking, so free now */
993 for(node = dag->DagNode.first; node; node= node->next) {
994 while (node->parent) {
995 itA = node->parent->next;
996 MEM_freeN(node->parent);
1003 * MainDAG is the DAG of all objects in current scene
1004 * used only for drawing there is one also in each scene
1006 static DagForest * MainDag = NULL;
1008 DagForest *getMainDag(void)
1014 void setMainDag(DagForest *dag)
1022 * in theory we should sweep the whole array
1023 * but in our case the first node is the scene
1024 * and is linked to every other object
1026 * for general case we will need to add outer loop
1030 * ToDo : change pos kludge
1033 /* adjust levels for drawing in oops space */
1034 void graph_bfs(void)
1037 DagNodeQueue *nqueue;
1043 /* fprintf(stderr,"starting BFS \n ------------\n"); */
1044 nqueue = queue_create(DAGQUEUEALLOC);
1045 for ( i=0; i<50; i++)
1049 * dagnode.first is alway the root (scene)
1051 node = MainDag->DagNode.first;
1053 node->color = DAG_WHITE;
1054 node->BFS_dist = 9999;
1059 node = MainDag->DagNode.first;
1060 if (node->color == DAG_WHITE) {
1061 node->color = DAG_GRAY;
1063 push_queue(nqueue,node);
1064 while(nqueue->count) {
1065 node = pop_queue(nqueue);
1067 minheight = pos[node->BFS_dist];
1069 while(itA != NULL) {
1070 if((itA->node->color == DAG_WHITE) ) {
1071 itA->node->color = DAG_GRAY;
1072 itA->node->BFS_dist = node->BFS_dist + 1;
1073 itA->node->k = (float) minheight;
1074 push_queue(nqueue,itA->node);
1078 fprintf(stderr,"bfs not dag tree edge color :%i \n",itA->node->color);
1084 if (pos[node->BFS_dist] > node->k ) {
1085 pos[node->BFS_dist] += 1;
1086 node->k = (float) pos[node->BFS_dist];
1088 pos[node->BFS_dist] = (int) node->k +1;
1090 set_node_xy(node, node->BFS_dist*DEPSX*2, pos[node->BFS_dist]*DEPSY*2);
1091 node->color = DAG_BLACK;
1093 fprintf(stderr,"BFS node : %20s %i %5.0f %5.0f\n",((ID *) node->ob)->name,node->BFS_dist, node->x, node->y);
1097 queue_delete(nqueue);
1100 int pre_and_post_BFS(DagForest *dag, short mask, graph_action_func pre_func, graph_action_func post_func, void **data) {
1103 node = dag->DagNode.first;
1104 return pre_and_post_source_BFS(dag, mask, node, pre_func, post_func, data);
1108 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 DagNodeQueue *nqueue;
1114 /* fprintf(stderr,"starting BFS \n ------------\n"); */
1117 * dagnode.first is alway the root (scene)
1119 node = dag->DagNode.first;
1120 nqueue = queue_create(DAGQUEUEALLOC);
1122 node->color = DAG_WHITE;
1123 node->BFS_dist = 9999;
1128 if (node->color == DAG_WHITE) {
1129 node->color = DAG_GRAY;
1131 pre_func(node->ob,data);
1133 while(nqueue->count) {
1134 node = pop_queue(nqueue);
1137 while(itA != NULL) {
1138 if((itA->node->color == DAG_WHITE) && (itA->type & mask)) {
1139 itA->node->color = DAG_GRAY;
1140 itA->node->BFS_dist = node->BFS_dist + 1;
1141 push_queue(nqueue,itA->node);
1142 pre_func(node->ob,data);
1145 else { // back or cross edge
1150 post_func(node->ob,data);
1151 node->color = DAG_BLACK;
1153 fprintf(stderr,"BFS node : %20s %i %5.0f %5.0f\n",((ID *) node->ob)->name,node->BFS_dist, node->x, node->y);
1157 queue_delete(nqueue);
1161 /* non recursive version of DFS, return queue -- outer loop present to catch odd cases (first level cycles)*/
1162 DagNodeQueue * graph_dfs(void)
1165 DagNodeQueue *nqueue;
1166 DagNodeQueue *retqueue;
1176 *fprintf(stderr,"starting DFS \n ------------\n");
1178 nqueue = queue_create(DAGQUEUEALLOC);
1179 retqueue = queue_create(MainDag->numNodes);
1180 for ( i=0; i<50; i++)
1184 * dagnode.first is alway the root (scene)
1186 node = MainDag->DagNode.first;
1188 node->color = DAG_WHITE;
1189 node->DFS_dist = 9999;
1190 node->DFS_dvtm = node->DFS_fntm = 9999;
1197 node = MainDag->DagNode.first;
1200 if (node->color == DAG_WHITE) {
1201 node->color = DAG_GRAY;
1203 node->DFS_dvtm = time;
1205 push_stack(nqueue,node);
1207 while(nqueue->count) {
1208 //graph_print_queue(nqueue);
1211 node = get_top_node_queue(nqueue);
1213 minheight = pos[node->DFS_dist];
1216 while(itA != NULL) {
1217 if((itA->node->color == DAG_WHITE) ) {
1218 itA->node->DFS_dvtm = time;
1219 itA->node->color = DAG_GRAY;
1222 itA->node->DFS_dist = node->DFS_dist + 1;
1223 itA->node->k = (float) minheight;
1224 push_stack(nqueue,itA->node);
1228 if (itA->node->color == DAG_GRAY) { // back edge
1229 fprintf(stderr,"dfs back edge :%15s %15s \n",((ID *) node->ob)->name, ((ID *) itA->node->ob)->name);
1231 } else if (itA->node->color == DAG_BLACK) {
1233 /* already processed node but we may want later to change distance either to shorter to longer.
1234 * DFS_dist is the first encounter
1236 /*if (node->DFS_dist >= itA->node->DFS_dist)
1237 itA->node->DFS_dist = node->DFS_dist + 1;
1239 fprintf(stderr,"dfs forward or cross edge :%15s %i-%i %15s %i-%i \n",
1240 ((ID *) node->ob)->name,
1243 ((ID *) itA->node->ob)->name,
1244 itA->node->DFS_dvtm,
1245 itA->node->DFS_fntm);
1248 fprintf(stderr,"dfs unknown edge \n");
1254 node = pop_queue(nqueue);
1255 node->color = DAG_BLACK;
1257 node->DFS_fntm = time;
1259 if (node->DFS_dist > maxpos)
1260 maxpos = node->DFS_dist;
1261 if (pos[node->DFS_dist] > node->k ) {
1262 pos[node->DFS_dist] += 1;
1263 node->k = (float) pos[node->DFS_dist];
1265 pos[node->DFS_dist] = (int) node->k +1;
1267 set_node_xy(node, node->DFS_dist*DEPSX*2, pos[node->DFS_dist]*DEPSY*2);
1270 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 );
1272 push_stack(retqueue,node);
1279 // fprintf(stderr,"i size : %i \n", maxpos);
1281 queue_delete(nqueue);
1286 int pre_and_post_DFS(DagForest *dag, short mask, graph_action_func pre_func, graph_action_func post_func, void **data) {
1289 node = dag->DagNode.first;
1290 return pre_and_post_source_DFS(dag, mask, node, pre_func, post_func, data);
1293 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 DagNodeQueue *nqueue;
1302 *fprintf(stderr,"starting DFS \n ------------\n");
1304 nqueue = queue_create(DAGQUEUEALLOC);
1307 * dagnode.first is alway the root (scene)
1309 node = dag->DagNode.first;
1311 node->color = DAG_WHITE;
1312 node->DFS_dist = 9999;
1313 node->DFS_dvtm = node->DFS_fntm = 9999;
1322 if (node->color == DAG_WHITE) {
1323 node->color = DAG_GRAY;
1325 node->DFS_dvtm = time;
1327 push_stack(nqueue,node);
1328 pre_func(node->ob,data);
1330 while(nqueue->count) {
1332 node = get_top_node_queue(nqueue);
1335 while(itA != NULL) {
1336 if((itA->node->color == DAG_WHITE) && (itA->type & mask) ) {
1337 itA->node->DFS_dvtm = time;
1338 itA->node->color = DAG_GRAY;
1341 itA->node->DFS_dist = node->DFS_dist + 1;
1342 push_stack(nqueue,itA->node);
1343 pre_func(node->ob,data);
1348 if (itA->node->color == DAG_GRAY) {// back edge
1351 // else if (itA->node->color == DAG_BLACK) { // cross or forward
1358 node = pop_queue(nqueue);
1359 node->color = DAG_BLACK;
1361 node->DFS_fntm = time;
1363 post_func(node->ob,data);
1369 queue_delete(nqueue);
1374 // used to get the obs owning a datablock
1375 struct DagNodeQueue *get_obparents(struct DagForest *dag, void *ob)
1377 DagNode * node, *node1;
1378 DagNodeQueue *nqueue;
1381 node = dag_find_node(dag,ob);
1385 else if (node->ancestor_count == 1) { // simple case
1386 nqueue = queue_create(1);
1387 push_queue(nqueue,node);
1388 } else { // need to go over the whole dag for adj list
1389 nqueue = queue_create(node->ancestor_count);
1391 node1 = dag->DagNode.first;
1393 if (node1->DFS_fntm > node->DFS_fntm) { // a parent is finished after child. must check adj list
1395 while(itA != NULL) {
1396 if ((itA->node == node) && (itA->type == DAG_RL_DATA)) {
1397 push_queue(nqueue,node);
1402 node1 = node1->next;
1408 struct DagNodeQueue *get_first_ancestors(struct DagForest *dag, void *ob)
1410 DagNode * node, *node1;
1411 DagNodeQueue *nqueue;
1414 node = dag_find_node(dag,ob);
1416 // need to go over the whole dag for adj list
1417 nqueue = queue_create(node->ancestor_count);
1419 node1 = dag->DagNode.first;
1421 if (node1->DFS_fntm > node->DFS_fntm) {
1423 while(itA != NULL) {
1424 if (itA->node == node) {
1425 push_queue(nqueue,node);
1430 node1 = node1->next;
1436 // standard DFS list
1437 struct DagNodeQueue *get_all_childs(struct DagForest *dag, void *ob)
1440 DagNodeQueue *nqueue;
1441 DagNodeQueue *retqueue;
1446 nqueue = queue_create(DAGQUEUEALLOC);
1447 retqueue = queue_create(dag->numNodes); // was MainDag... why? (ton)
1449 node = dag->DagNode.first;
1451 node->color = DAG_WHITE;
1457 node = dag_find_node(dag, ob); // could be done in loop above (ton)
1458 if(node) { // can be null for newly added objects
1460 node->color = DAG_GRAY;
1462 push_stack(nqueue,node);
1464 while(nqueue->count) {
1467 node = get_top_node_queue(nqueue);
1470 while(itA != NULL) {
1471 if((itA->node->color == DAG_WHITE) ) {
1472 itA->node->DFS_dvtm = time;
1473 itA->node->color = DAG_GRAY;
1476 push_stack(nqueue,itA->node);
1484 node = pop_queue(nqueue);
1485 node->color = DAG_BLACK;
1488 push_stack(retqueue,node);
1492 queue_delete(nqueue);
1497 short are_obs_related(struct DagForest *dag, void *ob1, void *ob2) {
1501 node = dag_find_node(dag, ob1);
1504 while(itA != NULL) {
1505 if((itA->node->ob == ob2) ) {
1506 return itA->node->type;
1510 return DAG_NO_RELATION;
1513 int is_acyclic( DagForest *dag) {
1514 return dag->is_acyclic;
1517 void set_node_xy(DagNode *node, float x, float y)
1524 /* debug test functions */
1526 void graph_print_queue(DagNodeQueue *nqueue)
1528 DagNodeQueueElem *queueElem;
1530 queueElem = nqueue->first;
1532 fprintf(stderr,"** %s %i %i-%i ",((ID *) queueElem->node->ob)->name,queueElem->node->color,queueElem->node->DFS_dvtm,queueElem->node->DFS_fntm);
1533 queueElem = queueElem->next;
1535 fprintf(stderr,"\n");
1538 void graph_print_queue_dist(DagNodeQueue *nqueue)
1540 DagNodeQueueElem *queueElem;
1543 queueElem = nqueue->first;
1544 max = queueElem->node->DFS_fntm;
1547 fprintf(stderr,"** %25s %2.2i-%2.2i ",((ID *) queueElem->node->ob)->name,queueElem->node->DFS_dvtm,queueElem->node->DFS_fntm);
1548 while (count < queueElem->node->DFS_dvtm-1) { fputc(' ',stderr); count++;}
1550 while (count < queueElem->node->DFS_fntm-2) { fputc('-',stderr); count++;}
1554 queueElem = queueElem->next;
1556 fprintf(stderr,"\n");
1559 void graph_print_adj_list(void)
1564 node = (getMainDag())->DagNode.first;
1566 fprintf(stderr,"node : %s col: %i",((ID *) node->ob)->name, node->color);
1569 fprintf(stderr,"-- %s ",((ID *) itA->node->ob)->name);
1573 fprintf(stderr,"\n");
1578 /* ************************ API *********************** */
1580 /* groups with objects in this scene need to be put in the right order as well */
1581 static void scene_sort_groups(Scene *sce)
1588 /* test; are group objects all in this scene? */
1589 for(ob= G.main->object.first; ob; ob= ob->id.next) {
1590 ob->id.flag &= ~LIB_DOIT;
1591 ob->id.newid= NULL; /* newid abuse for GroupObject */
1593 for(base = sce->base.first; base; base= base->next)
1594 base->object->id.flag |= LIB_DOIT;
1596 for(group= G.main->group.first; group; group= group->id.next) {
1597 for(go= group->gobject.first; go; go= go->next) {
1598 if((go->ob->id.flag & LIB_DOIT)==0)
1601 /* this group is entirely in this scene */
1603 ListBase listb= {NULL, NULL};
1605 for(go= group->gobject.first; go; go= go->next)
1606 go->ob->id.newid= (ID *)go;
1608 /* in order of sorted bases we reinsert group objects */
1609 for(base = sce->base.first; base; base= base->next) {
1611 if(base->object->id.newid) {
1612 go= (GroupObject *)base->object->id.newid;
1613 base->object->id.newid= NULL;
1614 BLI_remlink( &group->gobject, go);
1615 BLI_addtail( &listb, go);
1618 /* copy the newly sorted listbase */
1619 group->gobject= listb;
1624 /* sort the base list on dependency order */
1625 void DAG_scene_sort(struct Scene *sce)
1628 DagNodeQueue *nqueue;
1635 tempbase.first= tempbase.last= NULL;
1637 build_dag(sce, DAG_RL_ALL_BUT_DATA);
1639 dag_check_cycle(sce->theDag);
1641 nqueue = queue_create(DAGQUEUEALLOC);
1643 for(node = sce->theDag->DagNode.first; node; node= node->next) {
1644 node->color = DAG_WHITE;
1649 node = sce->theDag->DagNode.first;
1651 node->color = DAG_GRAY;
1653 push_stack(nqueue,node);
1655 while(nqueue->count) {
1658 node = get_top_node_queue(nqueue);
1661 while(itA != NULL) {
1662 if((itA->node->color == DAG_WHITE) ) {
1663 itA->node->DFS_dvtm = time;
1664 itA->node->color = DAG_GRAY;
1667 push_stack(nqueue,itA->node);
1676 node = pop_queue(nqueue);
1677 if (node->ob == sce) // we are done
1679 node->color = DAG_BLACK;
1682 base = sce->base.first;
1683 while (base && base->object != node->ob)
1686 BLI_remlink(&sce->base,base);
1687 BLI_addhead(&tempbase,base);
1693 // temporal correction for circular dependancies
1694 base = sce->base.first;
1696 BLI_remlink(&sce->base,base);
1697 BLI_addhead(&tempbase,base);
1699 printf("cyclic %s\n", base->object->id.name);
1700 base = sce->base.first;
1703 sce->base = tempbase;
1704 queue_delete(nqueue);
1706 /* all groups with objects in this scene gets resorted too */
1707 scene_sort_groups(sce);
1710 printf("\nordered\n");
1711 for(base = sce->base.first; base; base= base->next) {
1712 printf(" %s\n", base->object->id.name);
1716 sce->recalc |= SCE_PRV_CHANGED; /* test for 3d preview */
1719 /* node was checked to have lasttime != curtime and is if type ID_OB */
1720 static void flush_update_node(DagNode *node, unsigned int layer, int curtime)
1724 int oldflag, changed=0;
1725 unsigned int all_layer;
1727 node->lasttime= curtime;
1730 if(ob && (ob->recalc & OB_RECALC)) {
1733 /* got an object node that changes, now check relations */
1734 for(itA = node->child; itA; itA= itA->next) {
1735 all_layer |= itA->lay;
1736 /* the relationship is visible */
1737 if((itA->lay & layer)) { // XXX || (itA->node->ob == obedit)
1738 if(itA->node->type==ID_OB) {
1740 oldflag= obc->recalc;
1742 /* got a ob->obc relation, now check if flag needs flush */
1743 if(ob->recalc & OB_RECALC_OB) {
1744 if(itA->type & DAG_RL_OB_OB) {
1745 //printf("ob %s changes ob %s\n", ob->id.name, obc->id.name);
1746 obc->recalc |= OB_RECALC_OB;
1748 if(itA->type & DAG_RL_OB_DATA) {
1749 //printf("ob %s changes obdata %s\n", ob->id.name, obc->id.name);
1750 obc->recalc |= OB_RECALC_DATA;
1753 if(ob->recalc & OB_RECALC_DATA) {
1754 if(itA->type & DAG_RL_DATA_OB) {
1755 //printf("obdata %s changes ob %s\n", ob->id.name, obc->id.name);
1756 obc->recalc |= OB_RECALC_OB;
1758 if(itA->type & DAG_RL_DATA_DATA) {
1759 //printf("obdata %s changes obdata %s\n", ob->id.name, obc->id.name);
1760 obc->recalc |= OB_RECALC_DATA;
1763 if(oldflag!=obc->recalc) changed= 1;
1767 /* even nicer, we can clear recalc flags... */
1768 if((all_layer & layer)==0) { // XXX && (ob != obedit)) {
1769 /* but existing displaylists or derivedmesh should be freed */
1770 if(ob->recalc & OB_RECALC_DATA)
1771 object_free_display(ob);
1773 ob->recalc &= ~OB_RECALC;
1777 /* check case where child changes and parent forcing obdata to change */
1778 /* should be done regardless if this ob has recalc set */
1779 /* could merge this in with loop above...? (ton) */
1780 for(itA = node->child; itA; itA= itA->next) {
1781 /* the relationship is visible */
1782 if((itA->lay & layer)) { // XXX || (itA->node->ob == obedit)
1783 if(itA->node->type==ID_OB) {
1786 if((obc->recalc & OB_RECALC)==OB_RECALC_OB) {
1787 /* parent has deforming info */
1788 if(itA->type & (DAG_RL_OB_DATA|DAG_RL_DATA_DATA)) {
1789 // printf("parent %s changes ob %s\n", ob->id.name, obc->id.name);
1790 obc->recalc |= OB_RECALC_DATA;
1797 /* we only go deeper if node not checked or something changed */
1798 for(itA = node->child; itA; itA= itA->next) {
1799 if(changed || itA->node->lasttime!=curtime)
1800 flush_update_node(itA->node, layer, curtime);
1805 /* node was checked to have lasttime != curtime , and is of type ID_OB */
1806 static unsigned int flush_layer_node(Scene *sce, DagNode *node, int curtime)
1811 node->lasttime= curtime;
1813 for(base= sce->base.first; base; base= base->next) {
1814 if(node->ob == base->object) {
1815 node->lay= ((Object *)node->ob)->lay;
1820 for(itA = node->child; itA; itA= itA->next) {
1821 if(itA->node->type==ID_OB) {
1822 if(itA->node->lasttime!=curtime) {
1823 itA->lay= flush_layer_node(sce, itA->node, curtime); // lay is only set once for each relation
1825 else itA->lay= itA->node->lay;
1827 node->lay |= itA->lay;
1834 /* node was checked to have lasttime != curtime , and is of type ID_OB */
1835 static void flush_pointcache_reset(DagNode *node, int curtime, int reset)
1840 node->lasttime= curtime;
1842 for(itA = node->child; itA; itA= itA->next) {
1843 if(itA->node->type==ID_OB) {
1844 if(itA->node->lasttime!=curtime) {
1845 ob= (Object*)(node->ob);
1847 if(reset || (ob->recalc & OB_RECALC)) {
1848 if(BKE_ptcache_object_reset(ob, PTCACHE_RESET_DEPSGRAPH))
1849 ob->recalc |= OB_RECALC_DATA;
1851 flush_pointcache_reset(itA->node, curtime, 1);
1854 flush_pointcache_reset(itA->node, curtime, 0);
1860 /* flushes all recalc flags in objects down the dependency tree */
1861 void DAG_scene_flush_update(Scene *sce, unsigned int lay, int time)
1868 if(sce->theDag==NULL) {
1869 printf("DAG zero... not allowed to happen!\n");
1870 DAG_scene_sort(sce);
1873 firstnode= sce->theDag->DagNode.first; // always scene node
1875 for(itA = firstnode->child; itA; itA= itA->next)
1878 /* first we flush the layer flags */
1879 sce->theDag->time++; // so we know which nodes were accessed
1880 lasttime= sce->theDag->time;
1882 for(itA = firstnode->child; itA; itA= itA->next)
1883 if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB)
1884 flush_layer_node(sce, itA->node, lasttime);
1886 /* then we use the relationships + layer info to flush update events */
1887 sce->theDag->time++; // so we know which nodes were accessed
1888 lasttime= sce->theDag->time;
1889 for(itA = firstnode->child; itA; itA= itA->next)
1890 if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB)
1891 flush_update_node(itA->node, lay, lasttime);
1893 /* if update is not due to time change, do pointcache clears */
1895 sce->theDag->time++; // so we know which nodes were accessed
1896 lasttime= sce->theDag->time;
1897 for(itA = firstnode->child; itA; itA= itA->next) {
1898 if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB) {
1899 ob= (Object*)(itA->node->ob);
1901 if(ob->recalc & OB_RECALC) {
1902 if(BKE_ptcache_object_reset(ob, PTCACHE_RESET_DEPSGRAPH))
1903 ob->recalc |= OB_RECALC_DATA;
1905 flush_pointcache_reset(itA->node, lasttime, 1);
1908 flush_pointcache_reset(itA->node, lasttime, 0);
1914 static int object_modifiers_use_time(Object *ob)
1918 for (md=ob->modifiers.first; md; md=md->next)
1919 if (modifier_dependsOnTime(md))
1925 static short animdata_use_time(AnimData *adt)
1929 if(adt==NULL) return 0;
1931 /* check action - only if assigned, and it has anim curves */
1932 if (adt->action && adt->action->curves.first)
1935 /* check NLA tracks + strips */
1936 for (nlt= adt->nla_tracks.first; nlt; nlt= nlt->next) {
1937 if (nlt->strips.first)
1944 static void dag_object_time_update_flags(Object *ob)
1946 if(ob->constraints.first) {
1948 for (con = ob->constraints.first; con; con=con->next) {
1949 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
1950 ListBase targets = {NULL, NULL};
1951 bConstraintTarget *ct;
1953 if (cti && cti->get_constraint_targets) {
1954 cti->get_constraint_targets(con, &targets);
1956 for (ct= targets.first; ct; ct= ct->next) {
1958 ob->recalc |= OB_RECALC_OB;
1963 if (cti->flush_constraint_targets)
1964 cti->flush_constraint_targets(con, &targets, 1);
1969 if(ob->scriptlink.totscript) ob->recalc |= OB_RECALC_OB;
1972 /* motion path or bone child */
1973 if(ob->parent->type==OB_CURVE || ob->parent->type==OB_ARMATURE) ob->recalc |= OB_RECALC_OB;
1976 #if 0 // XXX old animation system
1977 if(ob->nlastrips.first) {
1979 bActionStrip *strip;
1980 /* this case is for groups with nla, whilst nla target has no action or nla */
1981 for(strip= ob->nlastrips.first; strip; strip= strip->next) {
1983 strip->object->recalc |= OB_RECALC;
1987 #endif // XXX old animation system
1989 if(animdata_use_time(ob->adt)) {
1990 ob->recalc |= OB_RECALC;
1991 ob->adt->recalc |= ADT_RECALC_ANIM;
1994 if((ob->adt) && (ob->type==OB_ARMATURE)) ob->recalc |= OB_RECALC_DATA;
1996 if(object_modifiers_use_time(ob)) ob->recalc |= OB_RECALC_DATA;
1997 if((ob->pose) && (ob->pose->flag & POSE_CONSTRAINTS_TIMEDEPEND)) ob->recalc |= OB_RECALC_DATA;
2008 if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
2009 ob->recalc |= OB_RECALC_DATA;
2010 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
2013 if(ob->particlesystem.first)
2014 ob->recalc |= OB_RECALC_DATA;
2020 if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
2021 ob->recalc |= OB_RECALC_DATA;
2022 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
2028 if(cu->nurb.first==NULL && cu->str && cu->vfont)
2029 ob->recalc |= OB_RECALC_DATA;
2034 if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
2035 ob->recalc |= OB_RECALC_DATA;
2036 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
2041 if(ob->transflag & OB_DUPLI) ob->recalc |= OB_RECALC_DATA;
2045 if(ob->particlesystem.first) {
2046 ParticleSystem *psys= ob->particlesystem.first;
2048 for(; psys; psys=psys->next) {
2049 if(psys_check_enabled(ob, psys)) {
2050 ob->recalc |= OB_RECALC_DATA;
2058 /* flag all objects that need recalc, for changes in time for example */
2059 void DAG_scene_update_flags(Scene *scene, unsigned int lay)
2067 /* set ob flags where animated systems are */
2068 for(SETLOOPER(scene, base)) {
2071 /* now if DagNode were part of base, the node->lay could be checked... */
2072 /* we do all now, since the scene_flush checks layers and clears recalc flags even */
2073 dag_object_time_update_flags(ob);
2075 /* handled in next loop */
2077 ob->dup_group->id.flag |= LIB_DOIT;
2080 /* we do groups each once */
2081 for(group= G.main->group.first; group; group= group->id.next) {
2082 if(group->id.flag & LIB_DOIT) {
2083 for(go= group->gobject.first; go; go= go->next) {
2084 dag_object_time_update_flags(go->ob);
2089 for(sce= scene; sce; sce= sce->set)
2090 DAG_scene_flush_update(sce, lay, 1);
2092 /* test: set time flag, to disable baked systems to update */
2093 for(SETLOOPER(scene, base)) {
2096 ob->recalc |= OB_RECALC_TIME;
2099 /* hrmf... an exception to look at once, for invisible camera object we do it over */
2101 dag_object_time_update_flags(scene->camera);
2103 /* and store the info in groupobject */
2104 for(group= G.main->group.first; group; group= group->id.next) {
2105 if(group->id.flag & LIB_DOIT) {
2106 for(go= group->gobject.first; go; go= go->next) {
2107 go->recalc= go->ob->recalc;
2108 // printf("ob %s recalc %d\n", go->ob->id.name, go->recalc);
2110 group->id.flag &= ~LIB_DOIT;
2117 /* flag this object and all its relations to recalc */
2118 /* if you need to do more objects, tag object yourself and
2119 use DAG_scene_flush_update() in end */
2120 void DAG_object_flush_update(Scene *sce, Object *ob, short flag)
2123 if(ob==NULL || sce->theDag==NULL) return;
2126 BKE_ptcache_object_reset(ob, PTCACHE_RESET_DEPSGRAPH);
2128 /* all users of this ob->data should be checked */
2129 /* BUT! displists for curves are still only on cu */
2130 if(flag & OB_RECALC_DATA) {
2131 if(ob->type!=OB_CURVE && ob->type!=OB_SURF) {
2133 if(id && id->us>1) {
2134 /* except when there's a key and shapes are locked */
2135 if(ob_get_key(ob) && (ob->shapeflag & (OB_SHAPE_LOCK|OB_SHAPE_TEMPLOCK)));
2138 for (obt=G.main->object.first; obt; obt= obt->id.next) {
2139 if (obt != ob && obt->data==ob->data) {
2140 obt->recalc |= OB_RECALC_DATA;
2141 BKE_ptcache_object_reset(obt, PTCACHE_RESET_DEPSGRAPH);
2149 // XXX if(G.curscreen)
2150 // DAG_scene_flush_update(sce, dag_screen_view3d_layers(), 0);
2152 DAG_scene_flush_update(sce, sce->lay, 0);
2155 /* recursively descends tree, each node only checked once */
2156 /* node is checked to be of type object */
2157 static int parent_check_node(DagNode *node, int curtime)
2161 node->lasttime= curtime;
2163 if(node->color==DAG_GRAY)
2166 for(itA = node->child; itA; itA= itA->next) {
2167 if(itA->node->type==ID_OB) {
2169 if(itA->node->color==DAG_GRAY)
2172 /* descend if not done */
2173 if(itA->node->lasttime!=curtime) {
2174 itA->node->color= parent_check_node(itA->node, curtime);
2176 if(itA->node->color==DAG_GRAY)
2185 /* all nodes that influence this object get tagged, for calculating the exact
2186 position of this object at a given timeframe */
2187 void DAG_object_update_flags(Scene *sce, Object *ob, unsigned int lay)
2192 /* tag nodes unchecked */
2193 for(node = sce->theDag->DagNode.first; node; node= node->next)
2194 node->color = DAG_WHITE;
2196 node= dag_find_node(sce->theDag, ob);
2198 /* object not in scene? then handle group exception. needs to be dagged once too */
2201 while( (group = find_group(ob, group)) ) {
2203 /* primitive; tag all... this call helps building groups for particles */
2204 for(go= group->gobject.first; go; go= go->next)
2205 go->ob->recalc= OB_RECALC;
2210 node->color = DAG_GRAY;
2212 sce->theDag->time++;
2213 node= sce->theDag->DagNode.first;
2214 for(itA = node->child; itA; itA= itA->next) {
2215 if(itA->node->type==ID_OB && itA->node->lasttime!=sce->theDag->time)
2216 itA->node->color= parent_check_node(itA->node, sce->theDag->time);
2219 /* set recalcs and flushes */
2220 DAG_scene_update_flags(sce, lay);
2222 /* now we clear recalcs, unless color is set */
2223 for(node = sce->theDag->DagNode.first; node; node= node->next) {
2224 if(node->type==ID_OB && node->color==DAG_WHITE) {
2225 Object *ob= node->ob;
2232 /* ******************* DAG FOR ARMATURE POSE ***************** */
2234 /* we assume its an armature with pose */
2235 void DAG_pose_sort(Object *ob)
2237 bPose *pose= ob->pose;
2238 bPoseChannel *pchan;
2241 DagNode *node2, *node3;
2244 DagNodeQueue *nqueue;
2250 ugly_hack_sorry= 0; // no ID structs
2252 rootnode = dag_add_node(dag, NULL); // node->ob becomes NULL
2254 /* we add the hierarchy and the constraints */
2255 for(pchan = pose->chanbase.first; pchan; pchan= pchan->next) {
2258 node = dag_get_node(dag, pchan);
2261 node2 = dag_get_node(dag, pchan->parent);
2262 dag_add_relation(dag, node2, node, 0, "Parent Relation");
2265 for (con = pchan->constraints.first; con; con=con->next) {
2266 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
2267 ListBase targets = {NULL, NULL};
2268 bConstraintTarget *ct;
2270 #if 0 // XXX old animation system... driver stuff to watch out for
2273 for(icu= con->ipo->curve.first; icu; icu= icu->next) {
2274 /* icu->driver->ob should actually point to ob->proxy if it
2275 * is a proxy, but since it wasn't set correct it older
2276 * files comparing with ob->proxy makes it work for those */
2277 if(icu->driver && (icu->driver->ob==ob || icu->driver->ob==ob->proxy)) {
2278 bPoseChannel *target= get_pose_channel(ob->pose, icu->driver->name);
2280 node2 = dag_get_node(dag, target);
2281 dag_add_relation(dag, node2, node, 0, "Ipo Driver");
2283 /* uncommented this line, results in dependencies
2284 * not being added properly for this constraint,
2285 * what is the purpose of this? - brecht */
2286 /*cti= NULL;*/ /* trick to get next loop skipped */
2291 #endif // XXX old animation system... driver stuff to watch out for
2293 if (cti && cti->get_constraint_targets) {
2294 cti->get_constraint_targets(con, &targets);
2296 for (ct= targets.first; ct; ct= ct->next) {
2297 if (ct->tar==ob && ct->subtarget[0]) {
2298 bPoseChannel *target= get_pose_channel(ob->pose, ct->subtarget);
2300 node2= dag_get_node(dag, target);
2301 dag_add_relation(dag, node2, node, 0, "IK Constraint");
2303 if (con->type==CONSTRAINT_TYPE_KINEMATIC) {
2304 bKinematicConstraint *data = (bKinematicConstraint *)con->data;
2305 bPoseChannel *parchan;
2308 /* exclude tip from chain? */
2309 if(!(data->flag & CONSTRAINT_IK_TIP))
2310 parchan= pchan->parent;
2314 /* Walk to the chain's root */
2316 node3= dag_get_node(dag, parchan);
2317 dag_add_relation(dag, node2, node3, 0, "IK Constraint");
2320 if (segcount==data->rootbone || segcount>255) break; // 255 is weak
2321 parchan= parchan->parent;
2328 if (cti->flush_constraint_targets)
2329 cti->flush_constraint_targets(con, &targets, 1);
2332 if (addtoroot == 1 ) {
2333 dag_add_relation(dag, rootnode, node, 0, "Root Bone Relation");
2337 dag_check_cycle(dag);
2339 /* now we try to sort... */
2340 tempbase.first= tempbase.last= NULL;
2342 nqueue = queue_create(DAGQUEUEALLOC);
2344 /* tag nodes unchecked */
2345 for(node = dag->DagNode.first; node; node= node->next)
2346 node->color = DAG_WHITE;
2348 node = dag->DagNode.first;
2350 node->color = DAG_GRAY;
2351 push_stack(nqueue, node);
2353 while(nqueue->count) {
2356 node = get_top_node_queue(nqueue);
2359 while(itA != NULL) {
2360 if((itA->node->color == DAG_WHITE) ) {
2361 itA->node->color = DAG_GRAY;
2362 push_stack(nqueue,itA->node);
2371 node = pop_queue(nqueue);
2372 if (node->ob == NULL) // we are done
2374 node->color = DAG_BLACK;
2376 /* put node in new list */
2377 BLI_remlink(&pose->chanbase, node->ob);
2378 BLI_addhead(&tempbase, node->ob);
2383 // temporal correction for circular dependancies
2384 while(pose->chanbase.first) {
2385 pchan= pose->chanbase.first;
2386 BLI_remlink(&pose->chanbase, pchan);
2387 BLI_addhead(&tempbase, pchan);
2389 printf("cyclic %s\n", pchan->name);
2392 pose->chanbase = tempbase;
2393 queue_delete(nqueue);
2395 // printf("\nordered\n");
2396 // for(pchan = pose->chanbase.first; pchan; pchan= pchan->next) {
2397 // printf(" %s\n", pchan->name);