Patch #34204: [Render Animation] Fails with "Error: Specified sample_fmt is not suppo...
[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_node_types.h"
48 #include "DNA_object_types.h"
49 #include "DNA_rigidbody_types.h"
50 #include "DNA_scene_types.h"
51 #include "DNA_screen_types.h"
52 #include "DNA_sequence_types.h"
53
54 #include "BLI_math.h"
55 #include "BLI_blenlib.h"
56 #include "BLI_utildefines.h"
57 #include "BLI_callbacks.h"
58 #include "BLI_string.h"
59
60 #include "BKE_anim.h"
61 #include "BKE_animsys.h"
62 #include "BKE_action.h"
63 #include "BKE_colortools.h"
64 #include "BKE_depsgraph.h"
65 #include "BKE_fcurve.h"
66 #include "BKE_global.h"
67 #include "BKE_group.h"
68 #include "BKE_idprop.h"
69 #include "BKE_library.h"
70 #include "BKE_main.h"
71 #include "BKE_mask.h"
72 #include "BKE_node.h"
73 #include "BKE_object.h"
74 #include "BKE_paint.h"
75 #include "BKE_pointcache.h"
76 #include "BKE_rigidbody.h"
77 #include "BKE_scene.h"
78 #include "BKE_sequencer.h"
79 #include "BKE_world.h"
80
81 #include "BKE_sound.h"
82
83 #include "RE_engine.h"
84
85 #include "IMB_colormanagement.h"
86
87 //XXX #include "BIF_previewrender.h"
88 //XXX #include "BIF_editseq.h"
89
90 #ifdef WIN32
91 #else
92 #include <sys/time.h>
93 #endif
94
95 void free_avicodecdata(AviCodecData *acd)
96 {
97         if (acd) {
98                 if (acd->lpFormat) {
99                         MEM_freeN(acd->lpFormat);
100                         acd->lpFormat = NULL;
101                         acd->cbFormat = 0;
102                 }
103                 if (acd->lpParms) {
104                         MEM_freeN(acd->lpParms);
105                         acd->lpParms = NULL;
106                         acd->cbParms = 0;
107                 }
108         }
109 }
110
111 void free_qtcodecdata(QuicktimeCodecData *qcd)
112 {
113         if (qcd) {
114                 if (qcd->cdParms) {
115                         MEM_freeN(qcd->cdParms);
116                         qcd->cdParms = NULL;
117                         qcd->cdSize = 0;
118                 }
119         }
120 }
121
122 static void remove_sequencer_fcurves(Scene *sce)
123 {
124         AnimData *adt = BKE_animdata_from_id(&sce->id);
125
126         if (adt && adt->action) {
127                 FCurve *fcu, *nextfcu;
128                 
129                 for (fcu = adt->action->curves.first; fcu; fcu = nextfcu) {
130                         nextfcu = fcu->next;
131                         
132                         if ((fcu->rna_path) && strstr(fcu->rna_path, "sequences_all")) {
133                                 action_groups_remove_channel(adt->action, fcu);
134                                 free_fcurve(fcu);
135                         }
136                 }
137         }
138 }
139
140 Scene *BKE_scene_copy(Scene *sce, int type)
141 {
142         Scene *scen;
143         ToolSettings *ts;
144         Base *base, *obase;
145         
146         if (type == SCE_COPY_EMPTY) {
147                 ListBase lb;
148                 /* XXX. main should become an arg */
149                 scen = BKE_scene_add(G.main, sce->id.name + 2);
150                 
151                 lb = scen->r.layers;
152                 scen->r = sce->r;
153                 scen->r.layers = lb;
154                 scen->unit = sce->unit;
155                 scen->physics_settings = sce->physics_settings;
156                 scen->gm = sce->gm;
157                 scen->audio = sce->audio;
158
159                 MEM_freeN(scen->toolsettings);
160         }
161         else {
162                 scen = BKE_libblock_copy(&sce->id);
163                 BLI_duplicatelist(&(scen->base), &(sce->base));
164                 
165                 clear_id_newpoins();
166                 
167                 id_us_plus((ID *)scen->world);
168                 id_us_plus((ID *)scen->set);
169                 id_us_plus((ID *)scen->gm.dome.warptext);
170
171                 scen->ed = NULL;
172                 scen->theDag = NULL;
173                 scen->obedit = NULL;
174                 scen->stats = NULL;
175                 scen->fps_info = NULL;
176                 scen->rigidbody_world = NULL; /* RB_TODO figure out a way of copying the rigid body world */
177
178                 BLI_duplicatelist(&(scen->markers), &(sce->markers));
179                 BLI_duplicatelist(&(scen->transform_spaces), &(sce->transform_spaces));
180                 BLI_duplicatelist(&(scen->r.layers), &(sce->r.layers));
181                 BKE_keyingsets_copy(&(scen->keyingsets), &(sce->keyingsets));
182
183                 if (sce->nodetree) {
184                         /* ID's are managed on both copy and switch */
185                         scen->nodetree = ntreeCopyTree(sce->nodetree);
186                         ntreeSwitchID(scen->nodetree, &sce->id, &scen->id);
187                 }
188
189                 obase = sce->base.first;
190                 base = scen->base.first;
191                 while (base) {
192                         id_us_plus(&base->object->id);
193                         if (obase == sce->basact) scen->basact = base;
194         
195                         obase = obase->next;
196                         base = base->next;
197                 }
198
199                 /* copy color management settings */
200                 BKE_color_managed_display_settings_copy(&scen->display_settings, &sce->display_settings);
201                 BKE_color_managed_view_settings_copy(&scen->view_settings, &sce->view_settings);
202                 BKE_color_managed_view_settings_copy(&scen->r.im_format.view_settings, &sce->r.im_format.view_settings);
203
204                 BLI_strncpy(scen->sequencer_colorspace_settings.name, sce->sequencer_colorspace_settings.name,
205                             sizeof(scen->sequencer_colorspace_settings.name));
206
207                 /* remove animation used by sequencer */
208                 if (type != SCE_COPY_FULL)
209                         remove_sequencer_fcurves(scen);
210         }
211
212         /* tool settings */
213         scen->toolsettings = MEM_dupallocN(sce->toolsettings);
214
215         ts = scen->toolsettings;
216         if (ts) {
217                 if (ts->vpaint) {
218                         ts->vpaint = MEM_dupallocN(ts->vpaint);
219                         ts->vpaint->paintcursor = NULL;
220                         ts->vpaint->vpaint_prev = NULL;
221                         ts->vpaint->wpaint_prev = NULL;
222                         BKE_paint_copy(&ts->vpaint->paint, &ts->vpaint->paint);
223                 }
224                 if (ts->wpaint) {
225                         ts->wpaint = MEM_dupallocN(ts->wpaint);
226                         ts->wpaint->paintcursor = NULL;
227                         ts->wpaint->vpaint_prev = NULL;
228                         ts->wpaint->wpaint_prev = NULL;
229                         BKE_paint_copy(&ts->wpaint->paint, &ts->wpaint->paint);
230                 }
231                 if (ts->sculpt) {
232                         ts->sculpt = MEM_dupallocN(ts->sculpt);
233                         BKE_paint_copy(&ts->sculpt->paint, &ts->sculpt->paint);
234                 }
235
236                 BKE_paint_copy(&ts->imapaint.paint, &ts->imapaint.paint);
237                 ts->imapaint.paintcursor = NULL;
238                 ts->particle.paintcursor = NULL;
239         }
240         
241         /* make a private copy of the avicodecdata */
242         if (sce->r.avicodecdata) {
243                 scen->r.avicodecdata = MEM_dupallocN(sce->r.avicodecdata);
244                 scen->r.avicodecdata->lpFormat = MEM_dupallocN(scen->r.avicodecdata->lpFormat);
245                 scen->r.avicodecdata->lpParms = MEM_dupallocN(scen->r.avicodecdata->lpParms);
246         }
247         
248         /* make a private copy of the qtcodecdata */
249         if (sce->r.qtcodecdata) {
250                 scen->r.qtcodecdata = MEM_dupallocN(sce->r.qtcodecdata);
251                 scen->r.qtcodecdata->cdParms = MEM_dupallocN(scen->r.qtcodecdata->cdParms);
252         }
253         
254         if (sce->r.ffcodecdata.properties) { /* intentionally check scen not sce. */
255                 scen->r.ffcodecdata.properties = IDP_CopyProperty(sce->r.ffcodecdata.properties);
256         }
257
258         /* NOTE: part of SCE_COPY_LINK_DATA and SCE_COPY_FULL operations
259          * are done outside of blenkernel with ED_objects_single_users! */
260
261         /*  camera */
262         if (type == SCE_COPY_LINK_DATA || type == SCE_COPY_FULL) {
263                 ID_NEW(scen->camera);
264         }
265         
266         /* before scene copy */
267         sound_create_scene(scen);
268
269         /* world */
270         if (type == SCE_COPY_FULL) {
271                 BKE_copy_animdata_id_action((ID *)scen);
272                 if (scen->world) {
273                         id_us_plus((ID *)scen->world);
274                         scen->world = BKE_world_copy(scen->world);
275                         BKE_copy_animdata_id_action((ID *)scen->world);
276                 }
277
278                 if (sce->ed) {
279                         scen->ed = MEM_callocN(sizeof(Editing), "addseq");
280                         scen->ed->seqbasep = &scen->ed->seqbase;
281                         BKE_sequence_base_dupli_recursive(sce, scen, &scen->ed->seqbase, &sce->ed->seqbase, SEQ_DUPE_ALL);
282                 }
283         }
284
285         return scen;
286 }
287
288 /* do not free scene itself */
289 void BKE_scene_free(Scene *sce)
290 {
291         Base *base;
292
293         /* check all sequences */
294         BKE_sequencer_clear_scene_in_allseqs(G.main, sce);
295
296         base = sce->base.first;
297         while (base) {
298                 base->object->id.us--;
299                 base = base->next;
300         }
301         /* do not free objects! */
302         
303         if (sce->gpd) {
304 #if 0   /* removed since this can be invalid memory when freeing everything */
305                 /* since the grease pencil data is freed before the scene.
306                  * since grease pencil data is not (yet?), shared between objects
307                  * its probably safe not to do this, some save and reload will free this. */
308                 sce->gpd->id.us--;
309 #endif
310                 sce->gpd = NULL;
311         }
312
313         BLI_freelistN(&sce->base);
314         BKE_sequencer_editing_free(sce);
315
316         BKE_free_animdata((ID *)sce);
317         BKE_keyingsets_free(&sce->keyingsets);
318         
319         if (sce->rigidbody_world)
320                 BKE_rigidbody_free_world(sce->rigidbody_world);
321         
322         if (sce->r.avicodecdata) {
323                 free_avicodecdata(sce->r.avicodecdata);
324                 MEM_freeN(sce->r.avicodecdata);
325                 sce->r.avicodecdata = NULL;
326         }
327         if (sce->r.qtcodecdata) {
328                 free_qtcodecdata(sce->r.qtcodecdata);
329                 MEM_freeN(sce->r.qtcodecdata);
330                 sce->r.qtcodecdata = NULL;
331         }
332         if (sce->r.ffcodecdata.properties) {
333                 IDP_FreeProperty(sce->r.ffcodecdata.properties);
334                 MEM_freeN(sce->r.ffcodecdata.properties);
335                 sce->r.ffcodecdata.properties = NULL;
336         }
337         
338         BLI_freelistN(&sce->markers);
339         BLI_freelistN(&sce->transform_spaces);
340         BLI_freelistN(&sce->r.layers);
341         
342         if (sce->toolsettings) {
343                 if (sce->toolsettings->vpaint) {
344                         BKE_paint_free(&sce->toolsettings->vpaint->paint);
345                         MEM_freeN(sce->toolsettings->vpaint);
346                 }
347                 if (sce->toolsettings->wpaint) {
348                         BKE_paint_free(&sce->toolsettings->wpaint->paint);
349                         MEM_freeN(sce->toolsettings->wpaint);
350                 }
351                 if (sce->toolsettings->sculpt) {
352                         BKE_paint_free(&sce->toolsettings->sculpt->paint);
353                         MEM_freeN(sce->toolsettings->sculpt);
354                 }
355                 if (sce->toolsettings->uvsculpt) {
356                         BKE_paint_free(&sce->toolsettings->uvsculpt->paint);
357                         MEM_freeN(sce->toolsettings->uvsculpt);
358                 }
359                 BKE_paint_free(&sce->toolsettings->imapaint.paint);
360
361                 MEM_freeN(sce->toolsettings);
362                 sce->toolsettings = NULL;
363         }
364         
365         DAG_scene_free(sce);
366         
367         if (sce->nodetree) {
368                 ntreeFreeTree(sce->nodetree);
369                 MEM_freeN(sce->nodetree);
370         }
371
372         if (sce->stats)
373                 MEM_freeN(sce->stats);
374         if (sce->fps_info)
375                 MEM_freeN(sce->fps_info);
376
377         sound_destroy_scene(sce);
378
379         BKE_color_managed_view_settings_free(&sce->view_settings);
380 }
381
382 Scene *BKE_scene_add(Main *bmain, const char *name)
383 {
384         Scene *sce;
385         ParticleEditSettings *pset;
386         int a;
387         const char *colorspace_name;
388
389         sce = BKE_libblock_alloc(&bmain->scene, ID_SCE, name);
390         sce->lay = sce->layact = 1;
391         
392         sce->r.mode = R_GAMMA | R_OSA | R_SHADOW | R_SSS | R_ENVMAP | R_RAYTRACE;
393         sce->r.cfra = 1;
394         sce->r.sfra = 1;
395         sce->r.efra = 250;
396         sce->r.frame_step = 1;
397         sce->r.xsch = 1920;
398         sce->r.ysch = 1080;
399         sce->r.xasp = 1;
400         sce->r.yasp = 1;
401         sce->r.tilex = 256;
402         sce->r.tiley = 256;
403         sce->r.mblur_samples = 1;
404         sce->r.filtertype = R_FILTER_MITCH;
405         sce->r.size = 50;
406
407         sce->r.im_format.planes = R_IMF_PLANES_RGB;
408         sce->r.im_format.imtype = R_IMF_IMTYPE_PNG;
409         sce->r.im_format.depth = R_IMF_CHAN_DEPTH_8;
410         sce->r.im_format.quality = 90;
411         sce->r.im_format.compress = 90;
412
413         sce->r.displaymode = R_OUTPUT_AREA;
414         sce->r.framapto = 100;
415         sce->r.images = 100;
416         sce->r.framelen = 1.0;
417         sce->r.blurfac = 0.5;
418         sce->r.frs_sec = 24;
419         sce->r.frs_sec_base = 1;
420         sce->r.edgeint = 10;
421         sce->r.ocres = 128;
422
423         /* OCIO_TODO: for forwards compatibility only, so if no tonecurve are used,
424          *            images would look in the same way as in current blender
425          *
426          *            perhaps at some point should be completely deprecated?
427          */
428         sce->r.color_mgt_flag |= R_COLOR_MANAGEMENT;
429
430         sce->r.gauss = 1.0;
431         
432         /* deprecated but keep for upwards compat */
433         sce->r.postgamma = 1.0;
434         sce->r.posthue = 0.0;
435         sce->r.postsat = 1.0;
436
437         sce->r.bake_mode = 1;    /* prevent to include render stuff here */
438         sce->r.bake_filter = 16;
439         sce->r.bake_osa = 5;
440         sce->r.bake_flag = R_BAKE_CLEAR;
441         sce->r.bake_normal_space = R_BAKE_SPACE_TANGENT;
442         sce->r.bake_samples = 256;
443         sce->r.bake_biasdist = 0.001;
444         sce->r.scemode = R_DOCOMP | R_DOSEQ | R_EXTENSION;
445         sce->r.stamp = R_STAMP_TIME | R_STAMP_FRAME | R_STAMP_DATE | R_STAMP_CAMERA | R_STAMP_SCENE | R_STAMP_FILENAME | R_STAMP_RENDERTIME;
446         sce->r.stamp_font_id = 12;
447         sce->r.fg_stamp[0] = sce->r.fg_stamp[1] = sce->r.fg_stamp[2] = 0.8f;
448         sce->r.fg_stamp[3] = 1.0f;
449         sce->r.bg_stamp[0] = sce->r.bg_stamp[1] = sce->r.bg_stamp[2] = 0.0f;
450         sce->r.bg_stamp[3] = 0.25f;
451         sce->r.raytrace_options = R_RAYTRACE_USE_INSTANCES;
452
453         sce->r.seq_prev_type = OB_SOLID;
454         sce->r.seq_rend_type = OB_SOLID;
455         sce->r.seq_flag = R_SEQ_GL_PREV;
456
457         sce->r.threads = 1;
458
459         sce->r.simplify_subsurf = 6;
460         sce->r.simplify_particles = 1.0f;
461         sce->r.simplify_shadowsamples = 16;
462         sce->r.simplify_aosss = 1.0f;
463
464         sce->r.border.xmin = 0.0f;
465         sce->r.border.ymin = 0.0f;
466         sce->r.border.xmax = 1.0f;
467         sce->r.border.ymax = 1.0f;
468         
469         sce->toolsettings = MEM_callocN(sizeof(struct ToolSettings), "Tool Settings Struct");
470         sce->toolsettings->cornertype = 1;
471         sce->toolsettings->degr = 90; 
472         sce->toolsettings->step = 9;
473         sce->toolsettings->turn = 1;
474         sce->toolsettings->extr_offs = 1; 
475         sce->toolsettings->doublimit = 0.001;
476         sce->toolsettings->segments = 32;
477         sce->toolsettings->rings = 32;
478         sce->toolsettings->vertices = 32;
479         sce->toolsettings->uvcalc_radius = 1.0f;
480         sce->toolsettings->uvcalc_cubesize = 1.0f;
481         sce->toolsettings->uvcalc_mapdir = 1;
482         sce->toolsettings->uvcalc_mapalign = 1;
483         sce->toolsettings->uvcalc_margin = 0.001f;
484         sce->toolsettings->unwrapper = 1;
485         sce->toolsettings->select_thresh = 0.01f;
486         sce->toolsettings->jointrilimit = 0.8f;
487
488         sce->toolsettings->selectmode = SCE_SELECT_VERTEX;
489         sce->toolsettings->uv_selectmode = UV_SELECT_VERTEX;
490         sce->toolsettings->normalsize = 0.1;
491         sce->toolsettings->autokey_mode = U.autokey_mode;
492
493         sce->toolsettings->skgen_resolution = 100;
494         sce->toolsettings->skgen_threshold_internal     = 0.01f;
495         sce->toolsettings->skgen_threshold_external     = 0.01f;
496         sce->toolsettings->skgen_angle_limit            = 45.0f;
497         sce->toolsettings->skgen_length_ratio           = 1.3f;
498         sce->toolsettings->skgen_length_limit           = 1.5f;
499         sce->toolsettings->skgen_correlation_limit      = 0.98f;
500         sce->toolsettings->skgen_symmetry_limit         = 0.1f;
501         sce->toolsettings->skgen_postpro = SKGEN_SMOOTH;
502         sce->toolsettings->skgen_postpro_passes = 1;
503         sce->toolsettings->skgen_options = SKGEN_FILTER_INTERNAL | SKGEN_FILTER_EXTERNAL | SKGEN_FILTER_SMART | SKGEN_HARMONIC | SKGEN_SUB_CORRELATION | SKGEN_STICK_TO_EMBEDDING;
504         sce->toolsettings->skgen_subdivisions[0] = SKGEN_SUB_CORRELATION;
505         sce->toolsettings->skgen_subdivisions[1] = SKGEN_SUB_LENGTH;
506         sce->toolsettings->skgen_subdivisions[2] = SKGEN_SUB_ANGLE;
507
508         sce->toolsettings->proportional_size = 1.0f;
509
510         sce->physics_settings.gravity[0] = 0.0f;
511         sce->physics_settings.gravity[1] = 0.0f;
512         sce->physics_settings.gravity[2] = -9.81f;
513         sce->physics_settings.flag = PHYS_GLOBAL_GRAVITY;
514
515         sce->unit.scale_length = 1.0f;
516
517         pset = &sce->toolsettings->particle;
518         pset->flag = PE_KEEP_LENGTHS | PE_LOCK_FIRST | PE_DEFLECT_EMITTER | PE_AUTO_VELOCITY;
519         pset->emitterdist = 0.25f;
520         pset->totrekey = 5;
521         pset->totaddkey = 5;
522         pset->brushtype = PE_BRUSH_NONE;
523         pset->draw_step = 2;
524         pset->fade_frames = 2;
525         pset->selectmode = SCE_SELECT_PATH;
526         for (a = 0; a < PE_TOT_BRUSH; a++) {
527                 pset->brush[a].strength = 0.5;
528                 pset->brush[a].size = 50;
529                 pset->brush[a].step = 10;
530                 pset->brush[a].count = 10;
531         }
532         pset->brush[PE_BRUSH_CUT].strength = 100;
533
534         sce->r.ffcodecdata.audio_mixrate = 44100;
535         sce->r.ffcodecdata.audio_volume = 1.0f;
536         sce->r.ffcodecdata.audio_bitrate = 192;
537         sce->r.ffcodecdata.audio_channels = 2;
538
539         BLI_strncpy(sce->r.engine, "BLENDER_RENDER", sizeof(sce->r.engine));
540
541         sce->audio.distance_model = 2.0f;
542         sce->audio.doppler_factor = 1.0f;
543         sce->audio.speed_of_sound = 343.3f;
544         sce->audio.volume = 1.0f;
545
546         BLI_strncpy(sce->r.pic, U.renderdir, sizeof(sce->r.pic));
547
548         BLI_rctf_init(&sce->r.safety, 0.1f, 0.9f, 0.1f, 0.9f);
549         sce->r.osa = 8;
550
551         /* note; in header_info.c the scene copy happens..., if you add more to renderdata it has to be checked there */
552         BKE_scene_add_render_layer(sce, NULL);
553         
554         /* game data */
555         sce->gm.stereoflag = STEREO_NOSTEREO;
556         sce->gm.stereomode = STEREO_ANAGLYPH;
557         sce->gm.eyeseparation = 0.10;
558
559         sce->gm.dome.angle = 180;
560         sce->gm.dome.mode = DOME_FISHEYE;
561         sce->gm.dome.res = 4;
562         sce->gm.dome.resbuf = 1.0f;
563         sce->gm.dome.tilt = 0;
564
565         sce->gm.xplay = 640;
566         sce->gm.yplay = 480;
567         sce->gm.freqplay = 60;
568         sce->gm.depth = 32;
569
570         sce->gm.gravity = 9.8f;
571         sce->gm.physicsEngine = WOPHY_BULLET;
572         sce->gm.mode = 32; //XXX ugly harcoding, still not sure we should drop mode. 32 == 1 << 5 == use_occlusion_culling 
573         sce->gm.occlusionRes = 128;
574         sce->gm.ticrate = 60;
575         sce->gm.maxlogicstep = 5;
576         sce->gm.physubstep = 1;
577         sce->gm.maxphystep = 5;
578         sce->gm.lineardeactthreshold = 0.8f;
579         sce->gm.angulardeactthreshold = 1.0f;
580         sce->gm.deactivationtime = 0.0f;
581
582         sce->gm.flag = GAME_DISPLAY_LISTS;
583         sce->gm.matmode = GAME_MAT_MULTITEX;
584
585         sce->gm.obstacleSimulation = OBSTSIMULATION_NONE;
586         sce->gm.levelHeight = 2.f;
587
588         sce->gm.recastData.cellsize = 0.3f;
589         sce->gm.recastData.cellheight = 0.2f;
590         sce->gm.recastData.agentmaxslope = M_PI / 2;
591         sce->gm.recastData.agentmaxclimb = 0.9f;
592         sce->gm.recastData.agentheight = 2.0f;
593         sce->gm.recastData.agentradius = 0.6f;
594         sce->gm.recastData.edgemaxlen = 12.0f;
595         sce->gm.recastData.edgemaxerror = 1.3f;
596         sce->gm.recastData.regionminsize = 8.f;
597         sce->gm.recastData.regionmergesize = 20.f;
598         sce->gm.recastData.vertsperpoly = 6;
599         sce->gm.recastData.detailsampledist = 6.0f;
600         sce->gm.recastData.detailsamplemaxerror = 1.0f;
601
602         sce->gm.exitkey = 218; // Blender key code for ESC
603
604         sound_create_scene(sce);
605
606         /* color management */
607         colorspace_name = IMB_colormanagement_role_colorspace_name_get(COLOR_ROLE_DEFAULT_SEQUENCER);
608
609         BKE_color_managed_display_settings_init(&sce->display_settings);
610         BKE_color_managed_view_settings_init(&sce->view_settings);
611         BLI_strncpy(sce->sequencer_colorspace_settings.name, colorspace_name,
612                     sizeof(sce->sequencer_colorspace_settings.name));
613
614         return sce;
615 }
616
617 Base *BKE_scene_base_find(Scene *scene, Object *ob)
618 {
619         return BLI_findptr(&scene->base, ob, offsetof(Base, object));
620 }
621
622 void BKE_scene_set_background(Main *bmain, Scene *scene)
623 {
624         Scene *sce;
625         Base *base;
626         Object *ob;
627         Group *group;
628         GroupObject *go;
629         int flag;
630         
631         /* check for cyclic sets, for reading old files but also for definite security (py?) */
632         BKE_scene_validate_setscene(bmain, scene);
633         
634         /* can happen when switching modes in other scenes */
635         if (scene->obedit && !(scene->obedit->mode & OB_MODE_EDIT))
636                 scene->obedit = NULL;
637
638         /* deselect objects (for dataselect) */
639         for (ob = bmain->object.first; ob; ob = ob->id.next)
640                 ob->flag &= ~(SELECT | OB_FROMGROUP);
641
642         /* group flags again */
643         for (group = bmain->group.first; group; group = group->id.next) {
644                 for (go = group->gobject.first; go; go = go->next) {
645                         if (go->ob) {
646                                 go->ob->flag |= OB_FROMGROUP;
647                         }
648                 }
649         }
650
651         /* sort baselist */
652         DAG_scene_relations_rebuild(bmain, scene);
653         
654         /* ensure dags are built for sets */
655         for (sce = scene; sce; sce = sce->set)
656                 DAG_scene_relations_update(bmain, sce);
657
658         /* copy layers and flags from bases to objects */
659         for (base = scene->base.first; base; base = base->next) {
660                 ob = base->object;
661                 ob->lay = base->lay;
662                 
663                 /* group patch... */
664                 base->flag &= ~(OB_FROMGROUP);
665                 flag = ob->flag & (OB_FROMGROUP);
666                 base->flag |= flag;
667                 
668                 /* not too nice... for recovering objects with lost data */
669                 //if (ob->pose == NULL) base->flag &= ~OB_POSEMODE;
670                 ob->flag = base->flag;
671         }
672         /* no full animation update, this to enable render code to work (render code calls own animation updates) */
673 }
674
675 /* called from creator.c */
676 Scene *BKE_scene_set_name(Main *bmain, const char *name)
677 {
678         Scene *sce = (Scene *)BKE_libblock_find_name(ID_SCE, name);
679         if (sce) {
680                 BKE_scene_set_background(bmain, sce);
681                 printf("Scene switch: '%s' in file: '%s'\n", name, G.main->name);
682                 return sce;
683         }
684
685         printf("Can't find scene: '%s' in file: '%s'\n", name, G.main->name);
686         return NULL;
687 }
688
689 void BKE_scene_unlink(Main *bmain, Scene *sce, Scene *newsce)
690 {
691         Scene *sce1;
692         bScreen *sc;
693
694         /* check all sets */
695         for (sce1 = bmain->scene.first; sce1; sce1 = sce1->id.next)
696                 if (sce1->set == sce)
697                         sce1->set = NULL;
698         
699         /* check render layer nodes in other scenes */
700         clear_scene_in_nodes(bmain, sce);
701         
702         /* al screens */
703         for (sc = bmain->screen.first; sc; sc = sc->id.next)
704                 if (sc->scene == sce)
705                         sc->scene = newsce;
706
707         BKE_libblock_free(&bmain->scene, sce);
708 }
709
710 /* used by metaballs
711  * doesn't return the original duplicated object, only dupli's
712  */
713 int BKE_scene_base_iter_next(Scene **scene, int val, Base **base, Object **ob)
714 {
715         static ListBase *duplilist = NULL;
716         static DupliObject *dupob;
717         static int fase = F_START, in_next_object = 0;
718         int run_again = 1;
719         
720         /* init */
721         if (val == 0) {
722                 fase = F_START;
723                 dupob = NULL;
724                 
725                 /* XXX particle systems with metas+dupligroups call this recursively */
726                 /* see bug #18725 */
727                 if (in_next_object) {
728                         printf("ERROR: Metaball generation called recursively, not supported\n");
729                         
730                         return F_ERROR;
731                 }
732         }
733         else {
734                 in_next_object = 1;
735                 
736                 /* run_again is set when a duplilist has been ended */
737                 while (run_again) {
738                         run_again = 0;
739
740                         /* the first base */
741                         if (fase == F_START) {
742                                 *base = (*scene)->base.first;
743                                 if (*base) {
744                                         *ob = (*base)->object;
745                                         fase = F_SCENE;
746                                 }
747                                 else {
748                                         /* exception: empty scene */
749                                         while ((*scene)->set) {
750                                                 (*scene) = (*scene)->set;
751                                                 if ((*scene)->base.first) {
752                                                         *base = (*scene)->base.first;
753                                                         *ob = (*base)->object;
754                                                         fase = F_SCENE;
755                                                         break;
756                                                 }
757                                         }
758                                 }
759                         }
760                         else {
761                                 if (*base && fase != F_DUPLI) {
762                                         *base = (*base)->next;
763                                         if (*base) {
764                                                 *ob = (*base)->object;
765                                         }
766                                         else {
767                                                 if (fase == F_SCENE) {
768                                                         /* (*scene) is finished, now do the set */
769                                                         while ((*scene)->set) {
770                                                                 (*scene) = (*scene)->set;
771                                                                 if ((*scene)->base.first) {
772                                                                         *base = (*scene)->base.first;
773                                                                         *ob = (*base)->object;
774                                                                         break;
775                                                                 }
776                                                         }
777                                                 }
778                                         }
779                                 }
780                         }
781                         
782                         if (*base == NULL) {
783                                 fase = F_START;
784                         }
785                         else {
786                                 if (fase != F_DUPLI) {
787                                         if ( (*base)->object->transflag & OB_DUPLI) {
788                                                 /* groups cannot be duplicated for mballs yet, 
789                                                  * this enters eternal loop because of 
790                                                  * makeDispListMBall getting called inside of group_duplilist */
791                                                 if ((*base)->object->dup_group == NULL) {
792                                                         duplilist = object_duplilist((*scene), (*base)->object, FALSE);
793                                                         
794                                                         dupob = duplilist->first;
795
796                                                         if (!dupob)
797                                                                 free_object_duplilist(duplilist);
798                                                 }
799                                         }
800                                 }
801                                 /* handle dupli's */
802                                 if (dupob) {
803                                         
804                                         copy_m4_m4(dupob->ob->obmat, dupob->mat);
805                                         
806                                         (*base)->flag |= OB_FROMDUPLI;
807                                         *ob = dupob->ob;
808                                         fase = F_DUPLI;
809                                         
810                                         dupob = dupob->next;
811                                 }
812                                 else if (fase == F_DUPLI) {
813                                         fase = F_SCENE;
814                                         (*base)->flag &= ~OB_FROMDUPLI;
815                                         
816                                         for (dupob = duplilist->first; dupob; dupob = dupob->next) {
817                                                 copy_m4_m4(dupob->ob->obmat, dupob->omat);
818                                         }
819                                         
820                                         free_object_duplilist(duplilist);
821                                         duplilist = NULL;
822                                         run_again = 1;
823                                 }
824                         }
825                 }
826         }
827
828 #if 0
829         if (ob && *ob) {
830                 printf("Scene: '%s', '%s'\n", (*scene)->id.name + 2, (*ob)->id.name + 2);
831         }
832 #endif
833
834         /* reset recursion test */
835         in_next_object = 0;
836         
837         return fase;
838 }
839
840 Object *BKE_scene_camera_find(Scene *sc)
841 {
842         Base *base;
843         
844         for (base = sc->base.first; base; base = base->next)
845                 if (base->object->type == OB_CAMERA)
846                         return base->object;
847
848         return NULL;
849 }
850
851 #ifdef DURIAN_CAMERA_SWITCH
852 Object *BKE_scene_camera_switch_find(Scene *scene)
853 {
854         TimeMarker *m;
855         int cfra = scene->r.cfra;
856         int frame = -(MAXFRAME + 1);
857         Object *camera = NULL;
858
859         for (m = scene->markers.first; m; m = m->next) {
860                 if (m->camera && (m->camera->restrictflag & OB_RESTRICT_RENDER) == 0 && (m->frame <= cfra) && (m->frame > frame)) {
861                         camera = m->camera;
862                         frame = m->frame;
863
864                         if (frame == cfra)
865                                 break;
866
867                 }
868         }
869         return camera;
870 }
871 #endif
872
873 int BKE_scene_camera_switch_update(Scene *scene)
874 {
875 #ifdef DURIAN_CAMERA_SWITCH
876         Object *camera = BKE_scene_camera_switch_find(scene);
877         if (camera) {
878                 scene->camera = camera;
879                 return 1;
880         }
881 #else
882         (void)scene;
883 #endif
884         return 0;
885 }
886
887 char *BKE_scene_find_marker_name(Scene *scene, int frame)
888 {
889         ListBase *markers = &scene->markers;
890         TimeMarker *m1, *m2;
891
892         /* search through markers for match */
893         for (m1 = markers->first, m2 = markers->last; m1 && m2; m1 = m1->next, m2 = m2->prev) {
894                 if (m1->frame == frame)
895                         return m1->name;
896
897                 if (m1 == m2)
898                         break;
899
900                 if (m2->frame == frame)
901                         return m2->name;
902         }
903
904         return NULL;
905 }
906
907 /* return the current marker for this frame,
908  * we can have more then 1 marker per frame, this just returns the first :/ */
909 char *BKE_scene_find_last_marker_name(Scene *scene, int frame)
910 {
911         TimeMarker *marker, *best_marker = NULL;
912         int best_frame = -MAXFRAME * 2;
913         for (marker = scene->markers.first; marker; marker = marker->next) {
914                 if (marker->frame == frame) {
915                         return marker->name;
916                 }
917
918                 if (marker->frame > best_frame && marker->frame < frame) {
919                         best_marker = marker;
920                         best_frame = marker->frame;
921                 }
922         }
923
924         return best_marker ? best_marker->name : NULL;
925 }
926
927
928 Base *BKE_scene_base_add(Scene *sce, Object *ob)
929 {
930         Base *b = MEM_callocN(sizeof(*b), "BKE_scene_base_add");
931         BLI_addhead(&sce->base, b);
932
933         b->object = ob;
934         b->flag = ob->flag;
935         b->lay = ob->lay;
936
937         return b;
938 }
939
940 void BKE_scene_base_unlink(Scene *sce, Base *base)
941 {
942         /* remove rigid body constraint from world before removing object */
943         if (base->object->rigidbody_constraint)
944                 BKE_rigidbody_remove_constraint(sce, base->object);
945         /* remove rigid body object from world before removing object */
946         if (base->object->rigidbody_object)
947                 BKE_rigidbody_remove_object(sce, base->object);
948         
949         BLI_remlink(&sce->base, base);
950 }
951
952 void BKE_scene_base_deselect_all(Scene *sce)
953 {
954         Base *b;
955
956         for (b = sce->base.first; b; b = b->next) {
957                 b->flag &= ~SELECT;
958                 b->object->flag = b->flag;
959         }
960 }
961
962 void BKE_scene_base_select(Scene *sce, Base *selbase)
963 {
964         selbase->flag |= SELECT;
965         selbase->object->flag = selbase->flag;
966
967         sce->basact = selbase;
968 }
969
970 /* checks for cycle, returns 1 if it's all OK */
971 int BKE_scene_validate_setscene(Main *bmain, Scene *sce)
972 {
973         Scene *scene;
974         int a, totscene;
975         
976         if (sce->set == NULL) return 1;
977         
978         totscene = 0;
979         for (scene = bmain->scene.first; scene; scene = scene->id.next)
980                 totscene++;
981         
982         for (a = 0, scene = sce; scene->set; scene = scene->set, a++) {
983                 /* more iterations than scenes means we have a cycle */
984                 if (a > totscene) {
985                         /* the tested scene gets zero'ed, that's typically current scene */
986                         sce->set = NULL;
987                         return 0;
988                 }
989         }
990
991         return 1;
992 }
993
994 /* This function is needed to cope with fractional frames - including two Blender rendering features
995  * mblur (motion blur that renders 'subframes' and blurs them together), and fields rendering. 
996  */
997 float BKE_scene_frame_get(Scene *scene)
998 {
999         return BKE_scene_frame_get_from_ctime(scene, scene->r.cfra);
1000 }
1001
1002 /* This function is used to obtain arbitrary fractional frames */
1003 float BKE_scene_frame_get_from_ctime(Scene *scene, const float frame)
1004 {
1005         float ctime = frame;
1006         ctime += scene->r.subframe;
1007         ctime *= scene->r.framelen;
1008         
1009         return ctime;
1010 }
1011
1012 /* drivers support/hacks 
1013  *  - this method is called from scene_update_tagged_recursive(), so gets included in viewport + render
1014  *      - these are always run since the depsgraph can't handle non-object data
1015  *      - these happen after objects are all done so that we can read in their final transform values,
1016  *        though this means that objects can't refer to scene info for guidance...
1017  */
1018 static void scene_update_drivers(Main *UNUSED(bmain), Scene *scene)
1019 {
1020         float ctime = BKE_scene_frame_get(scene);
1021         
1022         /* scene itself */
1023         if (scene->adt && scene->adt->drivers.first) {
1024                 BKE_animsys_evaluate_animdata(scene, &scene->id, scene->adt, ctime, ADT_RECALC_DRIVERS);
1025         }
1026
1027         /* world */
1028         /* TODO: what about world textures? but then those have nodes too... */
1029         if (scene->world) {
1030                 ID *wid = (ID *)scene->world;
1031                 AnimData *adt = BKE_animdata_from_id(wid);
1032                 
1033                 if (adt && adt->drivers.first)
1034                         BKE_animsys_evaluate_animdata(scene, wid, adt, ctime, ADT_RECALC_DRIVERS);
1035         }
1036         
1037         /* nodes */
1038         if (scene->nodetree) {
1039                 ID *nid = (ID *)scene->nodetree;
1040                 AnimData *adt = BKE_animdata_from_id(nid);
1041                 
1042                 if (adt && adt->drivers.first)
1043                         BKE_animsys_evaluate_animdata(scene, nid, adt, ctime, ADT_RECALC_DRIVERS);
1044         }
1045 }
1046
1047 /* deps hack - do extra recalcs at end */
1048 static void scene_depsgraph_hack(Scene *scene, Scene *scene_parent)
1049 {
1050         Base *base;
1051                 
1052         scene->customdata_mask = scene_parent->customdata_mask;
1053         
1054         /* sets first, we allow per definition current scene to have
1055          * dependencies on sets, but not the other way around. */
1056         if (scene->set)
1057                 scene_depsgraph_hack(scene->set, scene_parent);
1058         
1059         for (base = scene->base.first; base; base = base->next) {
1060                 Object *ob = base->object;
1061                 
1062                 if (ob->depsflag) {
1063                         int recalc = 0;
1064                         // printf("depshack %s\n", ob->id.name + 2);
1065                         
1066                         if (ob->depsflag & OB_DEPS_EXTRA_OB_RECALC)
1067                                 recalc |= OB_RECALC_OB;
1068                         if (ob->depsflag & OB_DEPS_EXTRA_DATA_RECALC)
1069                                 recalc |= OB_RECALC_DATA;
1070                         
1071                         ob->recalc |= recalc;
1072                         BKE_object_handle_update(scene_parent, ob);
1073                         
1074                         if (ob->dup_group && (ob->transflag & OB_DUPLIGROUP)) {
1075                                 GroupObject *go;
1076                                 
1077                                 for (go = ob->dup_group->gobject.first; go; go = go->next) {
1078                                         if (go->ob)
1079                                                 go->ob->recalc |= recalc;
1080                                 }
1081                                 group_handle_recalc_and_update(scene_parent, ob, ob->dup_group);
1082                         }
1083                 }
1084         }
1085
1086 }
1087
1088 static void scene_rebuild_rbw_recursive(Scene *scene, float ctime)
1089 {
1090         if (scene->set)
1091                 scene_rebuild_rbw_recursive(scene->set, ctime);
1092
1093         if (BKE_scene_check_rigidbody_active(scene))
1094                 BKE_rigidbody_rebuild_world(scene, ctime);
1095 }
1096
1097 static void scene_do_rb_simulation_recursive(Scene *scene, float ctime)
1098 {
1099         if (scene->set)
1100                 scene_do_rb_simulation_recursive(scene->set, ctime);
1101
1102         if (BKE_scene_check_rigidbody_active(scene))
1103                 BKE_rigidbody_do_simulation(scene, ctime);
1104 }
1105
1106 static void scene_update_tagged_recursive(Main *bmain, Scene *scene, Scene *scene_parent)
1107 {
1108         Base *base;
1109         
1110         scene->customdata_mask = scene_parent->customdata_mask;
1111
1112         /* sets first, we allow per definition current scene to have
1113          * dependencies on sets, but not the other way around. */
1114         if (scene->set)
1115                 scene_update_tagged_recursive(bmain, scene->set, scene_parent);
1116         
1117         /* scene objects */
1118         for (base = scene->base.first; base; base = base->next) {
1119                 Object *ob = base->object;
1120                 
1121                 BKE_object_handle_update_ex(scene_parent, ob, scene->rigidbody_world);
1122                 
1123                 if (ob->dup_group && (ob->transflag & OB_DUPLIGROUP))
1124                         group_handle_recalc_and_update(scene_parent, ob, ob->dup_group);
1125                         
1126                 /* always update layer, so that animating layers works (joshua july 2010) */
1127                 /* XXX commented out, this has depsgraph issues anyway - and this breaks setting scenes
1128                  * (on scene-set, the base-lay is copied to ob-lay (ton nov 2012) */
1129                 // base->lay = ob->lay;
1130         }
1131         
1132         /* scene drivers... */
1133         scene_update_drivers(bmain, scene);
1134
1135         /* update sound system animation */
1136         sound_update_scene(scene);
1137
1138         /* update masking curves */
1139         BKE_mask_update_scene(bmain, scene, FALSE);
1140         
1141 }
1142
1143 /* this is called in main loop, doing tagged updates before redraw */
1144 void BKE_scene_update_tagged(Main *bmain, Scene *scene)
1145 {
1146         Scene *sce_iter;
1147         
1148         /* keep this first */
1149         BLI_callback_exec(bmain, &scene->id, BLI_CB_EVT_SCENE_UPDATE_PRE);
1150
1151         /* (re-)build dependency graph if needed */
1152         for (sce_iter = scene; sce_iter; sce_iter = sce_iter->set)
1153                 DAG_scene_relations_update(bmain, sce_iter);
1154
1155         /* flush recalc flags to dependencies */
1156         DAG_ids_flush_tagged(bmain);
1157
1158         /* removed calls to quick_cache, see pointcache.c */
1159         
1160         /* clear "LIB_DOIT" flag from all materials, to prevent infinite recursion problems later 
1161          * when trying to find materials with drivers that need evaluating [#32017] 
1162          */
1163         tag_main_idcode(bmain, ID_MA, FALSE);
1164         tag_main_idcode(bmain, ID_LA, FALSE);
1165
1166         /* update all objects: drivers, matrices, displists, etc. flags set
1167          * by depgraph or manual, no layer check here, gets correct flushed
1168          *
1169          * in the future this should handle updates for all datablocks, not
1170          * only objects and scenes. - brecht */
1171         scene_update_tagged_recursive(bmain, scene, scene);
1172
1173         /* extra call here to recalc scene animation (for sequencer) */
1174         {
1175                 AnimData *adt = BKE_animdata_from_id(&scene->id);
1176                 float ctime = BKE_scene_frame_get(scene);
1177                 
1178                 if (adt && (adt->recalc & ADT_RECALC_ANIM))
1179                         BKE_animsys_evaluate_animdata(scene, &scene->id, adt, ctime, 0);
1180         }
1181         
1182         /* notify editors and python about recalc */
1183         BLI_callback_exec(bmain, &scene->id, BLI_CB_EVT_SCENE_UPDATE_POST);
1184         DAG_ids_check_recalc(bmain, scene, FALSE);
1185
1186         /* clear recalc flags */
1187         DAG_ids_clear_recalc(bmain);
1188 }
1189
1190 /* applies changes right away, does all sets too */
1191 void BKE_scene_update_for_newframe(Main *bmain, Scene *sce, unsigned int lay)
1192 {
1193         float ctime = BKE_scene_frame_get(sce);
1194         Scene *sce_iter;
1195         
1196         /* rebuild rigid body worlds before doing the actual frame update
1197          * this needs to be done on start frame but animation playback usually starts one frame later
1198          * we need to do it here to avoid rebuilding the world on every simulation change, which can be very expensive
1199          */
1200         scene_rebuild_rbw_recursive(sce, ctime);
1201
1202         /* keep this first */
1203         BLI_callback_exec(bmain, &sce->id, BLI_CB_EVT_FRAME_CHANGE_PRE);
1204         BLI_callback_exec(bmain, &sce->id, BLI_CB_EVT_SCENE_UPDATE_PRE);
1205
1206         sound_set_cfra(sce->r.cfra);
1207         
1208         /* clear animation overrides */
1209         /* XXX TODO... */
1210
1211         for (sce_iter = sce; sce_iter; sce_iter = sce_iter->set)
1212                 DAG_scene_relations_update(bmain, sce_iter);
1213
1214         /* flush recalc flags to dependencies, if we were only changing a frame
1215          * this would not be necessary, but if a user or a script has modified
1216          * some datablock before BKE_scene_update_tagged was called, we need the flush */
1217         DAG_ids_flush_tagged(bmain);
1218
1219         /* Following 2 functions are recursive
1220          * so don't call within 'scene_update_tagged_recursive' */
1221         DAG_scene_update_flags(bmain, sce, lay, TRUE);   // only stuff that moves or needs display still
1222
1223         BKE_mask_evaluate_all_masks(bmain, ctime, TRUE);
1224
1225         /* All 'standard' (i.e. without any dependencies) animation is handled here,
1226          * with an 'local' to 'macro' order of evaluation. This should ensure that
1227          * settings stored nestled within a hierarchy (i.e. settings in a Texture block
1228          * can be overridden by settings from Scene, which owns the Texture through a hierarchy
1229          * such as Scene->World->MTex/Texture) can still get correctly overridden.
1230          */
1231         BKE_animsys_evaluate_all_animation(bmain, sce, ctime);
1232         /*...done with recursive funcs */
1233
1234         /* clear "LIB_DOIT" flag from all materials, to prevent infinite recursion problems later 
1235          * when trying to find materials with drivers that need evaluating [#32017] 
1236          */
1237         tag_main_idcode(bmain, ID_MA, FALSE);
1238         tag_main_idcode(bmain, ID_LA, FALSE);
1239
1240         /* run rigidbody sim */
1241         /* NOTE: current position is so that rigidbody sim affects other objects, might change in the future */
1242         scene_do_rb_simulation_recursive(sce, ctime);
1243
1244         /* BKE_object_handle_update() on all objects, groups and sets */
1245         scene_update_tagged_recursive(bmain, sce, sce);
1246
1247         scene_depsgraph_hack(sce, sce);
1248
1249         /* notify editors and python about recalc */
1250         BLI_callback_exec(bmain, &sce->id, BLI_CB_EVT_SCENE_UPDATE_POST);
1251         BLI_callback_exec(bmain, &sce->id, BLI_CB_EVT_FRAME_CHANGE_POST);
1252
1253         DAG_ids_check_recalc(bmain, sce, TRUE);
1254
1255         /* clear recalc flags */
1256         DAG_ids_clear_recalc(bmain);
1257 }
1258
1259 /* return default layer, also used to patch old files */
1260 SceneRenderLayer *BKE_scene_add_render_layer(Scene *sce, const char *name)
1261 {
1262         SceneRenderLayer *srl;
1263
1264         if (!name)
1265                 name = "RenderLayer";
1266
1267         srl = MEM_callocN(sizeof(SceneRenderLayer), "new render layer");
1268         BLI_strncpy(srl->name, name, sizeof(srl->name));
1269         BLI_uniquename(&sce->r.layers, srl, "RenderLayer", '.', offsetof(SceneRenderLayer, name), sizeof(srl->name));
1270         BLI_addtail(&sce->r.layers, srl);
1271
1272         /* note, this is also in render, pipeline.c, to make layer when scenedata doesnt have it */
1273         srl->lay = (1 << 20) - 1;
1274         srl->layflag = 0x7FFF;   /* solid ztra halo edge strand */
1275         srl->passflag = SCE_PASS_COMBINED | SCE_PASS_Z;
1276
1277         return srl;
1278 }
1279
1280 int BKE_scene_remove_render_layer(Main *bmain, Scene *scene, SceneRenderLayer *srl)
1281 {
1282         const int act = BLI_findindex(&scene->r.layers, srl);
1283         Scene *sce;
1284
1285         if (act == -1) {
1286                 return 0;
1287         }
1288         else if ( (scene->r.layers.first == scene->r.layers.last) &&
1289                   (scene->r.layers.first == srl))
1290         {
1291                 /* ensure 1 layer is kept */
1292                 return 0;
1293         }
1294
1295         BLI_remlink(&scene->r.layers, srl);
1296         MEM_freeN(srl);
1297
1298         scene->r.actlay = 0;
1299
1300         for (sce = bmain->scene.first; sce; sce = sce->id.next) {
1301                 if (sce->nodetree) {
1302                         bNode *node;
1303                         for (node = sce->nodetree->nodes.first; node; node = node->next) {
1304                                 if (node->type == CMP_NODE_R_LAYERS && (Scene *)node->id == scene) {
1305                                         if (node->custom1 == act)
1306                                                 node->custom1 = 0;
1307                                         else if (node->custom1 > act)
1308                                                 node->custom1--;
1309                                 }
1310                         }
1311                 }
1312         }
1313
1314         return 1;
1315 }
1316
1317 /* render simplification */
1318
1319 int get_render_subsurf_level(RenderData *r, int lvl)
1320 {
1321         if (r->mode & R_SIMPLIFY)
1322                 return min_ii(r->simplify_subsurf, lvl);
1323         else
1324                 return lvl;
1325 }
1326
1327 int get_render_child_particle_number(RenderData *r, int num)
1328 {
1329         if (r->mode & R_SIMPLIFY)
1330                 return (int)(r->simplify_particles * num);
1331         else
1332                 return num;
1333 }
1334
1335 int get_render_shadow_samples(RenderData *r, int samples)
1336 {
1337         if ((r->mode & R_SIMPLIFY) && samples > 0)
1338                 return min_ii(r->simplify_shadowsamples, samples);
1339         else
1340                 return samples;
1341 }
1342
1343 float get_render_aosss_error(RenderData *r, float error)
1344 {
1345         if (r->mode & R_SIMPLIFY)
1346                 return ((1.0f - r->simplify_aosss) * 10.0f + 1.0f) * error;
1347         else
1348                 return error;
1349 }
1350
1351 /* helper function for the SETLOOPER macro */
1352 Base *_setlooper_base_step(Scene **sce_iter, Base *base)
1353 {
1354         if (base && base->next) {
1355                 /* common case, step to the next */
1356                 return base->next;
1357         }
1358         else if (base == NULL && (*sce_iter)->base.first) {
1359                 /* first time looping, return the scenes first base */
1360                 return (Base *)(*sce_iter)->base.first;
1361         }
1362         else {
1363                 /* reached the end, get the next base in the set */
1364                 while ((*sce_iter = (*sce_iter)->set)) {
1365                         base = (Base *)(*sce_iter)->base.first;
1366                         if (base) {
1367                                 return base;
1368                         }
1369                 }
1370         }
1371
1372         return NULL;
1373 }
1374
1375 int BKE_scene_use_new_shading_nodes(Scene *scene)
1376 {
1377         RenderEngineType *type = RE_engines_find(scene->r.engine);
1378         return (type && type->flag & RE_USE_SHADING_NODES);
1379 }
1380
1381 void BKE_scene_base_flag_to_objects(struct Scene *scene)
1382 {
1383         Base *base = scene->base.first;
1384
1385         while (base) {
1386                 base->object->flag = base->flag;
1387                 base = base->next;
1388         }
1389 }
1390
1391 void BKE_scene_base_flag_from_objects(struct Scene *scene)
1392 {
1393         Base *base = scene->base.first;
1394
1395         while (base) {
1396                 base->flag = base->object->flag;
1397                 base = base->next;
1398         }
1399 }
1400
1401 void BKE_scene_disable_color_management(Scene *scene)
1402 {
1403         ColorManagedDisplaySettings *display_settings = &scene->display_settings;
1404         ColorManagedViewSettings *view_settings = &scene->view_settings;
1405         const char *view;
1406         const char *none_display_name;
1407
1408         none_display_name = IMB_colormanagement_display_get_none_name();
1409
1410         BLI_strncpy(display_settings->display_device, none_display_name, sizeof(display_settings->display_device));
1411
1412         view = IMB_colormanagement_view_get_default_name(display_settings->display_device);
1413
1414         if (view) {
1415                 BLI_strncpy(view_settings->view_transform, view, sizeof(view_settings->view_transform));
1416         }
1417 }
1418
1419 int BKE_scene_check_color_management_enabled(const Scene *scene)
1420 {
1421         return strcmp(scene->display_settings.display_device, "None") != 0;
1422 }
1423
1424 int BKE_scene_check_rigidbody_active(const Scene *scene)
1425 {
1426         return scene && scene->rigidbody_world && scene->rigidbody_world->group && !(scene->rigidbody_world->flag & RBW_FLAG_MUTED);
1427 }