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 #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 static void dag_add_driver_relation(Ipo *ipo, DagForest *dag, DagNode *node, int isdata)
309 for(icu= ipo->curve.first; icu; icu= icu->next) {
312 if (icu->driver->type == IPO_DRIVER_TYPE_PYTHON) {
314 if ((icu->driver->flag & IPO_DRIVER_FLAG_INVALID) || (icu->driver->name[0] == '\0'))
315 continue; /* empty or invalid expression */
317 /* now we need refs to all objects mentioned in this
318 * pydriver expression, to call 'dag_add_relation'
319 * for each of them */
320 Object **obarray = BPY_pydriver_get_objects(icu->driver);
322 Object *ob, **oba = obarray;
326 node1 = dag_get_node(dag, ob);
327 if (ob->type == OB_ARMATURE)
328 dag_add_relation(dag, node1, node, isdata?DAG_RL_DATA_DATA:DAG_RL_DATA_OB, "Python Ipo Driver");
330 dag_add_relation(dag, node1, node, isdata?DAG_RL_OB_DATA:DAG_RL_OB_OB, "Python Ipo Driver");
338 else if (icu->driver->ob) {
339 node1 = dag_get_node(dag, icu->driver->ob);
340 if(icu->driver->blocktype==ID_AR)
341 dag_add_relation(dag, node1, node, isdata?DAG_RL_DATA_DATA:DAG_RL_DATA_OB, "Ipo Driver");
343 dag_add_relation(dag, node1, node, isdata?DAG_RL_OB_DATA:DAG_RL_OB_OB, "Ipo Driver");
349 static void dag_add_collision_field_relation(DagForest *dag, Object *ob, DagNode *node)
354 // would be nice to have a list of colliders here
355 // so for now walk all objects in scene check 'same layer rule'
356 for(base = G.scene->base.first; base; base= base->next) {
357 if((base->lay & ob->lay) && base->object->pd) {
358 Object *ob1= base->object;
359 if((ob1->pd->deflect || ob1->pd->forcefield) && (ob1 != ob)) {
360 node2 = dag_get_node(dag, ob1);
361 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Field Collision");
367 static void build_dag_object(DagForest *dag, DagNode *scenenode, Object *ob, int mask)
370 bConstraintChannel *conchan;
375 ParticleSystem *psys;
378 node = dag_get_node(dag, ob);
380 if ((ob->data) && (mask&DAG_RL_DATA)) {
381 node2 = dag_get_node(dag,ob->data);
382 dag_add_relation(dag,node,node2,DAG_RL_DATA, "Object-Data Relation");
383 node2->first_ancestor = ob;
384 node2->ancestor_count += 1;
387 if (ob->type == OB_ARMATURE) {
392 for (pchan = ob->pose->chanbase.first; pchan; pchan=pchan->next) {
393 for (con = pchan->constraints.first; con; con=con->next) {
394 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
395 ListBase targets = {NULL, NULL};
396 bConstraintTarget *ct;
398 if (cti && cti->get_constraint_targets) {
399 cti->get_constraint_targets(con, &targets);
401 for (ct= targets.first; ct; ct= ct->next) {
402 if (ct->tar && ct->tar != ob) {
403 // fprintf(stderr,"armature %s target :%s \n", ob->id.name, target->id.name);
404 node3 = dag_get_node(dag, ct->tar);
406 if (ct->subtarget[0])
407 dag_add_relation(dag,node3,node, DAG_RL_OB_DATA|DAG_RL_DATA_DATA, cti->name);
408 else if(ELEM(con->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO))
409 dag_add_relation(dag,node3,node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, cti->name);
411 dag_add_relation(dag,node3,node, DAG_RL_OB_DATA, cti->name);
415 if (cti->flush_constraint_targets)
416 cti->flush_constraint_targets(con, &targets, 1);
424 /* driver dependencies, nla modifiers */
426 dag_add_driver_relation(ob->ipo, dag, node, 0);
430 dag_add_driver_relation(key->ipo, dag, node, 1);
432 for (conchan=ob->constraintChannels.first; conchan; conchan=conchan->next)
434 dag_add_driver_relation(conchan->ipo, dag, node, 0);
437 bActionChannel *chan;
438 for (chan = ob->action->chanbase.first; chan; chan=chan->next){
440 dag_add_driver_relation(chan->ipo, dag, node, 1);
441 for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next)
443 dag_add_driver_relation(conchan->ipo, dag, node, 1);
446 if(ob->nlastrips.first) {
448 bActionChannel *chan;
449 for(strip= ob->nlastrips.first; strip; strip= strip->next) {
450 if(strip->act && strip->act!=ob->action)
451 for (chan = strip->act->chanbase.first; chan; chan=chan->next)
453 dag_add_driver_relation(chan->ipo, dag, node, 1);
454 if(strip->modifiers.first) {
455 bActionModifier *amod;
456 for(amod= strip->modifiers.first; amod; amod= amod->next) {
458 node2 = dag_get_node(dag, amod->ob);
459 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "NLA Strip Modifier");
465 if (ob->modifiers.first) {
468 for(md=ob->modifiers.first; md; md=md->next) {
469 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
471 if (mti->updateDepgraph) mti->updateDepgraph(md, dag, ob, node);
475 node2 = dag_get_node(dag,ob->parent);
477 switch(ob->partype) {
479 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Parent");
481 case PARVERT1: case PARVERT3: case PARBONE:
482 dag_add_relation(dag,node2,node,DAG_RL_DATA_OB|DAG_RL_OB_OB, "Vertex Parent");
485 if(ob->parent->type==OB_LATTICE)
486 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Lattice Parent");
487 else if(ob->parent->type==OB_CURVE) {
488 Curve *cu= ob->parent->data;
489 if(cu->flag & CU_PATH)
490 dag_add_relation(dag,node2,node,DAG_RL_DATA_OB|DAG_RL_OB_OB, "Curve Parent");
492 dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Curve Parent");
495 dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Curve Parent");
497 /* exception case: parent is duplivert */
498 if(ob->type==OB_MBALL && (ob->parent->transflag & OB_DUPLIVERTS)) {
499 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Duplivert");
505 node2 = dag_get_node(dag,ob->track);
506 dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Track To");
510 node2 = dag_get_node(dag, ob->proxy);
511 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Proxy");
512 /* inverted relation, so addtoroot shouldn't be set to zero */
514 if (ob->type==OB_CAMERA) {
515 Camera *cam = (Camera *)ob->data;
517 dag_add_driver_relation(cam->ipo, dag, node, 1);
520 node2 = dag_get_node(dag, cam->dof_ob);
521 dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Camera DoF");
524 if (ob->type==OB_LAMP) {
525 Lamp *la = (Lamp *)ob->data;
527 dag_add_driver_relation(la->ipo, dag, node, 1);
530 if (ob->transflag & OB_DUPLI) {
531 if((ob->transflag & OB_DUPLIGROUP) && ob->dup_group) {
533 for(go= ob->dup_group->gobject.first; go; go= go->next) {
535 node2 = dag_get_node(dag, go->ob);
536 /* node2 changes node1, this keeps animations updated in groups?? not logical? */
537 dag_add_relation(dag, node2, node, DAG_RL_OB_OB, "Dupligroup");
543 /* softbody collision */
544 if((ob->type==OB_MESH) || (ob->type==OB_CURVE) || (ob->type==OB_LATTICE))
545 if(modifiers_isSoftbodyEnabled(ob) || modifiers_isClothEnabled(ob))
546 dag_add_collision_field_relation(dag, ob, node);
548 if (ob->type==OB_MBALL) {
549 Object *mom= find_basis_mball(ob);
551 node2 = dag_get_node(dag, mom);
552 dag_add_relation(dag,node,node2,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Metaball"); // mom depends on children!
555 else if (ob->type==OB_CURVE) {
558 node2 = dag_get_node(dag, cu->bevobj);
559 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Curve Bevel");
562 node2 = dag_get_node(dag, cu->taperobj);
563 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Curve Taper");
566 dag_add_driver_relation(cu->ipo, dag, node, 1);
569 else if(ob->type==OB_FONT) {
571 if(cu->textoncurve) {
572 node2 = dag_get_node(dag, cu->textoncurve);
573 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Texture On Curve");
577 psys= ob->particlesystem.first;
579 ParticleEffectorCache *nec;
582 for(; psys; psys=psys->next) {
583 ParticleSettings *part= psys->part;
585 dag_add_relation(dag, node, node, DAG_RL_OB_DATA, "Particle-Object Relation");
587 if(part->phystype==PART_PHYS_KEYED && psys->keyed_ob &&
588 BLI_findlink(&psys->keyed_ob->particlesystem,psys->keyed_psys-1)) {
589 node2 = dag_get_node(dag, psys->keyed_ob);
590 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA, "Particle Keyed Physics");
593 if(part->draw_as == PART_DRAW_OB && part->dup_ob) {
594 node2 = dag_get_node(dag, part->dup_ob);
595 dag_add_relation(dag, node, node2, DAG_RL_OB_OB, "Particle Object Visualisation");
596 if(part->dup_ob->type == OB_MBALL)
597 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA, "Particle Object Visualisation");
600 if(part->draw_as == PART_DRAW_GR && part->dup_group) {
601 for(go=part->dup_group->gobject.first; go; go=go->next) {
602 node2 = dag_get_node(dag, go->ob);
603 dag_add_relation(dag, node, node2, DAG_RL_OB_OB, "Particle Group Visualisation");
607 if(psys->effectors.first)
608 psys_end_effectors(psys);
609 psys_init_effectors(ob,psys->part->eff_group,psys);
611 if(psys->effectors.first) {
612 for(nec= psys->effectors.first; nec; nec= nec->next) {
613 Object *ob1= nec->ob;
615 if(nec->type & PSYS_EC_EFFECTOR) {
616 node2 = dag_get_node(dag, ob1);
617 if(ob1->pd->forcefield==PFIELD_GUIDE)
618 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Particle Field");
620 dag_add_relation(dag, node2, node, DAG_RL_OB_DATA, "Particle Field");
622 else if(nec->type & PSYS_EC_DEFLECT) {
623 node2 = dag_get_node(dag, ob1);
624 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Particle Collision");
626 else if(nec->type & PSYS_EC_PARTICLE) {
627 node2 = dag_get_node(dag, ob1);
628 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA, "Particle Field");
631 if(nec->type & PSYS_EC_REACTOR) {
632 node2 = dag_get_node(dag, ob1);
633 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA, "Particle Reactor");
640 for (con = ob->constraints.first; con; con=con->next) {
641 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
642 ListBase targets = {NULL, NULL};
643 bConstraintTarget *ct;
645 if (cti && cti->get_constraint_targets) {
646 cti->get_constraint_targets(con, &targets);
648 for (ct= targets.first; ct; ct= ct->next) {
656 node2 = dag_get_node(dag, obt);
657 if (ELEM(con->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO))
658 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB|DAG_RL_OB_OB, cti->name);
660 if (ELEM3(obt->type, OB_ARMATURE, OB_MESH, OB_LATTICE) && (ct->subtarget[0]))
661 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB|DAG_RL_OB_OB, cti->name);
663 dag_add_relation(dag, node2, node, DAG_RL_OB_OB, cti->name);
668 if (cti->flush_constraint_targets)
669 cti->flush_constraint_targets(con, &targets, 1);
674 dag_add_relation(dag,scenenode,node,DAG_RL_SCENE, "Scene Relation");
677 struct DagForest *build_dag(struct Scene *sce, short mask)
697 /* add base node for scene. scene is always the first node in DAG */
698 scenenode = dag_add_node(dag, sce);
700 /* add current scene objects */
701 for(base = sce->base.first; base; base= base->next) {
704 build_dag_object(dag, scenenode, ob, mask);
706 build_dag_object(dag, scenenode, ob->proxy, mask);
708 /* handled in next loop */
710 ob->dup_group->id.flag |= LIB_DOIT;
713 /* add groups used in current scene objects */
714 for(group= G.main->group.first; group; group= group->id.next) {
715 if(group->id.flag & LIB_DOIT) {
716 for(go= group->gobject.first; go; go= go->next) {
717 build_dag_object(dag, scenenode, go->ob, mask);
719 group->id.flag &= ~LIB_DOIT;
723 /* Now all relations were built, but we need to solve 1 exceptional case;
724 When objects have multiple "parents" (for example parent + constraint working on same object)
725 the relation type has to be synced. One of the parents can change, and should give same event to child */
727 /* nodes were callocced, so we can use node->color for temporal storage */
728 for(node = sce->theDag->DagNode.first; node; node= node->next) {
729 if(node->type==ID_OB) {
730 for(itA = node->child; itA; itA= itA->next) {
731 if(itA->node->type==ID_OB) {
732 itA->node->color |= itA->type;
737 /* now set relations equal, so that when only one parent changes, the correct recalcs are found */
738 for(node = sce->theDag->DagNode.first; node; node= node->next) {
739 if(node->type==ID_OB) {
740 for(itA = node->child; itA; itA= itA->next) {
741 if(itA->node->type==ID_OB) {
742 itA->type |= itA->node->color;
748 // cycle detection and solving
749 // solve_cycles(dag);
755 void free_forest(DagForest *Dag)
756 { /* remove all nodes and deps */
760 DagNode *itN = Dag->DagNode.first;
781 Dag->DagNode.first = NULL;
782 Dag->DagNode.last = NULL;
787 DagNode * dag_find_node (DagForest *forest,void * fob)
789 DagNode *node = forest->DagNode.first;
799 static int ugly_hack_sorry= 1; // prevent type check
801 /* no checking of existance, use dag_find_node first or dag_get_node */
802 DagNode * dag_add_node (DagForest *forest, void * fob)
806 node = MEM_callocN(sizeof(DagNode),"DAG node");
809 node->color = DAG_WHITE;
811 if(ugly_hack_sorry) node->type = GS(((ID *) fob)->name); // sorry, done for pose sorting
812 if (forest->numNodes) {
813 ((DagNode *) forest->DagNode.last)->next = node;
814 forest->DagNode.last = node;
817 forest->DagNode.last = node;
818 forest->DagNode.first = node;
819 forest->numNodes = 1;
825 DagNode * dag_get_node (DagForest *forest,void * fob)
829 node = dag_find_node (forest, fob);
831 node = dag_add_node(forest, fob);
837 DagNode * dag_get_sub_node (DagForest *forest,void * fob)
840 DagAdjList *mainchild, *prev=NULL;
842 mainchild = ((DagNode *) forest->DagNode.first)->child;
843 /* remove from first node (scene) adj list if present */
845 if (mainchild->node == fob) {
847 prev->next = mainchild->next;
848 MEM_freeN(mainchild);
851 ((DagNode *) forest->DagNode.first)->child = mainchild->next;
852 MEM_freeN(mainchild);
857 mainchild = mainchild->next;
859 node = dag_find_node (forest, fob);
861 node = dag_add_node(forest, fob);
865 void dag_add_relation(DagForest *forest, DagNode *fob1, DagNode *fob2, short rel, char *name)
867 DagAdjList *itA = fob1->child;
869 while (itA) { /* search if relation exist already */
870 if (itA->node == fob2) {
877 /* create new relation and insert at head. MALLOC alert! */
878 itA = MEM_mallocN(sizeof(DagAdjList),"DAG adj list");
882 itA->next = fob1->child;
887 static void dag_add_parent_relation(DagForest *forest, DagNode *fob1, DagNode *fob2, short rel, char *name)
889 DagAdjList *itA = fob2->parent;
891 while (itA) { /* search if relation exist already */
892 if (itA->node == fob1) {
899 /* create new relation and insert at head. MALLOC alert! */
900 itA = MEM_mallocN(sizeof(DagAdjList),"DAG adj list");
904 itA->next = fob2->parent;
909 static char *dag_node_name(DagNode *node)
913 else if(ugly_hack_sorry)
914 return ((ID*)(node->ob))->name+2;
916 return ((bPoseChannel*)(node->ob))->name;
920 static void dag_node_print_dependencies(DagNode *node)
924 printf("%s depends on:\n", dag_node_name(node));
926 for(itA= node->parent; itA; itA= itA->next)
927 printf(" %s through %s\n", dag_node_name(itA->node), itA->name);
932 static int dag_node_print_dependency_recurs(DagNode *node, DagNode *endnode)
936 if(node->color == DAG_BLACK)
939 node->color= DAG_BLACK;
944 for(itA= node->parent; itA; itA= itA->next) {
945 if(dag_node_print_dependency_recurs(itA->node, endnode)) {
946 printf(" %s depends on %s through %s.\n", dag_node_name(node), dag_node_name(itA->node), itA->name);
954 static void dag_node_print_dependency_cycle(DagForest *dag, DagNode *startnode, DagNode *endnode, char *name)
958 for(node = dag->DagNode.first; node; node= node->next)
959 node->color= DAG_WHITE;
961 printf(" %s depends on %s through %s.\n", dag_node_name(endnode), dag_node_name(startnode), name);
962 dag_node_print_dependency_recurs(startnode, endnode);
967 * MainDAG is the DAG of all objects in current scene
968 * used only for drawing there is one also in each scene
970 static DagForest * MainDag = NULL;
972 DagForest *getMainDag(void)
978 void setMainDag(DagForest *dag)
986 * in theory we should sweep the whole array
987 * but in our case the first node is the scene
988 * and is linked to every other object
990 * for general case we will need to add outer loop
994 * ToDo : change pos kludge
997 /* adjust levels for drawing in oops space */
1001 DagNodeQueue *nqueue;
1007 /* fprintf(stderr,"starting BFS \n ------------\n"); */
1008 nqueue = queue_create(DAGQUEUEALLOC);
1009 for ( i=0; i<50; i++)
1013 * dagnode.first is alway the root (scene)
1015 node = MainDag->DagNode.first;
1017 node->color = DAG_WHITE;
1018 node->BFS_dist = 9999;
1023 node = MainDag->DagNode.first;
1024 if (node->color == DAG_WHITE) {
1025 node->color = DAG_GRAY;
1027 push_queue(nqueue,node);
1028 while(nqueue->count) {
1029 node = pop_queue(nqueue);
1031 minheight = pos[node->BFS_dist];
1033 while(itA != NULL) {
1034 if((itA->node->color == DAG_WHITE) ) {
1035 itA->node->color = DAG_GRAY;
1036 itA->node->BFS_dist = node->BFS_dist + 1;
1037 itA->node->k = (float) minheight;
1038 push_queue(nqueue,itA->node);
1042 fprintf(stderr,"bfs not dag tree edge color :%i \n",itA->node->color);
1048 if (pos[node->BFS_dist] > node->k ) {
1049 pos[node->BFS_dist] += 1;
1050 node->k = (float) pos[node->BFS_dist];
1052 pos[node->BFS_dist] = (int) node->k +1;
1054 set_node_xy(node, node->BFS_dist*DEPSX*2, pos[node->BFS_dist]*DEPSY*2);
1055 node->color = DAG_BLACK;
1057 fprintf(stderr,"BFS node : %20s %i %5.0f %5.0f\n",((ID *) node->ob)->name,node->BFS_dist, node->x, node->y);
1061 queue_delete(nqueue);
1064 int pre_and_post_BFS(DagForest *dag, short mask, graph_action_func pre_func, graph_action_func post_func, void **data) {
1067 node = dag->DagNode.first;
1068 return pre_and_post_source_BFS(dag, mask, node, pre_func, post_func, data);
1072 int pre_and_post_source_BFS(DagForest *dag, short mask, DagNode *source, graph_action_func pre_func, graph_action_func post_func, void **data)
1075 DagNodeQueue *nqueue;
1078 /* fprintf(stderr,"starting BFS \n ------------\n"); */
1081 * dagnode.first is alway the root (scene)
1083 node = dag->DagNode.first;
1084 nqueue = queue_create(DAGQUEUEALLOC);
1086 node->color = DAG_WHITE;
1087 node->BFS_dist = 9999;
1092 if (node->color == DAG_WHITE) {
1093 node->color = DAG_GRAY;
1095 pre_func(node->ob,data);
1097 while(nqueue->count) {
1098 node = pop_queue(nqueue);
1101 while(itA != NULL) {
1102 if((itA->node->color == DAG_WHITE) && (itA->type & mask)) {
1103 itA->node->color = DAG_GRAY;
1104 itA->node->BFS_dist = node->BFS_dist + 1;
1105 push_queue(nqueue,itA->node);
1106 pre_func(node->ob,data);
1109 else { // back or cross edge
1114 post_func(node->ob,data);
1115 node->color = DAG_BLACK;
1117 fprintf(stderr,"BFS node : %20s %i %5.0f %5.0f\n",((ID *) node->ob)->name,node->BFS_dist, node->x, node->y);
1121 queue_delete(nqueue);
1125 /* non recursive version of DFS, return queue -- outer loop present to catch odd cases (first level cycles)*/
1126 DagNodeQueue * graph_dfs(void)
1129 DagNodeQueue *nqueue;
1130 DagNodeQueue *retqueue;
1140 *fprintf(stderr,"starting DFS \n ------------\n");
1142 nqueue = queue_create(DAGQUEUEALLOC);
1143 retqueue = queue_create(MainDag->numNodes);
1144 for ( i=0; i<50; i++)
1148 * dagnode.first is alway the root (scene)
1150 node = MainDag->DagNode.first;
1152 node->color = DAG_WHITE;
1153 node->DFS_dist = 9999;
1154 node->DFS_dvtm = node->DFS_fntm = 9999;
1161 node = MainDag->DagNode.first;
1164 if (node->color == DAG_WHITE) {
1165 node->color = DAG_GRAY;
1167 node->DFS_dvtm = time;
1169 push_stack(nqueue,node);
1171 while(nqueue->count) {
1172 //graph_print_queue(nqueue);
1175 node = get_top_node_queue(nqueue);
1177 minheight = pos[node->DFS_dist];
1180 while(itA != NULL) {
1181 if((itA->node->color == DAG_WHITE) ) {
1182 itA->node->DFS_dvtm = time;
1183 itA->node->color = DAG_GRAY;
1186 itA->node->DFS_dist = node->DFS_dist + 1;
1187 itA->node->k = (float) minheight;
1188 push_stack(nqueue,itA->node);
1192 if (itA->node->color == DAG_GRAY) { // back edge
1193 fprintf(stderr,"dfs back edge :%15s %15s \n",((ID *) node->ob)->name, ((ID *) itA->node->ob)->name);
1195 } else if (itA->node->color == DAG_BLACK) {
1197 /* already processed node but we may want later to change distance either to shorter to longer.
1198 * DFS_dist is the first encounter
1200 /*if (node->DFS_dist >= itA->node->DFS_dist)
1201 itA->node->DFS_dist = node->DFS_dist + 1;
1203 fprintf(stderr,"dfs forward or cross edge :%15s %i-%i %15s %i-%i \n",
1204 ((ID *) node->ob)->name,
1207 ((ID *) itA->node->ob)->name,
1208 itA->node->DFS_dvtm,
1209 itA->node->DFS_fntm);
1212 fprintf(stderr,"dfs unknown edge \n");
1218 node = pop_queue(nqueue);
1219 node->color = DAG_BLACK;
1221 node->DFS_fntm = time;
1223 if (node->DFS_dist > maxpos)
1224 maxpos = node->DFS_dist;
1225 if (pos[node->DFS_dist] > node->k ) {
1226 pos[node->DFS_dist] += 1;
1227 node->k = (float) pos[node->DFS_dist];
1229 pos[node->DFS_dist] = (int) node->k +1;
1231 set_node_xy(node, node->DFS_dist*DEPSX*2, pos[node->DFS_dist]*DEPSY*2);
1234 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 );
1236 push_stack(retqueue,node);
1243 // fprintf(stderr,"i size : %i \n", maxpos);
1245 queue_delete(nqueue);
1250 int pre_and_post_DFS(DagForest *dag, short mask, graph_action_func pre_func, graph_action_func post_func, void **data) {
1253 node = dag->DagNode.first;
1254 return pre_and_post_source_DFS(dag, mask, node, pre_func, post_func, data);
1257 int pre_and_post_source_DFS(DagForest *dag, short mask, DagNode *source, graph_action_func pre_func, graph_action_func post_func, void **data)
1260 DagNodeQueue *nqueue;
1266 *fprintf(stderr,"starting DFS \n ------------\n");
1268 nqueue = queue_create(DAGQUEUEALLOC);
1271 * dagnode.first is alway the root (scene)
1273 node = dag->DagNode.first;
1275 node->color = DAG_WHITE;
1276 node->DFS_dist = 9999;
1277 node->DFS_dvtm = node->DFS_fntm = 9999;
1286 if (node->color == DAG_WHITE) {
1287 node->color = DAG_GRAY;
1289 node->DFS_dvtm = time;
1291 push_stack(nqueue,node);
1292 pre_func(node->ob,data);
1294 while(nqueue->count) {
1296 node = get_top_node_queue(nqueue);
1299 while(itA != NULL) {
1300 if((itA->node->color == DAG_WHITE) && (itA->type & mask) ) {
1301 itA->node->DFS_dvtm = time;
1302 itA->node->color = DAG_GRAY;
1305 itA->node->DFS_dist = node->DFS_dist + 1;
1306 push_stack(nqueue,itA->node);
1307 pre_func(node->ob,data);
1312 if (itA->node->color == DAG_GRAY) {// back edge
1315 // else if (itA->node->color == DAG_BLACK) { // cross or forward
1322 node = pop_queue(nqueue);
1323 node->color = DAG_BLACK;
1325 node->DFS_fntm = time;
1327 post_func(node->ob,data);
1333 queue_delete(nqueue);
1338 // used to get the obs owning a datablock
1339 struct DagNodeQueue *get_obparents(struct DagForest *dag, void *ob)
1341 DagNode * node, *node1;
1342 DagNodeQueue *nqueue;
1345 node = dag_find_node(dag,ob);
1349 else if (node->ancestor_count == 1) { // simple case
1350 nqueue = queue_create(1);
1351 push_queue(nqueue,node);
1352 } else { // need to go over the whole dag for adj list
1353 nqueue = queue_create(node->ancestor_count);
1355 node1 = dag->DagNode.first;
1357 if (node1->DFS_fntm > node->DFS_fntm) { // a parent is finished after child. must check adj list
1359 while(itA != NULL) {
1360 if ((itA->node == node) && (itA->type == DAG_RL_DATA)) {
1361 push_queue(nqueue,node);
1366 node1 = node1->next;
1372 struct DagNodeQueue *get_first_ancestors(struct DagForest *dag, void *ob)
1374 DagNode * node, *node1;
1375 DagNodeQueue *nqueue;
1378 node = dag_find_node(dag,ob);
1380 // need to go over the whole dag for adj list
1381 nqueue = queue_create(node->ancestor_count);
1383 node1 = dag->DagNode.first;
1385 if (node1->DFS_fntm > node->DFS_fntm) {
1387 while(itA != NULL) {
1388 if (itA->node == node) {
1389 push_queue(nqueue,node);
1394 node1 = node1->next;
1400 // standard DFS list
1401 struct DagNodeQueue *get_all_childs(struct DagForest *dag, void *ob)
1404 DagNodeQueue *nqueue;
1405 DagNodeQueue *retqueue;
1410 nqueue = queue_create(DAGQUEUEALLOC);
1411 retqueue = queue_create(dag->numNodes); // was MainDag... why? (ton)
1413 node = dag->DagNode.first;
1415 node->color = DAG_WHITE;
1421 node = dag_find_node(dag, ob); // could be done in loop above (ton)
1422 if(node) { // can be null for newly added objects
1424 node->color = DAG_GRAY;
1426 push_stack(nqueue,node);
1428 while(nqueue->count) {
1431 node = get_top_node_queue(nqueue);
1434 while(itA != NULL) {
1435 if((itA->node->color == DAG_WHITE) ) {
1436 itA->node->DFS_dvtm = time;
1437 itA->node->color = DAG_GRAY;
1440 push_stack(nqueue,itA->node);
1448 node = pop_queue(nqueue);
1449 node->color = DAG_BLACK;
1452 push_stack(retqueue,node);
1456 queue_delete(nqueue);
1461 short are_obs_related(struct DagForest *dag, void *ob1, void *ob2) {
1465 node = dag_find_node(dag, ob1);
1468 while(itA != NULL) {
1469 if((itA->node->ob == ob2) ) {
1470 return itA->node->type;
1474 return DAG_NO_RELATION;
1477 int is_acyclic( DagForest *dag) {
1478 return dag->is_acyclic;
1481 void set_node_xy(DagNode *node, float x, float y)
1488 /* debug test functions */
1490 void graph_print_queue(DagNodeQueue *nqueue)
1492 DagNodeQueueElem *queueElem;
1494 queueElem = nqueue->first;
1496 fprintf(stderr,"** %s %i %i-%i ",((ID *) queueElem->node->ob)->name,queueElem->node->color,queueElem->node->DFS_dvtm,queueElem->node->DFS_fntm);
1497 queueElem = queueElem->next;
1499 fprintf(stderr,"\n");
1502 void graph_print_queue_dist(DagNodeQueue *nqueue)
1504 DagNodeQueueElem *queueElem;
1507 queueElem = nqueue->first;
1508 max = queueElem->node->DFS_fntm;
1511 fprintf(stderr,"** %25s %2.2i-%2.2i ",((ID *) queueElem->node->ob)->name,queueElem->node->DFS_dvtm,queueElem->node->DFS_fntm);
1512 while (count < queueElem->node->DFS_dvtm-1) { fputc(' ',stderr); count++;}
1514 while (count < queueElem->node->DFS_fntm-2) { fputc('-',stderr); count++;}
1518 queueElem = queueElem->next;
1520 fprintf(stderr,"\n");
1523 void graph_print_adj_list(void)
1528 node = (getMainDag())->DagNode.first;
1530 fprintf(stderr,"node : %s col: %i",((ID *) node->ob)->name, node->color);
1533 fprintf(stderr,"-- %s ",((ID *) itA->node->ob)->name);
1537 fprintf(stderr,"\n");
1542 /* ************************ API *********************** */
1544 /* groups with objects in this scene need to be put in the right order as well */
1545 static void scene_sort_groups(Scene *sce)
1552 /* test; are group objects all in this scene? */
1553 for(ob= G.main->object.first; ob; ob= ob->id.next) {
1554 ob->id.flag &= ~LIB_DOIT;
1555 ob->id.newid= NULL; /* newid abuse for GroupObject */
1557 for(base = sce->base.first; base; base= base->next)
1558 base->object->id.flag |= LIB_DOIT;
1560 for(group= G.main->group.first; group; group= group->id.next) {
1561 for(go= group->gobject.first; go; go= go->next) {
1562 if((go->ob->id.flag & LIB_DOIT)==0)
1565 /* this group is entirely in this scene */
1567 ListBase listb= {NULL, NULL};
1569 for(go= group->gobject.first; go; go= go->next)
1570 go->ob->id.newid= (ID *)go;
1572 /* in order of sorted bases we reinsert group objects */
1573 for(base = sce->base.first; base; base= base->next) {
1575 if(base->object->id.newid) {
1576 go= (GroupObject *)base->object->id.newid;
1577 base->object->id.newid= NULL;
1578 BLI_remlink( &group->gobject, go);
1579 BLI_addtail( &listb, go);
1582 /* copy the newly sorted listbase */
1583 group->gobject= listb;
1588 /* sort the base list on dependency order */
1589 void DAG_scene_sort(struct Scene *sce)
1592 DagNodeQueue *nqueue;
1599 tempbase.first= tempbase.last= NULL;
1601 build_dag(sce, DAG_RL_ALL_BUT_DATA);
1603 nqueue = queue_create(DAGQUEUEALLOC);
1605 for(node = sce->theDag->DagNode.first; node; node= node->next) {
1606 node->color = DAG_WHITE;
1611 node = sce->theDag->DagNode.first;
1613 node->color = DAG_GRAY;
1615 push_stack(nqueue,node);
1617 while(nqueue->count) {
1620 node = get_top_node_queue(nqueue);
1623 while(itA != NULL) {
1624 if((itA->node->color == DAG_WHITE) ) {
1625 itA->node->DFS_dvtm = time;
1626 itA->node->color = DAG_GRAY;
1629 push_stack(nqueue,itA->node);
1638 node = pop_queue(nqueue);
1639 if (node->ob == sce) // we are done
1641 node->color = DAG_BLACK;
1644 base = sce->base.first;
1645 while (base && base->object != node->ob)
1648 BLI_remlink(&sce->base,base);
1649 BLI_addhead(&tempbase,base);
1655 // temporal correction for circular dependancies
1656 base = sce->base.first;
1658 BLI_remlink(&sce->base,base);
1659 BLI_addhead(&tempbase,base);
1661 printf("cyclic %s\n", base->object->id.name);
1662 base = sce->base.first;
1665 sce->base = tempbase;
1666 queue_delete(nqueue);
1668 /* all groups with objects in this scene gets resorted too */
1669 scene_sort_groups(sce);
1672 printf("\nordered\n");
1673 for(base = sce->base.first; base; base= base->next) {
1674 printf(" %s\n", base->object->id.name);
1678 sce->recalc |= SCE_PRV_CHANGED; /* test for 3d preview */
1681 /* node was checked to have lasttime != curtime and is if type ID_OB */
1682 static void flush_update_node(DagNode *node, unsigned int layer, int curtime)
1686 int oldflag, changed=0;
1687 unsigned int all_layer;
1689 node->lasttime= curtime;
1692 if(ob && (ob->recalc & OB_RECALC)) {
1695 /* got an object node that changes, now check relations */
1696 for(itA = node->child; itA; itA= itA->next) {
1697 all_layer |= itA->lay;
1698 /* the relationship is visible */
1699 if((itA->lay & layer) || (itA->node->ob == G.obedit)) {
1700 if(itA->node->type==ID_OB) {
1702 oldflag= obc->recalc;
1704 /* got a ob->obc relation, now check if flag needs flush */
1705 if(ob->recalc & OB_RECALC_OB) {
1706 if(itA->type & DAG_RL_OB_OB) {
1707 //printf("ob %s changes ob %s\n", ob->id.name, obc->id.name);
1708 obc->recalc |= OB_RECALC_OB;
1710 if(itA->type & DAG_RL_OB_DATA) {
1711 //printf("ob %s changes obdata %s\n", ob->id.name, obc->id.name);
1712 obc->recalc |= OB_RECALC_DATA;
1715 if(ob->recalc & OB_RECALC_DATA) {
1716 if(itA->type & DAG_RL_DATA_OB) {
1717 //printf("obdata %s changes ob %s\n", ob->id.name, obc->id.name);
1718 obc->recalc |= OB_RECALC_OB;
1720 if(itA->type & DAG_RL_DATA_DATA) {
1721 //printf("obdata %s changes obdata %s\n", ob->id.name, obc->id.name);
1722 obc->recalc |= OB_RECALC_DATA;
1725 if(oldflag!=obc->recalc) changed= 1;
1729 /* even nicer, we can clear recalc flags... */
1730 if((all_layer & layer)==0 && (ob != G.obedit)) {
1731 /* but existing displaylists or derivedmesh should be freed */
1732 if(ob->recalc & OB_RECALC_DATA)
1733 object_free_display(ob);
1735 ob->recalc &= ~OB_RECALC;
1739 /* check case where child changes and parent forcing obdata to change */
1740 /* should be done regardless if this ob has recalc set */
1741 /* could merge this in with loop above...? (ton) */
1742 for(itA = node->child; itA; itA= itA->next) {
1743 /* the relationship is visible */
1744 if((itA->lay & layer) || (itA->node->ob == G.obedit)) {
1745 if(itA->node->type==ID_OB) {
1748 if((obc->recalc & OB_RECALC)==OB_RECALC_OB) {
1749 /* parent has deforming info */
1750 if(itA->type & (DAG_RL_OB_DATA|DAG_RL_DATA_DATA)) {
1751 // printf("parent %s changes ob %s\n", ob->id.name, obc->id.name);
1752 obc->recalc |= OB_RECALC_DATA;
1759 /* we only go deeper if node not checked or something changed */
1760 for(itA = node->child; itA; itA= itA->next) {
1761 if(changed || itA->node->lasttime!=curtime)
1762 flush_update_node(itA->node, layer, curtime);
1767 /* node was checked to have lasttime != curtime , and is of type ID_OB */
1768 static unsigned int flush_layer_node(Scene *sce, DagNode *node, int curtime)
1773 node->lasttime= curtime;
1775 for(base= sce->base.first; base; base= base->next) {
1776 if(node->ob == base->object) {
1777 node->lay= ((Object *)node->ob)->lay;
1782 for(itA = node->child; itA; itA= itA->next) {
1783 if(itA->node->type==ID_OB) {
1784 if(itA->node->lasttime!=curtime) {
1785 itA->lay= flush_layer_node(sce, itA->node, curtime); // lay is only set once for each relation
1787 else itA->lay= itA->node->lay;
1789 node->lay |= itA->lay;
1796 /* node was checked to have lasttime != curtime , and is of type ID_OB */
1797 static void flush_pointcache_reset(DagNode *node, int curtime, int reset)
1802 node->lasttime= curtime;
1804 for(itA = node->child; itA; itA= itA->next) {
1805 if(itA->node->type==ID_OB) {
1806 if(itA->node->lasttime!=curtime) {
1807 ob= (Object*)(node->ob);
1809 if(reset || (ob->recalc & OB_RECALC)) {
1810 if(BKE_ptcache_object_reset(ob, PTCACHE_RESET_DEPSGRAPH))
1811 ob->recalc |= OB_RECALC_DATA;
1813 flush_pointcache_reset(itA->node, curtime, 1);
1816 flush_pointcache_reset(itA->node, curtime, 0);
1822 /* flushes all recalc flags in objects down the dependency tree */
1823 void DAG_scene_flush_update(Scene *sce, unsigned int lay, int time)
1830 if(sce->theDag==NULL) {
1831 printf("DAG zero... not allowed to happen!\n");
1832 DAG_scene_sort(sce);
1835 firstnode= sce->theDag->DagNode.first; // always scene node
1837 for(itA = firstnode->child; itA; itA= itA->next)
1840 /* first we flush the layer flags */
1841 sce->theDag->time++; // so we know which nodes were accessed
1842 lasttime= sce->theDag->time;
1844 for(itA = firstnode->child; itA; itA= itA->next)
1845 if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB)
1846 flush_layer_node(sce, itA->node, lasttime);
1848 /* then we use the relationships + layer info to flush update events */
1849 sce->theDag->time++; // so we know which nodes were accessed
1850 lasttime= sce->theDag->time;
1851 for(itA = firstnode->child; itA; itA= itA->next)
1852 if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB)
1853 flush_update_node(itA->node, lay, lasttime);
1855 /* if update is not due to time change, do pointcache clears */
1857 sce->theDag->time++; // so we know which nodes were accessed
1858 lasttime= sce->theDag->time;
1859 for(itA = firstnode->child; itA; itA= itA->next) {
1860 if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB) {
1861 ob= (Object*)(itA->node->ob);
1863 if(ob->recalc & OB_RECALC) {
1864 if(BKE_ptcache_object_reset(ob, PTCACHE_RESET_DEPSGRAPH))
1865 ob->recalc |= OB_RECALC_DATA;
1867 flush_pointcache_reset(itA->node, lasttime, 1);
1870 flush_pointcache_reset(itA->node, lasttime, 0);
1876 static int object_modifiers_use_time(Object *ob)
1880 for (md=ob->modifiers.first; md; md=md->next)
1881 if (modifier_dependsOnTime(md))
1887 static int exists_channel(Object *ob, char *name)
1889 bActionStrip *strip;
1892 if(get_action_channel(ob->action, name))
1895 for (strip=ob->nlastrips.first; strip; strip=strip->next)
1896 if(get_action_channel(strip->act, name))
1901 static void dag_object_time_update_flags(Object *ob)
1904 if(ob->ipo) ob->recalc |= OB_RECALC_OB;
1905 else if(ob->constraints.first) {
1907 for (con = ob->constraints.first; con; con=con->next) {
1908 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
1909 ListBase targets = {NULL, NULL};
1910 bConstraintTarget *ct;
1912 if (cti && cti->get_constraint_targets) {
1913 cti->get_constraint_targets(con, &targets);
1915 for (ct= targets.first; ct; ct= ct->next) {
1917 ob->recalc |= OB_RECALC_OB;
1922 if (cti->flush_constraint_targets)
1923 cti->flush_constraint_targets(con, &targets, 1);
1927 else if(ob->scriptlink.totscript) ob->recalc |= OB_RECALC_OB;
1928 else if(ob->parent) {
1929 /* motion path or bone child */
1930 if(ob->parent->type==OB_CURVE || ob->parent->type==OB_ARMATURE) ob->recalc |= OB_RECALC_OB;
1933 if(ob->action || ob->nlastrips.first) {
1934 /* since actions now are mixed, we set the recalcs on the safe side */
1935 ob->recalc |= OB_RECALC_OB;
1936 if(ob->type==OB_ARMATURE)
1937 ob->recalc |= OB_RECALC_DATA;
1938 else if(exists_channel(ob, "Shape"))
1939 ob->recalc |= OB_RECALC_DATA;
1940 else if(ob->dup_group) {
1941 bActionStrip *strip;
1942 /* this case is for groups with nla, whilst nla target has no action or nla */
1943 for(strip= ob->nlastrips.first; strip; strip= strip->next) {
1945 strip->object->recalc |= OB_RECALC;
1949 else if(modifiers_isSoftbodyEnabled(ob)) ob->recalc |= OB_RECALC_DATA;
1950 else if(object_modifiers_use_time(ob)) ob->recalc |= OB_RECALC_DATA;
1951 else if((ob->pose) && (ob->pose->flag & POSE_CONSTRAINTS_TIMEDEPEND)) ob->recalc |= OB_RECALC_DATA;
1961 if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
1962 ob->recalc |= OB_RECALC_DATA;
1963 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
1966 if(ob->particlesystem.first)
1967 ob->recalc |= OB_RECALC_DATA;
1973 if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
1974 ob->recalc |= OB_RECALC_DATA;
1975 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
1981 if(cu->nurb.first==NULL && cu->str && cu->vfont)
1982 ob->recalc |= OB_RECALC_DATA;
1987 if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
1988 ob->recalc |= OB_RECALC_DATA;
1989 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
1994 if(ob->transflag & OB_DUPLI) ob->recalc |= OB_RECALC_DATA;
1998 if(ob->particlesystem.first) {
1999 ParticleSystem *psys= ob->particlesystem.first;
2001 for(; psys; psys=psys->next) {
2002 if(psys_check_enabled(ob, psys)) {
2003 ob->recalc |= OB_RECALC_DATA;
2011 /* flag all objects that need recalc, for changes in time for example */
2012 void DAG_scene_update_flags(Scene *scene, unsigned int lay)
2020 /* set ob flags where animated systems are */
2021 for(SETLOOPER(scene, base)) {
2024 /* now if DagNode were part of base, the node->lay could be checked... */
2025 /* we do all now, since the scene_flush checks layers and clears recalc flags even */
2026 dag_object_time_update_flags(ob);
2028 /* handled in next loop */
2030 ob->dup_group->id.flag |= LIB_DOIT;
2033 /* we do groups each once */
2034 for(group= G.main->group.first; group; group= group->id.next) {
2035 if(group->id.flag & LIB_DOIT) {
2036 for(go= group->gobject.first; go; go= go->next) {
2037 dag_object_time_update_flags(go->ob);
2042 for(sce= scene; sce; sce= sce->set)
2043 DAG_scene_flush_update(sce, lay, 1);
2045 /* test: set time flag, to disable baked systems to update */
2046 for(SETLOOPER(scene, base)) {
2049 ob->recalc |= OB_RECALC_TIME;
2052 /* hrmf... an exception to look at once, for invisible camera object we do it over */
2054 dag_object_time_update_flags(scene->camera);
2056 /* and store the info in groupobject */
2057 for(group= G.main->group.first; group; group= group->id.next) {
2058 if(group->id.flag & LIB_DOIT) {
2059 for(go= group->gobject.first; go; go= go->next) {
2060 go->recalc= go->ob->recalc;
2061 // printf("ob %s recalc %d\n", go->ob->id.name, go->recalc);
2063 group->id.flag &= ~LIB_DOIT;
2069 /* for depgraph updating, all layers visible in a screen */
2070 /* this is a copy from editscreen.c... I need to think over a more proper solution for this */
2071 /* probably the DAG_object_flush_update() should give layer too? */
2072 /* or some kind of dag context... (DAG_set_layer) */
2073 static unsigned int dag_screen_view3d_layers(void)
2078 for(sa= G.curscreen->areabase.first; sa; sa= sa->next) {
2079 if(sa->spacetype==SPACE_VIEW3D)
2080 layer |= ((View3D *)sa->spacedata.first)->lay;
2086 /* flag this object and all its relations to recalc */
2087 /* if you need to do more objects, tag object yourself and
2088 use DAG_scene_flush_update() in end */
2089 void DAG_object_flush_update(Scene *sce, Object *ob, short flag)
2092 if(ob==NULL || sce->theDag==NULL) return;
2095 BKE_ptcache_object_reset(ob, PTCACHE_RESET_DEPSGRAPH);
2097 /* all users of this ob->data should be checked */
2098 /* BUT! displists for curves are still only on cu */
2099 if(flag & OB_RECALC_DATA) {
2100 if(ob->type!=OB_CURVE && ob->type!=OB_SURF) {
2102 if(id && id->us>1) {
2103 /* except when there's a key and shapes are locked */
2104 if(ob_get_key(ob) && (ob->shapeflag & (OB_SHAPE_LOCK|OB_SHAPE_TEMPLOCK)));
2107 for (obt=G.main->object.first; obt; obt= obt->id.next) {
2108 if (obt != ob && obt->data==ob->data) {
2109 obt->recalc |= OB_RECALC_DATA;
2110 BKE_ptcache_object_reset(obt, PTCACHE_RESET_DEPSGRAPH);
2119 DAG_scene_flush_update(sce, dag_screen_view3d_layers(), 0);
2121 DAG_scene_flush_update(sce, sce->lay, 0);
2124 /* recursively descends tree, each node only checked once */
2125 /* node is checked to be of type object */
2126 static int parent_check_node(DagNode *node, int curtime)
2130 node->lasttime= curtime;
2132 if(node->color==DAG_GRAY)
2135 for(itA = node->child; itA; itA= itA->next) {
2136 if(itA->node->type==ID_OB) {
2138 if(itA->node->color==DAG_GRAY)
2141 /* descend if not done */
2142 if(itA->node->lasttime!=curtime) {
2143 itA->node->color= parent_check_node(itA->node, curtime);
2145 if(itA->node->color==DAG_GRAY)
2154 /* all nodes that influence this object get tagged, for calculating the exact
2155 position of this object at a given timeframe */
2156 void DAG_object_update_flags(Scene *sce, Object *ob, unsigned int lay)
2161 /* tag nodes unchecked */
2162 for(node = sce->theDag->DagNode.first; node; node= node->next)
2163 node->color = DAG_WHITE;
2165 node= dag_find_node(sce->theDag, ob);
2167 /* object not in scene? then handle group exception. needs to be dagged once too */
2170 while( (group = find_group(ob, group)) ) {
2172 /* primitive; tag all... this call helps building groups for particles */
2173 for(go= group->gobject.first; go; go= go->next)
2174 go->ob->recalc= OB_RECALC;
2179 node->color = DAG_GRAY;
2181 sce->theDag->time++;
2182 node= sce->theDag->DagNode.first;
2183 for(itA = node->child; itA; itA= itA->next) {
2184 if(itA->node->type==ID_OB && itA->node->lasttime!=sce->theDag->time)
2185 itA->node->color= parent_check_node(itA->node, sce->theDag->time);
2188 /* set recalcs and flushes */
2189 DAG_scene_update_flags(sce, lay);
2191 /* now we clear recalcs, unless color is set */
2192 for(node = sce->theDag->DagNode.first; node; node= node->next) {
2193 if(node->type==ID_OB && node->color==DAG_WHITE) {
2194 Object *ob= node->ob;
2201 /* ******************* DAG FOR ARMATURE POSE ***************** */
2203 static int node_recurs_level(DagNode *node, int level)
2208 node->color= DAG_BLACK; /* done */
2211 for(itA= node->parent; itA; itA= itA->next) {
2212 if(itA->node->color==DAG_WHITE) {
2213 itA->node->ancestor_count= node_recurs_level(itA->node, level);
2214 newlevel= MAX2(newlevel, level+itA->node->ancestor_count);
2217 newlevel= MAX2(newlevel, level+itA->node->ancestor_count);
2223 static void pose_check_cycle(DagForest *dag)
2228 /* tag nodes unchecked */
2229 for(node = dag->DagNode.first; node; node= node->next)
2230 node->color= DAG_WHITE;
2232 for(node = dag->DagNode.first; node; node= node->next) {
2233 if(node->color==DAG_WHITE) {
2234 node->ancestor_count= node_recurs_level(node, 0);
2238 /* check relations, and print errors */
2239 for(node = dag->DagNode.first; node; node= node->next) {
2240 for(itA= node->parent; itA; itA= itA->next) {
2241 if(itA->node->ancestor_count > node->ancestor_count) {
2242 bPoseChannel *pchan= (bPoseChannel *)node->ob;
2243 bPoseChannel *parchan= (bPoseChannel *)itA->node->ob;
2245 if(pchan && parchan) {
2246 printf("Cycle detected:\n");
2247 dag_node_print_dependency_cycle(dag, itA->node, node, itA->name);
2254 /* we assume its an armature with pose */
2255 void DAG_pose_sort(Object *ob)
2257 bPose *pose= ob->pose;
2258 bPoseChannel *pchan;
2261 DagNode *node2, *node3;
2264 DagNodeQueue *nqueue;
2270 ugly_hack_sorry= 0; // no ID structs
2272 rootnode = dag_add_node(dag, NULL); // node->ob becomes NULL
2274 /* we add the hierarchy and the constraints */
2275 for(pchan = pose->chanbase.first; pchan; pchan= pchan->next) {
2278 node = dag_get_node(dag, pchan);
2281 node2 = dag_get_node(dag, pchan->parent);
2282 dag_add_relation(dag, node2, node, 0, "Parent Relation");
2283 dag_add_parent_relation(dag, node2, node, 0, "Parent Relation");
2286 for (con = pchan->constraints.first; con; con=con->next) {
2287 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
2288 ListBase targets = {NULL, NULL};
2289 bConstraintTarget *ct;
2293 for(icu= con->ipo->curve.first; icu; icu= icu->next) {
2294 /* icu->driver->ob should actually point to ob->proxy if it
2295 * is a proxy, but since it wasn't set correct it older
2296 * files comparing with ob->proxy makes it work for those */
2297 if(icu->driver && (icu->driver->ob==ob || icu->driver->ob==ob->proxy)) {
2298 bPoseChannel *target= get_pose_channel(ob->pose, icu->driver->name);
2300 node2 = dag_get_node(dag, target);
2301 dag_add_relation(dag, node2, node, 0, "Ipo Driver");
2302 dag_add_parent_relation(dag, node2, node, 0, "Ipo Driver");
2304 /* uncommented this line, results in dependencies
2305 * not being added properly for this constraint,
2306 * what is the purpose of this? - brecht */
2307 /*cti= NULL;*/ /* trick to get next loop skipped */
2313 if (cti && cti->get_constraint_targets) {
2314 cti->get_constraint_targets(con, &targets);
2316 for (ct= targets.first; ct; ct= ct->next) {
2317 if (ct->tar==ob && ct->subtarget[0]) {
2318 bPoseChannel *target= get_pose_channel(ob->pose, ct->subtarget);
2320 node2= dag_get_node(dag, target);
2321 dag_add_relation(dag, node2, node, 0, "IK Constraint");
2322 dag_add_parent_relation(dag, node2, node, 0, "IK Constraint");
2324 if (con->type==CONSTRAINT_TYPE_KINEMATIC) {
2325 bKinematicConstraint *data = (bKinematicConstraint *)con->data;
2326 bPoseChannel *parchan;
2329 /* exclude tip from chain? */
2330 if(!(data->flag & CONSTRAINT_IK_TIP))
2331 parchan= pchan->parent;
2335 /* Walk to the chain's root */
2337 node3= dag_get_node(dag, parchan);
2338 dag_add_relation(dag, node2, node3, 0, "IK Constraint");
2339 dag_add_parent_relation(dag, node2, node3, 0, "IK Constraint");
2342 if (segcount==data->rootbone || segcount>255) break; // 255 is weak
2343 parchan= parchan->parent;
2350 if (cti->flush_constraint_targets)
2351 cti->flush_constraint_targets(con, &targets, 1);
2354 if (addtoroot == 1 ) {
2355 dag_add_relation(dag, rootnode, node, 0, "Root Bone Relation");
2356 dag_add_parent_relation(dag, rootnode, node, 0, "Root Bone Relation");
2360 pose_check_cycle(dag);
2362 /* now we try to sort... */
2363 tempbase.first= tempbase.last= NULL;
2365 nqueue = queue_create(DAGQUEUEALLOC);
2367 /* tag nodes unchecked */
2368 for(node = dag->DagNode.first; node; node= node->next)
2369 node->color = DAG_WHITE;
2371 node = dag->DagNode.first;
2373 node->color = DAG_GRAY;
2374 push_stack(nqueue, node);
2376 while(nqueue->count) {
2379 node = get_top_node_queue(nqueue);
2382 while(itA != NULL) {
2383 if((itA->node->color == DAG_WHITE) ) {
2384 itA->node->color = DAG_GRAY;
2385 push_stack(nqueue,itA->node);
2394 node = pop_queue(nqueue);
2395 if (node->ob == NULL) // we are done
2397 node->color = DAG_BLACK;
2399 /* put node in new list */
2400 BLI_remlink(&pose->chanbase, node->ob);
2401 BLI_addhead(&tempbase, node->ob);
2406 // temporal correction for circular dependancies
2407 while(pose->chanbase.first) {
2408 pchan= pose->chanbase.first;
2409 BLI_remlink(&pose->chanbase, pchan);
2410 BLI_addhead(&tempbase, pchan);
2412 printf("cyclic %s\n", pchan->name);
2415 pose->chanbase = tempbase;
2416 queue_delete(nqueue);
2418 // printf("\nordered\n");
2419 // for(pchan = pose->chanbase.first; pchan; pchan= pchan->next) {
2420 // printf(" %s\n", pchan->name);