synched with trunk at revision 30597
[blender.git] / source / blender / blenkernel / intern / scene.c
index dd2a314..429cbfe 100644 (file)
 #include <stdio.h>
 #include <string.h>
 
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
 #ifndef WIN32 
 #include <unistd.h>
 #else
@@ -48,6 +44,7 @@
 #include "DNA_group_types.h"
 #include "DNA_scene_types.h"
 #include "DNA_screen_types.h"
+#include "DNA_sequence_types.h"
 
 #include "BKE_anim.h"
 #include "BKE_animsys.h"
@@ -201,6 +198,10 @@ Scene *copy_scene(Main *bmain, Scene *sce, int type)
                scen->r.qtcodecdata->cdParms = MEM_dupallocN(scen->r.qtcodecdata->cdParms);
        }
        
+       if(sce->r.ffcodecdata.properties) { /* intentionally check scen not sce. */
+               scen->r.ffcodecdata.properties= IDP_CopyProperty(sce->r.ffcodecdata.properties);
+       }
+
        /* NOTE: part of SCE_COPY_LINK_DATA and SCE_COPY_FULL operations
         * are done outside of blenkernel with ED_objects_single_users! */
 
@@ -215,6 +216,12 @@ Scene *copy_scene(Main *bmain, Scene *sce, int type)
                        id_us_plus((ID *)scen->world);
                        scen->world= copy_world(scen->world);
                }
+
+               if(sce->ed) {
+                       scen->ed= MEM_callocN( sizeof(Editing), "addseq");
+                       scen->ed->seqbasep= &scen->ed->seqbase;
+                       seqbase_dupli_recursive(sce, &scen->ed->seqbase, &sce->ed->seqbase, SEQ_DUPE_ALL);
+               }
        }
 
        sound_create_scene(scen);
@@ -314,7 +321,7 @@ Scene *add_scene(char *name)
        int a;
 
        sce= alloc_libblock(&G.main->scene, ID_SCE, name);
-       sce->lay= 1;
+       sce->lay= sce->layact= 1;
        
        sce->r.mode= R_GAMMA|R_OSA|R_SHADOW|R_SSS|R_ENVMAP|R_RAYTRACE;
        sce->r.cfra= 1;
@@ -476,6 +483,23 @@ Scene *add_scene(char *name)
        sce->gm.flag = GAME_DISPLAY_LISTS;
        sce->gm.matmode = GAME_MAT_MULTITEX;
 
+       sce->gm.obstacleSimulation= OBSTSIMULATION_NONE;
+       sce->gm.levelHeight = 2.f;
+
+       sce->gm.recastData.cellsize = 0.3f;
+       sce->gm.recastData.cellheight = 0.2f;
+       sce->gm.recastData.agentmaxslope = M_PI/2;
+       sce->gm.recastData.agentmaxclimb = 0.9f;
+       sce->gm.recastData.agentheight = 2.0f;
+       sce->gm.recastData.agentradius = 0.6f;
+       sce->gm.recastData.edgemaxlen = 12.0f;
+       sce->gm.recastData.edgemaxerror = 1.3f;
+       sce->gm.recastData.regionminsize = 50.f;
+       sce->gm.recastData.regionmergesize = 20.f;
+       sce->gm.recastData.vertsperpoly = 6;
+       sce->gm.recastData.detailsampledist = 6.0f;
+       sce->gm.recastData.detailsamplemaxerror = 1.0f;
+
        sound_create_scene(sce);
 
        return sce;
@@ -546,18 +570,17 @@ void set_scene_bg(Scene *scene)
 }
 
 /* called from creator.c */
