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