Merged changes in the trunk up to revision 41368.
[blender.git] / source / blender / blenkernel / intern / scene.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/scene.c
29  *  \ingroup bke
30  */
31
32
33 #include <stddef.h>
34 #include <stdio.h>
35 #include <string.h>
36
37 #ifndef WIN32 
38 #include <unistd.h>
39 #else
40 #include <io.h>
41 #endif
42
43 #include "MEM_guardedalloc.h"
44
45 #include "DNA_anim_types.h"
46 #include "DNA_group_types.h"
47 #include "DNA_object_types.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_screen_types.h"
50 #include "DNA_sequence_types.h"
51
52 #include "BLI_math.h"
53 #include "BLI_blenlib.h"
54 #include "BLI_utildefines.h"
55 #include "BLI_callbacks.h"
56
57 #include "BKE_anim.h"
58 #include "BKE_animsys.h"
59 #include "BKE_depsgraph.h"
60 #include "BKE_global.h"
61 #include "BKE_group.h"
62 #include "BKE_idprop.h"
63 #include "BKE_library.h"
64 #include "BKE_main.h"
65 #include "BKE_node.h"
66 #include "BKE_object.h"
67 #include "BKE_paint.h"
68 #include "BKE_pointcache.h"
69 #include "BKE_scene.h"
70 #include "BKE_sequencer.h"
71 #include "BKE_world.h"
72
73 #include "BKE_sound.h"
74
75 //XXX #include "BIF_previewrender.h"
76 //XXX #include "BIF_editseq.h"
77
78 #include "FRS_freestyle_config.h"
79
80 //XXX #include "nla.h"
81
82 #ifdef WIN32
83 #else
84 #include <sys/time.h>
85 #endif
86
87 void free_avicodecdata(AviCodecData *acd)
88 {
89         if (acd) {
90                 if (acd->lpFormat){
91                         MEM_freeN(acd->lpFormat);
92                         acd->lpFormat = NULL;
93                         acd->cbFormat = 0;
94                 }
95                 if (acd->lpParms){
96                         MEM_freeN(acd->lpParms);
97                         acd->lpParms = NULL;
98                         acd->cbParms = 0;
99                 }
100         }
101 }
102
103 void free_qtcodecdata(QuicktimeCodecData *qcd)
104 {
105         if (qcd) {
106                 if (qcd->cdParms){
107                         MEM_freeN(qcd->cdParms);
108                         qcd->cdParms = NULL;
109                         qcd->cdSize = 0;
110                 }
111         }
112 }
113
114 Scene *copy_scene(Scene *sce, int type)
115 {
116         Scene *scen;
117         ToolSettings *ts;
118         Base *base, *obase;
119         
120         if(type == SCE_COPY_EMPTY) {
121                 ListBase lb;
122                 scen= add_scene(sce->id.name+2);
123                 
124                 lb= scen->r.layers;
125                 scen->r= sce->r;
126                 scen->r.layers= lb;
127         }
128         else {
129                 scen= copy_libblock(sce);
130                 BLI_duplicatelist(&(scen->base), &(sce->base));
131                 
132                 clear_id_newpoins();
133                 
134                 id_us_plus((ID *)scen->world);
135                 id_us_plus((ID *)scen->set);
136                 id_us_plus((ID *)scen->gm.dome.warptext);
137
138                 scen->ed= NULL;
139                 scen->theDag= NULL;
140                 scen->obedit= NULL;
141                 scen->toolsettings= MEM_dupallocN(sce->toolsettings);
142                 scen->stats= NULL;
143                 scen->fps_info= NULL;
144
145                 ts= scen->toolsettings;
146                 if(ts) {
147                         if(ts->vpaint) {
148                                 ts->vpaint= MEM_dupallocN(ts->vpaint);
149                                 ts->vpaint->paintcursor= NULL;
150                                 ts->vpaint->vpaint_prev= NULL;
151                                 ts->vpaint->wpaint_prev= NULL;
152                                 copy_paint(&ts->vpaint->paint, &ts->vpaint->paint);
153                         }
154                         if(ts->wpaint) {
155                                 ts->wpaint= MEM_dupallocN(ts->wpaint);
156                                 ts->wpaint->paintcursor= NULL;
157                                 ts->wpaint->vpaint_prev= NULL;
158                                 ts->wpaint->wpaint_prev= NULL;
159                                 copy_paint(&ts->wpaint->paint, &ts->wpaint->paint);
160                         }
161                         if(ts->sculpt) {
162                                 ts->sculpt= MEM_dupallocN(ts->sculpt);
163                                 copy_paint(&ts->sculpt->paint, &ts->sculpt->paint);
164                         }
165
166                         copy_paint(&ts->imapaint.paint, &ts->imapaint.paint);
167                         ts->imapaint.paintcursor= NULL;
168
169                         ts->particle.paintcursor= NULL;
170                 }
171                 
172                 BLI_duplicatelist(&(scen->markers), &(sce->markers));
173                 BLI_duplicatelist(&(scen->transform_spaces), &(sce->transform_spaces));
174                 BLI_duplicatelist(&(scen->r.layers), &(sce->r.layers));
175                 BKE_keyingsets_copy(&(scen->keyingsets), &(sce->keyingsets));
176
177                 if(sce->nodetree) {
178                         scen->nodetree= ntreeCopyTree(sce->nodetree); /* copies actions */
179                         ntreeSwitchID(scen->nodetree, &sce->id, &scen->id);
180                 }
181
182                 obase= sce->base.first;
183                 base= scen->base.first;
184                 while(base) {
185                         id_us_plus(&base->object->id);
186                         if(obase==sce->basact) scen->basact= base;
187         
188                         obase= obase->next;
189                         base= base->next;
190                 }
191         }
192         
193         /* make a private copy of the avicodecdata */
194         if(sce->r.avicodecdata) {
195                 scen->r.avicodecdata = MEM_dupallocN(sce->r.avicodecdata);
196                 scen->r.avicodecdata->lpFormat = MEM_dupallocN(scen->r.avicodecdata->lpFormat);
197                 scen->r.avicodecdata->lpParms = MEM_dupallocN(scen->r.avicodecdata->lpParms);
198         }
199         
200         /* make a private copy of the qtcodecdata */
201         if(sce->r.qtcodecdata) {
202                 scen->r.qtcodecdata = MEM_dupallocN(sce->r.qtcodecdata);
203                 scen->r.qtcodecdata->cdParms = MEM_dupallocN(scen->r.qtcodecdata->cdParms);
204         }
205         
206         if(sce->r.ffcodecdata.properties) { /* intentionally check scen not sce. */
207                 scen->r.ffcodecdata.properties= IDP_CopyProperty(sce->r.ffcodecdata.properties);
208         }
209
210         /* NOTE: part of SCE_COPY_LINK_DATA and SCE_COPY_FULL operations
211          * are done outside of blenkernel with ED_objects_single_users! */
212
213         /*  camera */
214         if(type == SCE_COPY_LINK_DATA || type == SCE_COPY_FULL) {
215                 ID_NEW(scen->camera);
216         }
217         
218         /* before scene copy */
219         sound_create_scene(scen);
220
221         /* world */
222         if(type == SCE_COPY_FULL) {
223                 BKE_copy_animdata_id_action((ID *)scen);
224                 if(scen->world) {
225                         id_us_plus((ID *)scen->world);
226                         scen->world= copy_world(scen->world);
227                         BKE_copy_animdata_id_action((ID *)scen->world);
228                 }
229
230                 if(sce->ed) {
231                         scen->ed= MEM_callocN( sizeof(Editing), "addseq");
232                         scen->ed->seqbasep= &scen->ed->seqbase;
233                         seqbase_dupli_recursive(sce, scen, &scen->ed->seqbase, &sce->ed->seqbase, SEQ_DUPE_ALL);
234                 }
235         }
236
237         return scen;
238 }
239
240 /* do not free scene itself */
241 void free_scene(Scene *sce)
242 {
243         Base *base;
244         SceneRenderLayer *srl;
245         
246         base= sce->base.first;
247         while(base) {
248                 base->object->id.us--;
249                 base= base->next;
250         }
251         /* do not free objects! */
252         
253         if(sce->gpd) {
254 #if 0   // removed since this can be invalid memory when freeing everything
255                 // since the grease pencil data is free'd before the scene.
256                 // since grease pencil data is not (yet?), shared between objects
257                 // its probably safe not to do this, some save and reload will free this.
258                 sce->gpd->id.us--;
259 #endif
260                 sce->gpd= NULL;
261         }
262
263         BLI_freelistN(&sce->base);
264         seq_free_editing(sce);
265
266         BKE_free_animdata((ID *)sce);
267         BKE_keyingsets_free(&sce->keyingsets);
268         
269         if (sce->r.avicodecdata) {
270                 free_avicodecdata(sce->r.avicodecdata);
271                 MEM_freeN(sce->r.avicodecdata);
272                 sce->r.avicodecdata = NULL;
273         }
274         if (sce->r.qtcodecdata) {
275                 free_qtcodecdata(sce->r.qtcodecdata);
276                 MEM_freeN(sce->r.qtcodecdata);
277                 sce->r.qtcodecdata = NULL;
278         }
279         if (sce->r.ffcodecdata.properties) {
280                 IDP_FreeProperty(sce->r.ffcodecdata.properties);
281                 MEM_freeN(sce->r.ffcodecdata.properties);
282                 sce->r.ffcodecdata.properties = NULL;
283         }
284         
285         for(srl= sce->r.layers.first; srl; srl= srl->next) {
286                 FRS_free_freestyle_config(srl);
287         }
288         
289         BLI_freelistN(&sce->markers);
290         BLI_freelistN(&sce->transform_spaces);
291         BLI_freelistN(&sce->r.layers);
292         
293         if(sce->toolsettings) {
294                 if(sce->toolsettings->vpaint) {
295                         free_paint(&sce->toolsettings->vpaint->paint);
296                         MEM_freeN(sce->toolsettings->vpaint);
297                 }
298                 if(sce->toolsettings->wpaint) {
299                         free_paint(&sce->toolsettings->wpaint->paint);
300                         MEM_freeN(sce->toolsettings->wpaint);
301                 }
302                 if(sce->toolsettings->sculpt) {
303                         free_paint(&sce->toolsettings->sculpt->paint);
304                         MEM_freeN(sce->toolsettings->sculpt);
305                 }
306                 free_paint(&sce->toolsettings->imapaint.paint);
307
308                 MEM_freeN(sce->toolsettings);
309                 sce->toolsettings = NULL;       
310         }
311         
312         if (sce->theDag) {
313                 free_forest(sce->theDag);
314                 MEM_freeN(sce->theDag);
315         }
316         
317         if(sce->nodetree) {
318                 ntreeFreeTree(sce->nodetree);
319                 MEM_freeN(sce->nodetree);
320         }
321
322         if(sce->stats)
323                 MEM_freeN(sce->stats);
324         if(sce->fps_info)
325                 MEM_freeN(sce->fps_info);
326
327         sound_destroy_scene(sce);
328 }
329
330 Scene *add_scene(const char *name)
331 {
332         Main *bmain= G.main;
333         Scene *sce;
334         ParticleEditSettings *pset;
335         int a;
336
337         sce= alloc_libblock(&bmain->scene, ID_SCE, name);
338         sce->lay= sce->layact= 1;
339         
340         sce->r.mode= R_GAMMA|R_OSA|R_SHADOW|R_SSS|R_ENVMAP|R_RAYTRACE;
341         sce->r.cfra= 1;
342         sce->r.sfra= 1;
343         sce->r.efra= 250;
344         sce->r.frame_step= 1;
345         sce->r.xsch= 1920;
346         sce->r.ysch= 1080;
347         sce->r.xasp= 1;
348         sce->r.yasp= 1;
349         sce->r.xparts= 8;
350         sce->r.yparts= 8;
351         sce->r.mblur_samples= 1;
352         sce->r.filtertype= R_FILTER_MITCH;
353         sce->r.size= 50;
354         sce->r.planes= 24;
355         sce->r.imtype= R_PNG;
356         sce->r.quality= 90;
357         sce->r.displaymode= R_OUTPUT_AREA;
358         sce->r.framapto= 100;
359         sce->r.images= 100;
360         sce->r.framelen= 1.0;
361         sce->r.blurfac= 0.5;
362         sce->r.frs_sec= 24;
363         sce->r.frs_sec_base= 1;
364         sce->r.edgeint= 10;
365         sce->r.ocres = 128;
366         sce->r.color_mgt_flag |= R_COLOR_MANAGEMENT;
367         sce->r.gauss= 1.0;
368         
369         /* deprecated but keep for upwards compat */
370         sce->r.postgamma= 1.0;
371         sce->r.posthue= 0.0;
372         sce->r.postsat= 1.0;
373         
374         sce->r.bake_mode= 1;    /* prevent to include render stuff here */
375         sce->r.bake_filter= 2;
376         sce->r.bake_osa= 5;
377         sce->r.bake_flag= R_BAKE_CLEAR;
378         sce->r.bake_normal_space= R_BAKE_SPACE_TANGENT;
379         sce->r.scemode= R_DOCOMP|R_DOSEQ|R_EXTENSION;
380         sce->r.stamp= R_STAMP_TIME|R_STAMP_FRAME|R_STAMP_DATE|R_STAMP_CAMERA|R_STAMP_SCENE|R_STAMP_FILENAME|R_STAMP_RENDERTIME;
381         sce->r.stamp_font_id= 12;
382         sce->r.fg_stamp[0]= sce->r.fg_stamp[1]= sce->r.fg_stamp[2]= 0.8f;
383         sce->r.fg_stamp[3]= 1.0f;
384         sce->r.bg_stamp[0]= sce->r.bg_stamp[1]= sce->r.bg_stamp[2]= 0.0f;
385         sce->r.bg_stamp[3]= 0.25f;
386         sce->r.raytrace_options = R_RAYTRACE_USE_INSTANCES;
387
388         sce->r.seq_prev_type= OB_SOLID;
389         sce->r.seq_rend_type= OB_SOLID;
390         sce->r.seq_flag= R_SEQ_GL_PREV;
391
392         sce->r.threads= 1;
393
394         sce->r.simplify_subsurf= 6;
395         sce->r.simplify_particles= 1.0f;
396         sce->r.simplify_shadowsamples= 16;
397         sce->r.simplify_aosss= 1.0f;
398
399         sce->r.cineonblack= 95;
400         sce->r.cineonwhite= 685;
401         sce->r.cineongamma= 1.7f;
402
403         sce->r.border.xmin= 0.0f;
404         sce->r.border.ymin= 0.0f;
405         sce->r.border.xmax= 1.0f;
406         sce->r.border.ymax= 1.0f;
407         
408         sce->toolsettings = MEM_callocN(sizeof(struct ToolSettings),"Tool Settings Struct");
409         sce->toolsettings->cornertype=1;
410         sce->toolsettings->degr = 90; 
411         sce->toolsettings->step = 9;
412         sce->toolsettings->turn = 1;                            
413         sce->toolsettings->extr_offs = 1; 
414         sce->toolsettings->doublimit = 0.001;
415         sce->toolsettings->segments = 32;
416         sce->toolsettings->rings = 32;
417         sce->toolsettings->vertices = 32;
418         sce->toolsettings->editbutflag = 1;
419         sce->toolsettings->uvcalc_radius = 1.0f;
420         sce->toolsettings->uvcalc_cubesize = 1.0f;
421         sce->toolsettings->uvcalc_mapdir = 1;
422         sce->toolsettings->uvcalc_mapalign = 1;
423         sce->toolsettings->unwrapper = 1;
424         sce->toolsettings->select_thresh= 0.01f;
425         sce->toolsettings->jointrilimit = 0.8f;
426
427         sce->toolsettings->selectmode= SCE_SELECT_VERTEX;
428         sce->toolsettings->uv_selectmode= UV_SELECT_VERTEX;
429         sce->toolsettings->normalsize= 0.1;
430         sce->toolsettings->autokey_mode= U.autokey_mode;
431
432         sce->toolsettings->skgen_resolution = 100;
433         sce->toolsettings->skgen_threshold_internal     = 0.01f;
434         sce->toolsettings->skgen_threshold_external     = 0.01f;
435         sce->toolsettings->skgen_angle_limit                    = 45.0f;
436         sce->toolsettings->skgen_length_ratio                   = 1.3f;
437         sce->toolsettings->skgen_length_limit                   = 1.5f;
438         sce->toolsettings->skgen_correlation_limit              = 0.98f;
439         sce->toolsettings->skgen_symmetry_limit                 = 0.1f;
440         sce->toolsettings->skgen_postpro = SKGEN_SMOOTH;
441         sce->toolsettings->skgen_postpro_passes = 1;
442         sce->toolsettings->skgen_options = SKGEN_FILTER_INTERNAL|SKGEN_FILTER_EXTERNAL|SKGEN_FILTER_SMART|SKGEN_HARMONIC|SKGEN_SUB_CORRELATION|SKGEN_STICK_TO_EMBEDDING;
443         sce->toolsettings->skgen_subdivisions[0] = SKGEN_SUB_CORRELATION;
444         sce->toolsettings->skgen_subdivisions[1] = SKGEN_SUB_LENGTH;
445         sce->toolsettings->skgen_subdivisions[2] = SKGEN_SUB_ANGLE;
446
447         sce->toolsettings->proportional_size = 1.0f;
448
449         sce->physics_settings.gravity[0] = 0.0f;
450         sce->physics_settings.gravity[1] = 0.0f;
451         sce->physics_settings.gravity[2] = -9.81f;
452         sce->physics_settings.flag = PHYS_GLOBAL_GRAVITY;
453
454         sce->unit.scale_length = 1.0f;
455
456         pset= &sce->toolsettings->particle;
457         pset->flag= PE_KEEP_LENGTHS|PE_LOCK_FIRST|PE_DEFLECT_EMITTER|PE_AUTO_VELOCITY;
458         pset->emitterdist= 0.25f;
459         pset->totrekey= 5;
460         pset->totaddkey= 5;
461         pset->brushtype= PE_BRUSH_NONE;
462         pset->draw_step= 2;
463         pset->fade_frames= 2;
464         pset->selectmode= SCE_SELECT_PATH;
465         for(a=0; a<PE_TOT_BRUSH; a++) {
466                 pset->brush[a].strength= 0.5;
467                 pset->brush[a].size= 50;
468                 pset->brush[a].step= 10;
469                 pset->brush[a].count= 10;
470         }
471         pset->brush[PE_BRUSH_CUT].strength= 100;
472
473         sce->r.ffcodecdata.audio_mixrate = 44100;
474         sce->r.ffcodecdata.audio_volume = 1.0f;
475         sce->r.ffcodecdata.audio_bitrate = 192;
476         sce->r.ffcodecdata.audio_channels = 2;
477
478         BLI_strncpy(sce->r.engine, "BLENDER_RENDER", sizeof(sce->r.engine));
479
480         sce->audio.distance_model = 2.0f;
481         sce->audio.doppler_factor = 1.0f;
482         sce->audio.speed_of_sound = 343.3f;
483         sce->audio.volume = 1.0f;
484
485         BLI_strncpy(sce->r.pic, U.renderdir, sizeof(sce->r.pic));
486
487         BLI_init_rctf(&sce->r.safety, 0.1f, 0.9f, 0.1f, 0.9f);
488         sce->r.osa= 8;
489
490         /* note; in header_info.c the scene copy happens..., if you add more to renderdata it has to be checked there */
491         scene_add_render_layer(sce);
492         
493         /* game data */
494         sce->gm.stereoflag = STEREO_NOSTEREO;
495         sce->gm.stereomode = STEREO_ANAGLYPH;
496         sce->gm.eyeseparation = 0.10;
497
498         sce->gm.dome.angle = 180;
499         sce->gm.dome.mode = DOME_FISHEYE;
500         sce->gm.dome.res = 4;
501         sce->gm.dome.resbuf = 1.0f;
502         sce->gm.dome.tilt = 0;
503
504         sce->gm.xplay= 640;
505         sce->gm.yplay= 480;
506         sce->gm.freqplay= 60;
507         sce->gm.depth= 32;
508
509         sce->gm.gravity= 9.8f;
510         sce->gm.physicsEngine= WOPHY_BULLET;
511         sce->gm.mode = 32; //XXX ugly harcoding, still not sure we should drop mode. 32 == 1 << 5 == use_occlusion_culling 
512         sce->gm.occlusionRes = 128;
513         sce->gm.ticrate = 60;
514         sce->gm.maxlogicstep = 5;
515         sce->gm.physubstep = 1;
516         sce->gm.maxphystep = 5;
517
518         sce->gm.flag = GAME_DISPLAY_LISTS;
519         sce->gm.matmode = GAME_MAT_MULTITEX;
520
521         sce->gm.obstacleSimulation= OBSTSIMULATION_NONE;
522         sce->gm.levelHeight = 2.f;
523
524         sce->gm.recastData.cellsize = 0.3f;
525         sce->gm.recastData.cellheight = 0.2f;
526         sce->gm.recastData.agentmaxslope = M_PI/2;
527         sce->gm.recastData.agentmaxclimb = 0.9f;
528         sce->gm.recastData.agentheight = 2.0f;
529         sce->gm.recastData.agentradius = 0.6f;
530         sce->gm.recastData.edgemaxlen = 12.0f;
531         sce->gm.recastData.edgemaxerror = 1.3f;
532         sce->gm.recastData.regionminsize = 8.f;
533         sce->gm.recastData.regionmergesize = 20.f;
534         sce->gm.recastData.vertsperpoly = 6;
535         sce->gm.recastData.detailsampledist = 6.0f;
536         sce->gm.recastData.detailsamplemaxerror = 1.0f;
537
538         sound_create_scene(sce);
539
540         return sce;
541 }
542
543 Base *object_in_scene(Object *ob, Scene *sce)
544 {
545         Base *base;
546         
547         base= sce->base.first;
548         while(base) {
549                 if(base->object == ob) return base;
550                 base= base->next;
551         }
552         return NULL;
553 }
554
555 void set_scene_bg(Main *bmain, Scene *scene)
556 {
557         Scene *sce;
558         Base *base;
559         Object *ob;
560         Group *group;
561         GroupObject *go;
562         int flag;
563         
564         /* check for cyclic sets, for reading old files but also for definite security (py?) */
565         scene_check_setscene(bmain, scene);
566         
567         /* can happen when switching modes in other scenes */
568         if(scene->obedit && !(scene->obedit->mode & OB_MODE_EDIT))
569                 scene->obedit= NULL;
570
571         /* deselect objects (for dataselect) */
572         for(ob= bmain->object.first; ob; ob= ob->id.next)
573                 ob->flag &= ~(SELECT|OB_FROMGROUP);
574
575         /* group flags again */
576         for(group= bmain->group.first; group; group= group->id.next) {
577                 go= group->gobject.first;
578                 while(go) {
579                         if(go->ob) go->ob->flag |= OB_FROMGROUP;
580                         go= go->next;
581                 }
582         }
583
584         /* sort baselist */
585         DAG_scene_sort(bmain, scene);
586         
587         /* ensure dags are built for sets */
588         for(sce= scene->set; sce; sce= sce->set)
589                 if(sce->theDag==NULL)
590                         DAG_scene_sort(bmain, sce);
591
592         /* copy layers and flags from bases to objects */
593         for(base= scene->base.first; base; base= base->next) {
594                 ob= base->object;
595                 ob->lay= base->lay;
596                 
597                 /* group patch... */
598                 base->flag &= ~(OB_FROMGROUP);
599                 flag= ob->flag & (OB_FROMGROUP);
600                 base->flag |= flag;
601                 
602                 /* not too nice... for recovering objects with lost data */
603                 //if(ob->pose==NULL) base->flag &= ~OB_POSEMODE;
604                 ob->flag= base->flag;
605                 
606                 ob->ctime= -1234567.0;  /* force ipo to be calculated later */
607         }
608         /* no full animation update, this to enable render code to work (render code calls own animation updates) */
609 }
610
611 /* called from creator.c */
612 Scene *set_scene_name(Main *bmain, const char *name)
613 {
614         Scene *sce= (Scene *)find_id("SC", name);
615         if(sce) {
616                 set_scene_bg(bmain, sce);
617                 printf("Scene switch: '%s' in file: '%s'\n", name, G.main->name);
618                 return sce;
619         }
620
621         printf("Can't find scene: '%s' in file: '%s'\n", name, G.main->name);
622         return NULL;
623 }
624
625 void unlink_scene(Main *bmain, Scene *sce, Scene *newsce)
626 {
627         Scene *sce1;
628         bScreen *sc;
629
630         /* check all sets */
631         for(sce1= bmain->scene.first; sce1; sce1= sce1->id.next)
632                 if(sce1->set == sce)
633                         sce1->set= NULL;
634         
635         /* check all sequences */
636         clear_scene_in_allseqs(bmain, sce);
637
638         /* check render layer nodes in other scenes */
639         clear_scene_in_nodes(bmain, sce);
640         
641         /* al screens */
642         for(sc= bmain->screen.first; sc; sc= sc->id.next)
643                 if(sc->scene == sce)
644                         sc->scene= newsce;
645
646         free_libblock(&bmain->scene, sce);
647 }
648
649 /* used by metaballs
650  * doesnt return the original duplicated object, only dupli's
651  */
652 int next_object(Scene **scene, int val, Base **base, Object **ob)
653 {
654         static ListBase *duplilist= NULL;
655         static DupliObject *dupob;
656         static int fase= F_START, in_next_object= 0;
657         int run_again=1;
658         
659         /* init */
660         if(val==0) {
661                 fase= F_START;
662                 dupob= NULL;
663                 
664                 /* XXX particle systems with metas+dupligroups call this recursively */
665                 /* see bug #18725 */
666                 if(in_next_object) {
667                         printf("ERROR: MetaBall generation called recursively, not supported\n");
668                         
669                         return F_ERROR;
670                 }
671         }
672         else {
673                 in_next_object= 1;
674                 
675                 /* run_again is set when a duplilist has been ended */
676                 while(run_again) {
677                         run_again= 0;
678
679                         /* the first base */
680                         if(fase==F_START) {
681                                 *base= (*scene)->base.first;
682                                 if(*base) {
683                                         *ob= (*base)->object;
684                                         fase= F_SCENE;
685                                 }
686                                 else {
687                                         /* exception: empty scene */
688                                         while((*scene)->set) {
689                                                 (*scene)= (*scene)->set;
690                                                 if((*scene)->base.first) {
691                                                         *base= (*scene)->base.first;
692                                                         *ob= (*base)->object;
693                                                         fase= F_SCENE;
694                                                         break;
695                                                 }
696                                         }
697                                 }
698                         }
699                         else {
700                                 if(*base && fase!=F_DUPLI) {
701                                         *base= (*base)->next;
702                                         if(*base) *ob= (*base)->object;
703                                         else {
704                                                 if(fase==F_SCENE) {
705                                                         /* (*scene) is finished, now do the set */
706                                                         while((*scene)->set) {
707                                                                 (*scene)= (*scene)->set;
708                                                                 if((*scene)->base.first) {
709                                                                         *base= (*scene)->base.first;
710                                                                         *ob= (*base)->object;
711                                                                         break;
712                                                                 }
713                                                         }
714                                                 }
715                                         }
716                                 }
717                         }
718                         
719                         if(*base == NULL) fase= F_START;
720                         else {
721                                 if(fase!=F_DUPLI) {
722                                         if( (*base)->object->transflag & OB_DUPLI) {
723                                                 /* groups cannot be duplicated for mballs yet, 
724                                                 this enters eternal loop because of 
725                                                 makeDispListMBall getting called inside of group_duplilist */
726                                                 if((*base)->object->dup_group == NULL) {
727                                                         duplilist= object_duplilist((*scene), (*base)->object);
728                                                         
729                                                         dupob= duplilist->first;
730
731                                                         if(!dupob)
732                                                                 free_object_duplilist(duplilist);
733                                                 }
734                                         }
735                                 }
736                                 /* handle dupli's */
737                                 if(dupob) {
738                                         
739                                         copy_m4_m4(dupob->ob->obmat, dupob->mat);
740                                         
741                                         (*base)->flag |= OB_FROMDUPLI;
742                                         *ob= dupob->ob;
743                                         fase= F_DUPLI;
744                                         
745                                         dupob= dupob->next;
746                                 }
747                                 else if(fase==F_DUPLI) {
748                                         fase= F_SCENE;
749                                         (*base)->flag &= ~OB_FROMDUPLI;
750                                         
751                                         for(dupob= duplilist->first; dupob; dupob= dupob->next) {
752                                                 copy_m4_m4(dupob->ob->obmat, dupob->omat);
753                                         }
754                                         
755                                         free_object_duplilist(duplilist);
756                                         duplilist= NULL;
757                                         run_again= 1;
758                                 }
759                         }
760                 }
761         }
762         
763         /* if(ob && *ob) {
764                 printf("Scene: '%s', '%s'\n", (*scene)->id.name+2, (*ob)->id.name+2);
765         } */
766
767         /* reset recursion test */
768         in_next_object= 0;
769         
770         return fase;
771 }
772
773 Object *scene_find_camera(Scene *sc)
774 {
775         Base *base;
776         
777         for (base= sc->base.first; base; base= base->next)
778                 if (base->object->type==OB_CAMERA)
779                         return base->object;
780
781         return NULL;
782 }
783
784 #ifdef DURIAN_CAMERA_SWITCH
785 Object *scene_camera_switch_find(Scene *scene)
786 {
787         TimeMarker *m;
788         int cfra = scene->r.cfra;
789         int frame = -(MAXFRAME + 1);
790         Object *camera= NULL;
791
792         for (m= scene->markers.first; m; m= m->next) {
793                 if(m->camera && (m->camera->restrictflag & OB_RESTRICT_RENDER)==0 && (m->frame <= cfra) && (m->frame > frame)) {
794                         camera= m->camera;
795                         frame= m->frame;
796
797                         if(frame == cfra)
798                                 break;
799
800                 }
801         }
802         return camera;
803 }
804 #endif
805
806 int scene_camera_switch_update(Scene *scene)
807 {
808 #ifdef DURIAN_CAMERA_SWITCH
809         Object *camera= scene_camera_switch_find(scene);
810         if(camera) {
811                 scene->camera= camera;
812                 return 1;
813         }
814 #endif
815         return 0;
816 }
817
818 char *scene_find_marker_name(Scene *scene, int frame)
819 {
820         ListBase *markers= &scene->markers;
821         TimeMarker *m1, *m2;
822
823         /* search through markers for match */
824         for (m1=markers->first, m2=markers->last; m1 && m2; m1=m1->next, m2=m2->prev) {
825                 if (m1->frame==frame)
826                         return m1->name;
827
828                 if (m1 == m2)
829                         break;
830
831                 if (m2->frame==frame)
832                         return m2->name;
833         }
834
835         return NULL;
836 }
837
838 /* return the current marker for this frame,
839 we can have more then 1 marker per frame, this just returns the first :/ */
840 char *scene_find_last_marker_name(Scene *scene, int frame)
841 {
842         TimeMarker *marker, *best_marker = NULL;
843         int best_frame = -MAXFRAME*2;
844         for (marker= scene->markers.first; marker; marker= marker->next) {
845                 if (marker->frame==frame) {
846                         return marker->name;
847                 }
848
849                 if ( marker->frame > best_frame && marker->frame < frame) {
850                         best_marker = marker;
851                         best_frame = marker->frame;
852                 }
853         }
854
855         return best_marker ? best_marker->name : NULL;
856 }
857
858
859 Base *scene_add_base(Scene *sce, Object *ob)
860 {
861         Base *b= MEM_callocN(sizeof(*b), "scene_add_base");
862         BLI_addhead(&sce->base, b);
863
864         b->object= ob;
865         b->flag= ob->flag;
866         b->lay= ob->lay;
867
868         return b;
869 }
870
871 void scene_deselect_all(Scene *sce)
872 {
873         Base *b;
874
875         for (b= sce->base.first; b; b= b->next) {
876                 b->flag&= ~SELECT;
877                 b->object->flag= b->flag;
878         }
879 }
880
881 void scene_select_base(Scene *sce, Base *selbase)
882 {
883         scene_deselect_all(sce);
884
885         selbase->flag |= SELECT;
886         selbase->object->flag= selbase->flag;
887
888         sce->basact= selbase;
889 }
890
891 /* checks for cycle, returns 1 if it's all OK */
892 int scene_check_setscene(Main *bmain, Scene *sce)
893 {
894         Scene *scene;
895         int a, totscene;
896         
897         if(sce->set==NULL) return 1;
898         
899         totscene= 0;
900         for(scene= bmain->scene.first; scene; scene= scene->id.next)
901                 totscene++;
902         
903         for(a=0, scene=sce; scene->set; scene=scene->set, a++) {
904                 /* more iterations than scenes means we have a cycle */
905                 if(a > totscene) {
906                         /* the tested scene gets zero'ed, that's typically current scene */
907                         sce->set= NULL;
908                         return 0;
909                 }
910         }
911
912         return 1;
913 }
914
915 /* This function is needed to cope with fractional frames - including two Blender rendering features
916 * mblur (motion blur that renders 'subframes' and blurs them together), and fields rendering. */
917
918 /* see also bsystem_time in object.c */
919 float BKE_curframe(Scene *scene)
920 {
921         float ctime = scene->r.cfra;
922         ctime+= scene->r.subframe;
923         ctime*= scene->r.framelen;      
924
925         return ctime;
926 }
927
928 /* drivers support/hacks 
929  *      - this method is called from scene_update_tagged_recursive(), so gets included in viewport + render
930  *      - these are always run since the depsgraph can't handle non-object data
931  *      - these happen after objects are all done so that we can read in their final transform values,
932  *        though this means that objects can't refer to scene info for guidance...
933  */
934 static void scene_update_drivers(Main *UNUSED(bmain), Scene *scene)
935 {
936         float ctime = BKE_curframe(scene);
937         
938         /* scene itself */
939         if (scene->adt && scene->adt->drivers.first) {
940                 BKE_animsys_evaluate_animdata(scene, &scene->id, scene->adt, ctime, ADT_RECALC_DRIVERS);
941         }
942         
943         /* world */
944         // TODO: what about world textures? but then those have nodes too...
945         if (scene->world) {
946                 ID *wid = (ID *)scene->world;
947                 AnimData *adt= BKE_animdata_from_id(wid);
948                 
949                 if (adt && adt->drivers.first)
950                         BKE_animsys_evaluate_animdata(scene, wid, adt, ctime, ADT_RECALC_DRIVERS);
951         }
952         
953         /* nodes */
954         if (scene->nodetree) {
955                 ID *nid = (ID *)scene->nodetree;
956                 AnimData *adt= BKE_animdata_from_id(nid);
957                 
958                 if (adt && adt->drivers.first)
959                         BKE_animsys_evaluate_animdata(scene, nid, adt, ctime, ADT_RECALC_DRIVERS);
960         }
961 }
962
963 static void scene_update_tagged_recursive(Main *bmain, Scene *scene, Scene *scene_parent)
964 {
965         Base *base;
966         
967         
968         scene->customdata_mask= scene_parent->customdata_mask;
969
970         /* sets first, we allow per definition current scene to have
971            dependencies on sets, but not the other way around. */
972         if (scene->set)
973                 scene_update_tagged_recursive(bmain, scene->set, scene_parent);
974         
975         /* scene objects */
976         for (base= scene->base.first; base; base= base->next) {
977                 Object *ob= base->object;
978                 
979                 object_handle_update(scene_parent, ob);
980                 
981                 if(ob->dup_group && (ob->transflag & OB_DUPLIGROUP))
982                         group_handle_recalc_and_update(scene_parent, ob, ob->dup_group);
983                         
984                 /* always update layer, so that animating layers works */
985                 base->lay= ob->lay;
986         }
987         
988         /* scene drivers... */
989         scene_update_drivers(bmain, scene);
990
991         /* update sound system animation */
992         sound_update_scene(scene);
993 }
994
995 /* this is called in main loop, doing tagged updates before redraw */
996 void scene_update_tagged(Main *bmain, Scene *scene)
997 {
998         DAG_ids_flush_tagged(bmain);
999
1000         scene->physics_settings.quick_cache_step= 0;
1001
1002         /* update all objects: drivers, matrices, displists, etc. flags set
1003            by depgraph or manual, no layer check here, gets correct flushed */
1004
1005         scene_update_tagged_recursive(bmain, scene, scene);
1006
1007         /* recalc scene animation data here (for sequencer) */
1008         {
1009                 AnimData *adt= BKE_animdata_from_id(&scene->id);
1010                 float ctime = BKE_curframe(scene);
1011                 
1012                 if (adt && (adt->recalc & ADT_RECALC_ANIM))
1013                         BKE_animsys_evaluate_animdata(scene, &scene->id, adt, ctime, 0);
1014         }
1015         
1016         if (scene->physics_settings.quick_cache_step)
1017                 BKE_ptcache_quick_cache_all(bmain, scene);
1018
1019         /* in the future this should handle updates for all datablocks, not
1020            only objects and scenes. - brecht */
1021 }
1022
1023 /* applies changes right away, does all sets too */
1024 void scene_update_for_newframe(Main *bmain, Scene *sce, unsigned int lay)
1025 {
1026         float ctime = BKE_curframe(sce);
1027         Scene *sce_iter;
1028
1029         /* keep this first */
1030         BLI_exec_cb(bmain, (ID *)sce, BLI_CB_EVT_FRAME_CHANGE_PRE);
1031
1032         sound_set_cfra(sce->r.cfra);
1033         
1034         /* clear animation overrides */
1035         // XXX TODO...
1036
1037         for(sce_iter= sce; sce_iter; sce_iter= sce_iter->set) {
1038                 if(sce_iter->theDag==NULL)
1039                         DAG_scene_sort(bmain, sce_iter);
1040         }
1041
1042
1043         /* Following 2 functions are recursive
1044          * so dont call within 'scene_update_tagged_recursive' */
1045         DAG_scene_update_flags(bmain, sce, lay, TRUE);   // only stuff that moves or needs display still
1046
1047         /* All 'standard' (i.e. without any dependencies) animation is handled here,
1048          * with an 'local' to 'macro' order of evaluation. This should ensure that
1049          * settings stored nestled within a hierarchy (i.e. settings in a Texture block
1050          * can be overridden by settings from Scene, which owns the Texture through a hierarchy
1051          * such as Scene->World->MTex/Texture) can still get correctly overridden.
1052          */
1053         BKE_animsys_evaluate_all_animation(bmain, sce, ctime);
1054         /*...done with recusrive funcs */
1055
1056         /* object_handle_update() on all objects, groups and sets */
1057         scene_update_tagged_recursive(bmain, sce, sce);
1058
1059         /* keep this last */
1060         BLI_exec_cb(bmain, (ID *)sce, BLI_CB_EVT_FRAME_CHANGE_POST);
1061 }
1062
1063 /* return default layer, also used to patch old files */
1064 void scene_add_render_layer(Scene *sce)
1065 {
1066         SceneRenderLayer *srl;
1067 //      int tot= 1 + BLI_countlist(&sce->r.layers);
1068         
1069         srl= MEM_callocN(sizeof(SceneRenderLayer), "new render layer");
1070         strcpy(srl->name, "RenderLayer");
1071         BLI_uniquename(&sce->r.layers, srl, "RenderLayer", '.', offsetof(SceneRenderLayer, name), sizeof(srl->name));
1072         BLI_addtail(&sce->r.layers, srl);
1073
1074         /* note, this is also in render, pipeline.c, to make layer when scenedata doesnt have it */
1075         srl->lay= (1<<20) -1;
1076         srl->layflag= 0x7FFF;   /* solid ztra halo edge strand */
1077         srl->passflag= SCE_PASS_COMBINED|SCE_PASS_Z;
1078         FRS_add_freestyle_config( srl );
1079 }
1080
1081 /* render simplification */
1082
1083 int get_render_subsurf_level(RenderData *r, int lvl)
1084 {
1085         if(r->mode & R_SIMPLIFY)
1086                 return MIN2(r->simplify_subsurf, lvl);
1087         else
1088                 return lvl;
1089 }
1090
1091 int get_render_child_particle_number(RenderData *r, int num)
1092 {
1093         if(r->mode & R_SIMPLIFY)
1094                 return (int)(r->simplify_particles*num);
1095         else
1096                 return num;
1097 }
1098
1099 int get_render_shadow_samples(RenderData *r, int samples)
1100 {
1101         if((r->mode & R_SIMPLIFY) && samples > 0)
1102                 return MIN2(r->simplify_shadowsamples, samples);
1103         else
1104                 return samples;
1105 }
1106
1107 float get_render_aosss_error(RenderData *r, float error)
1108 {
1109         if(r->mode & R_SIMPLIFY)
1110                 return ((1.0f-r->simplify_aosss)*10.0f + 1.0f)*error;
1111         else
1112                 return error;
1113 }
1114
1115 /* helper function for the SETLOOPER macro */
1116 Base *_setlooper_base_step(Scene **sce_iter, Base *base)
1117 {
1118         if(base && base->next) {
1119                 /* common case, step to the next */
1120                 return base->next;
1121         }
1122         else if(base==NULL && (*sce_iter)->base.first) {
1123                 /* first time looping, return the scenes first base */
1124                 return (Base *)(*sce_iter)->base.first;
1125         }
1126         else {
1127                 /* reached the end, get the next base in the set */
1128                 while((*sce_iter= (*sce_iter)->set)) {
1129                         base= (Base *)(*sce_iter)->base.first;
1130                         if(base) {
1131                                 return base;
1132                         }
1133                 }
1134         }
1135
1136         return NULL;
1137 }