Blender V2.61 - r43446

scene.c

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