Blender  V2.59
scene.c
Go to the documentation of this file.
00001 /*  scene.c
00002  *  
00003  * 
00004  * $Id: scene.c 37363 2011-06-10 10:13:50Z campbellbarton $
00005  *
00006  * ***** BEGIN GPL LICENSE BLOCK *****
00007  *
00008  * This program is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU General Public License
00010  * as published by the Free Software Foundation; either version 2
00011  * of the License, or (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software Foundation,
00020  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00021  *
00022  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00023  * All rights reserved.
00024  *
00025  * The Original Code is: all of this file.
00026  *
00027  * Contributor(s): none yet.
00028  *
00029  * ***** END GPL LICENSE BLOCK *****
00030  */
00031 
00037 #include <stddef.h>
00038 #include <stdio.h>
00039 #include <string.h>
00040 
00041 #ifndef WIN32 
00042 #include <unistd.h>
00043 #else
00044 #include <io.h>
00045 #endif
00046 
00047 #include "MEM_guardedalloc.h"
00048 
00049 #include "DNA_anim_types.h"
00050 #include "DNA_group_types.h"
00051 #include "DNA_object_types.h"
00052 #include "DNA_scene_types.h"
00053 #include "DNA_screen_types.h"
00054 #include "DNA_sequence_types.h"
00055 
00056 #include "BLI_math.h"
00057 #include "BLI_blenlib.h"
00058 #include "BLI_utildefines.h"
00059 
00060 #include "BKE_anim.h"
00061 #include "BKE_animsys.h"
00062 #include "BKE_depsgraph.h"
00063 #include "BKE_global.h"
00064 #include "BKE_group.h"
00065 #include "BKE_idprop.h"
00066 #include "BKE_library.h"
00067 #include "BKE_main.h"
00068 #include "BKE_node.h"
00069 #include "BKE_object.h"
00070 #include "BKE_paint.h"
00071 #include "BKE_pointcache.h"
00072 #include "BKE_scene.h"
00073 #include "BKE_sequencer.h"
00074 #include "BKE_world.h"
00075 
00076 #include "BKE_sound.h"
00077 
00078 //XXX #include "BIF_previewrender.h"
00079 //XXX #include "BIF_editseq.h"
00080 
00081 //XXX #include "nla.h"
00082 
00083 #ifdef WIN32
00084 #else
00085 #include <sys/time.h>
00086 #endif
00087 
00088 void free_avicodecdata(AviCodecData *acd)
00089 {
00090         if (acd) {
00091                 if (acd->lpFormat){
00092                         MEM_freeN(acd->lpFormat);
00093                         acd->lpFormat = NULL;
00094                         acd->cbFormat = 0;
00095                 }
00096                 if (acd->lpParms){
00097                         MEM_freeN(acd->lpParms);
00098                         acd->lpParms = NULL;
00099                         acd->cbParms = 0;
00100                 }
00101         }
00102 }
00103 
00104 void free_qtcodecdata(QuicktimeCodecData *qcd)
00105 {
00106         if (qcd) {
00107                 if (qcd->cdParms){
00108                         MEM_freeN(qcd->cdParms);
00109                         qcd->cdParms = NULL;
00110                         qcd->cdSize = 0;
00111                 }
00112         }
00113 }
00114 
00115 Scene *copy_scene(Scene *sce, int type)
00116 {
00117         Scene *scen;
00118         ToolSettings *ts;
00119         Base *base, *obase;
00120         
00121         if(type == SCE_COPY_EMPTY) {
00122                 ListBase lb;
00123                 scen= add_scene(sce->id.name+2);
00124                 
00125                 lb= scen->r.layers;
00126                 scen->r= sce->r;
00127                 scen->r.layers= lb;
00128         }
00129         else {
00130                 scen= copy_libblock(sce);
00131                 BLI_duplicatelist(&(scen->base), &(sce->base));
00132                 
00133                 clear_id_newpoins();
00134                 
00135                 id_us_plus((ID *)scen->world);
00136                 id_us_plus((ID *)scen->set);
00137                 id_us_plus((ID *)scen->gm.dome.warptext);
00138 
00139                 scen->ed= NULL;
00140                 scen->theDag= NULL;
00141                 scen->obedit= NULL;
00142                 scen->toolsettings= MEM_dupallocN(sce->toolsettings);
00143                 scen->stats= NULL;
00144                 scen->fps_info= NULL;
00145 
00146                 ts= scen->toolsettings;
00147                 if(ts) {
00148                         if(ts->vpaint) {
00149                                 ts->vpaint= MEM_dupallocN(ts->vpaint);
00150                                 ts->vpaint->paintcursor= NULL;
00151                                 ts->vpaint->vpaint_prev= NULL;
00152                                 ts->vpaint->wpaint_prev= NULL;
00153                                 copy_paint(&ts->vpaint->paint, &ts->vpaint->paint);
00154                         }
00155                         if(ts->wpaint) {
00156                                 ts->wpaint= MEM_dupallocN(ts->wpaint);
00157                                 ts->wpaint->paintcursor= NULL;
00158                                 ts->wpaint->vpaint_prev= NULL;
00159                                 ts->wpaint->wpaint_prev= NULL;
00160                                 copy_paint(&ts->wpaint->paint, &ts->wpaint->paint);
00161                         }
00162                         if(ts->sculpt) {
00163                                 ts->sculpt= MEM_dupallocN(ts->sculpt);
00164                                 copy_paint(&ts->sculpt->paint, &ts->sculpt->paint);
00165                         }
00166 
00167                         copy_paint(&ts->imapaint.paint, &ts->imapaint.paint);
00168                         ts->imapaint.paintcursor= NULL;
00169 
00170                         ts->particle.paintcursor= NULL;
00171                 }
00172                 
00173                 BLI_duplicatelist(&(scen->markers), &(sce->markers));
00174                 BLI_duplicatelist(&(scen->transform_spaces), &(sce->transform_spaces));
00175                 BLI_duplicatelist(&(scen->r.layers), &(sce->r.layers));
00176                 BKE_keyingsets_copy(&(scen->keyingsets), &(sce->keyingsets));
00177 
00178                 if(sce->nodetree) {
00179                         scen->nodetree= ntreeCopyTree(sce->nodetree); /* copies actions */
00180                         ntreeSwitchID(scen->nodetree, &sce->id, &scen->id);
00181                 }
00182 
00183                 obase= sce->base.first;
00184                 base= scen->base.first;
00185                 while(base) {
00186                         id_us_plus(&base->object->id);
00187                         if(obase==sce->basact) scen->basact= base;
00188         
00189                         obase= obase->next;
00190                         base= base->next;
00191                 }
00192         }
00193         
00194         /* make a private copy of the avicodecdata */
00195         if(sce->r.avicodecdata) {
00196                 scen->r.avicodecdata = MEM_dupallocN(sce->r.avicodecdata);
00197                 scen->r.avicodecdata->lpFormat = MEM_dupallocN(scen->r.avicodecdata->lpFormat);
00198                 scen->r.avicodecdata->lpParms = MEM_dupallocN(scen->r.avicodecdata->lpParms);
00199         }
00200         
00201         /* make a private copy of the qtcodecdata */
00202         if(sce->r.qtcodecdata) {
00203                 scen->r.qtcodecdata = MEM_dupallocN(sce->r.qtcodecdata);
00204                 scen->r.qtcodecdata->cdParms = MEM_dupallocN(scen->r.qtcodecdata->cdParms);
00205         }
00206         
00207         if(sce->r.ffcodecdata.properties) { /* intentionally check scen not sce. */
00208                 scen->r.ffcodecdata.properties= IDP_CopyProperty(sce->r.ffcodecdata.properties);
00209         }
00210 
00211         /* NOTE: part of SCE_COPY_LINK_DATA and SCE_COPY_FULL operations
00212          * are done outside of blenkernel with ED_objects_single_users! */
00213 
00214         /*  camera */
00215         if(type == SCE_COPY_LINK_DATA || type == SCE_COPY_FULL) {
00216                 ID_NEW(scen->camera);
00217         }
00218         
00219         /* before scene copy */
00220         sound_create_scene(scen);
00221 
00222         /* world */
00223         if(type == SCE_COPY_FULL) {
00224                 BKE_copy_animdata_id_action((ID *)scen);
00225                 if(scen->world) {
00226                         id_us_plus((ID *)scen->world);
00227                         scen->world= copy_world(scen->world);
00228                         BKE_copy_animdata_id_action((ID *)scen->world);
00229                 }
00230 
00231                 if(sce->ed) {
00232                         scen->ed= MEM_callocN( sizeof(Editing), "addseq");
00233                         scen->ed->seqbasep= &scen->ed->seqbase;
00234                         seqbase_dupli_recursive(sce, scen, &scen->ed->seqbase, &sce->ed->seqbase, SEQ_DUPE_ALL);
00235                 }
00236         }
00237 
00238         return scen;
00239 }
00240 
00241 /* do not free scene itself */
00242 void free_scene(Scene *sce)
00243 {
00244         Base *base;
00245 
00246         base= sce->base.first;
00247         while(base) {
00248                 base->object->id.us--;
00249                 base= base->next;
00250         }
00251         /* do not free objects! */
00252         
00253         if(sce->gpd) {
00254 #if 0   // removed since this can be invalid memory when freeing everything
00255                 // since the grease pencil data is free'd before the scene.
00256                 // since grease pencil data is not (yet?), shared between objects
00257                 // its probably safe not to do this, some save and reload will free this.
00258                 sce->gpd->id.us--;
00259 #endif
00260                 sce->gpd= NULL;
00261         }
00262 
00263         BLI_freelistN(&sce->base);
00264         seq_free_editing(sce);
00265 
00266         BKE_free_animdata((ID *)sce);
00267         BKE_keyingsets_free(&sce->keyingsets);
00268         
00269         if (sce->r.avicodecdata) {
00270                 free_avicodecdata(sce->r.avicodecdata);
00271                 MEM_freeN(sce->r.avicodecdata);
00272                 sce->r.avicodecdata = NULL;
00273         }
00274         if (sce->r.qtcodecdata) {
00275                 free_qtcodecdata(sce->r.qtcodecdata);
00276                 MEM_freeN(sce->r.qtcodecdata);
00277                 sce->r.qtcodecdata = NULL;
00278         }
00279         if (sce->r.ffcodecdata.properties) {
00280                 IDP_FreeProperty(sce->r.ffcodecdata.properties);
00281                 MEM_freeN(sce->r.ffcodecdata.properties);
00282                 sce->r.ffcodecdata.properties = NULL;
00283         }
00284         
00285         BLI_freelistN(&sce->markers);
00286         BLI_freelistN(&sce->transform_spaces);
00287         BLI_freelistN(&sce->r.layers);
00288         
00289         if(sce->toolsettings) {
00290                 if(sce->toolsettings->vpaint) {
00291                         free_paint(&sce->toolsettings->vpaint->paint);
00292                         MEM_freeN(sce->toolsettings->vpaint);
00293                 }
00294                 if(sce->toolsettings->wpaint) {
00295                         free_paint(&sce->toolsettings->wpaint->paint);
00296                         MEM_freeN(sce->toolsettings->wpaint);
00297                 }
00298                 if(sce->toolsettings->sculpt) {
00299                         free_paint(&sce->toolsettings->sculpt->paint);
00300                         MEM_freeN(sce->toolsettings->sculpt);
00301                 }
00302                 free_paint(&sce->toolsettings->imapaint.paint);
00303 
00304                 MEM_freeN(sce->toolsettings);
00305                 sce->toolsettings = NULL;       
00306         }
00307         
00308         if (sce->theDag) {
00309                 free_forest(sce->theDag);
00310                 MEM_freeN(sce->theDag);
00311         }
00312         
00313         if(sce->nodetree) {
00314                 ntreeFreeTree(sce->nodetree);
00315                 MEM_freeN(sce->nodetree);
00316         }
00317 
00318         if(sce->stats)
00319                 MEM_freeN(sce->stats);
00320         if(sce->fps_info)
00321                 MEM_freeN(sce->fps_info);
00322 
00323         sound_destroy_scene(sce);
00324 }
00325 
00326 Scene *add_scene(const char *name)
00327 {
00328         Main *bmain= G.main;
00329         Scene *sce;
00330         ParticleEditSettings *pset;
00331         int a;
00332 
00333         sce= alloc_libblock(&bmain->scene, ID_SCE, name);
00334         sce->lay= sce->layact= 1;
00335         
00336         sce->r.mode= R_GAMMA|R_OSA|R_SHADOW|R_SSS|R_ENVMAP|R_RAYTRACE;
00337         sce->r.cfra= 1;
00338         sce->r.sfra= 1;
00339         sce->r.efra= 250;
00340         sce->r.frame_step= 1;
00341         sce->r.xsch= 1920;
00342         sce->r.ysch= 1080;
00343         sce->r.xasp= 1;
00344         sce->r.yasp= 1;
00345         sce->r.xparts= 8;
00346         sce->r.yparts= 8;
00347         sce->r.mblur_samples= 1;
00348         sce->r.filtertype= R_FILTER_MITCH;
00349         sce->r.size= 50;
00350         sce->r.planes= 24;
00351         sce->r.imtype= R_PNG;
00352         sce->r.quality= 90;
00353         sce->r.displaymode= R_OUTPUT_AREA;
00354         sce->r.framapto= 100;
00355         sce->r.images= 100;
00356         sce->r.framelen= 1.0;
00357         sce->r.blurfac= 0.5;
00358         sce->r.frs_sec= 24;
00359         sce->r.frs_sec_base= 1;
00360         sce->r.edgeint= 10;
00361         sce->r.ocres = 128;
00362         sce->r.color_mgt_flag |= R_COLOR_MANAGEMENT;
00363         sce->r.gauss= 1.0;
00364         
00365         /* deprecated but keep for upwards compat */
00366         sce->r.postgamma= 1.0;
00367         sce->r.posthue= 0.0;
00368         sce->r.postsat= 1.0;
00369         
00370         sce->r.bake_mode= 1;    /* prevent to include render stuff here */
00371         sce->r.bake_filter= 2;
00372         sce->r.bake_osa= 5;
00373         sce->r.bake_flag= R_BAKE_CLEAR;
00374         sce->r.bake_normal_space= R_BAKE_SPACE_TANGENT;
00375         sce->r.scemode= R_DOCOMP|R_DOSEQ|R_EXTENSION;
00376         sce->r.stamp= R_STAMP_TIME|R_STAMP_FRAME|R_STAMP_DATE|R_STAMP_CAMERA|R_STAMP_SCENE|R_STAMP_FILENAME|R_STAMP_RENDERTIME;
00377         sce->r.stamp_font_id= 12;
00378         sce->r.fg_stamp[0]= sce->r.fg_stamp[1]= sce->r.fg_stamp[2]= 0.8f;
00379         sce->r.fg_stamp[3]= 1.0f;
00380         sce->r.bg_stamp[0]= sce->r.bg_stamp[1]= sce->r.bg_stamp[2]= 0.0f;
00381         sce->r.bg_stamp[3]= 0.25f;
00382         sce->r.raytrace_options = R_RAYTRACE_USE_INSTANCES;
00383 
00384         sce->r.seq_prev_type= OB_SOLID;
00385         sce->r.seq_rend_type= OB_SOLID;
00386         sce->r.seq_flag= R_SEQ_GL_PREV;
00387 
00388         sce->r.threads= 1;
00389 
00390         sce->r.simplify_subsurf= 6;
00391         sce->r.simplify_particles= 1.0f;
00392         sce->r.simplify_shadowsamples= 16;
00393         sce->r.simplify_aosss= 1.0f;
00394 
00395         sce->r.cineonblack= 95;
00396         sce->r.cineonwhite= 685;
00397         sce->r.cineongamma= 1.7f;
00398 
00399         sce->r.border.xmin= 0.0f;
00400         sce->r.border.ymin= 0.0f;
00401         sce->r.border.xmax= 1.0f;
00402         sce->r.border.ymax= 1.0f;
00403         
00404         sce->toolsettings = MEM_callocN(sizeof(struct ToolSettings),"Tool Settings Struct");
00405         sce->toolsettings->cornertype=1;
00406         sce->toolsettings->degr = 90; 
00407         sce->toolsettings->step = 9;
00408         sce->toolsettings->turn = 1;                            
00409         sce->toolsettings->extr_offs = 1; 
00410         sce->toolsettings->doublimit = 0.001;
00411         sce->toolsettings->segments = 32;
00412         sce->toolsettings->rings = 32;
00413         sce->toolsettings->vertices = 32;
00414         sce->toolsettings->editbutflag = 1;
00415         sce->toolsettings->uvcalc_radius = 1.0f;
00416         sce->toolsettings->uvcalc_cubesize = 1.0f;
00417         sce->toolsettings->uvcalc_mapdir = 1;
00418         sce->toolsettings->uvcalc_mapalign = 1;
00419         sce->toolsettings->unwrapper = 1;
00420         sce->toolsettings->select_thresh= 0.01f;
00421         sce->toolsettings->jointrilimit = 0.8f;
00422 
00423         sce->toolsettings->selectmode= SCE_SELECT_VERTEX;
00424         sce->toolsettings->uv_selectmode= UV_SELECT_VERTEX;
00425         sce->toolsettings->normalsize= 0.1;
00426         sce->toolsettings->autokey_mode= U.autokey_mode;
00427 
00428         sce->toolsettings->skgen_resolution = 100;
00429         sce->toolsettings->skgen_threshold_internal     = 0.01f;
00430         sce->toolsettings->skgen_threshold_external     = 0.01f;
00431         sce->toolsettings->skgen_angle_limit                    = 45.0f;
00432         sce->toolsettings->skgen_length_ratio                   = 1.3f;
00433         sce->toolsettings->skgen_length_limit                   = 1.5f;
00434         sce->toolsettings->skgen_correlation_limit              = 0.98f;
00435         sce->toolsettings->skgen_symmetry_limit                 = 0.1f;
00436         sce->toolsettings->skgen_postpro = SKGEN_SMOOTH;
00437         sce->toolsettings->skgen_postpro_passes = 1;
00438         sce->toolsettings->skgen_options = SKGEN_FILTER_INTERNAL|SKGEN_FILTER_EXTERNAL|SKGEN_FILTER_SMART|SKGEN_HARMONIC|SKGEN_SUB_CORRELATION|SKGEN_STICK_TO_EMBEDDING;
00439         sce->toolsettings->skgen_subdivisions[0] = SKGEN_SUB_CORRELATION;
00440         sce->toolsettings->skgen_subdivisions[1] = SKGEN_SUB_LENGTH;
00441         sce->toolsettings->skgen_subdivisions[2] = SKGEN_SUB_ANGLE;
00442 
00443         sce->toolsettings->proportional_size = 1.0f;
00444 
00445         sce->physics_settings.gravity[0] = 0.0f;
00446         sce->physics_settings.gravity[1] = 0.0f;
00447         sce->physics_settings.gravity[2] = -9.81f;
00448         sce->physics_settings.flag = PHYS_GLOBAL_GRAVITY;
00449 
00450         sce->unit.scale_length = 1.0f;
00451 
00452         pset= &sce->toolsettings->particle;
00453         pset->flag= PE_KEEP_LENGTHS|PE_LOCK_FIRST|PE_DEFLECT_EMITTER|PE_AUTO_VELOCITY;
00454         pset->emitterdist= 0.25f;
00455         pset->totrekey= 5;
00456         pset->totaddkey= 5;
00457         pset->brushtype= PE_BRUSH_NONE;
00458         pset->draw_step= 2;
00459         pset->fade_frames= 2;
00460         pset->selectmode= SCE_SELECT_PATH;
00461         for(a=0; a<PE_TOT_BRUSH; a++) {
00462                 pset->brush[a].strength= 0.5;
00463                 pset->brush[a].size= 50;
00464                 pset->brush[a].step= 10;
00465                 pset->brush[a].count= 10;
00466         }
00467         pset->brush[PE_BRUSH_CUT].strength= 100;
00468 
00469         sce->r.ffcodecdata.audio_mixrate = 44100;
00470         sce->r.ffcodecdata.audio_volume = 1.0f;
00471         sce->r.ffcodecdata.audio_bitrate = 192;
00472 
00473         BLI_strncpy(sce->r.engine, "BLENDER_RENDER", sizeof(sce->r.engine));
00474 
00475         sce->audio.distance_model = 2.0;
00476         sce->audio.doppler_factor = 1.0;
00477         sce->audio.speed_of_sound = 343.3;
00478 
00479         BLI_strncpy(sce->r.pic, U.renderdir, sizeof(sce->r.pic));
00480 
00481         BLI_init_rctf(&sce->r.safety, 0.1f, 0.9f, 0.1f, 0.9f);
00482         sce->r.osa= 8;
00483 
00484         /* note; in header_info.c the scene copy happens..., if you add more to renderdata it has to be checked there */
00485         scene_add_render_layer(sce);
00486         
00487         /* game data */
00488         sce->gm.stereoflag = STEREO_NOSTEREO;
00489         sce->gm.stereomode = STEREO_ANAGLYPH;
00490         sce->gm.eyeseparation = 0.10;
00491 
00492         sce->gm.dome.angle = 180;
00493         sce->gm.dome.mode = DOME_FISHEYE;
00494         sce->gm.dome.res = 4;
00495         sce->gm.dome.resbuf = 1.0f;
00496         sce->gm.dome.tilt = 0;
00497 
00498         sce->gm.xplay= 640;
00499         sce->gm.yplay= 480;
00500         sce->gm.freqplay= 60;
00501         sce->gm.depth= 32;
00502 
00503         sce->gm.gravity= 9.8f;
00504         sce->gm.physicsEngine= WOPHY_BULLET;
00505         sce->gm.mode = 32; //XXX ugly harcoding, still not sure we should drop mode. 32 == 1 << 5 == use_occlusion_culling 
00506         sce->gm.occlusionRes = 128;
00507         sce->gm.ticrate = 60;
00508         sce->gm.maxlogicstep = 5;
00509         sce->gm.physubstep = 1;
00510         sce->gm.maxphystep = 5;
00511 
00512         sce->gm.flag = GAME_DISPLAY_LISTS;
00513         sce->gm.matmode = GAME_MAT_MULTITEX;
00514 
00515         sound_create_scene(sce);
00516 
00517         return sce;
00518 }
00519 
00520 Base *object_in_scene(Object *ob, Scene *sce)
00521 {
00522         Base *base;
00523         
00524         base= sce->base.first;
00525         while(base) {
00526                 if(base->object == ob) return base;
00527                 base= base->next;
00528         }
00529         return NULL;
00530 }
00531 
00532 void set_scene_bg(Main *bmain, Scene *scene)
00533 {
00534         Scene *sce;
00535         Base *base;
00536         Object *ob;
00537         Group *group;
00538         GroupObject *go;
00539         int flag;
00540         
00541         /* check for cyclic sets, for reading old files but also for definite security (py?) */
00542         scene_check_setscene(bmain, scene);
00543         
00544         /* can happen when switching modes in other scenes */
00545         if(scene->obedit && !(scene->obedit->mode & OB_MODE_EDIT))
00546                 scene->obedit= NULL;
00547 
00548         /* deselect objects (for dataselect) */
00549         for(ob= bmain->object.first; ob; ob= ob->id.next)
00550                 ob->flag &= ~(SELECT|OB_FROMGROUP);
00551 
00552         /* group flags again */
00553         for(group= bmain->group.first; group; group= group->id.next) {
00554                 go= group->gobject.first;
00555                 while(go) {
00556                         if(go->ob) go->ob->flag |= OB_FROMGROUP;
00557                         go= go->next;
00558                 }
00559         }
00560 
00561         /* sort baselist */
00562         DAG_scene_sort(bmain, scene);
00563         
00564         /* ensure dags are built for sets */
00565         for(sce= scene->set; sce; sce= sce->set)
00566                 if(sce->theDag==NULL)
00567                         DAG_scene_sort(bmain, sce);
00568 
00569         /* copy layers and flags from bases to objects */
00570         for(base= scene->base.first; base; base= base->next) {
00571                 ob= base->object;
00572                 ob->lay= base->lay;
00573                 
00574                 /* group patch... */
00575                 base->flag &= ~(OB_FROMGROUP);
00576                 flag= ob->flag & (OB_FROMGROUP);
00577                 base->flag |= flag;
00578                 
00579                 /* not too nice... for recovering objects with lost data */
00580                 //if(ob->pose==NULL) base->flag &= ~OB_POSEMODE;
00581                 ob->flag= base->flag;
00582                 
00583                 ob->ctime= -1234567.0;  /* force ipo to be calculated later */
00584         }
00585         /* no full animation update, this to enable render code to work (render code calls own animation updates) */
00586 }
00587 
00588 /* called from creator.c */
00589 Scene *set_scene_name(Main *bmain, const char *name)
00590 {
00591         Scene *sce= (Scene *)find_id("SC", name);
00592         if(sce) {
00593                 set_scene_bg(bmain, sce);
00594                 printf("Scene switch: '%s' in file: '%s'\n", name, G.main->name);
00595                 return sce;
00596         }
00597 
00598         printf("Can't find scene: '%s' in file: '%s'\n", name, G.main->name);
00599         return NULL;
00600 }
00601 
00602 void unlink_scene(Main *bmain, Scene *sce, Scene *newsce)
00603 {
00604         Scene *sce1;
00605         bScreen *sc;
00606 
00607         /* check all sets */
00608         for(sce1= bmain->scene.first; sce1; sce1= sce1->id.next)
00609                 if(sce1->set == sce)
00610                         sce1->set= NULL;
00611         
00612         /* check all sequences */
00613         clear_scene_in_allseqs(bmain, sce);
00614 
00615         /* check render layer nodes in other scenes */
00616         clear_scene_in_nodes(bmain, sce);
00617         
00618         /* al screens */
00619         for(sc= bmain->screen.first; sc; sc= sc->id.next)
00620                 if(sc->scene == sce)
00621                         sc->scene= newsce;
00622 
00623         free_libblock(&bmain->scene, sce);
00624 }
00625 
00626 /* used by metaballs
00627  * doesnt return the original duplicated object, only dupli's
00628  */
00629 int next_object(Scene **scene, int val, Base **base, Object **ob)
00630 {
00631         static ListBase *duplilist= NULL;
00632         static DupliObject *dupob;
00633         static int fase= F_START, in_next_object= 0;
00634         int run_again=1;
00635         
00636         /* init */
00637         if(val==0) {
00638                 fase= F_START;
00639                 dupob= NULL;
00640                 
00641                 /* XXX particle systems with metas+dupligroups call this recursively */
00642                 /* see bug #18725 */
00643                 if(in_next_object) {
00644                         printf("ERROR: MetaBall generation called recursively, not supported\n");
00645                         
00646                         return F_ERROR;
00647                 }
00648         }
00649         else {
00650                 in_next_object= 1;
00651                 
00652                 /* run_again is set when a duplilist has been ended */
00653                 while(run_again) {
00654                         run_again= 0;
00655 
00656                         /* the first base */
00657                         if(fase==F_START) {
00658                                 *base= (*scene)->base.first;
00659                                 if(*base) {
00660                                         *ob= (*base)->object;
00661                                         fase= F_SCENE;
00662                                 }
00663                                 else {
00664                                         /* exception: empty scene */
00665                                         while((*scene)->set) {
00666                                                 (*scene)= (*scene)->set;
00667                                                 if((*scene)->base.first) {
00668                                                         *base= (*scene)->base.first;
00669                                                         *ob= (*base)->object;
00670                                                         fase= F_SCENE;
00671                                                         break;
00672                                                 }
00673                                         }
00674                                 }
00675                         }
00676                         else {
00677                                 if(*base && fase!=F_DUPLI) {
00678                                         *base= (*base)->next;
00679                                         if(*base) *ob= (*base)->object;
00680                                         else {
00681                                                 if(fase==F_SCENE) {
00682                                                         /* (*scene) is finished, now do the set */
00683                                                         while((*scene)->set) {
00684                                                                 (*scene)= (*scene)->set;
00685                                                                 if((*scene)->base.first) {
00686                                                                         *base= (*scene)->base.first;
00687                                                                         *ob= (*base)->object;
00688                                                                         break;
00689                                                                 }
00690                                                         }
00691                                                 }
00692                                         }
00693                                 }
00694                         }
00695                         
00696                         if(*base == NULL) fase= F_START;
00697                         else {
00698                                 if(fase!=F_DUPLI) {
00699                                         if( (*base)->object->transflag & OB_DUPLI) {
00700                                                 /* groups cannot be duplicated for mballs yet, 
00701                                                 this enters eternal loop because of 
00702                                                 makeDispListMBall getting called inside of group_duplilist */
00703                                                 if((*base)->object->dup_group == NULL) {
00704                                                         duplilist= object_duplilist((*scene), (*base)->object);
00705                                                         
00706                                                         dupob= duplilist->first;
00707 
00708                                                         if(!dupob)
00709                                                                 free_object_duplilist(duplilist);
00710                                                 }
00711                                         }
00712                                 }
00713                                 /* handle dupli's */
00714                                 if(dupob) {
00715                                         
00716                                         copy_m4_m4(dupob->ob->obmat, dupob->mat);
00717                                         
00718                                         (*base)->flag |= OB_FROMDUPLI;
00719                                         *ob= dupob->ob;
00720                                         fase= F_DUPLI;
00721                                         
00722                                         dupob= dupob->next;
00723                                 }
00724                                 else if(fase==F_DUPLI) {
00725                                         fase= F_SCENE;
00726                                         (*base)->flag &= ~OB_FROMDUPLI;
00727                                         
00728                                         for(dupob= duplilist->first; dupob; dupob= dupob->next) {
00729                                                 copy_m4_m4(dupob->ob->obmat, dupob->omat);
00730                                         }
00731                                         
00732                                         free_object_duplilist(duplilist);
00733                                         duplilist= NULL;
00734                                         run_again= 1;
00735                                 }
00736                         }
00737                 }
00738         }
00739         
00740         /* if(ob && *ob) {
00741                 printf("Scene: '%s', '%s'\n", (*scene)->id.name+2, (*ob)->id.name+2);
00742         } */
00743 
00744         /* reset recursion test */
00745         in_next_object= 0;
00746         
00747         return fase;
00748 }
00749 
00750 Object *scene_find_camera(Scene *sc)
00751 {
00752         Base *base;
00753         
00754         for (base= sc->base.first; base; base= base->next)
00755                 if (base->object->type==OB_CAMERA)
00756                         return base->object;
00757 
00758         return NULL;
00759 }
00760 
00761 #ifdef DURIAN_CAMERA_SWITCH
00762 Object *scene_camera_switch_find(Scene *scene)
00763 {
00764         TimeMarker *m;
00765         int cfra = scene->r.cfra;
00766         int frame = -(MAXFRAME + 1);
00767         Object *camera= NULL;
00768 
00769         for (m= scene->markers.first; m; m= m->next) {
00770                 if(m->camera && (m->camera->restrictflag & OB_RESTRICT_RENDER)==0 && (m->frame <= cfra) && (m->frame > frame)) {
00771                         camera= m->camera;
00772                         frame= m->frame;
00773 
00774                         if(frame == cfra)
00775                                 break;
00776 
00777                 }
00778         }
00779         return camera;
00780 }
00781 #endif
00782 
00783 int scene_camera_switch_update(Scene *scene)
00784 {
00785 #ifdef DURIAN_CAMERA_SWITCH
00786         Object *camera= scene_camera_switch_find(scene);
00787         if(camera) {
00788                 scene->camera= camera;
00789                 return 1;
00790         }
00791 #endif
00792         return 0;
00793 }
00794 
00795 char *scene_find_marker_name(Scene *scene, int frame)
00796 {
00797         ListBase *markers= &scene->markers;
00798         TimeMarker *m1, *m2;
00799 
00800         /* search through markers for match */
00801         for (m1=markers->first, m2=markers->last; m1 && m2; m1=m1->next, m2=m2->prev) {
00802                 if (m1->frame==frame)
00803                         return m1->name;
00804 
00805                 if (m1 == m2)
00806                         break;
00807 
00808                 if (m2->frame==frame)
00809                         return m2->name;
00810         }
00811 
00812         return NULL;
00813 }
00814 
00815 /* return the current marker for this frame,
00816 we can have more then 1 marker per frame, this just returns the first :/ */
00817 char *scene_find_last_marker_name(Scene *scene, int frame)
00818 {
00819         TimeMarker *marker, *best_marker = NULL;
00820         int best_frame = -MAXFRAME*2;
00821         for (marker= scene->markers.first; marker; marker= marker->next) {
00822                 if (marker->frame==frame) {
00823                         return marker->name;
00824                 }
00825 
00826                 if ( marker->frame > best_frame && marker->frame < frame) {
00827                         best_marker = marker;
00828                         best_frame = marker->frame;
00829                 }
00830         }
00831 
00832         return best_marker ? best_marker->name : NULL;
00833 }
00834 
00835 
00836 Base *scene_add_base(Scene *sce, Object *ob)
00837 {
00838         Base *b= MEM_callocN(sizeof(*b), "scene_add_base");
00839         BLI_addhead(&sce->base, b);
00840 
00841         b->object= ob;
00842         b->flag= ob->flag;
00843         b->lay= ob->lay;
00844 
00845         return b;
00846 }
00847 
00848 void scene_deselect_all(Scene *sce)
00849 {
00850         Base *b;
00851 
00852         for (b= sce->base.first; b; b= b->next) {
00853                 b->flag&= ~SELECT;
00854                 b->object->flag= b->flag;
00855         }
00856 }
00857 
00858 void scene_select_base(Scene *sce, Base *selbase)
00859 {
00860         scene_deselect_all(sce);
00861 
00862         selbase->flag |= SELECT;
00863         selbase->object->flag= selbase->flag;
00864 
00865         sce->basact= selbase;
00866 }
00867 
00868 /* checks for cycle, returns 1 if it's all OK */
00869 int scene_check_setscene(Main *bmain, Scene *sce)
00870 {
00871         Scene *scene;
00872         int a, totscene;
00873         
00874         if(sce->set==NULL) return 1;
00875         
00876         totscene= 0;
00877         for(scene= bmain->scene.first; scene; scene= scene->id.next)
00878                 totscene++;
00879         
00880         for(a=0, scene=sce; scene->set; scene=scene->set, a++) {
00881                 /* more iterations than scenes means we have a cycle */
00882                 if(a > totscene) {
00883                         /* the tested scene gets zero'ed, that's typically current scene */
00884                         sce->set= NULL;
00885                         return 0;
00886                 }
00887         }
00888 
00889         return 1;
00890 }
00891 
00892 /* This function is needed to cope with fractional frames - including two Blender rendering features
00893 * mblur (motion blur that renders 'subframes' and blurs them together), and fields rendering. */
00894 
00895 /* see also bsystem_time in object.c */
00896 float BKE_curframe(Scene *scene)
00897 {
00898         float ctime = scene->r.cfra;
00899         ctime+= scene->r.subframe;
00900         ctime*= scene->r.framelen;      
00901 
00902         return ctime;
00903 }
00904 
00905 /* drivers support/hacks 
00906  *      - this method is called from scene_update_tagged_recursive(), so gets included in viewport + render
00907  *      - these are always run since the depsgraph can't handle non-object data
00908  *      - these happen after objects are all done so that we can read in their final transform values,
00909  *        though this means that objects can't refer to scene info for guidance...
00910  */
00911 static void scene_update_drivers(Main *UNUSED(bmain), Scene *scene)
00912 {
00913         float ctime = BKE_curframe(scene);
00914         
00915         /* scene itself */
00916         if (scene->adt && scene->adt->drivers.first) {
00917                 BKE_animsys_evaluate_animdata(&scene->id, scene->adt, ctime, ADT_RECALC_DRIVERS);
00918         }
00919         
00920         /* world */
00921         // TODO: what about world textures? but then those have nodes too...
00922         if (scene->world) {
00923                 ID *wid = (ID *)scene->world;
00924                 AnimData *adt= BKE_animdata_from_id(wid);
00925                 
00926                 if (adt && adt->drivers.first)
00927                         BKE_animsys_evaluate_animdata(wid, adt, ctime, ADT_RECALC_DRIVERS);
00928         }
00929         
00930         /* nodes */
00931         if (scene->nodetree) {
00932                 ID *nid = (ID *)scene->nodetree;
00933                 AnimData *adt= BKE_animdata_from_id(nid);
00934                 
00935                 if (adt && adt->drivers.first)
00936                         BKE_animsys_evaluate_animdata(nid, adt, ctime, ADT_RECALC_DRIVERS);
00937         }
00938 }
00939 
00940 static void scene_update_tagged_recursive(Main *bmain, Scene *scene, Scene *scene_parent)
00941 {
00942         Base *base;
00943         
00944         
00945         scene->customdata_mask= scene_parent->customdata_mask;
00946 
00947         /* sets first, we allow per definition current scene to have
00948            dependencies on sets, but not the other way around. */
00949         if (scene->set)
00950                 scene_update_tagged_recursive(bmain, scene->set, scene_parent);
00951         
00952         /* scene objects */
00953         for (base= scene->base.first; base; base= base->next) {
00954                 Object *ob= base->object;
00955                 
00956                 object_handle_update(scene_parent, ob);
00957                 
00958                 if(ob->dup_group && (ob->transflag & OB_DUPLIGROUP))
00959                         group_handle_recalc_and_update(scene_parent, ob, ob->dup_group);
00960                         
00961                 /* always update layer, so that animating layers works */
00962                 base->lay= ob->lay;
00963         }
00964         
00965         /* scene drivers... */
00966         scene_update_drivers(bmain, scene);
00967 }
00968 
00969 /* this is called in main loop, doing tagged updates before redraw */
00970 void scene_update_tagged(Main *bmain, Scene *scene)
00971 {
00972         DAG_ids_flush_tagged(bmain);
00973 
00974         scene->physics_settings.quick_cache_step= 0;
00975 
00976         /* update all objects: drivers, matrices, displists, etc. flags set
00977            by depgraph or manual, no layer check here, gets correct flushed */
00978 
00979         scene_update_tagged_recursive(bmain, scene, scene);
00980 
00981         /* recalc scene animation data here (for sequencer) */
00982         {
00983                 AnimData *adt= BKE_animdata_from_id(&scene->id);
00984                 float ctime = BKE_curframe(scene);
00985                 
00986                 if (adt && (adt->recalc & ADT_RECALC_ANIM))
00987                         BKE_animsys_evaluate_animdata(&scene->id, adt, ctime, 0);
00988         }
00989         
00990         if (scene->physics_settings.quick_cache_step)
00991                 BKE_ptcache_quick_cache_all(bmain, scene);
00992 
00993         /* in the future this should handle updates for all datablocks, not
00994            only objects and scenes. - brecht */
00995 }
00996 
00997 /* applies changes right away, does all sets too */
00998 void scene_update_for_newframe(Main *bmain, Scene *sce, unsigned int lay)
00999 {
01000         float ctime = BKE_curframe(sce);
01001         Scene *sce_iter;
01002         
01003         /* clear animation overrides */
01004         // XXX TODO...
01005 
01006         for(sce_iter= sce; sce_iter; sce_iter= sce_iter->set) {
01007                 if(sce_iter->theDag==NULL)
01008                         DAG_scene_sort(bmain, sce_iter);
01009         }
01010 
01011 
01012         /* Following 2 functions are recursive
01013          * so dont call within 'scene_update_tagged_recursive' */
01014         DAG_scene_update_flags(bmain, sce, lay, TRUE);   // only stuff that moves or needs display still
01015 
01016         /* All 'standard' (i.e. without any dependencies) animation is handled here,
01017          * with an 'local' to 'macro' order of evaluation. This should ensure that
01018          * settings stored nestled within a hierarchy (i.e. settings in a Texture block
01019          * can be overridden by settings from Scene, which owns the Texture through a hierarchy
01020          * such as Scene->World->MTex/Texture) can still get correctly overridden.
01021          */
01022         BKE_animsys_evaluate_all_animation(bmain, ctime);
01023         /*...done with recusrive funcs */
01024 
01025         /* object_handle_update() on all objects, groups and sets */
01026         scene_update_tagged_recursive(bmain, sce, sce);
01027 }
01028 
01029 /* return default layer, also used to patch old files */
01030 void scene_add_render_layer(Scene *sce)
01031 {
01032         SceneRenderLayer *srl;
01033 //      int tot= 1 + BLI_countlist(&sce->r.layers);
01034         
01035         srl= MEM_callocN(sizeof(SceneRenderLayer), "new render layer");
01036         strcpy(srl->name, "RenderLayer");
01037         BLI_uniquename(&sce->r.layers, srl, "RenderLayer", '.', offsetof(SceneRenderLayer, name), sizeof(srl->name));
01038         BLI_addtail(&sce->r.layers, srl);
01039 
01040         /* note, this is also in render, pipeline.c, to make layer when scenedata doesnt have it */
01041         srl->lay= (1<<20) -1;
01042         srl->layflag= 0x7FFF;   /* solid ztra halo edge strand */
01043         srl->passflag= SCE_PASS_COMBINED|SCE_PASS_Z;
01044 }
01045 
01046 /* render simplification */
01047 
01048 int get_render_subsurf_level(RenderData *r, int lvl)
01049 {
01050         if(r->mode & R_SIMPLIFY)
01051                 return MIN2(r->simplify_subsurf, lvl);
01052         else
01053                 return lvl;
01054 }
01055 
01056 int get_render_child_particle_number(RenderData *r, int num)
01057 {
01058         if(r->mode & R_SIMPLIFY)
01059                 return (int)(r->simplify_particles*num);
01060         else
01061                 return num;
01062 }
01063 
01064 int get_render_shadow_samples(RenderData *r, int samples)
01065 {
01066         if((r->mode & R_SIMPLIFY) && samples > 0)
01067                 return MIN2(r->simplify_shadowsamples, samples);
01068         else
01069                 return samples;
01070 }
01071 
01072 float get_render_aosss_error(RenderData *r, float error)
01073 {
01074         if(r->mode & R_SIMPLIFY)
01075                 return ((1.0f-r->simplify_aosss)*10.0f + 1.0f)*error;
01076         else
01077                 return error;
01078 }
01079 
01080 /* helper function for the SETLOOPER macro */
01081 Base *_setlooper_base_step(Scene **sce_iter, Base *base)
01082 {
01083         if(base && base->next) {
01084                 /* common case, step to the next */
01085                 return base->next;
01086         }
01087         else if(base==NULL && (*sce_iter)->base.first) {
01088                 /* first time looping, return the scenes first base */
01089                 return (Base *)(*sce_iter)->base.first;
01090         }
01091         else {
01092                 /* reached the end, get the next base in the set */
01093                 while((*sce_iter= (*sce_iter)->set)) {
01094                         base= (Base *)(*sce_iter)->base.first;
01095                         if(base) {
01096                                 return base;
01097                         }
01098                 }
01099         }
01100 
01101         return NULL;
01102 }