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 "BMF_Api.h"
38 #include "BLI_blenlib.h"
39 #include "BLI_arithb.h"
41 #include "DNA_action_types.h"
42 #include "DNA_armature_types.h"
43 #include "DNA_curve_types.h"
44 #include "DNA_camera_types.h"
46 #include "DNA_effect_types.h"
47 #include "DNA_group_types.h"
48 #include "DNA_lattice_types.h"
49 #include "DNA_lamp_types.h"
50 #include "DNA_key_types.h"
51 #include "DNA_mesh_types.h"
52 #include "DNA_modifier_types.h"
53 #include "DNA_nla_types.h"
54 #include "DNA_object_types.h"
55 #include "DNA_object_force.h"
56 #include "DNA_object_fluidsim.h"
57 #include "DNA_oops_types.h"
58 #include "DNA_particle_types.h"
59 #include "DNA_scene_types.h"
60 #include "DNA_screen_types.h"
61 #include "DNA_space_types.h"
62 #include "DNA_view2d_types.h"
63 #include "DNA_view3d_types.h"
65 #include "BKE_action.h"
66 #include "BKE_effect.h"
67 #include "BKE_global.h"
68 #include "BKE_group.h"
71 #include "BKE_mball.h"
72 #include "BKE_modifier.h"
73 #include "BKE_object.h"
74 #include "BKE_particle.h"
75 #include "BKE_pointcache.h"
76 #include "BKE_utildefines.h"
77 #include "BKE_scene.h"
79 #include "MEM_guardedalloc.h"
82 #ifndef DISABLE_PYTHON
83 #include "BPY_extern.h"
86 #include "depsgraph_private.h"
88 /* Queue and stack operations for dag traversal
90 * the queue store a list of freenodes to avoid successives alloc/dealloc
93 DagNodeQueue * queue_create (int slots)
96 DagNodeQueueElem * elem;
99 queue = MEM_mallocN(sizeof(DagNodeQueue),"DAG queue");
100 queue->freenodes = MEM_mallocN(sizeof(DagNodeQueue),"DAG queue");
103 queue->first = queue->last = NULL;
104 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem3");
107 queue->freenodes->first = queue->freenodes->last = elem;
109 for (i = 1; i <slots;i++) {
110 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem4");
113 queue->freenodes->last->next = elem;
114 queue->freenodes->last = elem;
116 queue->freenodes->count = slots;
120 void queue_raz(DagNodeQueue *queue)
122 DagNodeQueueElem * elem;
125 if (queue->freenodes->last)
126 queue->freenodes->last->next = elem;
128 queue->freenodes->first = queue->freenodes->last = elem;
131 queue->freenodes->count++;
135 queue->freenodes->count++;
137 queue->freenodes->last = elem;
141 void queue_delete(DagNodeQueue *queue)
143 DagNodeQueueElem * elem;
144 DagNodeQueueElem * temp;
153 elem = queue->freenodes->first;
160 MEM_freeN(queue->freenodes);
164 /* insert in queue, remove in front */
165 void push_queue(DagNodeQueue *queue, DagNode *node)
167 DagNodeQueueElem * elem;
171 fprintf(stderr,"pushing null node \n");
174 /*fprintf(stderr,"BFS push : %s %d\n",((ID *) node->ob)->name, queue->count);*/
176 elem = queue->freenodes->first;
178 queue->freenodes->first = elem->next;
179 if ( queue->freenodes->last == elem) {
180 queue->freenodes->last = NULL;
181 queue->freenodes->first = NULL;
183 queue->freenodes->count--;
184 } else { /* alllocating more */
185 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem1");
188 queue->freenodes->first = queue->freenodes->last = elem;
190 for (i = 1; i <DAGQUEUEALLOC;i++) {
191 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem2");
194 queue->freenodes->last->next = elem;
195 queue->freenodes->last = elem;
197 queue->freenodes->count = DAGQUEUEALLOC;
199 elem = queue->freenodes->first;
200 queue->freenodes->first = elem->next;
204 if (queue->last != NULL)
205 queue->last->next = elem;
207 if (queue->first == NULL) {
214 /* insert in front, remove in front */
215 void push_stack(DagNodeQueue *queue, DagNode *node)
217 DagNodeQueueElem * elem;
220 elem = queue->freenodes->first;
222 queue->freenodes->first = elem->next;
223 if ( queue->freenodes->last == elem) {
224 queue->freenodes->last = NULL;
225 queue->freenodes->first = NULL;
227 queue->freenodes->count--;
228 } else { /* alllocating more */
229 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem1");
232 queue->freenodes->first = queue->freenodes->last = elem;
234 for (i = 1; i <DAGQUEUEALLOC;i++) {
235 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem2");
238 queue->freenodes->last->next = elem;
239 queue->freenodes->last = elem;
241 queue->freenodes->count = DAGQUEUEALLOC;
243 elem = queue->freenodes->first;
244 queue->freenodes->first = elem->next;
246 elem->next = queue->first;
249 if (queue->last == NULL)
255 DagNode * pop_queue(DagNodeQueue *queue)
257 DagNodeQueueElem * elem;
262 queue->first = elem->next;
263 if (queue->last == elem) {
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;
276 queue->freenodes->count++;
279 fprintf(stderr,"return null \n");
284 void *pop_ob_queue(struct DagNodeQueue *queue) {
285 return(pop_queue(queue)->ob);
288 DagNode * get_top_node_queue(DagNodeQueue *queue)
290 return queue->first->node;
293 int queue_count(struct DagNodeQueue *queue){
298 DagForest * dag_init()
301 /* use callocN to init all zero */
302 forest = MEM_callocN(sizeof(DagForest),"DAG root");
306 static void dag_add_driver_relation(Ipo *ipo, DagForest *dag, DagNode *node, int isdata)
311 for(icu= ipo->curve.first; icu; icu= icu->next) {
314 if (icu->driver->type == IPO_DRIVER_TYPE_PYTHON) {
316 if ((icu->driver->flag & IPO_DRIVER_FLAG_INVALID) || (icu->driver->name[0] == '\0'))
317 continue; /* empty or invalid expression */
318 #ifndef DISABLE_PYTHON
320 /* now we need refs to all objects mentioned in this
321 * pydriver expression, to call 'dag_add_relation'
322 * for each of them */
323 Object **obarray = BPY_pydriver_get_objects(icu->driver);
325 Object *ob, **oba = obarray;
329 node1 = dag_get_node(dag, ob);
330 if (ob->type == OB_ARMATURE)
331 dag_add_relation(dag, node1, node, isdata?DAG_RL_DATA_DATA:DAG_RL_DATA_OB, "Python Ipo Driver");
333 dag_add_relation(dag, node1, node, isdata?DAG_RL_OB_DATA:DAG_RL_OB_OB, "Python Ipo Driver");
340 #endif /* DISABLE_PYTHON */
342 else if (icu->driver->ob) {
343 node1 = dag_get_node(dag, icu->driver->ob);
344 if(icu->driver->blocktype==ID_AR)
345 dag_add_relation(dag, node1, node, isdata?DAG_RL_DATA_DATA:DAG_RL_DATA_OB, "Ipo Driver");
347 dag_add_relation(dag, node1, node, isdata?DAG_RL_OB_DATA:DAG_RL_OB_OB, "Ipo Driver");
353 static void dag_add_collision_field_relation(DagForest *dag, Object *ob, DagNode *node)
358 // would be nice to have a list of colliders here
359 // so for now walk all objects in scene check 'same layer rule'
360 for(base = G.scene->base.first; base; base= base->next) {
361 if((base->lay & ob->lay) && base->object->pd) {
362 Object *ob1= base->object;
363 if((ob1->pd->deflect || ob1->pd->forcefield) && (ob1 != ob)) {
364 node2 = dag_get_node(dag, ob1);
365 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Field Collision");
371 static void build_dag_object(DagForest *dag, DagNode *scenenode, Object *ob, int mask)
374 bConstraintChannel *conchan;
379 ParticleSystem *psys;
382 node = dag_get_node(dag, ob);
384 if ((ob->data) && (mask&DAG_RL_DATA)) {
385 node2 = dag_get_node(dag,ob->data);
386 dag_add_relation(dag,node,node2,DAG_RL_DATA, "Object-Data Relation");
387 node2->first_ancestor = ob;
388 node2->ancestor_count += 1;
391 if (ob->type == OB_ARMATURE) {
396 for (pchan = ob->pose->chanbase.first; pchan; pchan=pchan->next) {
397 for (con = pchan->constraints.first; con; con=con->next) {
398 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
399 ListBase targets = {NULL, NULL};
400 bConstraintTarget *ct;
402 if (cti && cti->get_constraint_targets) {
403 cti->get_constraint_targets(con, &targets);
405 for (ct= targets.first; ct; ct= ct->next) {
406 if (ct->tar && ct->tar != ob) {
407 // fprintf(stderr,"armature %s target :%s \n", ob->id.name, target->id.name);
408 node3 = dag_get_node(dag, ct->tar);
410 if (ct->subtarget[0])
411 dag_add_relation(dag,node3,node, DAG_RL_OB_DATA|DAG_RL_DATA_DATA, cti->name);
412 else if(ELEM(con->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO))
413 dag_add_relation(dag,node3,node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, cti->name);
415 dag_add_relation(dag,node3,node, DAG_RL_OB_DATA, cti->name);
419 if (cti->flush_constraint_targets)
420 cti->flush_constraint_targets(con, &targets, 1);
428 /* driver dependencies, nla modifiers */
430 dag_add_driver_relation(ob->ipo, dag, node, 0);
434 dag_add_driver_relation(key->ipo, dag, node, 1);
436 for (conchan=ob->constraintChannels.first; conchan; conchan=conchan->next)
438 dag_add_driver_relation(conchan->ipo, dag, node, 0);
441 bActionChannel *chan;
442 for (chan = ob->action->chanbase.first; chan; chan=chan->next){
444 dag_add_driver_relation(chan->ipo, dag, node, 1);
445 for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next)
447 dag_add_driver_relation(conchan->ipo, dag, node, 1);
450 if(ob->nlastrips.first) {
452 bActionChannel *chan;
453 for(strip= ob->nlastrips.first; strip; strip= strip->next) {
454 if(strip->act && strip->act!=ob->action)
455 for (chan = strip->act->chanbase.first; chan; chan=chan->next)
457 dag_add_driver_relation(chan->ipo, dag, node, 1);
458 if(strip->modifiers.first) {
459 bActionModifier *amod;
460 for(amod= strip->modifiers.first; amod; amod= amod->next) {
462 node2 = dag_get_node(dag, amod->ob);
463 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "NLA Strip Modifier");
469 if (ob->modifiers.first) {
472 for(md=ob->modifiers.first; md; md=md->next) {
473 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
475 if (mti->updateDepgraph) mti->updateDepgraph(md, dag, ob, node);
479 node2 = dag_get_node(dag,ob->parent);
481 switch(ob->partype) {
483 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Parent");
485 case PARVERT1: case PARVERT3: case PARBONE:
486 dag_add_relation(dag,node2,node,DAG_RL_DATA_OB|DAG_RL_OB_OB, "Vertex Parent");
489 if(ob->parent->type==OB_LATTICE)
490 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Lattice Parent");
491 else if(ob->parent->type==OB_CURVE) {
492 Curve *cu= ob->parent->data;
493 if(cu->flag & CU_PATH)
494 dag_add_relation(dag,node2,node,DAG_RL_DATA_OB|DAG_RL_OB_OB, "Curve Parent");
496 dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Curve Parent");
499 dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Curve Parent");
501 /* exception case: parent is duplivert */
502 if(ob->type==OB_MBALL && (ob->parent->transflag & OB_DUPLIVERTS)) {
503 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Duplivert");
509 node2 = dag_get_node(dag,ob->track);
510 dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Track To");
514 node2 = dag_get_node(dag, ob->proxy);
515 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Proxy");
516 /* inverted relation, so addtoroot shouldn't be set to zero */
518 if (ob->type==OB_CAMERA) {
519 Camera *cam = (Camera *)ob->data;
521 dag_add_driver_relation(cam->ipo, dag, node, 1);
524 node2 = dag_get_node(dag, cam->dof_ob);
525 dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Camera DoF");
528 if (ob->type==OB_LAMP) {
529 Lamp *la = (Lamp *)ob->data;
531 dag_add_driver_relation(la->ipo, dag, node, 1);
534 if (ob->transflag & OB_DUPLI) {
535 if((ob->transflag & OB_DUPLIGROUP) && ob->dup_group) {
537 for(go= ob->dup_group->gobject.first; go; go= go->next) {
539 node2 = dag_get_node(dag, go->ob);
540 /* node2 changes node1, this keeps animations updated in groups?? not logical? */
541 dag_add_relation(dag, node2, node, DAG_RL_OB_OB, "Dupligroup");
547 /* softbody collision */
548 if((ob->type==OB_MESH) || (ob->type==OB_CURVE) || (ob->type==OB_LATTICE))
549 if(modifiers_isSoftbodyEnabled(ob) || modifiers_isClothEnabled(ob))
550 dag_add_collision_field_relation(dag, ob, node);
552 if (ob->type==OB_MBALL) {
553 Object *mom= find_basis_mball(ob);
555 node2 = dag_get_node(dag, mom);
556 dag_add_relation(dag,node,node2,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Metaball"); // mom depends on children!
559 else if (ob->type==OB_CURVE) {
562 node2 = dag_get_node(dag, cu->bevobj);
563 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Curve Bevel");
566 node2 = dag_get_node(dag, cu->taperobj);
567 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Curve Taper");
570 dag_add_driver_relation(cu->ipo, dag, node, 1);
573 else if(ob->type==OB_FONT) {
575 if(cu->textoncurve) {
576 node2 = dag_get_node(dag, cu->textoncurve);
577 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Texture On Curve");
581 psys= ob->particlesystem.first;
583 ParticleEffectorCache *nec;
586 for(; psys; psys=psys->next) {
587 ParticleSettings *part= psys->part;
589 dag_add_relation(dag, node, node, DAG_RL_OB_DATA, "Particle-Object Relation");
591 if(psys->flag & PSYS_DISABLED || psys->flag & PSYS_DELETE)
594 if(part->phystype==PART_PHYS_KEYED && psys->keyed_ob &&
595 BLI_findlink(&psys->keyed_ob->particlesystem,psys->keyed_psys-1)) {
596 node2 = dag_get_node(dag, psys->keyed_ob);
597 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA, "Particle Keyed Physics");
600 if(part->draw_as == PART_DRAW_OB && part->dup_ob) {
601 node2 = dag_get_node(dag, part->dup_ob);
602 dag_add_relation(dag, node, node2, DAG_RL_OB_OB, "Particle Object Visualisation");
603 if(part->dup_ob->type == OB_MBALL)
604 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA, "Particle Object Visualisation");
607 if(part->draw_as == PART_DRAW_GR && part->dup_group) {
608 for(go=part->dup_group->gobject.first; go; go=go->next) {
609 node2 = dag_get_node(dag, go->ob);
610 dag_add_relation(dag, node, node2, DAG_RL_OB_OB, "Particle Group Visualisation");
614 if(psys->effectors.first)
615 psys_end_effectors(psys);
616 psys_init_effectors(ob,psys->part->eff_group,psys);
618 if(psys->effectors.first) {
619 for(nec= psys->effectors.first; nec; nec= nec->next) {
620 Object *ob1= nec->ob;
622 if(nec->type & PSYS_EC_EFFECTOR) {
623 node2 = dag_get_node(dag, ob1);
624 if(ob1->pd->forcefield==PFIELD_GUIDE)
625 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Particle Field");
627 dag_add_relation(dag, node2, node, DAG_RL_OB_DATA, "Particle Field");
629 else if(nec->type & PSYS_EC_DEFLECT) {
630 node2 = dag_get_node(dag, ob1);
631 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Particle Collision");
633 else if(nec->type & PSYS_EC_PARTICLE) {
634 node2 = dag_get_node(dag, ob1);
635 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA, "Particle Field");
638 if(nec->type & PSYS_EC_REACTOR) {
639 node2 = dag_get_node(dag, ob1);
640 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA, "Particle Reactor");
647 for (con = ob->constraints.first; con; con=con->next) {
648 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
649 ListBase targets = {NULL, NULL};
650 bConstraintTarget *ct;
652 if (cti && cti->get_constraint_targets) {
653 cti->get_constraint_targets(con, &targets);
655 for (ct= targets.first; ct; ct= ct->next) {
663 node2 = dag_get_node(dag, obt);
664 if (ELEM(con->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO))
665 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB|DAG_RL_OB_OB, cti->name);
667 if (ELEM3(obt->type, OB_ARMATURE, OB_MESH, OB_LATTICE) && (ct->subtarget[0]))
668 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB|DAG_RL_OB_OB, cti->name);
670 dag_add_relation(dag, node2, node, DAG_RL_OB_OB, cti->name);
675 if (cti->flush_constraint_targets)
676 cti->flush_constraint_targets(con, &targets, 1);
681 dag_add_relation(dag,scenenode,node,DAG_RL_SCENE, "Scene Relation");
684 struct DagForest *build_dag(struct Scene *sce, short mask)
704 /* add base node for scene. scene is always the first node in DAG */
705 scenenode = dag_add_node(dag, sce);
707 /* add current scene objects */
708 for(base = sce->base.first; base; base= base->next) {
711 build_dag_object(dag, scenenode, ob, mask);
713 build_dag_object(dag, scenenode, ob->proxy, mask);
715 /* handled in next loop */
717 ob->dup_group->id.flag |= LIB_DOIT;
720 /* add groups used in current scene objects */
721 for(group= G.main->group.first; group; group= group->id.next) {
722 if(group->id.flag & LIB_DOIT) {
723 for(go= group->gobject.first; go; go= go->next) {
724 build_dag_object(dag, scenenode, go->ob, mask);
726 group->id.flag &= ~LIB_DOIT;
730 /* Now all relations were built, but we need to solve 1 exceptional case;
731 When objects have multiple "parents" (for example parent + constraint working on same object)
732 the relation type has to be synced. One of the parents can change, and should give same event to child */
734 /* nodes were callocced, so we can use node->color for temporal storage */
735 for(node = sce->theDag->DagNode.first; node; node= node->next) {
736 if(node->type==ID_OB) {
737 for(itA = node->child; itA; itA= itA->next) {
738 if(itA->node->type==ID_OB) {
739 itA->node->color |= itA->type;
744 /* now set relations equal, so that when only one parent changes, the correct recalcs are found */
745 for(node = sce->theDag->DagNode.first; node; node= node->next) {
746 if(node->type==ID_OB) {
747 for(itA = node->child; itA; itA= itA->next) {
748 if(itA->node->type==ID_OB) {
749 itA->type |= itA->node->color;
755 // cycle detection and solving
756 // solve_cycles(dag);
762 void free_forest(DagForest *Dag)
763 { /* remove all nodes and deps */
767 DagNode *itN = Dag->DagNode.first;
788 Dag->DagNode.first = NULL;
789 Dag->DagNode.last = NULL;
794 DagNode * dag_find_node (DagForest *forest,void * fob)
796 DagNode *node = forest->DagNode.first;
806 static int ugly_hack_sorry= 1; // prevent type check
808 /* no checking of existance, use dag_find_node first or dag_get_node */
809 DagNode * dag_add_node (DagForest *forest, void * fob)
813 node = MEM_callocN(sizeof(DagNode),"DAG node");
816 node->color = DAG_WHITE;
818 if(ugly_hack_sorry) node->type = GS(((ID *) fob)->name); // sorry, done for pose sorting
819 if (forest->numNodes) {
820 ((DagNode *) forest->DagNode.last)->next = node;
821 forest->DagNode.last = node;
824 forest->DagNode.last = node;
825 forest->DagNode.first = node;
826 forest->numNodes = 1;
832 DagNode * dag_get_node (DagForest *forest,void * fob)
836 node = dag_find_node (forest, fob);
838 node = dag_add_node(forest, fob);
844 DagNode * dag_get_sub_node (DagForest *forest,void * fob)
847 DagAdjList *mainchild, *prev=NULL;
849 mainchild = ((DagNode *) forest->DagNode.first)->child;
850 /* remove from first node (scene) adj list if present */
852 if (mainchild->node == fob) {
854 prev->next = mainchild->next;
855 MEM_freeN(mainchild);
858 ((DagNode *) forest->DagNode.first)->child = mainchild->next;
859 MEM_freeN(mainchild);
864 mainchild = mainchild->next;
866 node = dag_find_node (forest, fob);
868 node = dag_add_node(forest, fob);
872 static void dag_add_parent_relation(DagForest *forest, DagNode *fob1, DagNode *fob2, short rel, char *name)
874 DagAdjList *itA = fob2->parent;
876 while (itA) { /* search if relation exist already */
877 if (itA->node == fob1) {
884 /* create new relation and insert at head. MALLOC alert! */
885 itA = MEM_mallocN(sizeof(DagAdjList),"DAG adj list");
889 itA->next = fob2->parent;
894 void dag_add_relation(DagForest *forest, DagNode *fob1, DagNode *fob2, short rel, char *name)
896 DagAdjList *itA = fob1->child;
898 /* parent relation is for cycle checking */
899 dag_add_parent_relation(forest, fob1, fob2, rel, name);
901 while (itA) { /* search if relation exist already */
902 if (itA->node == fob2) {
909 /* create new relation and insert at head. MALLOC alert! */
910 itA = MEM_mallocN(sizeof(DagAdjList),"DAG adj list");
914 itA->next = fob1->child;
919 static char *dag_node_name(DagNode *node)
923 else if(ugly_hack_sorry)
924 return ((ID*)(node->ob))->name+2;
926 return ((bPoseChannel*)(node->ob))->name;
930 static void dag_node_print_dependencies(DagNode *node)
934 printf("%s depends on:\n", dag_node_name(node));
936 for(itA= node->parent; itA; itA= itA->next)
937 printf(" %s through %s\n", dag_node_name(itA->node), itA->name);
942 static int dag_node_print_dependency_recurs(DagNode *node, DagNode *endnode)
946 if(node->color == DAG_BLACK)
949 node->color= DAG_BLACK;
954 for(itA= node->parent; itA; itA= itA->next) {
955 if(dag_node_print_dependency_recurs(itA->node, endnode)) {
956 printf(" %s depends on %s through %s.\n", dag_node_name(node), dag_node_name(itA->node), itA->name);
964 static void dag_node_print_dependency_cycle(DagForest *dag, DagNode *startnode, DagNode *endnode, char *name)
968 for(node = dag->DagNode.first; node; node= node->next)
969 node->color= DAG_WHITE;
971 printf(" %s depends on %s through %s.\n", dag_node_name(endnode), dag_node_name(startnode), name);
972 dag_node_print_dependency_recurs(startnode, endnode);
976 static int dag_node_recurs_level(DagNode *node, int level)
981 node->color= DAG_BLACK; /* done */
984 for(itA= node->parent; itA; itA= itA->next) {
985 if(itA->node->color==DAG_WHITE) {
986 itA->node->ancestor_count= dag_node_recurs_level(itA->node, level);
987 newlevel= MAX2(newlevel, level+itA->node->ancestor_count);
990 newlevel= MAX2(newlevel, level+itA->node->ancestor_count);
996 static void dag_check_cycle(DagForest *dag)
1001 /* tag nodes unchecked */
1002 for(node = dag->DagNode.first; node; node= node->next)
1003 node->color= DAG_WHITE;
1005 for(node = dag->DagNode.first; node; node= node->next) {
1006 if(node->color==DAG_WHITE) {
1007 node->ancestor_count= dag_node_recurs_level(node, 0);
1011 /* check relations, and print errors */
1012 for(node = dag->DagNode.first; node; node= node->next) {
1013 for(itA= node->parent; itA; itA= itA->next) {
1014 if(itA->node->ancestor_count > node->ancestor_count) {
1015 if(node->ob && itA->node->ob) {
1016 printf("Dependency cycle detected:\n");
1017 dag_node_print_dependency_cycle(dag, itA->node, node, itA->name);
1023 /* parent relations are only needed for cycle checking, so free now */
1024 for(node = dag->DagNode.first; node; node= node->next) {
1025 while (node->parent) {
1026 itA = node->parent->next;
1027 MEM_freeN(node->parent);
1034 * MainDAG is the DAG of all objects in current scene
1035 * used only for drawing there is one also in each scene
1037 static DagForest * MainDag = NULL;
1039 DagForest *getMainDag(void)
1045 void setMainDag(DagForest *dag)
1053 * in theory we should sweep the whole array
1054 * but in our case the first node is the scene
1055 * and is linked to every other object
1057 * for general case we will need to add outer loop
1061 * ToDo : change pos kludge
1064 /* adjust levels for drawing in oops space */
1065 void graph_bfs(void)
1068 DagNodeQueue *nqueue;
1074 /* fprintf(stderr,"starting BFS \n ------------\n"); */
1075 nqueue = queue_create(DAGQUEUEALLOC);
1076 for ( i=0; i<50; i++)
1080 * dagnode.first is alway the root (scene)
1082 node = MainDag->DagNode.first;
1084 node->color = DAG_WHITE;
1085 node->BFS_dist = 9999;
1090 node = MainDag->DagNode.first;
1091 if (node->color == DAG_WHITE) {
1092 node->color = DAG_GRAY;
1094 push_queue(nqueue,node);
1095 while(nqueue->count) {
1096 node = pop_queue(nqueue);
1098 minheight = pos[node->BFS_dist];
1100 while(itA != NULL) {
1101 if((itA->node->color == DAG_WHITE) ) {
1102 itA->node->color = DAG_GRAY;
1103 itA->node->BFS_dist = node->BFS_dist + 1;
1104 itA->node->k = (float) minheight;
1105 push_queue(nqueue,itA->node);
1109 fprintf(stderr,"bfs not dag tree edge color :%i \n",itA->node->color);
1115 if (pos[node->BFS_dist] > node->k ) {
1116 pos[node->BFS_dist] += 1;
1117 node->k = (float) pos[node->BFS_dist];
1119 pos[node->BFS_dist] = (int) node->k +1;
1121 set_node_xy(node, node->BFS_dist*DEPSX*2, pos[node->BFS_dist]*DEPSY*2);
1122 node->color = DAG_BLACK;
1124 fprintf(stderr,"BFS node : %20s %i %5.0f %5.0f\n",((ID *) node->ob)->name,node->BFS_dist, node->x, node->y);
1128 queue_delete(nqueue);
1131 int pre_and_post_BFS(DagForest *dag, short mask, graph_action_func pre_func, graph_action_func post_func, void **data) {
1134 node = dag->DagNode.first;
1135 return pre_and_post_source_BFS(dag, mask, node, pre_func, post_func, data);
1139 int pre_and_post_source_BFS(DagForest *dag, short mask, DagNode *source, graph_action_func pre_func, graph_action_func post_func, void **data)
1142 DagNodeQueue *nqueue;
1145 /* fprintf(stderr,"starting BFS \n ------------\n"); */
1148 * dagnode.first is alway the root (scene)
1150 node = dag->DagNode.first;
1151 nqueue = queue_create(DAGQUEUEALLOC);
1153 node->color = DAG_WHITE;
1154 node->BFS_dist = 9999;
1159 if (node->color == DAG_WHITE) {
1160 node->color = DAG_GRAY;
1162 pre_func(node->ob,data);
1164 while(nqueue->count) {
1165 node = pop_queue(nqueue);
1168 while(itA != NULL) {
1169 if((itA->node->color == DAG_WHITE) && (itA->type & mask)) {
1170 itA->node->color = DAG_GRAY;
1171 itA->node->BFS_dist = node->BFS_dist + 1;
1172 push_queue(nqueue,itA->node);
1173 pre_func(node->ob,data);
1176 else { // back or cross edge
1181 post_func(node->ob,data);
1182 node->color = DAG_BLACK;
1184 fprintf(stderr,"BFS node : %20s %i %5.0f %5.0f\n",((ID *) node->ob)->name,node->BFS_dist, node->x, node->y);
1188 queue_delete(nqueue);
1192 /* non recursive version of DFS, return queue -- outer loop present to catch odd cases (first level cycles)*/
1193 DagNodeQueue * graph_dfs(void)
1196 DagNodeQueue *nqueue;
1197 DagNodeQueue *retqueue;
1207 *fprintf(stderr,"starting DFS \n ------------\n");
1209 nqueue = queue_create(DAGQUEUEALLOC);
1210 retqueue = queue_create(MainDag->numNodes);
1211 for ( i=0; i<50; i++)
1215 * dagnode.first is alway the root (scene)
1217 node = MainDag->DagNode.first;
1219 node->color = DAG_WHITE;
1220 node->DFS_dist = 9999;
1221 node->DFS_dvtm = node->DFS_fntm = 9999;
1228 node = MainDag->DagNode.first;
1231 if (node->color == DAG_WHITE) {
1232 node->color = DAG_GRAY;
1234 node->DFS_dvtm = time;
1236 push_stack(nqueue,node);
1238 while(nqueue->count) {
1239 //graph_print_queue(nqueue);
1242 node = get_top_node_queue(nqueue);
1244 minheight = pos[node->DFS_dist];
1247 while(itA != NULL) {
1248 if((itA->node->color == DAG_WHITE) ) {
1249 itA->node->DFS_dvtm = time;
1250 itA->node->color = DAG_GRAY;
1253 itA->node->DFS_dist = node->DFS_dist + 1;
1254 itA->node->k = (float) minheight;
1255 push_stack(nqueue,itA->node);
1259 if (itA->node->color == DAG_GRAY) { // back edge
1260 fprintf(stderr,"dfs back edge :%15s %15s \n",((ID *) node->ob)->name, ((ID *) itA->node->ob)->name);
1262 } else if (itA->node->color == DAG_BLACK) {
1264 /* already processed node but we may want later to change distance either to shorter to longer.
1265 * DFS_dist is the first encounter
1267 /*if (node->DFS_dist >= itA->node->DFS_dist)
1268 itA->node->DFS_dist = node->DFS_dist + 1;
1270 fprintf(stderr,"dfs forward or cross edge :%15s %i-%i %15s %i-%i \n",
1271 ((ID *) node->ob)->name,
1274 ((ID *) itA->node->ob)->name,
1275 itA->node->DFS_dvtm,
1276 itA->node->DFS_fntm);
1279 fprintf(stderr,"dfs unknown edge \n");
1285 node = pop_queue(nqueue);
1286 node->color = DAG_BLACK;
1288 node->DFS_fntm = time;
1290 if (node->DFS_dist > maxpos)
1291 maxpos = node->DFS_dist;
1292 if (pos[node->DFS_dist] > node->k ) {
1293 pos[node->DFS_dist] += 1;
1294 node->k = (float) pos[node->DFS_dist];
1296 pos[node->DFS_dist] = (int) node->k +1;
1298 set_node_xy(node, node->DFS_dist*DEPSX*2, pos[node->DFS_dist]*DEPSY*2);
1301 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 );
1303 push_stack(retqueue,node);
1310 // fprintf(stderr,"i size : %i \n", maxpos);
1312 queue_delete(nqueue);
1317 int pre_and_post_DFS(DagForest *dag, short mask, graph_action_func pre_func, graph_action_func post_func, void **data) {
1320 node = dag->DagNode.first;
1321 return pre_and_post_source_DFS(dag, mask, node, pre_func, post_func, data);
1324 int pre_and_post_source_DFS(DagForest *dag, short mask, DagNode *source, graph_action_func pre_func, graph_action_func post_func, void **data)
1327 DagNodeQueue *nqueue;
1333 *fprintf(stderr,"starting DFS \n ------------\n");
1335 nqueue = queue_create(DAGQUEUEALLOC);
1338 * dagnode.first is alway the root (scene)
1340 node = dag->DagNode.first;
1342 node->color = DAG_WHITE;
1343 node->DFS_dist = 9999;
1344 node->DFS_dvtm = node->DFS_fntm = 9999;
1353 if (node->color == DAG_WHITE) {
1354 node->color = DAG_GRAY;
1356 node->DFS_dvtm = time;
1358 push_stack(nqueue,node);
1359 pre_func(node->ob,data);
1361 while(nqueue->count) {
1363 node = get_top_node_queue(nqueue);
1366 while(itA != NULL) {
1367 if((itA->node->color == DAG_WHITE) && (itA->type & mask) ) {
1368 itA->node->DFS_dvtm = time;
1369 itA->node->color = DAG_GRAY;
1372 itA->node->DFS_dist = node->DFS_dist + 1;
1373 push_stack(nqueue,itA->node);
1374 pre_func(node->ob,data);
1379 if (itA->node->color == DAG_GRAY) {// back edge
1382 // else if (itA->node->color == DAG_BLACK) { // cross or forward
1389 node = pop_queue(nqueue);
1390 node->color = DAG_BLACK;
1392 node->DFS_fntm = time;
1394 post_func(node->ob,data);
1400 queue_delete(nqueue);
1405 // used to get the obs owning a datablock
1406 struct DagNodeQueue *get_obparents(struct DagForest *dag, void *ob)
1408 DagNode * node, *node1;
1409 DagNodeQueue *nqueue;
1412 node = dag_find_node(dag,ob);
1416 else if (node->ancestor_count == 1) { // simple case
1417 nqueue = queue_create(1);
1418 push_queue(nqueue,node);
1419 } else { // need to go over the whole dag for adj list
1420 nqueue = queue_create(node->ancestor_count);
1422 node1 = dag->DagNode.first;
1424 if (node1->DFS_fntm > node->DFS_fntm) { // a parent is finished after child. must check adj list
1426 while(itA != NULL) {
1427 if ((itA->node == node) && (itA->type == DAG_RL_DATA)) {
1428 push_queue(nqueue,node);
1433 node1 = node1->next;
1439 struct DagNodeQueue *get_first_ancestors(struct DagForest *dag, void *ob)
1441 DagNode * node, *node1;
1442 DagNodeQueue *nqueue;
1445 node = dag_find_node(dag,ob);
1447 // need to go over the whole dag for adj list
1448 nqueue = queue_create(node->ancestor_count);
1450 node1 = dag->DagNode.first;
1452 if (node1->DFS_fntm > node->DFS_fntm) {
1454 while(itA != NULL) {
1455 if (itA->node == node) {
1456 push_queue(nqueue,node);
1461 node1 = node1->next;
1467 // standard DFS list
1468 struct DagNodeQueue *get_all_childs(struct DagForest *dag, void *ob)
1471 DagNodeQueue *nqueue;
1472 DagNodeQueue *retqueue;
1477 nqueue = queue_create(DAGQUEUEALLOC);
1478 retqueue = queue_create(dag->numNodes); // was MainDag... why? (ton)
1480 node = dag->DagNode.first;
1482 node->color = DAG_WHITE;
1488 node = dag_find_node(dag, ob); // could be done in loop above (ton)
1489 if(node) { // can be null for newly added objects
1491 node->color = DAG_GRAY;
1493 push_stack(nqueue,node);
1495 while(nqueue->count) {
1498 node = get_top_node_queue(nqueue);
1501 while(itA != NULL) {
1502 if((itA->node->color == DAG_WHITE) ) {
1503 itA->node->DFS_dvtm = time;
1504 itA->node->color = DAG_GRAY;
1507 push_stack(nqueue,itA->node);
1515 node = pop_queue(nqueue);
1516 node->color = DAG_BLACK;
1519 push_stack(retqueue,node);
1523 queue_delete(nqueue);
1528 short are_obs_related(struct DagForest *dag, void *ob1, void *ob2) {
1532 node = dag_find_node(dag, ob1);
1535 while(itA != NULL) {
1536 if((itA->node->ob == ob2) ) {
1537 return itA->node->type;
1541 return DAG_NO_RELATION;
1544 int is_acyclic( DagForest *dag) {
1545 return dag->is_acyclic;
1548 void set_node_xy(DagNode *node, float x, float y)
1555 /* debug test functions */
1557 void graph_print_queue(DagNodeQueue *nqueue)
1559 DagNodeQueueElem *queueElem;
1561 queueElem = nqueue->first;
1563 fprintf(stderr,"** %s %i %i-%i ",((ID *) queueElem->node->ob)->name,queueElem->node->color,queueElem->node->DFS_dvtm,queueElem->node->DFS_fntm);
1564 queueElem = queueElem->next;
1566 fprintf(stderr,"\n");
1569 void graph_print_queue_dist(DagNodeQueue *nqueue)
1571 DagNodeQueueElem *queueElem;
1574 queueElem = nqueue->first;
1575 max = queueElem->node->DFS_fntm;
1578 fprintf(stderr,"** %25s %2.2i-%2.2i ",((ID *) queueElem->node->ob)->name,queueElem->node->DFS_dvtm,queueElem->node->DFS_fntm);
1579 while (count < queueElem->node->DFS_dvtm-1) { fputc(' ',stderr); count++;}
1581 while (count < queueElem->node->DFS_fntm-2) { fputc('-',stderr); count++;}
1585 queueElem = queueElem->next;
1587 fprintf(stderr,"\n");
1590 void graph_print_adj_list(void)
1595 node = (getMainDag())->DagNode.first;
1597 fprintf(stderr,"node : %s col: %i",((ID *) node->ob)->name, node->color);
1600 fprintf(stderr,"-- %s ",((ID *) itA->node->ob)->name);
1604 fprintf(stderr,"\n");
1609 /* ************************ API *********************** */
1611 /* groups with objects in this scene need to be put in the right order as well */
1612 static void scene_sort_groups(Scene *sce)
1619 /* test; are group objects all in this scene? */
1620 for(ob= G.main->object.first; ob; ob= ob->id.next) {
1621 ob->id.flag &= ~LIB_DOIT;
1622 ob->id.newid= NULL; /* newid abuse for GroupObject */
1624 for(base = sce->base.first; base; base= base->next)
1625 base->object->id.flag |= LIB_DOIT;
1627 for(group= G.main->group.first; group; group= group->id.next) {
1628 for(go= group->gobject.first; go; go= go->next) {
1629 if((go->ob->id.flag & LIB_DOIT)==0)
1632 /* this group is entirely in this scene */
1634 ListBase listb= {NULL, NULL};
1636 for(go= group->gobject.first; go; go= go->next)
1637 go->ob->id.newid= (ID *)go;
1639 /* in order of sorted bases we reinsert group objects */
1640 for(base = sce->base.first; base; base= base->next) {
1642 if(base->object->id.newid) {
1643 go= (GroupObject *)base->object->id.newid;
1644 base->object->id.newid= NULL;
1645 BLI_remlink( &group->gobject, go);
1646 BLI_addtail( &listb, go);
1649 /* copy the newly sorted listbase */
1650 group->gobject= listb;
1655 /* sort the base list on dependency order */
1656 void DAG_scene_sort(struct Scene *sce)
1659 DagNodeQueue *nqueue;
1666 tempbase.first= tempbase.last= NULL;
1668 build_dag(sce, DAG_RL_ALL_BUT_DATA);
1670 dag_check_cycle(sce->theDag);
1672 nqueue = queue_create(DAGQUEUEALLOC);
1674 for(node = sce->theDag->DagNode.first; node; node= node->next) {
1675 node->color = DAG_WHITE;
1680 node = sce->theDag->DagNode.first;
1682 node->color = DAG_GRAY;
1684 push_stack(nqueue,node);
1686 while(nqueue->count) {
1689 node = get_top_node_queue(nqueue);
1692 while(itA != NULL) {
1693 if((itA->node->color == DAG_WHITE) ) {
1694 itA->node->DFS_dvtm = time;
1695 itA->node->color = DAG_GRAY;
1698 push_stack(nqueue,itA->node);
1707 node = pop_queue(nqueue);
1708 if (node->ob == sce) // we are done
1710 node->color = DAG_BLACK;
1713 base = sce->base.first;
1714 while (base && base->object != node->ob)
1717 BLI_remlink(&sce->base,base);
1718 BLI_addhead(&tempbase,base);
1724 // temporal correction for circular dependancies
1725 base = sce->base.first;
1727 BLI_remlink(&sce->base,base);
1728 BLI_addhead(&tempbase,base);
1730 printf("cyclic %s\n", base->object->id.name);
1731 base = sce->base.first;
1734 sce->base = tempbase;
1735 queue_delete(nqueue);
1737 /* all groups with objects in this scene gets resorted too */
1738 scene_sort_groups(sce);
1741 printf("\nordered\n");
1742 for(base = sce->base.first; base; base= base->next) {
1743 printf(" %s\n", base->object->id.name);
1747 sce->recalc |= SCE_PRV_CHANGED; /* test for 3d preview */
1750 /* node was checked to have lasttime != curtime and is if type ID_OB */
1751 static void flush_update_node(DagNode *node, unsigned int layer, int curtime)
1755 int oldflag, changed=0;
1756 unsigned int all_layer;
1758 node->lasttime= curtime;
1761 if(ob && (ob->recalc & OB_RECALC)) {
1764 /* got an object node that changes, now check relations */
1765 for(itA = node->child; itA; itA= itA->next) {
1766 all_layer |= itA->lay;
1767 /* the relationship is visible */
1768 if((itA->lay & layer) || (itA->node->ob == G.obedit)) {
1769 if(itA->node->type==ID_OB) {
1771 oldflag= obc->recalc;
1773 /* got a ob->obc relation, now check if flag needs flush */
1774 if(ob->recalc & OB_RECALC_OB) {
1775 if(itA->type & DAG_RL_OB_OB) {
1776 //printf("ob %s changes ob %s\n", ob->id.name, obc->id.name);
1777 obc->recalc |= OB_RECALC_OB;
1779 if(itA->type & DAG_RL_OB_DATA) {
1780 //printf("ob %s changes obdata %s\n", ob->id.name, obc->id.name);
1781 obc->recalc |= OB_RECALC_DATA;
1784 if(ob->recalc & OB_RECALC_DATA) {
1785 if(itA->type & DAG_RL_DATA_OB) {
1786 //printf("obdata %s changes ob %s\n", ob->id.name, obc->id.name);
1787 obc->recalc |= OB_RECALC_OB;
1789 if(itA->type & DAG_RL_DATA_DATA) {
1790 //printf("obdata %s changes obdata %s\n", ob->id.name, obc->id.name);
1791 obc->recalc |= OB_RECALC_DATA;
1794 if(oldflag!=obc->recalc) changed= 1;
1798 /* even nicer, we can clear recalc flags... */
1799 if((all_layer & layer)==0 && (ob != G.obedit)) {
1800 /* but existing displaylists or derivedmesh should be freed */
1801 if(ob->recalc & OB_RECALC_DATA)
1802 object_free_display(ob);
1804 ob->recalc &= ~OB_RECALC;
1808 /* check case where child changes and parent forcing obdata to change */
1809 /* should be done regardless if this ob has recalc set */
1810 /* could merge this in with loop above...? (ton) */
1811 for(itA = node->child; itA; itA= itA->next) {
1812 /* the relationship is visible */
1813 if((itA->lay & layer) || (itA->node->ob == G.obedit)) {
1814 if(itA->node->type==ID_OB) {
1817 if((obc->recalc & OB_RECALC)==OB_RECALC_OB) {
1818 /* parent has deforming info */
1819 if(itA->type & (DAG_RL_OB_DATA|DAG_RL_DATA_DATA)) {
1820 // printf("parent %s changes ob %s\n", ob->id.name, obc->id.name);
1821 obc->recalc |= OB_RECALC_DATA;
1828 /* we only go deeper if node not checked or something changed */
1829 for(itA = node->child; itA; itA= itA->next) {
1830 if(changed || itA->node->lasttime!=curtime)
1831 flush_update_node(itA->node, layer, curtime);
1836 /* node was checked to have lasttime != curtime , and is of type ID_OB */
1837 static unsigned int flush_layer_node(Scene *sce, DagNode *node, int curtime)
1842 node->lasttime= curtime;
1844 for(base= sce->base.first; base; base= base->next) {
1845 if(node->ob == base->object) {
1846 node->lay= ((Object *)node->ob)->lay;
1851 for(itA = node->child; itA; itA= itA->next) {
1852 if(itA->node->type==ID_OB) {
1853 if(itA->node->lasttime!=curtime) {
1854 itA->lay= flush_layer_node(sce, itA->node, curtime); // lay is only set once for each relation
1856 else itA->lay= itA->node->lay;
1858 node->lay |= itA->lay;
1865 /* node was checked to have lasttime != curtime , and is of type ID_OB */
1866 static void flush_pointcache_reset(DagNode *node, int curtime, int reset)
1871 node->lasttime= curtime;
1873 for(itA = node->child; itA; itA= itA->next) {
1874 if(itA->node->type==ID_OB) {
1875 if(itA->node->lasttime!=curtime) {
1876 ob= (Object*)(node->ob);
1878 if(reset || (ob->recalc & OB_RECALC)) {
1879 if(BKE_ptcache_object_reset(ob, PTCACHE_RESET_DEPSGRAPH))
1880 ob->recalc |= OB_RECALC_DATA;
1882 flush_pointcache_reset(itA->node, curtime, 1);
1885 flush_pointcache_reset(itA->node, curtime, 0);
1891 /* flushes all recalc flags in objects down the dependency tree */
1892 void DAG_scene_flush_update(Scene *sce, unsigned int lay, int time)
1899 if(sce->theDag==NULL) {
1900 printf("DAG zero... not allowed to happen!\n");
1901 DAG_scene_sort(sce);
1904 firstnode= sce->theDag->DagNode.first; // always scene node
1906 for(itA = firstnode->child; itA; itA= itA->next)
1909 /* first we flush the layer flags */
1910 sce->theDag->time++; // so we know which nodes were accessed
1911 lasttime= sce->theDag->time;
1913 for(itA = firstnode->child; itA; itA= itA->next)
1914 if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB)
1915 flush_layer_node(sce, itA->node, lasttime);
1917 /* then we use the relationships + layer info to flush update events */
1918 sce->theDag->time++; // so we know which nodes were accessed
1919 lasttime= sce->theDag->time;
1920 for(itA = firstnode->child; itA; itA= itA->next)
1921 if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB)
1922 flush_update_node(itA->node, lay, lasttime);
1924 /* if update is not due to time change, do pointcache clears */
1926 sce->theDag->time++; // so we know which nodes were accessed
1927 lasttime= sce->theDag->time;
1928 for(itA = firstnode->child; itA; itA= itA->next) {
1929 if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB) {
1930 ob= (Object*)(itA->node->ob);
1932 if(ob->recalc & OB_RECALC) {
1933 if(BKE_ptcache_object_reset(ob, PTCACHE_RESET_DEPSGRAPH))
1934 ob->recalc |= OB_RECALC_DATA;
1936 flush_pointcache_reset(itA->node, lasttime, 1);
1939 flush_pointcache_reset(itA->node, lasttime, 0);
1945 static int object_modifiers_use_time(Object *ob)
1949 for (md=ob->modifiers.first; md; md=md->next)
1950 if (modifier_dependsOnTime(md))
1956 static int exists_channel(Object *ob, char *name)
1958 bActionStrip *strip;
1961 if(get_action_channel(ob->action, name))
1964 for (strip=ob->nlastrips.first; strip; strip=strip->next)
1965 if(get_action_channel(strip->act, name))
1970 static void dag_object_time_update_flags(Object *ob)
1973 if(ob->ipo) ob->recalc |= OB_RECALC_OB;
1974 else if(ob->constraints.first) {
1976 for (con = ob->constraints.first; con; con=con->next) {
1977 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
1978 ListBase targets = {NULL, NULL};
1979 bConstraintTarget *ct;
1981 if (cti && cti->get_constraint_targets) {
1982 cti->get_constraint_targets(con, &targets);
1984 for (ct= targets.first; ct; ct= ct->next) {
1986 ob->recalc |= OB_RECALC_OB;
1991 if (cti->flush_constraint_targets)
1992 cti->flush_constraint_targets(con, &targets, 1);
1997 if(ob->scriptlink.totscript) ob->recalc |= OB_RECALC_OB;
2000 /* motion path or bone child */
2001 if(ob->parent->type==OB_CURVE || ob->parent->type==OB_ARMATURE) ob->recalc |= OB_RECALC_OB;
2004 if(ob->action || ob->nlastrips.first) {
2005 /* since actions now are mixed, we set the recalcs on the safe side */
2006 ob->recalc |= OB_RECALC_OB;
2007 if(ob->type==OB_ARMATURE)
2008 ob->recalc |= OB_RECALC_DATA;
2009 else if(exists_channel(ob, "Shape"))
2010 ob->recalc |= OB_RECALC_DATA;
2011 else if(ob->dup_group) {
2012 bActionStrip *strip;
2013 /* this case is for groups with nla, whilst nla target has no action or nla */
2014 for(strip= ob->nlastrips.first; strip; strip= strip->next) {
2016 strip->object->recalc |= OB_RECALC;
2021 if(object_modifiers_use_time(ob)) ob->recalc |= OB_RECALC_DATA;
2022 if((ob->pose) && (ob->pose->flag & POSE_CONSTRAINTS_TIMEDEPEND)) ob->recalc |= OB_RECALC_DATA;
2033 if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
2034 ob->recalc |= OB_RECALC_DATA;
2035 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
2038 if(ob->particlesystem.first)
2039 ob->recalc |= OB_RECALC_DATA;
2045 if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
2046 ob->recalc |= OB_RECALC_DATA;
2047 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
2053 if(cu->nurb.first==NULL && cu->str && cu->vfont)
2054 ob->recalc |= OB_RECALC_DATA;
2059 if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
2060 ob->recalc |= OB_RECALC_DATA;
2061 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
2066 if(ob->transflag & OB_DUPLI) ob->recalc |= OB_RECALC_DATA;
2070 if(ob->particlesystem.first) {
2071 ParticleSystem *psys= ob->particlesystem.first;
2073 for(; psys; psys=psys->next) {
2074 if(psys_check_enabled(ob, psys)) {
2075 ob->recalc |= OB_RECALC_DATA;
2083 /* flag all objects that need recalc, for changes in time for example */
2084 void DAG_scene_update_flags(Scene *scene, unsigned int lay)
2092 /* set ob flags where animated systems are */
2093 for(SETLOOPER(scene, base)) {
2096 /* now if DagNode were part of base, the node->lay could be checked... */
2097 /* we do all now, since the scene_flush checks layers and clears recalc flags even */
2098 dag_object_time_update_flags(ob);
2100 /* handled in next loop */
2102 ob->dup_group->id.flag |= LIB_DOIT;
2105 /* we do groups each once */
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 dag_object_time_update_flags(go->ob);
2114 for(sce= scene; sce; sce= sce->set)
2115 DAG_scene_flush_update(sce, lay, 1);
2117 /* test: set time flag, to disable baked systems to update */
2118 for(SETLOOPER(scene, base)) {
2121 ob->recalc |= OB_RECALC_TIME;
2124 /* hrmf... an exception to look at once, for invisible camera object we do it over */
2126 dag_object_time_update_flags(scene->camera);
2128 /* and store the info in groupobject */
2129 for(group= G.main->group.first; group; group= group->id.next) {
2130 if(group->id.flag & LIB_DOIT) {
2131 for(go= group->gobject.first; go; go= go->next) {
2132 go->recalc= go->ob->recalc;
2133 // printf("ob %s recalc %d\n", go->ob->id.name, go->recalc);
2135 group->id.flag &= ~LIB_DOIT;
2141 /* for depgraph updating, all layers visible in a screen */
2142 /* this is a copy from editscreen.c... I need to think over a more proper solution for this */
2143 /* probably the DAG_object_flush_update() should give layer too? */
2144 /* or some kind of dag context... (DAG_set_layer) */
2145 static unsigned int dag_screen_view3d_layers(void)
2150 for(sa= G.curscreen->areabase.first; sa; sa= sa->next) {
2151 if(sa->spacetype==SPACE_VIEW3D)
2152 layer |= ((View3D *)sa->spacedata.first)->lay;
2158 /* flag this object and all its relations to recalc */
2159 /* if you need to do more objects, tag object yourself and
2160 use DAG_scene_flush_update() in end */
2161 void DAG_object_flush_update(Scene *sce, Object *ob, short flag)
2164 if(ob==NULL || sce->theDag==NULL) return;
2167 BKE_ptcache_object_reset(ob, PTCACHE_RESET_DEPSGRAPH);
2169 /* all users of this ob->data should be checked */
2170 /* BUT! displists for curves are still only on cu */
2171 if(flag & OB_RECALC_DATA) {
2172 if(ob->type!=OB_CURVE && ob->type!=OB_SURF) {
2174 if(id && id->us>1) {
2175 /* except when there's a key and shapes are locked */
2176 if(ob_get_key(ob) && (ob->shapeflag & (OB_SHAPE_LOCK|OB_SHAPE_TEMPLOCK)));
2179 for (obt=G.main->object.first; obt; obt= obt->id.next) {
2180 if (obt != ob && obt->data==ob->data) {
2181 obt->recalc |= OB_RECALC_DATA;
2182 BKE_ptcache_object_reset(obt, PTCACHE_RESET_DEPSGRAPH);
2191 DAG_scene_flush_update(sce, dag_screen_view3d_layers(), 0);
2193 DAG_scene_flush_update(sce, sce->lay, 0);
2196 /* recursively descends tree, each node only checked once */
2197 /* node is checked to be of type object */
2198 static int parent_check_node(DagNode *node, int curtime)
2202 node->lasttime= curtime;
2204 if(node->color==DAG_GRAY)
2207 for(itA = node->child; itA; itA= itA->next) {
2208 if(itA->node->type==ID_OB) {
2210 if(itA->node->color==DAG_GRAY)
2213 /* descend if not done */
2214 if(itA->node->lasttime!=curtime) {
2215 itA->node->color= parent_check_node(itA->node, curtime);
2217 if(itA->node->color==DAG_GRAY)
2226 /* all nodes that influence this object get tagged, for calculating the exact
2227 position of this object at a given timeframe */
2228 void DAG_object_update_flags(Scene *sce, Object *ob, unsigned int lay)
2233 /* tag nodes unchecked */
2234 for(node = sce->theDag->DagNode.first; node; node= node->next)
2235 node->color = DAG_WHITE;
2237 node= dag_find_node(sce->theDag, ob);
2239 /* object not in scene? then handle group exception. needs to be dagged once too */
2242 while( (group = find_group(ob, group)) ) {
2244 /* primitive; tag all... this call helps building groups for particles */
2245 for(go= group->gobject.first; go; go= go->next)
2246 go->ob->recalc= OB_RECALC;
2251 node->color = DAG_GRAY;
2253 sce->theDag->time++;
2254 node= sce->theDag->DagNode.first;
2255 for(itA = node->child; itA; itA= itA->next) {
2256 if(itA->node->type==ID_OB && itA->node->lasttime!=sce->theDag->time)
2257 itA->node->color= parent_check_node(itA->node, sce->theDag->time);
2260 /* set recalcs and flushes */
2261 DAG_scene_update_flags(sce, lay);
2263 /* now we clear recalcs, unless color is set */
2264 for(node = sce->theDag->DagNode.first; node; node= node->next) {
2265 if(node->type==ID_OB && node->color==DAG_WHITE) {
2266 Object *ob= node->ob;
2273 /* ******************* DAG FOR ARMATURE POSE ***************** */
2275 /* we assume its an armature with pose */
2276 void DAG_pose_sort(Object *ob)
2278 bPose *pose= ob->pose;
2279 bPoseChannel *pchan;
2282 DagNode *node2, *node3;
2285 DagNodeQueue *nqueue;
2291 ugly_hack_sorry= 0; // no ID structs
2293 rootnode = dag_add_node(dag, NULL); // node->ob becomes NULL
2295 /* we add the hierarchy and the constraints */
2296 for(pchan = pose->chanbase.first; pchan; pchan= pchan->next) {
2299 node = dag_get_node(dag, pchan);
2302 node2 = dag_get_node(dag, pchan->parent);
2303 dag_add_relation(dag, node2, node, 0, "Parent Relation");
2306 for (con = pchan->constraints.first; con; con=con->next) {
2307 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
2308 ListBase targets = {NULL, NULL};
2309 bConstraintTarget *ct;
2313 for(icu= con->ipo->curve.first; icu; icu= icu->next) {
2314 /* icu->driver->ob should actually point to ob->proxy if it
2315 * is a proxy, but since it wasn't set correct it older
2316 * files comparing with ob->proxy makes it work for those */
2317 if(icu->driver && (icu->driver->ob==ob || icu->driver->ob==ob->proxy)) {
2318 bPoseChannel *target= get_pose_channel(ob->pose, icu->driver->name);
2320 node2 = dag_get_node(dag, target);
2321 dag_add_relation(dag, node2, node, 0, "Ipo Driver");
2323 /* uncommented this line, results in dependencies
2324 * not being added properly for this constraint,
2325 * what is the purpose of this? - brecht */
2326 /*cti= NULL;*/ /* trick to get next loop skipped */
2332 if (cti && cti->get_constraint_targets) {
2333 cti->get_constraint_targets(con, &targets);
2335 for (ct= targets.first; ct; ct= ct->next) {
2336 if (ct->tar==ob && ct->subtarget[0]) {
2337 bPoseChannel *target= get_pose_channel(ob->pose, ct->subtarget);
2339 node2= dag_get_node(dag, target);
2340 dag_add_relation(dag, node2, node, 0, "IK Constraint");
2342 if (con->type==CONSTRAINT_TYPE_KINEMATIC) {
2343 bKinematicConstraint *data = (bKinematicConstraint *)con->data;
2344 bPoseChannel *parchan;
2347 /* exclude tip from chain? */
2348 if(!(data->flag & CONSTRAINT_IK_TIP))
2349 parchan= pchan->parent;
2353 /* Walk to the chain's root */
2355 node3= dag_get_node(dag, parchan);
2356 dag_add_relation(dag, node2, node3, 0, "IK Constraint");
2359 if (segcount==data->rootbone || segcount>255) break; // 255 is weak
2360 parchan= parchan->parent;
2367 if (cti->flush_constraint_targets)
2368 cti->flush_constraint_targets(con, &targets, 1);
2371 if (addtoroot == 1 ) {
2372 dag_add_relation(dag, rootnode, node, 0, "Root Bone Relation");
2376 dag_check_cycle(dag);
2378 /* now we try to sort... */
2379 tempbase.first= tempbase.last= NULL;
2381 nqueue = queue_create(DAGQUEUEALLOC);
2383 /* tag nodes unchecked */
2384 for(node = dag->DagNode.first; node; node= node->next)
2385 node->color = DAG_WHITE;
2387 node = dag->DagNode.first;
2389 node->color = DAG_GRAY;
2390 push_stack(nqueue, node);
2392 while(nqueue->count) {
2395 node = get_top_node_queue(nqueue);
2398 while(itA != NULL) {
2399 if((itA->node->color == DAG_WHITE) ) {
2400 itA->node->color = DAG_GRAY;
2401 push_stack(nqueue,itA->node);
2410 node = pop_queue(nqueue);
2411 if (node->ob == NULL) // we are done
2413 node->color = DAG_BLACK;
2415 /* put node in new list */
2416 BLI_remlink(&pose->chanbase, node->ob);
2417 BLI_addhead(&tempbase, node->ob);
2422 // temporal correction for circular dependancies
2423 while(pose->chanbase.first) {
2424 pchan= pose->chanbase.first;
2425 BLI_remlink(&pose->chanbase, pchan);
2426 BLI_addhead(&tempbase, pchan);
2428 printf("cyclic %s\n", pchan->name);
2431 pose->chanbase = tempbase;
2432 queue_delete(nqueue);
2434 // printf("\nordered\n");
2435 // for(pchan = pose->chanbase.first; pchan; pchan= pchan->next) {
2436 // printf(" %s\n", pchan->name);