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