-void set_scene_name(char *name)
+Scene *set_scene_name(char *name)
 {
-       Scene *sce;
-
-       for (sce= G.main->scene.first; sce; sce= sce->id.next) {
-               if (BLI_streq(name, sce->id.name+2)) {
-                       set_scene_bg(sce);
-                       return;
-               }
+       Scene *sce= (Scene *)find_id("SC", name);
+       if(sce) {
+               set_scene_bg(sce);
+               printf("Scene switch: '%s' in file: '%s'\n", name, G.sce);
+               return sce;
        }
-       
-       //XXX error("Can't find scene: %s", name);
+
+       printf("Can't find scene: '%s' in file: '%s'\n", name, G.sce);
+       return NULL;
 }
 
 void unlink_scene(Main *bmain, Scene *sce, Scene *newsce)
@@ -587,7 +610,7 @@ void unlink_scene(Main *bmain, Scene *sce, Scene *newsce)
 /* used by metaballs
  * doesnt return the original duplicated object, only dupli's
  */
-int next_object(Scene *scene, int val, Base **base, Object **ob)
+int next_object(Scene **scene, int val, Base **base, Object **ob)
 {
        static ListBase *duplilist= NULL;
        static DupliObject *dupob;
@@ -616,17 +639,21 @@ int next_object(Scene *scene, int val, Base **base, Object **ob)
 
                        /* the first base */
                        if(fase==F_START) {
-                               *base= scene->base.first;
+                               *base= (*scene)->base.first;
                                if(*base) {
                                        *ob= (*base)->object;
                                        fase= F_SCENE;
                                }
                                else {
                                        /* exception: empty scene */
-                                       if(scene->set && scene->set->base.first) {
-                                               *base= scene->set->base.first;
-                                               *ob= (*base)->object;
-                                               fase= F_SET;
+                                       while((*scene)->set) {
+                                               (*scene)= (*scene)->set;
+                                               if((*scene)->base.first) {
+                                                       *base= (*scene)->base.first;
+                                                       *ob= (*base)->object;
+                                                       fase= F_SCENE;
+                                                       break;
+                                               }
                                        }
                                }
                        }
@@ -636,11 +663,14 @@ int next_object(Scene *scene, int val, Base **base, Object **ob)
                                        if(*base) *ob= (*base)->object;
                                        else {
                                                if(fase==F_SCENE) {
-                                                       /* scene is finished, now do the set */
-                                                       if(scene->set && scene->set->base.first) {
-                                                               *base= scene->set->base.first;
-                                                               *ob= (*base)->object;
-                                                               fase= F_SET;
+                                                       /* (*scene) is finished, now do the set */
+                                                       while((*scene)->set) {
+                                                               (*scene)= (*scene)->set;
+                                                               if((*scene)->base.first) {
+                                                                       *base= (*scene)->base.first;
+                                                                       *ob= (*base)->object;
+                                                                       break;
+                                                               }
                                                        }
                                                }
                                        }
@@ -655,7 +685,7 @@ int next_object(Scene *scene, int val, Base **base, Object **ob)
                                                this enters eternal loop because of 
                                                makeDispListMBall getting called inside of group_duplilist */
                                                if((*base)->object->dup_group == NULL) {
-                                                       duplilist= object_duplilist(scene, (*base)->object);
+                                                       duplilist= object_duplilist((*scene), (*base)->object);
                                                        
                                                        dupob= duplilist->first;
 
@@ -691,6 +721,10 @@ int next_object(Scene *scene, int val, Base **base, Object **ob)
                }
        }
        
+       /* if(ob && *ob) {
+               printf("Scene: '%s', '%s'\n", (*scene)->id.name+2, (*ob)->id.name+2);
+       } */
+
        /* reset recursion test */
        in_next_object= 0;
        
@@ -872,73 +906,62 @@ int scene_check_setscene(Scene *sce)
        return 1;
 }
 
-/* This (evil) function is needed to cope with two legacy Blender rendering features
-* mblur (motion blur that renders 'subframes' and blurs them together), and fields 
-* rendering. Thus, the use of ugly globals from object.c
-*/
-// BAD... EVIL... JUJU...!!!!
-// XXX moved here temporarily
-float frame_to_float (Scene *scene, int cfra)          /* see also bsystem_time in object.c */
+/* This function is needed to cope with fractional frames - including two Blender rendering features
+* mblur (motion blur that renders 'subframes' and blurs them together), and fields rendering. */
+
+/* see also bsystem_time in object.c */
+float BKE_curframe(Scene *scene)
 {
-       extern float bluroffs;  /* bad stuff borrowed from object.c */
-       extern float fieldoffs;
-       float ctime;
-       
-       ctime= (float)cfra;
-       ctime+= bluroffs+fieldoffs;
-       ctime*= scene->r.framelen;
-       
+       float ctime = scene->r.cfra;
+       ctime+= scene->r.subframe;
+       ctime*= scene->r.framelen;      
+
        return ctime;
 }
 
-static void scene_update_newframe(Scene *sce, unsigned int lay)
+static void scene_update_tagged_recursive(Scene *scene, Scene *scene_parent)
 {
        Base *base;
-       Object *ob;
-       
-       for(base= sce->base.first; base; base= base->next) {
-               ob= base->object;
-               
-               object_handle_update(sce, ob);   // bke_object.h
-               
-               /* only update layer when an ipo */
-                       // XXX old animation system
-               //if(ob->ipo && has_ipo_code(ob->ipo, OB_LAY) ) {
-               //      base->lay= ob->lay;
-               //}
+
+       /* sets first, we allow per definition current scene to have
+          dependencies on sets, but not the other way around. */
+       if(scene->set)
+               scene_update_tagged_recursive(scene->set, scene_parent);
+
+       for(base= scene->base.first; base; base= base->next) {
+               Object *ob= base->object;
+
+               object_handle_update(scene_parent, ob);
+
+               if(ob->dup_group && (ob->transflag & OB_DUPLIGROUP))
+                       group_handle_recalc_and_update(scene_parent, ob, ob->dup_group);
+                       
+               /* always update layer, so that animating layers works */
+               base->lay= ob->lay;
        }
 }
 
 /* this is called in main loop, doing tagged updates before redraw */
 void scene_update_tagged(Scene *scene)
 {
-       Scene *sce;
-       Base *base;
-       float ctime = frame_to_float(scene, scene->r.cfra); 
+       scene->physics_settings.quick_cache_step= 0;
 
        /* update all objects: drivers, matrices, displists, etc. flags set
           by depgraph or manual, no layer check here, gets correct flushed */
 
-       /* sets first, we allow per definition current scene to have
-          dependencies on sets, but not the other way around. */
-       if(scene->set) {
-               for(SETLOOPER(scene->set, base))
-                       object_handle_update(scene, base->object);
-       }
-       
-       for(base= scene->base.first; base; base= base->next) {
-               object_handle_update(scene, base->object);
-       }
+       scene_update_tagged_recursive(scene, scene);
 
        /* recalc scene animation data here (for sequencer) */
        {
+               float ctime = BKE_curframe(scene); 
                AnimData *adt= BKE_animdata_from_id(&scene->id);
 
                if(adt && (adt->recalc & ADT_RECALC_ANIM))
                        BKE_animsys_evaluate_animdata(&scene->id, adt, ctime, 0);
        }
 
-       BKE_ptcache_quick_cache_all(scene);
+       if(scene->physics_settings.quick_cache_step)
+               BKE_ptcache_quick_cache_all(scene);
 
        /* in the future this should handle updates for all datablocks, not
           only objects and scenes. - brecht */
@@ -947,7 +970,7 @@ void scene_update_tagged(Scene *scene)
 /* applies changes right away, does all sets too */
 void scene_update_for_newframe(Scene *sce, unsigned int lay)
 {
-       float ctime = frame_to_float(sce, sce->r.cfra);
+       float ctime = BKE_curframe(sce);
        Scene *sce_iter;
        
        /* clear animation overrides */
@@ -960,7 +983,7 @@ void scene_update_for_newframe(Scene *sce, unsigned int lay)
 
 
        /* Following 2 functions are recursive
-        * so dont call within 'scene_update_newframe' */
+        * so dont call within 'scene_update_tagged_recursive' */
        DAG_scene_update_flags(sce, lay);   // only stuff that moves or needs display still
 
        /* All 'standard' (i.e. without any dependencies) animation is handled here,
@@ -972,12 +995,8 @@ void scene_update_for_newframe(Scene *sce, unsigned int lay)
        BKE_animsys_evaluate_all_animation(G.main, ctime);
        /*...done with recusrive funcs */
 
-
-       /* sets first, we allow per definition current scene to have dependencies on sets */
-       for(sce_iter= sce->set; sce_iter; sce_iter= sce_iter->set)
-               scene_update_newframe(sce_iter, lay);
-
-       scene_update_newframe(sce, lay);
+       /* object_handle_update() on all objects, groups and sets */
+       scene_update_tagged_recursive(sce, sce);
 }
 
 /* return default layer, also used to patch old files */
@@ -1031,3 +1050,26 @@ float get_render_aosss_error(RenderData *r, float error)
                return error;
 }
 
+/* helper function for the SETLOOPER macro */
+Base *_setlooper_base_step(Scene **sce, Base *base)
+{
+    if(base && base->next) {
+        /* common case, step to the next */
+        return base->next;
+    }
+    else if(base==NULL && (*sce)->base.first) {
+        /* first time looping, return the scenes first base */
+        return (Base *)(*sce)->base.first;
+    }
+    else {
+        /* reached the end, get the next base in the set */
+        while((*sce= (*sce)->set)) {
+            base= (Base *)(*sce)->base.first;
+            if(base) {
+                return base;
+            }
+        }
+    }
+
+    return NULL;
+}