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_anim_types.h"
42 #include "DNA_action_types.h"
43 #include "DNA_armature_types.h"
44 #include "DNA_curve_types.h"
45 #include "DNA_camera_types.h"
47 #include "DNA_effect_types.h"
48 #include "DNA_group_types.h"
49 #include "DNA_lattice_types.h"
50 #include "DNA_lamp_types.h"
51 #include "DNA_key_types.h"
52 #include "DNA_mesh_types.h"
53 #include "DNA_modifier_types.h"
54 #include "DNA_nla_types.h"
55 #include "DNA_object_types.h"
56 #include "DNA_object_force.h"
57 #include "DNA_object_fluidsim.h"
58 #include "DNA_oops_types.h"
59 #include "DNA_particle_types.h"
60 #include "DNA_scene_types.h"
61 #include "DNA_screen_types.h"
62 #include "DNA_space_types.h"
63 #include "DNA_view2d_types.h"
64 #include "DNA_view3d_types.h"
66 #include "BKE_action.h"
67 #include "BKE_effect.h"
68 #include "BKE_global.h"
69 #include "BKE_group.h"
72 #include "BKE_mball.h"
73 #include "BKE_modifier.h"
74 #include "BKE_object.h"
75 #include "BKE_particle.h"
76 #include "BKE_pointcache.h"
77 #include "BKE_utildefines.h"
78 #include "BKE_scene.h"
80 #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 /* isdata = object data... */
307 // XXX this needs to be extended to be more flexible (so that not only objects are evaluated via depsgraph)...
308 static void dag_add_driver_relation(AnimData *adt, DagForest *dag, DagNode *node, int isdata)
313 for (fcu= adt->drivers.first; fcu; fcu= fcu->next) {
314 ChannelDriver *driver= fcu->driver;
316 if (driver->type == DRIVER_TYPE_PYTHON) {
317 /* PyDriver / 'Expression' */
319 /* skip if invalid in some way */
320 if ((driver->flag & DRIVER_FLAG_INVALID) || (driver->expression[0] == '\0'))
322 #ifndef DISABLE_PYTHON
324 /* now we need refs to all objects mentioned in this
325 * pydriver expression, to call 'dag_add_relation'
326 * for each of them */
327 Object **obarray = NULL; // XXX BPY_pydriver_get_objects(fcu->driver);
329 Object *ob, **oba = obarray;
333 node1 = dag_get_node(dag, ob);
334 if (ob->type == OB_ARMATURE)
335 dag_add_relation(dag, node1, node, isdata?DAG_RL_DATA_DATA:DAG_RL_DATA_OB, "Python Driver");
337 dag_add_relation(dag, node1, node, isdata?DAG_RL_OB_DATA:DAG_RL_OB_OB, "Python Driver");
344 #endif /* DISABLE_PYTHON */
346 else if (driver->type == DRIVER_TYPE_ROTDIFF) {
347 // XXX rotational difference
349 else if (driver->id) {
350 if(GS(driver->id->name)==ID_OB) {
351 /* normal channel-drives-channel */
352 node1 = dag_get_node(dag, driver->id);
354 // XXX how to find out rnapath is bone?
355 if( ((Object *)driver->id)->type==OB_ARMATURE )
356 dag_add_relation(dag, node1, node, isdata?DAG_RL_DATA_DATA:DAG_RL_DATA_OB, "Ipo Driver");
358 dag_add_relation(dag, node1, node, isdata?DAG_RL_OB_DATA:DAG_RL_OB_OB, "Ipo Driver");
361 #if 0 // XXX old 'normal' type
363 else if (icu->driver->ob) {
364 node1 = dag_get_node(dag, icu->driver->ob);
365 if(icu->driver->blocktype==ID_AR)
366 dag_add_relation(dag, node1, node, isdata?DAG_RL_DATA_DATA:DAG_RL_DATA_OB, "Ipo Driver");
368 dag_add_relation(dag, node1, node, isdata?DAG_RL_OB_DATA:DAG_RL_OB_OB, "Ipo Driver");
370 #endif // XXX old 'normal' type
374 static void dag_add_collision_field_relation(DagForest *dag, Scene *scene, Object *ob, DagNode *node)
379 // would be nice to have a list of colliders here
380 // so for now walk all objects in scene check 'same layer rule'
381 for(base = scene->base.first; base; base= base->next) {
382 if((base->lay & ob->lay) && base->object->pd) {
383 Object *ob1= base->object;
384 if((ob1->pd->deflect || ob1->pd->forcefield) && (ob1 != ob)) {
385 node2 = dag_get_node(dag, ob1);
386 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Field Collision");
392 static void build_dag_object(DagForest *dag, DagNode *scenenode, Scene *scene, Object *ob, int mask)
399 ParticleSystem *psys;
402 node = dag_get_node(dag, ob);
404 if ((ob->data) && (mask&DAG_RL_DATA)) {
405 node2 = dag_get_node(dag,ob->data);
406 dag_add_relation(dag,node,node2,DAG_RL_DATA, "Object-Data Relation");
407 node2->first_ancestor = ob;
408 node2->ancestor_count += 1;
411 if (ob->type == OB_ARMATURE) {
416 for (pchan = ob->pose->chanbase.first; pchan; pchan=pchan->next) {
417 for (con = pchan->constraints.first; con; con=con->next) {
418 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
419 ListBase targets = {NULL, NULL};
420 bConstraintTarget *ct;
422 if (cti && cti->get_constraint_targets) {
423 cti->get_constraint_targets(con, &targets);
425 for (ct= targets.first; ct; ct= ct->next) {
426 if (ct->tar && ct->tar != ob) {
427 // fprintf(stderr,"armature %s target :%s \n", ob->id.name, target->id.name);
428 node3 = dag_get_node(dag, ct->tar);
430 if (ct->subtarget[0])
431 dag_add_relation(dag,node3,node, DAG_RL_OB_DATA|DAG_RL_DATA_DATA, cti->name);
432 else if(ELEM(con->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO))
433 dag_add_relation(dag,node3,node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, cti->name);
435 dag_add_relation(dag,node3,node, DAG_RL_OB_DATA, cti->name);
439 if (cti->flush_constraint_targets)
440 cti->flush_constraint_targets(con, &targets, 1);
448 /* driver dependencies, nla modifiers */
449 #if 0 // XXX old animation system
451 dag_add_driver_relation(ob->ipo, dag, node, 0);
455 dag_add_driver_relation(key->ipo, dag, node, 1);
457 for (conchan=ob->constraintChannels.first; conchan; conchan=conchan->next)
459 dag_add_driver_relation(conchan->ipo, dag, node, 0);
462 bActionChannel *chan;
463 for (chan = ob->action->chanbase.first; chan; chan=chan->next){
465 dag_add_driver_relation(chan->ipo, dag, node, 1);
466 for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next)
468 dag_add_driver_relation(conchan->ipo, dag, node, 1);
471 if(ob->nlastrips.first) {
473 bActionChannel *chan;
474 for(strip= ob->nlastrips.first; strip; strip= strip->next) {
475 if(strip->act && strip->act!=ob->action)
476 for (chan = strip->act->chanbase.first; chan; chan=chan->next)
478 dag_add_driver_relation(chan->ipo, dag, node, 1);
479 if(strip->modifiers.first) {
480 bActionModifier *amod;
481 for(amod= strip->modifiers.first; amod; amod= amod->next) {
483 node2 = dag_get_node(dag, amod->ob);
484 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "NLA Strip Modifier");
490 #endif // XXX old animation system
492 dag_add_driver_relation(ob->adt, dag, node, (ob->type == OB_ARMATURE)); // XXX isdata arg here doesn't give an accurate picture of situation
496 dag_add_driver_relation(key->adt, dag, node, 1);
498 if (ob->modifiers.first) {
501 for(md=ob->modifiers.first; md; md=md->next) {
502 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
504 if (mti->updateDepgraph) mti->updateDepgraph(md, dag, scene, ob, node);
508 node2 = dag_get_node(dag,ob->parent);
510 switch(ob->partype) {
512 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Parent");
514 case PARVERT1: case PARVERT3: case PARBONE:
515 dag_add_relation(dag,node2,node,DAG_RL_DATA_OB|DAG_RL_OB_OB, "Vertex Parent");
518 if(ob->parent->type==OB_LATTICE)
519 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Lattice Parent");
520 else if(ob->parent->type==OB_CURVE) {
521 Curve *cu= ob->parent->data;
522 if(cu->flag & CU_PATH)
523 dag_add_relation(dag,node2,node,DAG_RL_DATA_OB|DAG_RL_OB_OB, "Curve Parent");
525 dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Curve Parent");
528 dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Parent");
530 /* exception case: parent is duplivert */
531 if(ob->type==OB_MBALL && (ob->parent->transflag & OB_DUPLIVERTS)) {
532 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Duplivert");
538 node2 = dag_get_node(dag,ob->track);
539 dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Track To");
543 node2 = dag_get_node(dag, ob->proxy);
544 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Proxy");
545 /* inverted relation, so addtoroot shouldn't be set to zero */
549 if (ob->type==OB_CAMERA) {
550 Camera *cam = (Camera *)ob->data;
552 dag_add_driver_relation(cam->adt, dag, node, 1);
554 node2 = dag_get_node(dag, cam->dof_ob);
555 dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Camera DoF");
558 if (ob->type==OB_LAMP) {
559 Lamp *la = (Lamp *)ob->data;
561 dag_add_driver_relation(la->adt, dag, node, 1);
564 if (ob->transflag & OB_DUPLI) {
565 if((ob->transflag & OB_DUPLIGROUP) && ob->dup_group) {
567 for(go= ob->dup_group->gobject.first; go; go= go->next) {
569 node2 = dag_get_node(dag, go->ob);
570 /* node2 changes node1, this keeps animations updated in groups?? not logical? */
571 dag_add_relation(dag, node2, node, DAG_RL_OB_OB, "Dupligroup");
577 /* softbody collision */
578 if((ob->type==OB_MESH) || (ob->type==OB_CURVE) || (ob->type==OB_LATTICE))
579 if(modifiers_isSoftbodyEnabled(ob) || modifiers_isClothEnabled(ob))
580 dag_add_collision_field_relation(dag, scene, ob, node);
582 if (ob->type==OB_MBALL) {
583 Object *mom= find_basis_mball(scene, ob);
585 node2 = dag_get_node(dag, mom);
586 dag_add_relation(dag,node,node2,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Metaball"); // mom depends on children!
589 else if (ob->type==OB_CURVE) {
592 node2 = dag_get_node(dag, cu->bevobj);
593 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Curve Bevel");
596 node2 = dag_get_node(dag, cu->taperobj);
597 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Curve Taper");
600 dag_add_driver_relation(cu->adt, dag, node, 1);
602 else if(ob->type==OB_FONT) {
604 if(cu->textoncurve) {
605 node2 = dag_get_node(dag, cu->textoncurve);
606 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Texture On Curve");
610 psys= ob->particlesystem.first;
612 ParticleEffectorCache *nec;
615 for(; psys; psys=psys->next) {
616 ParticleSettings *part= psys->part;
618 dag_add_relation(dag, node, node, DAG_RL_OB_DATA, "Particle-Object Relation");
620 if(psys->flag & PSYS_DISABLED || psys->flag & PSYS_DELETE)
623 if(part->phystype==PART_PHYS_KEYED && psys->keyed_ob &&
624 BLI_findlink(&psys->keyed_ob->particlesystem,psys->keyed_psys-1)) {
625 node2 = dag_get_node(dag, psys->keyed_ob);
626 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA, "Particle Keyed Physics");
629 if(part->draw_as == PART_DRAW_OB && part->dup_ob) {
630 node2 = dag_get_node(dag, part->dup_ob);
631 dag_add_relation(dag, node, node2, DAG_RL_OB_OB, "Particle Object Visualisation");
632 if(part->dup_ob->type == OB_MBALL)
633 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA, "Particle Object Visualisation");
636 if(part->draw_as == PART_DRAW_GR && part->dup_group) {
637 for(go=part->dup_group->gobject.first; go; go=go->next) {
638 node2 = dag_get_node(dag, go->ob);
639 dag_add_relation(dag, node, node2, DAG_RL_OB_OB, "Particle Group Visualisation");
643 if(psys->effectors.first)
644 psys_end_effectors(psys);
645 psys_init_effectors(scene, ob, psys->part->eff_group, psys);
647 if(psys->effectors.first) {
648 for(nec= psys->effectors.first; nec; nec= nec->next) {
649 Object *ob1= nec->ob;
651 if(nec->type & PSYS_EC_EFFECTOR) {
652 node2 = dag_get_node(dag, ob1);
653 if(ob1->pd->forcefield==PFIELD_GUIDE)
654 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Particle Field");
656 dag_add_relation(dag, node2, node, DAG_RL_OB_DATA, "Particle Field");
658 else if(nec->type & PSYS_EC_DEFLECT) {
659 node2 = dag_get_node(dag, ob1);
660 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Particle Collision");
662 else if(nec->type & PSYS_EC_PARTICLE) {
663 node2 = dag_get_node(dag, ob1);
664 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA, "Particle Field");
667 if(nec->type & PSYS_EC_REACTOR) {
668 node2 = dag_get_node(dag, ob1);
669 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA, "Particle Reactor");
676 for (con = ob->constraints.first; con; con=con->next) {
677 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
678 ListBase targets = {NULL, NULL};
679 bConstraintTarget *ct;
681 if (cti && cti->get_constraint_targets) {
682 cti->get_constraint_targets(con, &targets);
684 for (ct= targets.first; ct; ct= ct->next) {
692 node2 = dag_get_node(dag, obt);
693 if (ELEM(con->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO))
694 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB|DAG_RL_OB_OB, cti->name);
696 if (ELEM3(obt->type, OB_ARMATURE, OB_MESH, OB_LATTICE) && (ct->subtarget[0]))
697 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB|DAG_RL_OB_OB, cti->name);
699 dag_add_relation(dag, node2, node, DAG_RL_OB_OB, cti->name);
704 if (cti->flush_constraint_targets)
705 cti->flush_constraint_targets(con, &targets, 1);
710 dag_add_relation(dag,scenenode,node,DAG_RL_SCENE, "Scene Relation");
713 struct DagForest *build_dag(struct Scene *sce, short mask)
733 /* add base node for scene. scene is always the first node in DAG */
734 scenenode = dag_add_node(dag, sce);
736 /* add current scene objects */
737 for(base = sce->base.first; base; base= base->next) {
740 build_dag_object(dag, scenenode, sce, ob, mask);
742 build_dag_object(dag, scenenode, sce, ob->proxy, mask);
744 /* handled in next loop */
746 ob->dup_group->id.flag |= LIB_DOIT;
749 /* add groups used in current scene objects */
750 for(group= G.main->group.first; group; group= group->id.next) {
751 if(group->id.flag & LIB_DOIT) {
752 for(go= group->gobject.first; go; go= go->next) {
753 build_dag_object(dag, scenenode, sce, go->ob, mask);
755 group->id.flag &= ~LIB_DOIT;
759 /* Now all relations were built, but we need to solve 1 exceptional case;
760 When objects have multiple "parents" (for example parent + constraint working on same object)
761 the relation type has to be synced. One of the parents can change, and should give same event to child */
763 /* nodes were callocced, so we can use node->color for temporal storage */
764 for(node = sce->theDag->DagNode.first; node; node= node->next) {
765 if(node->type==ID_OB) {
766 for(itA = node->child; itA; itA= itA->next) {
767 if(itA->node->type==ID_OB) {
768 itA->node->color |= itA->type;
773 /* now set relations equal, so that when only one parent changes, the correct recalcs are found */
774 for(node = sce->theDag->DagNode.first; node; node= node->next) {
775 if(node->type==ID_OB) {
776 for(itA = node->child; itA; itA= itA->next) {
777 if(itA->node->type==ID_OB) {
778 itA->type |= itA->node->color;
784 // cycle detection and solving
785 // solve_cycles(dag);
791 void free_forest(DagForest *Dag)
792 { /* remove all nodes and deps */
796 DagNode *itN = Dag->DagNode.first;
817 Dag->DagNode.first = NULL;
818 Dag->DagNode.last = NULL;
823 DagNode * dag_find_node (DagForest *forest,void * fob)
825 DagNode *node = forest->DagNode.first;
835 static int ugly_hack_sorry= 1; // prevent type check
837 /* no checking of existance, use dag_find_node first or dag_get_node */
838 DagNode * dag_add_node (DagForest *forest, void * fob)
842 node = MEM_callocN(sizeof(DagNode),"DAG node");
845 node->color = DAG_WHITE;
847 if(ugly_hack_sorry) node->type = GS(((ID *) fob)->name); // sorry, done for pose sorting
848 if (forest->numNodes) {
849 ((DagNode *) forest->DagNode.last)->next = node;
850 forest->DagNode.last = node;
853 forest->DagNode.last = node;
854 forest->DagNode.first = node;
855 forest->numNodes = 1;
861 DagNode * dag_get_node (DagForest *forest,void * fob)
865 node = dag_find_node (forest, fob);
867 node = dag_add_node(forest, fob);
873 DagNode * dag_get_sub_node (DagForest *forest,void * fob)
876 DagAdjList *mainchild, *prev=NULL;
878 mainchild = ((DagNode *) forest->DagNode.first)->child;
879 /* remove from first node (scene) adj list if present */
881 if (mainchild->node == fob) {
883 prev->next = mainchild->next;
884 MEM_freeN(mainchild);
887 ((DagNode *) forest->DagNode.first)->child = mainchild->next;
888 MEM_freeN(mainchild);
893 mainchild = mainchild->next;
895 node = dag_find_node (forest, fob);
897 node = dag_add_node(forest, fob);
901 static void dag_add_parent_relation(DagForest *forest, DagNode *fob1, DagNode *fob2, short rel, char *name)
903 DagAdjList *itA = fob2->parent;
905 while (itA) { /* search if relation exist already */
906 if (itA->node == fob1) {
913 /* create new relation and insert at head. MALLOC alert! */
914 itA = MEM_mallocN(sizeof(DagAdjList),"DAG adj list");
918 itA->next = fob2->parent;
923 void dag_add_relation(DagForest *forest, DagNode *fob1, DagNode *fob2, short rel, char *name)
925 DagAdjList *itA = fob1->child;
927 /* parent relation is for cycle checking */
928 dag_add_parent_relation(forest, fob1, fob2, rel, name);
930 while (itA) { /* search if relation exist already */
931 if (itA->node == fob2) {
938 /* create new relation and insert at head. MALLOC alert! */
939 itA = MEM_mallocN(sizeof(DagAdjList),"DAG adj list");
943 itA->next = fob1->child;
948 static char *dag_node_name(DagNode *node)
952 else if(ugly_hack_sorry)
953 return ((ID*)(node->ob))->name+2;
955 return ((bPoseChannel*)(node->ob))->name;
959 static void dag_node_print_dependencies(DagNode *node)
963 printf("%s depends on:\n", dag_node_name(node));
965 for(itA= node->parent; itA; itA= itA->next)
966 printf(" %s through %s\n", dag_node_name(itA->node), itA->name);
971 static int dag_node_print_dependency_recurs(DagNode *node, DagNode *endnode)
975 if(node->color == DAG_BLACK)
978 node->color= DAG_BLACK;
983 for(itA= node->parent; itA; itA= itA->next) {
984 if(dag_node_print_dependency_recurs(itA->node, endnode)) {
985 printf(" %s depends on %s through %s.\n", dag_node_name(node), dag_node_name(itA->node), itA->name);
993 static void dag_node_print_dependency_cycle(DagForest *dag, DagNode *startnode, DagNode *endnode, char *name)
997 for(node = dag->DagNode.first; node; node= node->next)
998 node->color= DAG_WHITE;
1000 printf(" %s depends on %s through %s.\n", dag_node_name(endnode), dag_node_name(startnode), name);
1001 dag_node_print_dependency_recurs(startnode, endnode);
1005 static int dag_node_recurs_level(DagNode *node, int level)
1010 node->color= DAG_BLACK; /* done */
1013 for(itA= node->parent; itA; itA= itA->next) {
1014 if(itA->node->color==DAG_WHITE) {
1015 itA->node->ancestor_count= dag_node_recurs_level(itA->node, level);
1016 newlevel= MAX2(newlevel, level+itA->node->ancestor_count);
1019 newlevel= MAX2(newlevel, level+itA->node->ancestor_count);
1025 static void dag_check_cycle(DagForest *dag)
1030 /* tag nodes unchecked */
1031 for(node = dag->DagNode.first; node; node= node->next)
1032 node->color= DAG_WHITE;
1034 for(node = dag->DagNode.first; node; node= node->next) {
1035 if(node->color==DAG_WHITE) {
1036 node->ancestor_count= dag_node_recurs_level(node, 0);
1040 /* check relations, and print errors */
1041 for(node = dag->DagNode.first; node; node= node->next) {
1042 for(itA= node->parent; itA; itA= itA->next) {
1043 if(itA->node->ancestor_count > node->ancestor_count) {
1044 if(node->ob && itA->node->ob) {
1045 printf("Dependency cycle detected:\n");
1046 dag_node_print_dependency_cycle(dag, itA->node, node, itA->name);
1052 /* parent relations are only needed for cycle checking, so free now */
1053 for(node = dag->DagNode.first; node; node= node->next) {
1054 while (node->parent) {
1055 itA = node->parent->next;
1056 MEM_freeN(node->parent);
1063 * MainDAG is the DAG of all objects in current scene
1064 * used only for drawing there is one also in each scene
1066 static DagForest * MainDag = NULL;
1068 DagForest *getMainDag(void)
1074 void setMainDag(DagForest *dag)
1082 * in theory we should sweep the whole array
1083 * but in our case the first node is the scene
1084 * and is linked to every other object
1086 * for general case we will need to add outer loop
1090 * ToDo : change pos kludge
1093 /* adjust levels for drawing in oops space */
1094 void graph_bfs(void)
1097 DagNodeQueue *nqueue;
1103 /* fprintf(stderr,"starting BFS \n ------------\n"); */
1104 nqueue = queue_create(DAGQUEUEALLOC);
1105 for ( i=0; i<50; i++)
1109 * dagnode.first is alway the root (scene)
1111 node = MainDag->DagNode.first;
1113 node->color = DAG_WHITE;
1114 node->BFS_dist = 9999;
1119 node = MainDag->DagNode.first;
1120 if (node->color == DAG_WHITE) {
1121 node->color = DAG_GRAY;
1123 push_queue(nqueue,node);
1124 while(nqueue->count) {
1125 node = pop_queue(nqueue);
1127 minheight = pos[node->BFS_dist];
1129 while(itA != NULL) {
1130 if((itA->node->color == DAG_WHITE) ) {
1131 itA->node->color = DAG_GRAY;
1132 itA->node->BFS_dist = node->BFS_dist + 1;
1133 itA->node->k = (float) minheight;
1134 push_queue(nqueue,itA->node);
1138 fprintf(stderr,"bfs not dag tree edge color :%i \n",itA->node->color);
1144 if (pos[node->BFS_dist] > node->k ) {
1145 pos[node->BFS_dist] += 1;
1146 node->k = (float) pos[node->BFS_dist];
1148 pos[node->BFS_dist] = (int) node->k +1;
1150 set_node_xy(node, node->BFS_dist*DEPSX*2, pos[node->BFS_dist]*DEPSY*2);
1151 node->color = DAG_BLACK;
1153 fprintf(stderr,"BFS node : %20s %i %5.0f %5.0f\n",((ID *) node->ob)->name,node->BFS_dist, node->x, node->y);
1157 queue_delete(nqueue);
1160 int pre_and_post_BFS(DagForest *dag, short mask, graph_action_func pre_func, graph_action_func post_func, void **data) {
1163 node = dag->DagNode.first;
1164 return pre_and_post_source_BFS(dag, mask, node, pre_func, post_func, data);
1168 int pre_and_post_source_BFS(DagForest *dag, short mask, DagNode *source, graph_action_func pre_func, graph_action_func post_func, void **data)
1171 DagNodeQueue *nqueue;
1174 /* fprintf(stderr,"starting BFS \n ------------\n"); */
1177 * dagnode.first is alway the root (scene)
1179 node = dag->DagNode.first;
1180 nqueue = queue_create(DAGQUEUEALLOC);
1182 node->color = DAG_WHITE;
1183 node->BFS_dist = 9999;
1188 if (node->color == DAG_WHITE) {
1189 node->color = DAG_GRAY;
1191 pre_func(node->ob,data);
1193 while(nqueue->count) {
1194 node = pop_queue(nqueue);
1197 while(itA != NULL) {
1198 if((itA->node->color == DAG_WHITE) && (itA->type & mask)) {
1199 itA->node->color = DAG_GRAY;
1200 itA->node->BFS_dist = node->BFS_dist + 1;
1201 push_queue(nqueue,itA->node);
1202 pre_func(node->ob,data);
1205 else { // back or cross edge
1210 post_func(node->ob,data);
1211 node->color = DAG_BLACK;
1213 fprintf(stderr,"BFS node : %20s %i %5.0f %5.0f\n",((ID *) node->ob)->name,node->BFS_dist, node->x, node->y);
1217 queue_delete(nqueue);
1221 /* non recursive version of DFS, return queue -- outer loop present to catch odd cases (first level cycles)*/
1222 DagNodeQueue * graph_dfs(void)
1225 DagNodeQueue *nqueue;
1226 DagNodeQueue *retqueue;
1236 *fprintf(stderr,"starting DFS \n ------------\n");
1238 nqueue = queue_create(DAGQUEUEALLOC);
1239 retqueue = queue_create(MainDag->numNodes);
1240 for ( i=0; i<50; i++)
1244 * dagnode.first is alway the root (scene)
1246 node = MainDag->DagNode.first;
1248 node->color = DAG_WHITE;
1249 node->DFS_dist = 9999;
1250 node->DFS_dvtm = node->DFS_fntm = 9999;
1257 node = MainDag->DagNode.first;
1260 if (node->color == DAG_WHITE) {
1261 node->color = DAG_GRAY;
1263 node->DFS_dvtm = time;
1265 push_stack(nqueue,node);
1267 while(nqueue->count) {
1268 //graph_print_queue(nqueue);
1271 node = get_top_node_queue(nqueue);
1273 minheight = pos[node->DFS_dist];
1276 while(itA != NULL) {
1277 if((itA->node->color == DAG_WHITE) ) {
1278 itA->node->DFS_dvtm = time;
1279 itA->node->color = DAG_GRAY;
1282 itA->node->DFS_dist = node->DFS_dist + 1;
1283 itA->node->k = (float) minheight;
1284 push_stack(nqueue,itA->node);
1288 if (itA->node->color == DAG_GRAY) { // back edge
1289 fprintf(stderr,"dfs back edge :%15s %15s \n",((ID *) node->ob)->name, ((ID *) itA->node->ob)->name);
1291 } else if (itA->node->color == DAG_BLACK) {
1293 /* already processed node but we may want later to change distance either to shorter to longer.
1294 * DFS_dist is the first encounter
1296 /*if (node->DFS_dist >= itA->node->DFS_dist)
1297 itA->node->DFS_dist = node->DFS_dist + 1;
1299 fprintf(stderr,"dfs forward or cross edge :%15s %i-%i %15s %i-%i \n",
1300 ((ID *) node->ob)->name,
1303 ((ID *) itA->node->ob)->name,
1304 itA->node->DFS_dvtm,
1305 itA->node->DFS_fntm);
1308 fprintf(stderr,"dfs unknown edge \n");
1314 node = pop_queue(nqueue);
1315 node->color = DAG_BLACK;
1317 node->DFS_fntm = time;
1319 if (node->DFS_dist > maxpos)
1320 maxpos = node->DFS_dist;
1321 if (pos[node->DFS_dist] > node->k ) {
1322 pos[node->DFS_dist] += 1;
1323 node->k = (float) pos[node->DFS_dist];
1325 pos[node->DFS_dist] = (int) node->k +1;
1327 set_node_xy(node, node->DFS_dist*DEPSX*2, pos[node->DFS_dist]*DEPSY*2);
1330 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 );
1332 push_stack(retqueue,node);
1339 // fprintf(stderr,"i size : %i \n", maxpos);
1341 queue_delete(nqueue);
1346 int pre_and_post_DFS(DagForest *dag, short mask, graph_action_func pre_func, graph_action_func post_func, void **data) {
1349 node = dag->DagNode.first;
1350 return pre_and_post_source_DFS(dag, mask, node, pre_func, post_func, data);
1353 int pre_and_post_source_DFS(DagForest *dag, short mask, DagNode *source, graph_action_func pre_func, graph_action_func post_func, void **data)
1356 DagNodeQueue *nqueue;
1362 *fprintf(stderr,"starting DFS \n ------------\n");
1364 nqueue = queue_create(DAGQUEUEALLOC);
1367 * dagnode.first is alway the root (scene)
1369 node = dag->DagNode.first;
1371 node->color = DAG_WHITE;
1372 node->DFS_dist = 9999;
1373 node->DFS_dvtm = node->DFS_fntm = 9999;
1382 if (node->color == DAG_WHITE) {
1383 node->color = DAG_GRAY;
1385 node->DFS_dvtm = time;
1387 push_stack(nqueue,node);
1388 pre_func(node->ob,data);
1390 while(nqueue->count) {
1392 node = get_top_node_queue(nqueue);
1395 while(itA != NULL) {
1396 if((itA->node->color == DAG_WHITE) && (itA->type & mask) ) {
1397 itA->node->DFS_dvtm = time;
1398 itA->node->color = DAG_GRAY;
1401 itA->node->DFS_dist = node->DFS_dist + 1;
1402 push_stack(nqueue,itA->node);
1403 pre_func(node->ob,data);
1408 if (itA->node->color == DAG_GRAY) {// back edge
1411 // else if (itA->node->color == DAG_BLACK) { // cross or forward
1418 node = pop_queue(nqueue);
1419 node->color = DAG_BLACK;
1421 node->DFS_fntm = time;
1423 post_func(node->ob,data);
1429 queue_delete(nqueue);
1434 // used to get the obs owning a datablock
1435 struct DagNodeQueue *get_obparents(struct DagForest *dag, void *ob)
1437 DagNode * node, *node1;
1438 DagNodeQueue *nqueue;
1441 node = dag_find_node(dag,ob);
1445 else if (node->ancestor_count == 1) { // simple case
1446 nqueue = queue_create(1);
1447 push_queue(nqueue,node);
1448 } else { // need to go over the whole dag for adj list
1449 nqueue = queue_create(node->ancestor_count);
1451 node1 = dag->DagNode.first;
1453 if (node1->DFS_fntm > node->DFS_fntm) { // a parent is finished after child. must check adj list
1455 while(itA != NULL) {
1456 if ((itA->node == node) && (itA->type == DAG_RL_DATA)) {
1457 push_queue(nqueue,node);
1462 node1 = node1->next;
1468 struct DagNodeQueue *get_first_ancestors(struct DagForest *dag, void *ob)
1470 DagNode * node, *node1;
1471 DagNodeQueue *nqueue;
1474 node = dag_find_node(dag,ob);
1476 // need to go over the whole dag for adj list
1477 nqueue = queue_create(node->ancestor_count);
1479 node1 = dag->DagNode.first;
1481 if (node1->DFS_fntm > node->DFS_fntm) {
1483 while(itA != NULL) {
1484 if (itA->node == node) {
1485 push_queue(nqueue,node);
1490 node1 = node1->next;
1496 // standard DFS list
1497 struct DagNodeQueue *get_all_childs(struct DagForest *dag, void *ob)
1500 DagNodeQueue *nqueue;
1501 DagNodeQueue *retqueue;
1506 nqueue = queue_create(DAGQUEUEALLOC);
1507 retqueue = queue_create(dag->numNodes); // was MainDag... why? (ton)
1509 node = dag->DagNode.first;
1511 node->color = DAG_WHITE;
1517 node = dag_find_node(dag, ob); // could be done in loop above (ton)
1518 if(node) { // can be null for newly added objects
1520 node->color = DAG_GRAY;
1522 push_stack(nqueue,node);
1524 while(nqueue->count) {
1527 node = get_top_node_queue(nqueue);
1530 while(itA != NULL) {
1531 if((itA->node->color == DAG_WHITE) ) {
1532 itA->node->DFS_dvtm = time;
1533 itA->node->color = DAG_GRAY;
1536 push_stack(nqueue,itA->node);
1544 node = pop_queue(nqueue);
1545 node->color = DAG_BLACK;
1548 push_stack(retqueue,node);
1552 queue_delete(nqueue);
1557 short are_obs_related(struct DagForest *dag, void *ob1, void *ob2) {
1561 node = dag_find_node(dag, ob1);
1564 while(itA != NULL) {
1565 if((itA->node->ob == ob2) ) {
1566 return itA->node->type;
1570 return DAG_NO_RELATION;
1573 int is_acyclic( DagForest *dag) {
1574 return dag->is_acyclic;
1577 void set_node_xy(DagNode *node, float x, float y)
1584 /* debug test functions */
1586 void graph_print_queue(DagNodeQueue *nqueue)
1588 DagNodeQueueElem *queueElem;
1590 queueElem = nqueue->first;
1592 fprintf(stderr,"** %s %i %i-%i ",((ID *) queueElem->node->ob)->name,queueElem->node->color,queueElem->node->DFS_dvtm,queueElem->node->DFS_fntm);
1593 queueElem = queueElem->next;
1595 fprintf(stderr,"\n");
1598 void graph_print_queue_dist(DagNodeQueue *nqueue)
1600 DagNodeQueueElem *queueElem;
1603 queueElem = nqueue->first;
1604 max = queueElem->node->DFS_fntm;
1607 fprintf(stderr,"** %25s %2.2i-%2.2i ",((ID *) queueElem->node->ob)->name,queueElem->node->DFS_dvtm,queueElem->node->DFS_fntm);
1608 while (count < queueElem->node->DFS_dvtm-1) { fputc(' ',stderr); count++;}
1610 while (count < queueElem->node->DFS_fntm-2) { fputc('-',stderr); count++;}
1614 queueElem = queueElem->next;
1616 fprintf(stderr,"\n");
1619 void graph_print_adj_list(void)
1624 node = (getMainDag())->DagNode.first;
1626 fprintf(stderr,"node : %s col: %i",((ID *) node->ob)->name, node->color);
1629 fprintf(stderr,"-- %s ",((ID *) itA->node->ob)->name);
1633 fprintf(stderr,"\n");
1638 /* ************************ API *********************** */
1640 /* groups with objects in this scene need to be put in the right order as well */
1641 static void scene_sort_groups(Scene *sce)
1648 /* test; are group objects all in this scene? */
1649 for(ob= G.main->object.first; ob; ob= ob->id.next) {
1650 ob->id.flag &= ~LIB_DOIT;
1651 ob->id.newid= NULL; /* newid abuse for GroupObject */
1653 for(base = sce->base.first; base; base= base->next)
1654 base->object->id.flag |= LIB_DOIT;
1656 for(group= G.main->group.first; group; group= group->id.next) {
1657 for(go= group->gobject.first; go; go= go->next) {
1658 if((go->ob->id.flag & LIB_DOIT)==0)
1661 /* this group is entirely in this scene */
1663 ListBase listb= {NULL, NULL};
1665 for(go= group->gobject.first; go; go= go->next)
1666 go->ob->id.newid= (ID *)go;
1668 /* in order of sorted bases we reinsert group objects */
1669 for(base = sce->base.first; base; base= base->next) {
1671 if(base->object->id.newid) {
1672 go= (GroupObject *)base->object->id.newid;
1673 base->object->id.newid= NULL;
1674 BLI_remlink( &group->gobject, go);
1675 BLI_addtail( &listb, go);
1678 /* copy the newly sorted listbase */
1679 group->gobject= listb;
1684 /* sort the base list on dependency order */
1685 void DAG_scene_sort(struct Scene *sce)
1688 DagNodeQueue *nqueue;
1695 tempbase.first= tempbase.last= NULL;
1697 build_dag(sce, DAG_RL_ALL_BUT_DATA);
1699 dag_check_cycle(sce->theDag);
1701 nqueue = queue_create(DAGQUEUEALLOC);
1703 for(node = sce->theDag->DagNode.first; node; node= node->next) {
1704 node->color = DAG_WHITE;
1709 node = sce->theDag->DagNode.first;
1711 node->color = DAG_GRAY;
1713 push_stack(nqueue,node);
1715 while(nqueue->count) {
1718 node = get_top_node_queue(nqueue);
1721 while(itA != NULL) {
1722 if((itA->node->color == DAG_WHITE) ) {
1723 itA->node->DFS_dvtm = time;
1724 itA->node->color = DAG_GRAY;
1727 push_stack(nqueue,itA->node);
1736 node = pop_queue(nqueue);
1737 if (node->ob == sce) // we are done
1739 node->color = DAG_BLACK;
1742 base = sce->base.first;
1743 while (base && base->object != node->ob)
1746 BLI_remlink(&sce->base,base);
1747 BLI_addhead(&tempbase,base);
1753 // temporal correction for circular dependancies
1754 base = sce->base.first;
1756 BLI_remlink(&sce->base,base);
1757 BLI_addhead(&tempbase,base);
1759 printf("cyclic %s\n", base->object->id.name);
1760 base = sce->base.first;
1763 sce->base = tempbase;
1764 queue_delete(nqueue);
1766 /* all groups with objects in this scene gets resorted too */
1767 scene_sort_groups(sce);
1770 printf("\nordered\n");
1771 for(base = sce->base.first; base; base= base->next) {
1772 printf(" %s\n", base->object->id.name);
1776 sce->recalc |= SCE_PRV_CHANGED; /* test for 3d preview */
1779 /* node was checked to have lasttime != curtime and is if type ID_OB */
1780 static void flush_update_node(DagNode *node, unsigned int layer, int curtime)
1784 int oldflag, changed=0;
1785 unsigned int all_layer;
1787 node->lasttime= curtime;
1790 if(ob && (ob->recalc & OB_RECALC)) {
1793 /* got an object node that changes, now check relations */
1794 for(itA = node->child; itA; itA= itA->next) {
1795 all_layer |= itA->lay;
1796 /* the relationship is visible */
1797 if((itA->lay & layer)) { // XXX || (itA->node->ob == obedit)
1798 if(itA->node->type==ID_OB) {
1800 oldflag= obc->recalc;
1802 /* got a ob->obc relation, now check if flag needs flush */
1803 if(ob->recalc & OB_RECALC_OB) {
1804 if(itA->type & DAG_RL_OB_OB) {
1805 //printf("ob %s changes ob %s\n", ob->id.name, obc->id.name);
1806 obc->recalc |= OB_RECALC_OB;
1808 if(itA->type & DAG_RL_OB_DATA) {
1809 //printf("ob %s changes obdata %s\n", ob->id.name, obc->id.name);
1810 obc->recalc |= OB_RECALC_DATA;
1813 if(ob->recalc & OB_RECALC_DATA) {
1814 if(itA->type & DAG_RL_DATA_OB) {
1815 //printf("obdata %s changes ob %s\n", ob->id.name, obc->id.name);
1816 obc->recalc |= OB_RECALC_OB;
1818 if(itA->type & DAG_RL_DATA_DATA) {
1819 //printf("obdata %s changes obdata %s\n", ob->id.name, obc->id.name);
1820 obc->recalc |= OB_RECALC_DATA;
1823 if(oldflag!=obc->recalc) changed= 1;
1827 /* even nicer, we can clear recalc flags... */
1828 if((all_layer & layer)==0) { // XXX && (ob != obedit)) {
1829 /* but existing displaylists or derivedmesh should be freed */
1830 if(ob->recalc & OB_RECALC_DATA)
1831 object_free_display(ob);
1833 ob->recalc &= ~OB_RECALC;
1837 /* check case where child changes and parent forcing obdata to change */
1838 /* should be done regardless if this ob has recalc set */
1839 /* could merge this in with loop above...? (ton) */
1840 for(itA = node->child; itA; itA= itA->next) {
1841 /* the relationship is visible */
1842 if((itA->lay & layer)) { // XXX || (itA->node->ob == obedit)
1843 if(itA->node->type==ID_OB) {
1846 if((obc->recalc & OB_RECALC)==OB_RECALC_OB) {
1847 /* parent has deforming info */
1848 if(itA->type & (DAG_RL_OB_DATA|DAG_RL_DATA_DATA)) {
1849 // printf("parent %s changes ob %s\n", ob->id.name, obc->id.name);
1850 obc->recalc |= OB_RECALC_DATA;
1857 /* we only go deeper if node not checked or something changed */
1858 for(itA = node->child; itA; itA= itA->next) {
1859 if(changed || itA->node->lasttime!=curtime)
1860 flush_update_node(itA->node, layer, curtime);
1865 /* node was checked to have lasttime != curtime , and is of type ID_OB */
1866 static unsigned int flush_layer_node(Scene *sce, DagNode *node, int curtime)
1871 node->lasttime= curtime;
1873 for(base= sce->base.first; base; base= base->next) {
1874 if(node->ob == base->object) {
1875 node->lay= ((Object *)node->ob)->lay;
1880 for(itA = node->child; itA; itA= itA->next) {
1881 if(itA->node->type==ID_OB) {
1882 if(itA->node->lasttime!=curtime) {
1883 itA->lay= flush_layer_node(sce, itA->node, curtime); // lay is only set once for each relation
1885 else itA->lay= itA->node->lay;
1887 node->lay |= itA->lay;
1894 /* node was checked to have lasttime != curtime , and is of type ID_OB */
1895 static void flush_pointcache_reset(DagNode *node, int curtime, int reset)
1900 node->lasttime= curtime;
1902 for(itA = node->child; itA; itA= itA->next) {
1903 if(itA->node->type==ID_OB) {
1904 if(itA->node->lasttime!=curtime) {
1905 ob= (Object*)(node->ob);
1907 if(reset || (ob->recalc & OB_RECALC)) {
1908 if(BKE_ptcache_object_reset(ob, PTCACHE_RESET_DEPSGRAPH))
1909 ob->recalc |= OB_RECALC_DATA;
1911 flush_pointcache_reset(itA->node, curtime, 1);
1914 flush_pointcache_reset(itA->node, curtime, 0);
1920 /* flushes all recalc flags in objects down the dependency tree */
1921 void DAG_scene_flush_update(Scene *sce, unsigned int lay, int time)
1928 if(sce->theDag==NULL) {
1929 printf("DAG zero... not allowed to happen!\n");
1930 DAG_scene_sort(sce);
1933 firstnode= sce->theDag->DagNode.first; // always scene node
1935 for(itA = firstnode->child; itA; itA= itA->next)
1938 /* first we flush the layer flags */
1939 sce->theDag->time++; // so we know which nodes were accessed
1940 lasttime= sce->theDag->time;
1942 for(itA = firstnode->child; itA; itA= itA->next)
1943 if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB)
1944 flush_layer_node(sce, itA->node, lasttime);
1946 /* then we use the relationships + layer info to flush update events */
1947 sce->theDag->time++; // so we know which nodes were accessed
1948 lasttime= sce->theDag->time;
1949 for(itA = firstnode->child; itA; itA= itA->next)
1950 if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB)
1951 flush_update_node(itA->node, lay, lasttime);
1953 /* if update is not due to time change, do pointcache clears */
1955 sce->theDag->time++; // so we know which nodes were accessed
1956 lasttime= sce->theDag->time;
1957 for(itA = firstnode->child; itA; itA= itA->next) {
1958 if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB) {
1959 ob= (Object*)(itA->node->ob);
1961 if(ob->recalc & OB_RECALC) {
1962 if(BKE_ptcache_object_reset(ob, PTCACHE_RESET_DEPSGRAPH))
1963 ob->recalc |= OB_RECALC_DATA;
1965 flush_pointcache_reset(itA->node, lasttime, 1);
1968 flush_pointcache_reset(itA->node, lasttime, 0);
1974 static int object_modifiers_use_time(Object *ob)
1978 for (md=ob->modifiers.first; md; md=md->next)
1979 if (modifier_dependsOnTime(md))
1985 static short animdata_use_time(AnimData *adt)
1989 if(adt==NULL) return 0;
1991 /* check action - only if assigned, and it has anim curves */
1992 if (adt->action && adt->action->curves.first)
1995 /* check NLA tracks + strips */
1996 for (nlt= adt->nla_tracks.first; nlt; nlt= nlt->next) {
1997 if (nlt->strips.first)
2004 static void dag_object_time_update_flags(Object *ob)
2006 if(ob->constraints.first) {
2008 for (con = ob->constraints.first; con; con=con->next) {
2009 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
2010 ListBase targets = {NULL, NULL};
2011 bConstraintTarget *ct;
2013 if (cti && cti->get_constraint_targets) {
2014 cti->get_constraint_targets(con, &targets);
2016 for (ct= targets.first; ct; ct= ct->next) {
2018 ob->recalc |= OB_RECALC_OB;
2023 if (cti->flush_constraint_targets)
2024 cti->flush_constraint_targets(con, &targets, 1);
2029 if(ob->scriptlink.totscript) ob->recalc |= OB_RECALC_OB;
2032 /* motion path or bone child */
2033 if(ob->parent->type==OB_CURVE || ob->parent->type==OB_ARMATURE) ob->recalc |= OB_RECALC_OB;
2036 #if 0 // XXX old animation system
2037 if(ob->nlastrips.first) {
2039 bActionStrip *strip;
2040 /* this case is for groups with nla, whilst nla target has no action or nla */
2041 for(strip= ob->nlastrips.first; strip; strip= strip->next) {
2043 strip->object->recalc |= OB_RECALC;
2047 #endif // XXX old animation system
2049 if(animdata_use_time(ob->adt)) {
2050 ob->recalc |= OB_RECALC;
2051 ob->adt->recalc |= ADT_RECALC_ANIM;
2054 if((ob->adt) && (ob->type==OB_ARMATURE)) ob->recalc |= OB_RECALC_DATA;
2056 if(object_modifiers_use_time(ob)) ob->recalc |= OB_RECALC_DATA;
2057 if((ob->pose) && (ob->pose->flag & POSE_CONSTRAINTS_TIMEDEPEND)) ob->recalc |= OB_RECALC_DATA;
2068 if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
2069 ob->recalc |= OB_RECALC_DATA;
2070 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
2073 if(ob->particlesystem.first)
2074 ob->recalc |= OB_RECALC_DATA;
2080 if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
2081 ob->recalc |= OB_RECALC_DATA;
2082 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
2088 if(cu->nurb.first==NULL && cu->str && cu->vfont)
2089 ob->recalc |= OB_RECALC_DATA;
2094 if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
2095 ob->recalc |= OB_RECALC_DATA;
2096 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
2101 if(ob->transflag & OB_DUPLI) ob->recalc |= OB_RECALC_DATA;
2105 if(ob->particlesystem.first) {
2106 ParticleSystem *psys= ob->particlesystem.first;
2108 for(; psys; psys=psys->next) {
2109 if(psys_check_enabled(ob, psys)) {
2110 ob->recalc |= OB_RECALC_DATA;
2118 /* flag all objects that need recalc, for changes in time for example */
2119 void DAG_scene_update_flags(Scene *scene, unsigned int lay)
2127 /* set ob flags where animated systems are */
2128 for(SETLOOPER(scene, base)) {
2131 /* now if DagNode were part of base, the node->lay could be checked... */
2132 /* we do all now, since the scene_flush checks layers and clears recalc flags even */
2133 dag_object_time_update_flags(ob);
2135 /* handled in next loop */
2137 ob->dup_group->id.flag |= LIB_DOIT;
2140 /* we do groups each once */
2141 for(group= G.main->group.first; group; group= group->id.next) {
2142 if(group->id.flag & LIB_DOIT) {
2143 for(go= group->gobject.first; go; go= go->next) {
2144 dag_object_time_update_flags(go->ob);
2149 for(sce= scene; sce; sce= sce->set)
2150 DAG_scene_flush_update(sce, lay, 1);
2152 /* test: set time flag, to disable baked systems to update */
2153 for(SETLOOPER(scene, base)) {
2156 ob->recalc |= OB_RECALC_TIME;
2159 /* hrmf... an exception to look at once, for invisible camera object we do it over */
2161 dag_object_time_update_flags(scene->camera);
2163 /* and store the info in groupobject */
2164 for(group= G.main->group.first; group; group= group->id.next) {
2165 if(group->id.flag & LIB_DOIT) {
2166 for(go= group->gobject.first; go; go= go->next) {
2167 go->recalc= go->ob->recalc;
2168 // printf("ob %s recalc %d\n", go->ob->id.name, go->recalc);
2170 group->id.flag &= ~LIB_DOIT;
2177 /* flag this object and all its relations to recalc */
2178 /* if you need to do more objects, tag object yourself and
2179 use DAG_scene_flush_update() in end */
2180 void DAG_object_flush_update(Scene *sce, Object *ob, short flag)
2183 if(ob==NULL || sce->theDag==NULL) return;
2186 BKE_ptcache_object_reset(ob, PTCACHE_RESET_DEPSGRAPH);
2188 /* all users of this ob->data should be checked */
2189 /* BUT! displists for curves are still only on cu */
2190 if(flag & OB_RECALC_DATA) {
2191 if(ob->type!=OB_CURVE && ob->type!=OB_SURF) {
2193 if(id && id->us>1) {
2194 /* except when there's a key and shapes are locked */
2195 if(ob_get_key(ob) && (ob->shapeflag & (OB_SHAPE_LOCK|OB_SHAPE_TEMPLOCK)));
2198 for (obt=G.main->object.first; obt; obt= obt->id.next) {
2199 if (obt != ob && obt->data==ob->data) {
2200 obt->recalc |= OB_RECALC_DATA;
2201 BKE_ptcache_object_reset(obt, PTCACHE_RESET_DEPSGRAPH);
2209 // XXX if(G.curscreen)
2210 // DAG_scene_flush_update(sce, dag_screen_view3d_layers(), 0);
2212 DAG_scene_flush_update(sce, sce->lay, 0);
2215 /* recursively descends tree, each node only checked once */
2216 /* node is checked to be of type object */
2217 static int parent_check_node(DagNode *node, int curtime)
2221 node->lasttime= curtime;
2223 if(node->color==DAG_GRAY)
2226 for(itA = node->child; itA; itA= itA->next) {
2227 if(itA->node->type==ID_OB) {
2229 if(itA->node->color==DAG_GRAY)
2232 /* descend if not done */
2233 if(itA->node->lasttime!=curtime) {
2234 itA->node->color= parent_check_node(itA->node, curtime);
2236 if(itA->node->color==DAG_GRAY)
2245 /* all nodes that influence this object get tagged, for calculating the exact
2246 position of this object at a given timeframe */
2247 void DAG_object_update_flags(Scene *sce, Object *ob, unsigned int lay)
2252 /* tag nodes unchecked */
2253 for(node = sce->theDag->DagNode.first; node; node= node->next)
2254 node->color = DAG_WHITE;
2256 node= dag_find_node(sce->theDag, ob);
2258 /* object not in scene? then handle group exception. needs to be dagged once too */
2261 while( (group = find_group(ob, group)) ) {
2263 /* primitive; tag all... this call helps building groups for particles */
2264 for(go= group->gobject.first; go; go= go->next)
2265 go->ob->recalc= OB_RECALC;
2270 node->color = DAG_GRAY;
2272 sce->theDag->time++;
2273 node= sce->theDag->DagNode.first;
2274 for(itA = node->child; itA; itA= itA->next) {
2275 if(itA->node->type==ID_OB && itA->node->lasttime!=sce->theDag->time)
2276 itA->node->color= parent_check_node(itA->node, sce->theDag->time);
2279 /* set recalcs and flushes */
2280 DAG_scene_update_flags(sce, lay);
2282 /* now we clear recalcs, unless color is set */
2283 for(node = sce->theDag->DagNode.first; node; node= node->next) {
2284 if(node->type==ID_OB && node->color==DAG_WHITE) {
2285 Object *ob= node->ob;
2292 /* ******************* DAG FOR ARMATURE POSE ***************** */
2294 /* we assume its an armature with pose */
2295 void DAG_pose_sort(Object *ob)
2297 bPose *pose= ob->pose;
2298 bPoseChannel *pchan;
2301 DagNode *node2, *node3;
2304 DagNodeQueue *nqueue;
2310 ugly_hack_sorry= 0; // no ID structs
2312 rootnode = dag_add_node(dag, NULL); // node->ob becomes NULL
2314 /* we add the hierarchy and the constraints */
2315 for(pchan = pose->chanbase.first; pchan; pchan= pchan->next) {
2318 node = dag_get_node(dag, pchan);
2321 node2 = dag_get_node(dag, pchan->parent);
2322 dag_add_relation(dag, node2, node, 0, "Parent Relation");
2325 for (con = pchan->constraints.first; con; con=con->next) {
2326 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
2327 ListBase targets = {NULL, NULL};
2328 bConstraintTarget *ct;
2330 #if 0 // XXX old animation system... driver stuff to watch out for
2333 for(icu= con->ipo->curve.first; icu; icu= icu->next) {
2334 /* icu->driver->ob should actually point to ob->proxy if it
2335 * is a proxy, but since it wasn't set correct it older
2336 * files comparing with ob->proxy makes it work for those */
2337 if(icu->driver && (icu->driver->ob==ob || icu->driver->ob==ob->proxy)) {
2338 bPoseChannel *target= get_pose_channel(ob->pose, icu->driver->name);
2340 node2 = dag_get_node(dag, target);
2341 dag_add_relation(dag, node2, node, 0, "Ipo Driver");
2343 /* uncommented this line, results in dependencies
2344 * not being added properly for this constraint,
2345 * what is the purpose of this? - brecht */
2346 /*cti= NULL;*/ /* trick to get next loop skipped */
2351 #endif // XXX old animation system... driver stuff to watch out for
2353 if (cti && cti->get_constraint_targets) {
2354 cti->get_constraint_targets(con, &targets);
2356 for (ct= targets.first; ct; ct= ct->next) {
2357 if (ct->tar==ob && ct->subtarget[0]) {
2358 bPoseChannel *target= get_pose_channel(ob->pose, ct->subtarget);
2360 node2= dag_get_node(dag, target);
2361 dag_add_relation(dag, node2, node, 0, "IK Constraint");
2363 if (con->type==CONSTRAINT_TYPE_KINEMATIC) {
2364 bKinematicConstraint *data = (bKinematicConstraint *)con->data;
2365 bPoseChannel *parchan;
2368 /* exclude tip from chain? */
2369 if(!(data->flag & CONSTRAINT_IK_TIP))
2370 parchan= pchan->parent;
2374 /* Walk to the chain's root */
2376 node3= dag_get_node(dag, parchan);
2377 dag_add_relation(dag, node2, node3, 0, "IK Constraint");
2380 if (segcount==data->rootbone || segcount>255) break; // 255 is weak
2381 parchan= parchan->parent;
2388 if (cti->flush_constraint_targets)
2389 cti->flush_constraint_targets(con, &targets, 1);
2392 if (addtoroot == 1 ) {
2393 dag_add_relation(dag, rootnode, node, 0, "Root Bone Relation");
2397 dag_check_cycle(dag);
2399 /* now we try to sort... */
2400 tempbase.first= tempbase.last= NULL;
2402 nqueue = queue_create(DAGQUEUEALLOC);
2404 /* tag nodes unchecked */
2405 for(node = dag->DagNode.first; node; node= node->next)
2406 node->color = DAG_WHITE;
2408 node = dag->DagNode.first;
2410 node->color = DAG_GRAY;
2411 push_stack(nqueue, node);
2413 while(nqueue->count) {
2416 node = get_top_node_queue(nqueue);
2419 while(itA != NULL) {
2420 if((itA->node->color == DAG_WHITE) ) {
2421 itA->node->color = DAG_GRAY;
2422 push_stack(nqueue,itA->node);
2431 node = pop_queue(nqueue);
2432 if (node->ob == NULL) // we are done
2434 node->color = DAG_BLACK;
2436 /* put node in new list */
2437 BLI_remlink(&pose->chanbase, node->ob);
2438 BLI_addhead(&tempbase, node->ob);
2443 // temporal correction for circular dependancies
2444 while(pose->chanbase.first) {
2445 pchan= pose->chanbase.first;
2446 BLI_remlink(&pose->chanbase, pchan);
2447 BLI_addhead(&tempbase, pchan);
2449 printf("cyclic %s\n", pchan->name);
2452 pose->chanbase = tempbase;
2453 queue_delete(nqueue);
2455 // printf("\nordered\n");
2456 // for(pchan = pose->chanbase.first; pchan; pchan= pchan->next) {
2457 // printf(" %s\n", pchan->name);