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 = 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 /* normal channel-drives-channel */
351 node1 = dag_get_node(dag, driver->id); // XXX we assume that id is an object...
353 // XXX what happens for bone drivers?
354 dag_add_relation(dag, node1, node, isdata?DAG_RL_OB_DATA:DAG_RL_OB_OB, "Ipo Driver");
356 #if 0 // XXX old 'normal' type
358 else if (icu->driver->ob) {
359 node1 = dag_get_node(dag, icu->driver->ob);
360 if(icu->driver->blocktype==ID_AR)
361 dag_add_relation(dag, node1, node, isdata?DAG_RL_DATA_DATA:DAG_RL_DATA_OB, "Ipo Driver");
363 dag_add_relation(dag, node1, node, isdata?DAG_RL_OB_DATA:DAG_RL_OB_OB, "Ipo Driver");
365 #endif // XXX old 'normal' type
369 static void dag_add_collision_field_relation(DagForest *dag, Scene *scene, Object *ob, DagNode *node)
374 // would be nice to have a list of colliders here
375 // so for now walk all objects in scene check 'same layer rule'
376 for(base = scene->base.first; base; base= base->next) {
377 if((base->lay & ob->lay) && base->object->pd) {
378 Object *ob1= base->object;
379 if((ob1->pd->deflect || ob1->pd->forcefield) && (ob1 != ob)) {
380 node2 = dag_get_node(dag, ob1);
381 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Field Collision");
387 static void build_dag_object(DagForest *dag, DagNode *scenenode, Scene *scene, Object *ob, int mask)
394 ParticleSystem *psys;
397 node = dag_get_node(dag, ob);
399 if ((ob->data) && (mask&DAG_RL_DATA)) {
400 node2 = dag_get_node(dag,ob->data);
401 dag_add_relation(dag,node,node2,DAG_RL_DATA, "Object-Data Relation");
402 node2->first_ancestor = ob;
403 node2->ancestor_count += 1;
406 if (ob->type == OB_ARMATURE) {
411 for (pchan = ob->pose->chanbase.first; pchan; pchan=pchan->next) {
412 for (con = pchan->constraints.first; con; con=con->next) {
413 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
414 ListBase targets = {NULL, NULL};
415 bConstraintTarget *ct;
417 if (cti && cti->get_constraint_targets) {
418 cti->get_constraint_targets(con, &targets);
420 for (ct= targets.first; ct; ct= ct->next) {
421 if (ct->tar && ct->tar != ob) {
422 // fprintf(stderr,"armature %s target :%s \n", ob->id.name, target->id.name);
423 node3 = dag_get_node(dag, ct->tar);
425 if (ct->subtarget[0])
426 dag_add_relation(dag,node3,node, DAG_RL_OB_DATA|DAG_RL_DATA_DATA, cti->name);
427 else if(ELEM(con->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO))
428 dag_add_relation(dag,node3,node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, cti->name);
430 dag_add_relation(dag,node3,node, DAG_RL_OB_DATA, cti->name);
434 if (cti->flush_constraint_targets)
435 cti->flush_constraint_targets(con, &targets, 1);
443 /* driver dependencies, nla modifiers */
444 #if 0 // XXX old animation system
446 dag_add_driver_relation(ob->ipo, dag, node, 0);
450 dag_add_driver_relation(key->ipo, dag, node, 1);
452 for (conchan=ob->constraintChannels.first; conchan; conchan=conchan->next)
454 dag_add_driver_relation(conchan->ipo, dag, node, 0);
457 bActionChannel *chan;
458 for (chan = ob->action->chanbase.first; chan; chan=chan->next){
460 dag_add_driver_relation(chan->ipo, dag, node, 1);
461 for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next)
463 dag_add_driver_relation(conchan->ipo, dag, node, 1);
466 if(ob->nlastrips.first) {
468 bActionChannel *chan;
469 for(strip= ob->nlastrips.first; strip; strip= strip->next) {
470 if(strip->act && strip->act!=ob->action)
471 for (chan = strip->act->chanbase.first; chan; chan=chan->next)
473 dag_add_driver_relation(chan->ipo, dag, node, 1);
474 if(strip->modifiers.first) {
475 bActionModifier *amod;
476 for(amod= strip->modifiers.first; amod; amod= amod->next) {
478 node2 = dag_get_node(dag, amod->ob);
479 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "NLA Strip Modifier");
485 #endif // XXX old animation system
487 dag_add_driver_relation(ob->adt, dag, node, (ob->type == OB_ARMATURE)); // XXX isdata arg here doesn't give an accurate picture of situation
491 dag_add_driver_relation(key->adt, dag, node, 1);
493 if (ob->modifiers.first) {
496 for(md=ob->modifiers.first; md; md=md->next) {
497 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
499 if (mti->updateDepgraph) mti->updateDepgraph(md, dag, scene, ob, node);
503 node2 = dag_get_node(dag,ob->parent);
505 switch(ob->partype) {
507 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Parent");
509 case PARVERT1: case PARVERT3: case PARBONE:
510 dag_add_relation(dag,node2,node,DAG_RL_DATA_OB|DAG_RL_OB_OB, "Vertex Parent");
513 if(ob->parent->type==OB_LATTICE)
514 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Lattice Parent");
515 else if(ob->parent->type==OB_CURVE) {
516 Curve *cu= ob->parent->data;
517 if(cu->flag & CU_PATH)
518 dag_add_relation(dag,node2,node,DAG_RL_DATA_OB|DAG_RL_OB_OB, "Curve Parent");
520 dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Curve Parent");
523 dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Parent");
525 /* exception case: parent is duplivert */
526 if(ob->type==OB_MBALL && (ob->parent->transflag & OB_DUPLIVERTS)) {
527 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Duplivert");
533 node2 = dag_get_node(dag,ob->track);
534 dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Track To");
538 node2 = dag_get_node(dag, ob->proxy);
539 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Proxy");
540 /* inverted relation, so addtoroot shouldn't be set to zero */
544 if (ob->type==OB_CAMERA) {
545 Camera *cam = (Camera *)ob->data;
547 dag_add_driver_relation(cam->adt, dag, node, 1);
549 node2 = dag_get_node(dag, cam->dof_ob);
550 dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Camera DoF");
553 if (ob->type==OB_LAMP) {
554 Lamp *la = (Lamp *)ob->data;
556 dag_add_driver_relation(la->adt, dag, node, 1);
559 if (ob->transflag & OB_DUPLI) {
560 if((ob->transflag & OB_DUPLIGROUP) && ob->dup_group) {
562 for(go= ob->dup_group->gobject.first; go; go= go->next) {
564 node2 = dag_get_node(dag, go->ob);
565 /* node2 changes node1, this keeps animations updated in groups?? not logical? */
566 dag_add_relation(dag, node2, node, DAG_RL_OB_OB, "Dupligroup");
572 /* softbody collision */
573 if((ob->type==OB_MESH) || (ob->type==OB_CURVE) || (ob->type==OB_LATTICE))
574 if(modifiers_isSoftbodyEnabled(ob) || modifiers_isClothEnabled(ob))
575 dag_add_collision_field_relation(dag, scene, ob, node);
577 if (ob->type==OB_MBALL) {
578 Object *mom= find_basis_mball(scene, ob);
580 node2 = dag_get_node(dag, mom);
581 dag_add_relation(dag,node,node2,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Metaball"); // mom depends on children!
584 else if (ob->type==OB_CURVE) {
587 node2 = dag_get_node(dag, cu->bevobj);
588 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Curve Bevel");
591 node2 = dag_get_node(dag, cu->taperobj);
592 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Curve Taper");
595 dag_add_driver_relation(cu->adt, dag, node, 1);
597 else if(ob->type==OB_FONT) {
599 if(cu->textoncurve) {
600 node2 = dag_get_node(dag, cu->textoncurve);
601 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Texture On Curve");
605 psys= ob->particlesystem.first;
607 ParticleEffectorCache *nec;
610 for(; psys; psys=psys->next) {
611 ParticleSettings *part= psys->part;
613 dag_add_relation(dag, node, node, DAG_RL_OB_DATA, "Particle-Object Relation");
615 if(psys->flag & PSYS_DISABLED || psys->flag & PSYS_DELETE)
618 if(part->phystype==PART_PHYS_KEYED && psys->keyed_ob &&
619 BLI_findlink(&psys->keyed_ob->particlesystem,psys->keyed_psys-1)) {
620 node2 = dag_get_node(dag, psys->keyed_ob);
621 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA, "Particle Keyed Physics");
624 if(part->draw_as == PART_DRAW_OB && part->dup_ob) {
625 node2 = dag_get_node(dag, part->dup_ob);
626 dag_add_relation(dag, node, node2, DAG_RL_OB_OB, "Particle Object Visualisation");
627 if(part->dup_ob->type == OB_MBALL)
628 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA, "Particle Object Visualisation");
631 if(part->draw_as == PART_DRAW_GR && part->dup_group) {
632 for(go=part->dup_group->gobject.first; go; go=go->next) {
633 node2 = dag_get_node(dag, go->ob);
634 dag_add_relation(dag, node, node2, DAG_RL_OB_OB, "Particle Group Visualisation");
638 if(psys->effectors.first)
639 psys_end_effectors(psys);
640 psys_init_effectors(scene, ob, psys->part->eff_group, psys);
642 if(psys->effectors.first) {
643 for(nec= psys->effectors.first; nec; nec= nec->next) {
644 Object *ob1= nec->ob;
646 if(nec->type & PSYS_EC_EFFECTOR) {
647 node2 = dag_get_node(dag, ob1);
648 if(ob1->pd->forcefield==PFIELD_GUIDE)
649 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Particle Field");
651 dag_add_relation(dag, node2, node, DAG_RL_OB_DATA, "Particle Field");
653 else if(nec->type & PSYS_EC_DEFLECT) {
654 node2 = dag_get_node(dag, ob1);
655 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Particle Collision");
657 else if(nec->type & PSYS_EC_PARTICLE) {
658 node2 = dag_get_node(dag, ob1);
659 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA, "Particle Field");
662 if(nec->type & PSYS_EC_REACTOR) {
663 node2 = dag_get_node(dag, ob1);
664 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA, "Particle Reactor");
671 for (con = ob->constraints.first; con; con=con->next) {
672 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
673 ListBase targets = {NULL, NULL};
674 bConstraintTarget *ct;
676 if (cti && cti->get_constraint_targets) {
677 cti->get_constraint_targets(con, &targets);
679 for (ct= targets.first; ct; ct= ct->next) {
687 node2 = dag_get_node(dag, obt);
688 if (ELEM(con->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO))
689 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB|DAG_RL_OB_OB, cti->name);
691 if (ELEM3(obt->type, OB_ARMATURE, OB_MESH, OB_LATTICE) && (ct->subtarget[0]))
692 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB|DAG_RL_OB_OB, cti->name);
694 dag_add_relation(dag, node2, node, DAG_RL_OB_OB, cti->name);
699 if (cti->flush_constraint_targets)
700 cti->flush_constraint_targets(con, &targets, 1);
705 dag_add_relation(dag,scenenode,node,DAG_RL_SCENE, "Scene Relation");
708 struct DagForest *build_dag(struct Scene *sce, short mask)
728 /* add base node for scene. scene is always the first node in DAG */
729 scenenode = dag_add_node(dag, sce);
731 /* add current scene objects */
732 for(base = sce->base.first; base; base= base->next) {
735 build_dag_object(dag, scenenode, sce, ob, mask);
737 build_dag_object(dag, scenenode, sce, ob->proxy, mask);
739 /* handled in next loop */
741 ob->dup_group->id.flag |= LIB_DOIT;
744 /* add groups used in current scene objects */
745 for(group= G.main->group.first; group; group= group->id.next) {
746 if(group->id.flag & LIB_DOIT) {
747 for(go= group->gobject.first; go; go= go->next) {
748 build_dag_object(dag, scenenode, sce, go->ob, mask);
750 group->id.flag &= ~LIB_DOIT;
754 /* Now all relations were built, but we need to solve 1 exceptional case;
755 When objects have multiple "parents" (for example parent + constraint working on same object)
756 the relation type has to be synced. One of the parents can change, and should give same event to child */
758 /* nodes were callocced, so we can use node->color for temporal storage */
759 for(node = sce->theDag->DagNode.first; node; node= node->next) {
760 if(node->type==ID_OB) {
761 for(itA = node->child; itA; itA= itA->next) {
762 if(itA->node->type==ID_OB) {
763 itA->node->color |= itA->type;
768 /* now set relations equal, so that when only one parent changes, the correct recalcs are found */
769 for(node = sce->theDag->DagNode.first; node; node= node->next) {
770 if(node->type==ID_OB) {
771 for(itA = node->child; itA; itA= itA->next) {
772 if(itA->node->type==ID_OB) {
773 itA->type |= itA->node->color;
779 // cycle detection and solving
780 // solve_cycles(dag);
786 void free_forest(DagForest *Dag)
787 { /* remove all nodes and deps */
791 DagNode *itN = Dag->DagNode.first;
812 Dag->DagNode.first = NULL;
813 Dag->DagNode.last = NULL;
818 DagNode * dag_find_node (DagForest *forest,void * fob)
820 DagNode *node = forest->DagNode.first;
830 static int ugly_hack_sorry= 1; // prevent type check
832 /* no checking of existance, use dag_find_node first or dag_get_node */
833 DagNode * dag_add_node (DagForest *forest, void * fob)
837 node = MEM_callocN(sizeof(DagNode),"DAG node");
840 node->color = DAG_WHITE;
842 if(ugly_hack_sorry) node->type = GS(((ID *) fob)->name); // sorry, done for pose sorting
843 if (forest->numNodes) {
844 ((DagNode *) forest->DagNode.last)->next = node;
845 forest->DagNode.last = node;
848 forest->DagNode.last = node;
849 forest->DagNode.first = node;
850 forest->numNodes = 1;
856 DagNode * dag_get_node (DagForest *forest,void * fob)
860 node = dag_find_node (forest, fob);
862 node = dag_add_node(forest, fob);
868 DagNode * dag_get_sub_node (DagForest *forest,void * fob)
871 DagAdjList *mainchild, *prev=NULL;
873 mainchild = ((DagNode *) forest->DagNode.first)->child;
874 /* remove from first node (scene) adj list if present */
876 if (mainchild->node == fob) {
878 prev->next = mainchild->next;
879 MEM_freeN(mainchild);
882 ((DagNode *) forest->DagNode.first)->child = mainchild->next;
883 MEM_freeN(mainchild);
888 mainchild = mainchild->next;
890 node = dag_find_node (forest, fob);
892 node = dag_add_node(forest, fob);
896 static void dag_add_parent_relation(DagForest *forest, DagNode *fob1, DagNode *fob2, short rel, char *name)
898 DagAdjList *itA = fob2->parent;
900 while (itA) { /* search if relation exist already */
901 if (itA->node == fob1) {
908 /* create new relation and insert at head. MALLOC alert! */
909 itA = MEM_mallocN(sizeof(DagAdjList),"DAG adj list");
913 itA->next = fob2->parent;
918 void dag_add_relation(DagForest *forest, DagNode *fob1, DagNode *fob2, short rel, char *name)
920 DagAdjList *itA = fob1->child;
922 /* parent relation is for cycle checking */
923 dag_add_parent_relation(forest, fob1, fob2, rel, name);
925 while (itA) { /* search if relation exist already */
926 if (itA->node == fob2) {
933 /* create new relation and insert at head. MALLOC alert! */
934 itA = MEM_mallocN(sizeof(DagAdjList),"DAG adj list");
938 itA->next = fob1->child;
943 static char *dag_node_name(DagNode *node)
947 else if(ugly_hack_sorry)
948 return ((ID*)(node->ob))->name+2;
950 return ((bPoseChannel*)(node->ob))->name;
954 static void dag_node_print_dependencies(DagNode *node)
958 printf("%s depends on:\n", dag_node_name(node));
960 for(itA= node->parent; itA; itA= itA->next)
961 printf(" %s through %s\n", dag_node_name(itA->node), itA->name);
966 static int dag_node_print_dependency_recurs(DagNode *node, DagNode *endnode)
970 if(node->color == DAG_BLACK)
973 node->color= DAG_BLACK;
978 for(itA= node->parent; itA; itA= itA->next) {
979 if(dag_node_print_dependency_recurs(itA->node, endnode)) {
980 printf(" %s depends on %s through %s.\n", dag_node_name(node), dag_node_name(itA->node), itA->name);
988 static void dag_node_print_dependency_cycle(DagForest *dag, DagNode *startnode, DagNode *endnode, char *name)
992 for(node = dag->DagNode.first; node; node= node->next)
993 node->color= DAG_WHITE;
995 printf(" %s depends on %s through %s.\n", dag_node_name(endnode), dag_node_name(startnode), name);
996 dag_node_print_dependency_recurs(startnode, endnode);
1000 static int dag_node_recurs_level(DagNode *node, int level)
1005 node->color= DAG_BLACK; /* done */
1008 for(itA= node->parent; itA; itA= itA->next) {
1009 if(itA->node->color==DAG_WHITE) {
1010 itA->node->ancestor_count= dag_node_recurs_level(itA->node, level);
1011 newlevel= MAX2(newlevel, level+itA->node->ancestor_count);
1014 newlevel= MAX2(newlevel, level+itA->node->ancestor_count);
1020 static void dag_check_cycle(DagForest *dag)
1025 /* tag nodes unchecked */
1026 for(node = dag->DagNode.first; node; node= node->next)
1027 node->color= DAG_WHITE;
1029 for(node = dag->DagNode.first; node; node= node->next) {
1030 if(node->color==DAG_WHITE) {
1031 node->ancestor_count= dag_node_recurs_level(node, 0);
1035 /* check relations, and print errors */
1036 for(node = dag->DagNode.first; node; node= node->next) {
1037 for(itA= node->parent; itA; itA= itA->next) {
1038 if(itA->node->ancestor_count > node->ancestor_count) {
1039 if(node->ob && itA->node->ob) {
1040 printf("Dependency cycle detected:\n");
1041 dag_node_print_dependency_cycle(dag, itA->node, node, itA->name);
1047 /* parent relations are only needed for cycle checking, so free now */
1048 for(node = dag->DagNode.first; node; node= node->next) {
1049 while (node->parent) {
1050 itA = node->parent->next;
1051 MEM_freeN(node->parent);
1058 * MainDAG is the DAG of all objects in current scene
1059 * used only for drawing there is one also in each scene
1061 static DagForest * MainDag = NULL;
1063 DagForest *getMainDag(void)
1069 void setMainDag(DagForest *dag)
1077 * in theory we should sweep the whole array
1078 * but in our case the first node is the scene
1079 * and is linked to every other object
1081 * for general case we will need to add outer loop
1085 * ToDo : change pos kludge
1088 /* adjust levels for drawing in oops space */
1089 void graph_bfs(void)
1092 DagNodeQueue *nqueue;
1098 /* fprintf(stderr,"starting BFS \n ------------\n"); */
1099 nqueue = queue_create(DAGQUEUEALLOC);
1100 for ( i=0; i<50; i++)
1104 * dagnode.first is alway the root (scene)
1106 node = MainDag->DagNode.first;
1108 node->color = DAG_WHITE;
1109 node->BFS_dist = 9999;
1114 node = MainDag->DagNode.first;
1115 if (node->color == DAG_WHITE) {
1116 node->color = DAG_GRAY;
1118 push_queue(nqueue,node);
1119 while(nqueue->count) {
1120 node = pop_queue(nqueue);
1122 minheight = pos[node->BFS_dist];
1124 while(itA != NULL) {
1125 if((itA->node->color == DAG_WHITE) ) {
1126 itA->node->color = DAG_GRAY;
1127 itA->node->BFS_dist = node->BFS_dist + 1;
1128 itA->node->k = (float) minheight;
1129 push_queue(nqueue,itA->node);
1133 fprintf(stderr,"bfs not dag tree edge color :%i \n",itA->node->color);
1139 if (pos[node->BFS_dist] > node->k ) {
1140 pos[node->BFS_dist] += 1;
1141 node->k = (float) pos[node->BFS_dist];
1143 pos[node->BFS_dist] = (int) node->k +1;
1145 set_node_xy(node, node->BFS_dist*DEPSX*2, pos[node->BFS_dist]*DEPSY*2);
1146 node->color = DAG_BLACK;
1148 fprintf(stderr,"BFS node : %20s %i %5.0f %5.0f\n",((ID *) node->ob)->name,node->BFS_dist, node->x, node->y);
1152 queue_delete(nqueue);
1155 int pre_and_post_BFS(DagForest *dag, short mask, graph_action_func pre_func, graph_action_func post_func, void **data) {
1158 node = dag->DagNode.first;
1159 return pre_and_post_source_BFS(dag, mask, node, pre_func, post_func, data);
1163 int pre_and_post_source_BFS(DagForest *dag, short mask, DagNode *source, graph_action_func pre_func, graph_action_func post_func, void **data)
1166 DagNodeQueue *nqueue;
1169 /* fprintf(stderr,"starting BFS \n ------------\n"); */
1172 * dagnode.first is alway the root (scene)
1174 node = dag->DagNode.first;
1175 nqueue = queue_create(DAGQUEUEALLOC);
1177 node->color = DAG_WHITE;
1178 node->BFS_dist = 9999;
1183 if (node->color == DAG_WHITE) {
1184 node->color = DAG_GRAY;
1186 pre_func(node->ob,data);
1188 while(nqueue->count) {
1189 node = pop_queue(nqueue);
1192 while(itA != NULL) {
1193 if((itA->node->color == DAG_WHITE) && (itA->type & mask)) {
1194 itA->node->color = DAG_GRAY;
1195 itA->node->BFS_dist = node->BFS_dist + 1;
1196 push_queue(nqueue,itA->node);
1197 pre_func(node->ob,data);
1200 else { // back or cross edge
1205 post_func(node->ob,data);
1206 node->color = DAG_BLACK;
1208 fprintf(stderr,"BFS node : %20s %i %5.0f %5.0f\n",((ID *) node->ob)->name,node->BFS_dist, node->x, node->y);
1212 queue_delete(nqueue);
1216 /* non recursive version of DFS, return queue -- outer loop present to catch odd cases (first level cycles)*/
1217 DagNodeQueue * graph_dfs(void)
1220 DagNodeQueue *nqueue;
1221 DagNodeQueue *retqueue;
1231 *fprintf(stderr,"starting DFS \n ------------\n");
1233 nqueue = queue_create(DAGQUEUEALLOC);
1234 retqueue = queue_create(MainDag->numNodes);
1235 for ( i=0; i<50; i++)
1239 * dagnode.first is alway the root (scene)
1241 node = MainDag->DagNode.first;
1243 node->color = DAG_WHITE;
1244 node->DFS_dist = 9999;
1245 node->DFS_dvtm = node->DFS_fntm = 9999;
1252 node = MainDag->DagNode.first;
1255 if (node->color == DAG_WHITE) {
1256 node->color = DAG_GRAY;
1258 node->DFS_dvtm = time;
1260 push_stack(nqueue,node);
1262 while(nqueue->count) {
1263 //graph_print_queue(nqueue);
1266 node = get_top_node_queue(nqueue);
1268 minheight = pos[node->DFS_dist];
1271 while(itA != NULL) {
1272 if((itA->node->color == DAG_WHITE) ) {
1273 itA->node->DFS_dvtm = time;
1274 itA->node->color = DAG_GRAY;
1277 itA->node->DFS_dist = node->DFS_dist + 1;
1278 itA->node->k = (float) minheight;
1279 push_stack(nqueue,itA->node);
1283 if (itA->node->color == DAG_GRAY) { // back edge
1284 fprintf(stderr,"dfs back edge :%15s %15s \n",((ID *) node->ob)->name, ((ID *) itA->node->ob)->name);
1286 } else if (itA->node->color == DAG_BLACK) {
1288 /* already processed node but we may want later to change distance either to shorter to longer.
1289 * DFS_dist is the first encounter
1291 /*if (node->DFS_dist >= itA->node->DFS_dist)
1292 itA->node->DFS_dist = node->DFS_dist + 1;
1294 fprintf(stderr,"dfs forward or cross edge :%15s %i-%i %15s %i-%i \n",
1295 ((ID *) node->ob)->name,
1298 ((ID *) itA->node->ob)->name,
1299 itA->node->DFS_dvtm,
1300 itA->node->DFS_fntm);
1303 fprintf(stderr,"dfs unknown edge \n");
1309 node = pop_queue(nqueue);
1310 node->color = DAG_BLACK;
1312 node->DFS_fntm = time;
1314 if (node->DFS_dist > maxpos)
1315 maxpos = node->DFS_dist;
1316 if (pos[node->DFS_dist] > node->k ) {
1317 pos[node->DFS_dist] += 1;
1318 node->k = (float) pos[node->DFS_dist];
1320 pos[node->DFS_dist] = (int) node->k +1;
1322 set_node_xy(node, node->DFS_dist*DEPSX*2, pos[node->DFS_dist]*DEPSY*2);
1325 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 );
1327 push_stack(retqueue,node);
1334 // fprintf(stderr,"i size : %i \n", maxpos);
1336 queue_delete(nqueue);
1341 int pre_and_post_DFS(DagForest *dag, short mask, graph_action_func pre_func, graph_action_func post_func, void **data) {
1344 node = dag->DagNode.first;
1345 return pre_and_post_source_DFS(dag, mask, node, pre_func, post_func, data);
1348 int pre_and_post_source_DFS(DagForest *dag, short mask, DagNode *source, graph_action_func pre_func, graph_action_func post_func, void **data)
1351 DagNodeQueue *nqueue;
1357 *fprintf(stderr,"starting DFS \n ------------\n");
1359 nqueue = queue_create(DAGQUEUEALLOC);
1362 * dagnode.first is alway the root (scene)
1364 node = dag->DagNode.first;
1366 node->color = DAG_WHITE;
1367 node->DFS_dist = 9999;
1368 node->DFS_dvtm = node->DFS_fntm = 9999;
1377 if (node->color == DAG_WHITE) {
1378 node->color = DAG_GRAY;
1380 node->DFS_dvtm = time;
1382 push_stack(nqueue,node);
1383 pre_func(node->ob,data);
1385 while(nqueue->count) {
1387 node = get_top_node_queue(nqueue);
1390 while(itA != NULL) {
1391 if((itA->node->color == DAG_WHITE) && (itA->type & mask) ) {
1392 itA->node->DFS_dvtm = time;
1393 itA->node->color = DAG_GRAY;
1396 itA->node->DFS_dist = node->DFS_dist + 1;
1397 push_stack(nqueue,itA->node);
1398 pre_func(node->ob,data);
1403 if (itA->node->color == DAG_GRAY) {// back edge
1406 // else if (itA->node->color == DAG_BLACK) { // cross or forward
1413 node = pop_queue(nqueue);
1414 node->color = DAG_BLACK;
1416 node->DFS_fntm = time;
1418 post_func(node->ob,data);
1424 queue_delete(nqueue);
1429 // used to get the obs owning a datablock
1430 struct DagNodeQueue *get_obparents(struct DagForest *dag, void *ob)
1432 DagNode * node, *node1;
1433 DagNodeQueue *nqueue;
1436 node = dag_find_node(dag,ob);
1440 else if (node->ancestor_count == 1) { // simple case
1441 nqueue = queue_create(1);
1442 push_queue(nqueue,node);
1443 } else { // need to go over the whole dag for adj list
1444 nqueue = queue_create(node->ancestor_count);
1446 node1 = dag->DagNode.first;
1448 if (node1->DFS_fntm > node->DFS_fntm) { // a parent is finished after child. must check adj list
1450 while(itA != NULL) {
1451 if ((itA->node == node) && (itA->type == DAG_RL_DATA)) {
1452 push_queue(nqueue,node);
1457 node1 = node1->next;
1463 struct DagNodeQueue *get_first_ancestors(struct DagForest *dag, void *ob)
1465 DagNode * node, *node1;
1466 DagNodeQueue *nqueue;
1469 node = dag_find_node(dag,ob);
1471 // need to go over the whole dag for adj list
1472 nqueue = queue_create(node->ancestor_count);
1474 node1 = dag->DagNode.first;
1476 if (node1->DFS_fntm > node->DFS_fntm) {
1478 while(itA != NULL) {
1479 if (itA->node == node) {
1480 push_queue(nqueue,node);
1485 node1 = node1->next;
1491 // standard DFS list
1492 struct DagNodeQueue *get_all_childs(struct DagForest *dag, void *ob)
1495 DagNodeQueue *nqueue;
1496 DagNodeQueue *retqueue;
1501 nqueue = queue_create(DAGQUEUEALLOC);
1502 retqueue = queue_create(dag->numNodes); // was MainDag... why? (ton)
1504 node = dag->DagNode.first;
1506 node->color = DAG_WHITE;
1512 node = dag_find_node(dag, ob); // could be done in loop above (ton)
1513 if(node) { // can be null for newly added objects
1515 node->color = DAG_GRAY;
1517 push_stack(nqueue,node);
1519 while(nqueue->count) {
1522 node = get_top_node_queue(nqueue);
1525 while(itA != NULL) {
1526 if((itA->node->color == DAG_WHITE) ) {
1527 itA->node->DFS_dvtm = time;
1528 itA->node->color = DAG_GRAY;
1531 push_stack(nqueue,itA->node);
1539 node = pop_queue(nqueue);
1540 node->color = DAG_BLACK;
1543 push_stack(retqueue,node);
1547 queue_delete(nqueue);
1552 short are_obs_related(struct DagForest *dag, void *ob1, void *ob2) {
1556 node = dag_find_node(dag, ob1);
1559 while(itA != NULL) {
1560 if((itA->node->ob == ob2) ) {
1561 return itA->node->type;
1565 return DAG_NO_RELATION;
1568 int is_acyclic( DagForest *dag) {
1569 return dag->is_acyclic;
1572 void set_node_xy(DagNode *node, float x, float y)
1579 /* debug test functions */
1581 void graph_print_queue(DagNodeQueue *nqueue)
1583 DagNodeQueueElem *queueElem;
1585 queueElem = nqueue->first;
1587 fprintf(stderr,"** %s %i %i-%i ",((ID *) queueElem->node->ob)->name,queueElem->node->color,queueElem->node->DFS_dvtm,queueElem->node->DFS_fntm);
1588 queueElem = queueElem->next;
1590 fprintf(stderr,"\n");
1593 void graph_print_queue_dist(DagNodeQueue *nqueue)
1595 DagNodeQueueElem *queueElem;
1598 queueElem = nqueue->first;
1599 max = queueElem->node->DFS_fntm;
1602 fprintf(stderr,"** %25s %2.2i-%2.2i ",((ID *) queueElem->node->ob)->name,queueElem->node->DFS_dvtm,queueElem->node->DFS_fntm);
1603 while (count < queueElem->node->DFS_dvtm-1) { fputc(' ',stderr); count++;}
1605 while (count < queueElem->node->DFS_fntm-2) { fputc('-',stderr); count++;}
1609 queueElem = queueElem->next;
1611 fprintf(stderr,"\n");
1614 void graph_print_adj_list(void)
1619 node = (getMainDag())->DagNode.first;
1621 fprintf(stderr,"node : %s col: %i",((ID *) node->ob)->name, node->color);
1624 fprintf(stderr,"-- %s ",((ID *) itA->node->ob)->name);
1628 fprintf(stderr,"\n");
1633 /* ************************ API *********************** */
1635 /* groups with objects in this scene need to be put in the right order as well */
1636 static void scene_sort_groups(Scene *sce)
1643 /* test; are group objects all in this scene? */
1644 for(ob= G.main->object.first; ob; ob= ob->id.next) {
1645 ob->id.flag &= ~LIB_DOIT;
1646 ob->id.newid= NULL; /* newid abuse for GroupObject */
1648 for(base = sce->base.first; base; base= base->next)
1649 base->object->id.flag |= LIB_DOIT;
1651 for(group= G.main->group.first; group; group= group->id.next) {
1652 for(go= group->gobject.first; go; go= go->next) {
1653 if((go->ob->id.flag & LIB_DOIT)==0)
1656 /* this group is entirely in this scene */
1658 ListBase listb= {NULL, NULL};
1660 for(go= group->gobject.first; go; go= go->next)
1661 go->ob->id.newid= (ID *)go;
1663 /* in order of sorted bases we reinsert group objects */
1664 for(base = sce->base.first; base; base= base->next) {
1666 if(base->object->id.newid) {
1667 go= (GroupObject *)base->object->id.newid;
1668 base->object->id.newid= NULL;
1669 BLI_remlink( &group->gobject, go);
1670 BLI_addtail( &listb, go);
1673 /* copy the newly sorted listbase */
1674 group->gobject= listb;
1679 /* sort the base list on dependency order */
1680 void DAG_scene_sort(struct Scene *sce)
1683 DagNodeQueue *nqueue;
1690 tempbase.first= tempbase.last= NULL;
1692 build_dag(sce, DAG_RL_ALL_BUT_DATA);
1694 dag_check_cycle(sce->theDag);
1696 nqueue = queue_create(DAGQUEUEALLOC);
1698 for(node = sce->theDag->DagNode.first; node; node= node->next) {
1699 node->color = DAG_WHITE;
1704 node = sce->theDag->DagNode.first;
1706 node->color = DAG_GRAY;
1708 push_stack(nqueue,node);
1710 while(nqueue->count) {
1713 node = get_top_node_queue(nqueue);
1716 while(itA != NULL) {
1717 if((itA->node->color == DAG_WHITE) ) {
1718 itA->node->DFS_dvtm = time;
1719 itA->node->color = DAG_GRAY;
1722 push_stack(nqueue,itA->node);
1731 node = pop_queue(nqueue);
1732 if (node->ob == sce) // we are done
1734 node->color = DAG_BLACK;
1737 base = sce->base.first;
1738 while (base && base->object != node->ob)
1741 BLI_remlink(&sce->base,base);
1742 BLI_addhead(&tempbase,base);
1748 // temporal correction for circular dependancies
1749 base = sce->base.first;
1751 BLI_remlink(&sce->base,base);
1752 BLI_addhead(&tempbase,base);
1754 printf("cyclic %s\n", base->object->id.name);
1755 base = sce->base.first;
1758 sce->base = tempbase;
1759 queue_delete(nqueue);
1761 /* all groups with objects in this scene gets resorted too */
1762 scene_sort_groups(sce);
1765 printf("\nordered\n");
1766 for(base = sce->base.first; base; base= base->next) {
1767 printf(" %s\n", base->object->id.name);
1771 sce->recalc |= SCE_PRV_CHANGED; /* test for 3d preview */
1774 /* node was checked to have lasttime != curtime and is if type ID_OB */
1775 static void flush_update_node(DagNode *node, unsigned int layer, int curtime)
1779 int oldflag, changed=0;
1780 unsigned int all_layer;
1782 node->lasttime= curtime;
1785 if(ob && (ob->recalc & OB_RECALC)) {
1788 /* got an object node that changes, now check relations */
1789 for(itA = node->child; itA; itA= itA->next) {
1790 all_layer |= itA->lay;
1791 /* the relationship is visible */
1792 if((itA->lay & layer)) { // XXX || (itA->node->ob == obedit)
1793 if(itA->node->type==ID_OB) {
1795 oldflag= obc->recalc;
1797 /* got a ob->obc relation, now check if flag needs flush */
1798 if(ob->recalc & OB_RECALC_OB) {
1799 if(itA->type & DAG_RL_OB_OB) {
1800 //printf("ob %s changes ob %s\n", ob->id.name, obc->id.name);
1801 obc->recalc |= OB_RECALC_OB;
1803 if(itA->type & DAG_RL_OB_DATA) {
1804 //printf("ob %s changes obdata %s\n", ob->id.name, obc->id.name);
1805 obc->recalc |= OB_RECALC_DATA;
1808 if(ob->recalc & OB_RECALC_DATA) {
1809 if(itA->type & DAG_RL_DATA_OB) {
1810 //printf("obdata %s changes ob %s\n", ob->id.name, obc->id.name);
1811 obc->recalc |= OB_RECALC_OB;
1813 if(itA->type & DAG_RL_DATA_DATA) {
1814 //printf("obdata %s changes obdata %s\n", ob->id.name, obc->id.name);
1815 obc->recalc |= OB_RECALC_DATA;
1818 if(oldflag!=obc->recalc) changed= 1;
1822 /* even nicer, we can clear recalc flags... */
1823 if((all_layer & layer)==0) { // XXX && (ob != obedit)) {
1824 /* but existing displaylists or derivedmesh should be freed */
1825 if(ob->recalc & OB_RECALC_DATA)
1826 object_free_display(ob);
1828 ob->recalc &= ~OB_RECALC;
1832 /* check case where child changes and parent forcing obdata to change */
1833 /* should be done regardless if this ob has recalc set */
1834 /* could merge this in with loop above...? (ton) */
1835 for(itA = node->child; itA; itA= itA->next) {
1836 /* the relationship is visible */
1837 if((itA->lay & layer)) { // XXX || (itA->node->ob == obedit)
1838 if(itA->node->type==ID_OB) {
1841 if((obc->recalc & OB_RECALC)==OB_RECALC_OB) {
1842 /* parent has deforming info */
1843 if(itA->type & (DAG_RL_OB_DATA|DAG_RL_DATA_DATA)) {
1844 // printf("parent %s changes ob %s\n", ob->id.name, obc->id.name);
1845 obc->recalc |= OB_RECALC_DATA;
1852 /* we only go deeper if node not checked or something changed */
1853 for(itA = node->child; itA; itA= itA->next) {
1854 if(changed || itA->node->lasttime!=curtime)
1855 flush_update_node(itA->node, layer, curtime);
1860 /* node was checked to have lasttime != curtime , and is of type ID_OB */
1861 static unsigned int flush_layer_node(Scene *sce, DagNode *node, int curtime)
1866 node->lasttime= curtime;
1868 for(base= sce->base.first; base; base= base->next) {
1869 if(node->ob == base->object) {
1870 node->lay= ((Object *)node->ob)->lay;
1875 for(itA = node->child; itA; itA= itA->next) {
1876 if(itA->node->type==ID_OB) {
1877 if(itA->node->lasttime!=curtime) {
1878 itA->lay= flush_layer_node(sce, itA->node, curtime); // lay is only set once for each relation
1880 else itA->lay= itA->node->lay;
1882 node->lay |= itA->lay;
1889 /* node was checked to have lasttime != curtime , and is of type ID_OB */
1890 static void flush_pointcache_reset(DagNode *node, int curtime, int reset)
1895 node->lasttime= curtime;
1897 for(itA = node->child; itA; itA= itA->next) {
1898 if(itA->node->type==ID_OB) {
1899 if(itA->node->lasttime!=curtime) {
1900 ob= (Object*)(node->ob);
1902 if(reset || (ob->recalc & OB_RECALC)) {
1903 if(BKE_ptcache_object_reset(ob, PTCACHE_RESET_DEPSGRAPH))
1904 ob->recalc |= OB_RECALC_DATA;
1906 flush_pointcache_reset(itA->node, curtime, 1);
1909 flush_pointcache_reset(itA->node, curtime, 0);
1915 /* flushes all recalc flags in objects down the dependency tree */
1916 void DAG_scene_flush_update(Scene *sce, unsigned int lay, int time)
1923 if(sce->theDag==NULL) {
1924 printf("DAG zero... not allowed to happen!\n");
1925 DAG_scene_sort(sce);
1928 firstnode= sce->theDag->DagNode.first; // always scene node
1930 for(itA = firstnode->child; itA; itA= itA->next)
1933 /* first we flush the layer flags */
1934 sce->theDag->time++; // so we know which nodes were accessed
1935 lasttime= sce->theDag->time;
1937 for(itA = firstnode->child; itA; itA= itA->next)
1938 if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB)
1939 flush_layer_node(sce, itA->node, lasttime);
1941 /* then we use the relationships + layer info to flush update events */
1942 sce->theDag->time++; // so we know which nodes were accessed
1943 lasttime= sce->theDag->time;
1944 for(itA = firstnode->child; itA; itA= itA->next)
1945 if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB)
1946 flush_update_node(itA->node, lay, lasttime);
1948 /* if update is not due to time change, do pointcache clears */
1950 sce->theDag->time++; // so we know which nodes were accessed
1951 lasttime= sce->theDag->time;
1952 for(itA = firstnode->child; itA; itA= itA->next) {
1953 if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB) {
1954 ob= (Object*)(itA->node->ob);
1956 if(ob->recalc & OB_RECALC) {
1957 if(BKE_ptcache_object_reset(ob, PTCACHE_RESET_DEPSGRAPH))
1958 ob->recalc |= OB_RECALC_DATA;
1960 flush_pointcache_reset(itA->node, lasttime, 1);
1963 flush_pointcache_reset(itA->node, lasttime, 0);
1969 static int object_modifiers_use_time(Object *ob)
1973 for (md=ob->modifiers.first; md; md=md->next)
1974 if (modifier_dependsOnTime(md))
1980 static short animdata_use_time(AnimData *adt)
1984 if(adt==NULL) return 0;
1986 /* check action - only if assigned, and it has anim curves */
1987 if (adt->action && adt->action->curves.first)
1990 /* check NLA tracks + strips */
1991 for (nlt= adt->nla_tracks.first; nlt; nlt= nlt->next) {
1992 if (nlt->strips.first)
1999 static void dag_object_time_update_flags(Object *ob)
2001 if(ob->constraints.first) {
2003 for (con = ob->constraints.first; con; con=con->next) {
2004 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
2005 ListBase targets = {NULL, NULL};
2006 bConstraintTarget *ct;
2008 if (cti && cti->get_constraint_targets) {
2009 cti->get_constraint_targets(con, &targets);
2011 for (ct= targets.first; ct; ct= ct->next) {
2013 ob->recalc |= OB_RECALC_OB;
2018 if (cti->flush_constraint_targets)
2019 cti->flush_constraint_targets(con, &targets, 1);
2024 if(ob->scriptlink.totscript) ob->recalc |= OB_RECALC_OB;
2027 /* motion path or bone child */
2028 if(ob->parent->type==OB_CURVE || ob->parent->type==OB_ARMATURE) ob->recalc |= OB_RECALC_OB;
2031 #if 0 // XXX old animation system
2032 if(ob->nlastrips.first) {
2034 bActionStrip *strip;
2035 /* this case is for groups with nla, whilst nla target has no action or nla */
2036 for(strip= ob->nlastrips.first; strip; strip= strip->next) {
2038 strip->object->recalc |= OB_RECALC;
2042 #endif // XXX old animation system
2043 if(animdata_use_time(ob->adt)) ob->recalc |= OB_RECALC;
2044 if((ob->adt) && (ob->type==OB_ARMATURE)) ob->recalc |= OB_RECALC_DATA;
2046 if(object_modifiers_use_time(ob)) ob->recalc |= OB_RECALC_DATA;
2047 if((ob->pose) && (ob->pose->flag & POSE_CONSTRAINTS_TIMEDEPEND)) ob->recalc |= OB_RECALC_DATA;
2058 if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
2059 ob->recalc |= OB_RECALC_DATA;
2060 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
2063 if(ob->particlesystem.first)
2064 ob->recalc |= OB_RECALC_DATA;
2070 if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
2071 ob->recalc |= OB_RECALC_DATA;
2072 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
2078 if(cu->nurb.first==NULL && cu->str && cu->vfont)
2079 ob->recalc |= OB_RECALC_DATA;
2084 if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
2085 ob->recalc |= OB_RECALC_DATA;
2086 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
2091 if(ob->transflag & OB_DUPLI) ob->recalc |= OB_RECALC_DATA;
2095 if(ob->particlesystem.first) {
2096 ParticleSystem *psys= ob->particlesystem.first;
2098 for(; psys; psys=psys->next) {
2099 if(psys_check_enabled(ob, psys)) {
2100 ob->recalc |= OB_RECALC_DATA;
2108 /* flag all objects that need recalc, for changes in time for example */
2109 void DAG_scene_update_flags(Scene *scene, unsigned int lay)
2117 /* set ob flags where animated systems are */
2118 for(SETLOOPER(scene, base)) {
2121 /* now if DagNode were part of base, the node->lay could be checked... */
2122 /* we do all now, since the scene_flush checks layers and clears recalc flags even */
2123 dag_object_time_update_flags(ob);
2125 /* handled in next loop */
2127 ob->dup_group->id.flag |= LIB_DOIT;
2130 /* we do groups each once */
2131 for(group= G.main->group.first; group; group= group->id.next) {
2132 if(group->id.flag & LIB_DOIT) {
2133 for(go= group->gobject.first; go; go= go->next) {
2134 dag_object_time_update_flags(go->ob);
2139 for(sce= scene; sce; sce= sce->set)
2140 DAG_scene_flush_update(sce, lay, 1);
2142 /* test: set time flag, to disable baked systems to update */
2143 for(SETLOOPER(scene, base)) {
2146 ob->recalc |= OB_RECALC_TIME;
2149 /* hrmf... an exception to look at once, for invisible camera object we do it over */
2151 dag_object_time_update_flags(scene->camera);
2153 /* and store the info in groupobject */
2154 for(group= G.main->group.first; group; group= group->id.next) {
2155 if(group->id.flag & LIB_DOIT) {
2156 for(go= group->gobject.first; go; go= go->next) {
2157 go->recalc= go->ob->recalc;
2158 // printf("ob %s recalc %d\n", go->ob->id.name, go->recalc);
2160 group->id.flag &= ~LIB_DOIT;
2167 /* flag this object and all its relations to recalc */
2168 /* if you need to do more objects, tag object yourself and
2169 use DAG_scene_flush_update() in end */
2170 void DAG_object_flush_update(Scene *sce, Object *ob, short flag)
2173 if(ob==NULL || sce->theDag==NULL) return;
2176 BKE_ptcache_object_reset(ob, PTCACHE_RESET_DEPSGRAPH);
2178 /* all users of this ob->data should be checked */
2179 /* BUT! displists for curves are still only on cu */
2180 if(flag & OB_RECALC_DATA) {
2181 if(ob->type!=OB_CURVE && ob->type!=OB_SURF) {
2183 if(id && id->us>1) {
2184 /* except when there's a key and shapes are locked */
2185 if(ob_get_key(ob) && (ob->shapeflag & (OB_SHAPE_LOCK|OB_SHAPE_TEMPLOCK)));
2188 for (obt=G.main->object.first; obt; obt= obt->id.next) {
2189 if (obt != ob && obt->data==ob->data) {
2190 obt->recalc |= OB_RECALC_DATA;
2191 BKE_ptcache_object_reset(obt, PTCACHE_RESET_DEPSGRAPH);
2199 // XXX if(G.curscreen)
2200 // DAG_scene_flush_update(sce, dag_screen_view3d_layers(), 0);
2202 DAG_scene_flush_update(sce, sce->lay, 0);
2205 /* recursively descends tree, each node only checked once */
2206 /* node is checked to be of type object */
2207 static int parent_check_node(DagNode *node, int curtime)
2211 node->lasttime= curtime;
2213 if(node->color==DAG_GRAY)
2216 for(itA = node->child; itA; itA= itA->next) {
2217 if(itA->node->type==ID_OB) {
2219 if(itA->node->color==DAG_GRAY)
2222 /* descend if not done */
2223 if(itA->node->lasttime!=curtime) {
2224 itA->node->color= parent_check_node(itA->node, curtime);
2226 if(itA->node->color==DAG_GRAY)
2235 /* all nodes that influence this object get tagged, for calculating the exact
2236 position of this object at a given timeframe */
2237 void DAG_object_update_flags(Scene *sce, Object *ob, unsigned int lay)
2242 /* tag nodes unchecked */
2243 for(node = sce->theDag->DagNode.first; node; node= node->next)
2244 node->color = DAG_WHITE;
2246 node= dag_find_node(sce->theDag, ob);
2248 /* object not in scene? then handle group exception. needs to be dagged once too */
2251 while( (group = find_group(ob, group)) ) {
2253 /* primitive; tag all... this call helps building groups for particles */
2254 for(go= group->gobject.first; go; go= go->next)
2255 go->ob->recalc= OB_RECALC;
2260 node->color = DAG_GRAY;
2262 sce->theDag->time++;
2263 node= sce->theDag->DagNode.first;
2264 for(itA = node->child; itA; itA= itA->next) {
2265 if(itA->node->type==ID_OB && itA->node->lasttime!=sce->theDag->time)
2266 itA->node->color= parent_check_node(itA->node, sce->theDag->time);
2269 /* set recalcs and flushes */
2270 DAG_scene_update_flags(sce, lay);
2272 /* now we clear recalcs, unless color is set */
2273 for(node = sce->theDag->DagNode.first; node; node= node->next) {
2274 if(node->type==ID_OB && node->color==DAG_WHITE) {
2275 Object *ob= node->ob;
2282 /* ******************* DAG FOR ARMATURE POSE ***************** */
2284 /* we assume its an armature with pose */
2285 void DAG_pose_sort(Object *ob)
2287 bPose *pose= ob->pose;
2288 bPoseChannel *pchan;
2291 DagNode *node2, *node3;
2294 DagNodeQueue *nqueue;
2300 ugly_hack_sorry= 0; // no ID structs
2302 rootnode = dag_add_node(dag, NULL); // node->ob becomes NULL
2304 /* we add the hierarchy and the constraints */
2305 for(pchan = pose->chanbase.first; pchan; pchan= pchan->next) {
2308 node = dag_get_node(dag, pchan);
2311 node2 = dag_get_node(dag, pchan->parent);
2312 dag_add_relation(dag, node2, node, 0, "Parent Relation");
2315 for (con = pchan->constraints.first; con; con=con->next) {
2316 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
2317 ListBase targets = {NULL, NULL};
2318 bConstraintTarget *ct;
2320 #if 0 // XXX old animation system... driver stuff to watch out for
2323 for(icu= con->ipo->curve.first; icu; icu= icu->next) {
2324 /* icu->driver->ob should actually point to ob->proxy if it
2325 * is a proxy, but since it wasn't set correct it older
2326 * files comparing with ob->proxy makes it work for those */
2327 if(icu->driver && (icu->driver->ob==ob || icu->driver->ob==ob->proxy)) {
2328 bPoseChannel *target= get_pose_channel(ob->pose, icu->driver->name);
2330 node2 = dag_get_node(dag, target);
2331 dag_add_relation(dag, node2, node, 0, "Ipo Driver");
2333 /* uncommented this line, results in dependencies
2334 * not being added properly for this constraint,
2335 * what is the purpose of this? - brecht */
2336 /*cti= NULL;*/ /* trick to get next loop skipped */
2341 #endif // XXX old animation system... driver stuff to watch out for
2343 if (cti && cti->get_constraint_targets) {
2344 cti->get_constraint_targets(con, &targets);
2346 for (ct= targets.first; ct; ct= ct->next) {
2347 if (ct->tar==ob && ct->subtarget[0]) {
2348 bPoseChannel *target= get_pose_channel(ob->pose, ct->subtarget);
2350 node2= dag_get_node(dag, target);
2351 dag_add_relation(dag, node2, node, 0, "IK Constraint");
2353 if (con->type==CONSTRAINT_TYPE_KINEMATIC) {
2354 bKinematicConstraint *data = (bKinematicConstraint *)con->data;
2355 bPoseChannel *parchan;
2358 /* exclude tip from chain? */
2359 if(!(data->flag & CONSTRAINT_IK_TIP))
2360 parchan= pchan->parent;
2364 /* Walk to the chain's root */
2366 node3= dag_get_node(dag, parchan);
2367 dag_add_relation(dag, node2, node3, 0, "IK Constraint");
2370 if (segcount==data->rootbone || segcount>255) break; // 255 is weak
2371 parchan= parchan->parent;
2378 if (cti->flush_constraint_targets)
2379 cti->flush_constraint_targets(con, &targets, 1);
2382 if (addtoroot == 1 ) {
2383 dag_add_relation(dag, rootnode, node, 0, "Root Bone Relation");
2387 dag_check_cycle(dag);
2389 /* now we try to sort... */
2390 tempbase.first= tempbase.last= NULL;
2392 nqueue = queue_create(DAGQUEUEALLOC);
2394 /* tag nodes unchecked */
2395 for(node = dag->DagNode.first; node; node= node->next)
2396 node->color = DAG_WHITE;
2398 node = dag->DagNode.first;
2400 node->color = DAG_GRAY;
2401 push_stack(nqueue, node);
2403 while(nqueue->count) {
2406 node = get_top_node_queue(nqueue);
2409 while(itA != NULL) {
2410 if((itA->node->color == DAG_WHITE) ) {
2411 itA->node->color = DAG_GRAY;
2412 push_stack(nqueue,itA->node);
2421 node = pop_queue(nqueue);
2422 if (node->ob == NULL) // we are done
2424 node->color = DAG_BLACK;
2426 /* put node in new list */
2427 BLI_remlink(&pose->chanbase, node->ob);
2428 BLI_addhead(&tempbase, node->ob);
2433 // temporal correction for circular dependancies
2434 while(pose->chanbase.first) {
2435 pchan= pose->chanbase.first;
2436 BLI_remlink(&pose->chanbase, pchan);
2437 BLI_addhead(&tempbase, pchan);
2439 printf("cyclic %s\n", pchan->name);
2442 pose->chanbase = tempbase;
2443 queue_delete(nqueue);
2445 // printf("\nordered\n");
2446 // for(pchan = pose->chanbase.first; pchan; pchan= pchan->next) {
2447 // printf(" %s\n", pchan->name);