6a5886faa679bc6e60b90c0187cd8b99a18b4908
[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.scemode = R_DOCOMP | R_DOSEQ | R_EXTENSION;
436         sce->r.stamp = R_STAMP_TIME | R_STAMP_FRAME | R_STAMP_DATE | R_STAMP_CAMERA | R_STAMP_SCENE | R_STAMP_FILENAME | R_STAMP_RENDERTIME;
437         sce->r.stamp_font_id = 12;
438         sce->r.fg_stamp[0] = sce->r.fg_stamp[1] = sce->r.fg_stamp[2] = 0.8f;
439         sce->r.fg_stamp[3] = 1.0f;
440         sce->r.bg_stamp[0] = sce->r.bg_stamp[1] = sce->r.bg_stamp[2] = 0.0f;
441         sce->r.bg_stamp[3] = 0.25f;
442         sce->r.raytrace_options = R_RAYTRACE_USE_INSTANCES;
443
444         sce->r.seq_prev_type = OB_SOLID;
445         sce->r.seq_rend_type = OB_SOLID;
446         sce->r.seq_flag = R_SEQ_GL_PREV;
447
448         sce->r.threads = 1;
449
450         sce->r.simplify_subsurf = 6;
451         sce->r.simplify_particles = 1.0f;
452         sce->r.simplify_shadowsamples = 16;
453         sce->r.simplify_aosss = 1.0f;
454
455         sce->r.border.xmin = 0.0f;
456         sce->r.border.ymin = 0.0f;
457         sce->r.border.xmax = 1.0f;
458         sce->r.border.ymax = 1.0f;
459         
460         sce->toolsettings = MEM_callocN(sizeof(struct ToolSettings), "Tool Settings Struct");
461         sce->toolsettings->cornertype = 1;
462         sce->toolsettings->degr = 90; 
463         sce->toolsettings->step = 9;
464         sce->toolsettings->turn = 1;
465         sce->toolsettings->extr_offs = 1; 
466         sce->toolsettings->doublimit = 0.001;
467         sce->toolsettings->segments = 32;
468         sce->toolsettings->rings = 32;
469         sce->toolsettings->vertices = 32;
470         sce->toolsettings->uvcalc_radius = 1.0f;
471         sce->toolsettings->uvcalc_cubesize = 1.0f;
472         sce->toolsettings->uvcalc_mapdir = 1;
473         sce->toolsettings->uvcalc_mapalign = 1;
474         sce->toolsettings->uvcalc_margin = 0.001f;
475         sce->toolsettings->unwrapper = 1;
476         sce->toolsettings->select_thresh = 0.01f;
477         sce->toolsettings->jointrilimit = 0.8f;
478
479         sce->toolsettings->selectmode = SCE_SELECT_VERTEX;
480         sce->toolsettings->uv_selectmode = UV_SELECT_VERTEX;
481         sce->toolsettings->normalsize = 0.1;
482         sce->toolsettings->autokey_mode = U.autokey_mode;
483
484         sce->toolsettings->skgen_resolution = 100;
485         sce->toolsettings->skgen_threshold_internal     = 0.01f;
486         sce->toolsettings->skgen_threshold_external     = 0.01f;
487         sce->toolsettings->skgen_angle_limit            = 45.0f;
488         sce->toolsettings->skgen_length_ratio           = 1.3f;
489         sce->toolsettings->skgen_length_limit           = 1.5f;
490         sce->toolsettings->skgen_correlation_limit      = 0.98f;
491         sce->toolsettings->skgen_symmetry_limit         = 0.1f;
492         sce->toolsettings->skgen_postpro = SKGEN_SMOOTH;
493         sce->toolsettings->skgen_postpro_passes = 1;
494         sce->toolsettings->skgen_options = SKGEN_FILTER_INTERNAL | SKGEN_FILTER_EXTERNAL | SKGEN_FILTER_SMART | SKGEN_HARMONIC | SKGEN_SUB_CORRELATION | SKGEN_STICK_TO_EMBEDDING;
495         sce->toolsettings->skgen_subdivisions[0] = SKGEN_SUB_CORRELATION;
496         sce->toolsettings->skgen_subdivisions[1] = SKGEN_SUB_LENGTH;
497         sce->toolsettings->skgen_subdivisions[2] = SKGEN_SUB_ANGLE;
498
499         sce->toolsettings->proportional_size = 1.0f;
500
501         sce->physics_settings.gravity[0] = 0.0f;
502         sce->physics_settings.gravity[1] = 0.0f;
503         sce->physics_settings.gravity[2] = -9.81f;
504         sce->physics_settings.flag = PHYS_GLOBAL_GRAVITY;
505
506         sce->unit.scale_length = 1.0f;
507
508         pset = &sce->toolsettings->particle;
509         pset->flag = PE_KEEP_LENGTHS | PE_LOCK_FIRST | PE_DEFLECT_EMITTER | PE_AUTO_VELOCITY;
510         pset->emitterdist = 0.25f;
511         pset->totrekey = 5;
512         pset->totaddkey = 5;
513         pset->brushtype = PE_BRUSH_NONE;
514         pset->draw_step = 2;
515         pset->fade_frames = 2;
516         pset->selectmode = SCE_SELECT_PATH;
517         for (a = 0; a < PE_TOT_BRUSH; a++) {
518                 pset->brush[a].strength = 0.5;
519                 pset->brush[a].size = 50;
520                 pset->brush[a].step = 10;
521                 pset->brush[a].count = 10;
522         }
523         pset->brush[PE_BRUSH_CUT].strength = 100;
524
525         sce->r.ffcodecdata.audio_mixrate = 44100;
526         sce->r.ffcodecdata.audio_volume = 1.0f;
527         sce->r.ffcodecdata.audio_bitrate = 192;
528         sce->r.ffcodecdata.audio_channels = 2;
529
530         BLI_strncpy(sce->r.engine, "BLENDER_RENDER", sizeof(sce->r.engine));
531
532         sce->audio.distance_model = 2.0f;
533         sce->audio.doppler_factor = 1.0f;
534         sce->audio.speed_of_sound = 343.3f;
535         sce->audio.volume = 1.0f;
536
537         BLI_strncpy(sce->r.pic, U.renderdir, sizeof(sce->r.pic));
538
539         BLI_rctf_init(&sce->r.safety, 0.1f, 0.9f, 0.1f, 0.9f);
540         sce->r.osa = 8;
541
542         /* note; in header_info.c the scene copy happens..., if you add more to renderdata it has to be checked there */
543         BKE_scene_add_render_layer(sce, NULL);
544         
545         /* game data */
546         sce->gm.stereoflag = STEREO_NOSTEREO;
547         sce->gm.stereomode = STEREO_ANAGLYPH;
548         sce->gm.eyeseparation = 0.10;
549
550         sce->gm.dome.angle = 180;
551         sce->gm.dome.mode = DOME_FISHEYE;
552         sce->gm.dome.res = 4;
553         sce->gm.dome.resbuf = 1.0f;
554         sce->gm.dome.tilt = 0;
555
556         sce->gm.xplay = 640;
557         sce->gm.yplay = 480;
558         sce->gm.freqplay = 60;
559         sce->gm.depth = 32;
560
561         sce->gm.gravity = 9.8f;
562         sce->gm.physicsEngine = WOPHY_BULLET;
563         sce->gm.mode = 32; //XXX ugly harcoding, still not sure we should drop mode. 32 == 1 << 5 == use_occlusion_culling 
564         sce->gm.occlusionRes = 128;
565         sce->gm.ticrate = 60;
566         sce->gm.maxlogicstep = 5;
567         sce->gm.physubstep = 1;
568         sce->gm.maxphystep = 5;
569         sce->gm.lineardeactthreshold = 0.8f;
570         sce->gm.angulardeactthreshold = 1.0f;
571         sce->gm.deactivationtime = 0.0f;
572
573         sce->gm.flag = GAME_DISPLAY_LISTS;
574         sce->gm.matmode = GAME_MAT_MULTITEX;
575
576         sce->gm.obstacleSimulation = OBSTSIMULATION_NONE;
577         sce->gm.levelHeight = 2.f;
578
579         sce->gm.recastData.cellsize = 0.3f;
580         sce->gm.recastData.cellheight = 0.2f;
581         sce->gm.recastData.agentmaxslope = M_PI / 2;
582         sce->gm.recastData.agentmaxclimb = 0.9f;
583         sce->gm.recastData.agentheight = 2.0f;
584         sce->gm.recastData.agentradius = 0.6f;
585         sce->gm.recastData.edgemaxlen = 12.0f;
586         sce->gm.recastData.edgemaxerror = 1.3f;
587         sce->gm.recastData.regionminsize = 8.f;
588         sce->gm.recastData.regionmergesize = 20.f;
589         sce->gm.recastData.vertsperpoly = 6;
590         sce->gm.recastData.detailsampledist = 6.0f;
591         sce->gm.recastData.detailsamplemaxerror = 1.0f;
592
593         sce->gm.exitkey = 218; // Blender key code for ESC
594
595         sound_create_scene(sce);
596
597         /* color management */
598         colorspace_name = IMB_colormanagement_role_colorspace_name_get(COLOR_ROLE_DEFAULT_SEQUENCER);
599
600         BKE_color_managed_display_settings_init(&sce->display_settings);
601         BKE_color_managed_view_settings_init(&sce->view_settings);
602         BLI_strncpy(sce->sequencer_colorspace_settings.name, colorspace_name,
603                     sizeof(sce->sequencer_colorspace_settings.name));
604
605         return sce;
606 }
607
608 Scene *BKE_scene_add(Main *bmain, const char *name)
609 {
610         return scene_add(bmain, name);
611 }
612
613 Base *BKE_scene_base_find(Scene *scene, Object *ob)
614 {
615         return BLI_findptr(&scene->base, ob, offsetof(Base, object));
616 }
617
618 void BKE_scene_set_background(Main *bmain, Scene *scene)
619 {
620         Scene *sce;
621         Base *base;
622         Object *ob;
623         Group *group;
624         GroupObject *go;
625         int flag;
626         
627         /* check for cyclic sets, for reading old files but also for definite security (py?) */
628         BKE_scene_validate_setscene(bmain, scene);
629         
630         /* can happen when switching modes in other scenes */
631         if (scene->obedit && !(scene->obedit->mode & OB_MODE_EDIT))
632                 scene->obedit = NULL;
633
634         /* deselect objects (for dataselect) */
635         for (ob = bmain->object.first; ob; ob = ob->id.next)
636                 ob->flag &= ~(SELECT | OB_FROMGROUP);
637
638         /* group flags again */
639         for (group = bmain->group.first; group; group = group->id.next) {
640                 for (go = group->gobject.first; go; go = go->next) {
641                         if (go->ob) {
642                                 go->ob->flag |= OB_FROMGROUP;
643                         }
644                 }
645         }
646
647         /* sort baselist */
648         DAG_scene_sort(bmain, scene);
649         
650         /* ensure dags are built for sets */
651         for (sce = scene->set; sce; sce = sce->set)
652                 if (sce->theDag == NULL)
653                         DAG_scene_sort(bmain, sce);
654
655         /* copy layers and flags from bases to objects */
656         for (base = scene->base.first; base; base = base->next) {
657                 ob = base->object;
658                 ob->lay = base->lay;
659                 
660                 /* group patch... */
661                 base->flag &= ~(OB_FROMGROUP);
662                 flag = ob->flag & (OB_FROMGROUP);
663                 base->flag |= flag;
664                 
665                 /* not too nice... for recovering objects with lost data */
666                 //if (ob->pose == NULL) base->flag &= ~OB_POSEMODE;
667                 ob->flag = base->flag;
668         }
669         /* no full animation update, this to enable render code to work (render code calls own animation updates) */
670 }
671
672 /* called from creator.c */
673 Scene *BKE_scene_set_name(Main *bmain, const char *name)
674 {
675         Scene *sce = (Scene *)BKE_libblock_find_name(ID_SCE, name);
676         if (sce) {
677                 BKE_scene_set_background(bmain, sce);
678                 printf("Scene switch: '%s' in file: '%s'\n", name, G.main->name);
679                 return sce;
680         }
681
682         printf("Can't find scene: '%s' in file: '%s'\n", name, G.main->name);
683         return NULL;
684 }
685
686 void BKE_scene_unlink(Main *bmain, Scene *sce, Scene *newsce)
687 {
688         Scene *sce1;
689         bScreen *sc;
690
691         /* check all sets */
692         for (sce1 = bmain->scene.first; sce1; sce1 = sce1->id.next)
693                 if (sce1->set == sce)
694                         sce1->set = NULL;
695         
696         /* check all sequences */
697         BKE_sequencer_clear_scene_in_allseqs(bmain, sce);
698
699         /* check render layer nodes in other scenes */
700         clear_scene_in_nodes(bmain, sce);
701         
702         /* al screens */
703         for (sc = bmain->screen.first; sc; sc = sc->id.next)
704                 if (sc->scene == sce)
705                         sc->scene = newsce;
706
707         BKE_libblock_free(&bmain->scene, sce);
708 }
709
710 /* used by metaballs
711  * doesnt return the original duplicated object, only dupli's
712  */
713 int BKE_scene_base_iter_next(Scene **scene, int val, Base **base, Object **ob)
714 {
715         static ListBase *duplilist = NULL;
716         static DupliObject *dupob;
717         static int fase = F_START, in_next_object = 0;
718         int run_again = 1;
719         
720         /* init */
721         if (val == 0) {
722                 fase = F_START;
723                 dupob = NULL;
724                 
725                 /* XXX particle systems with metas+dupligroups call this recursively */
726                 /* see bug #18725 */
727                 if (in_next_object) {
728                         printf("ERROR: Metaball generation called recursively, not supported\n");
729                         
730                         return F_ERROR;
731                 }
732         }
733         else {
734                 in_next_object = 1;
735                 
736                 /* run_again is set when a duplilist has been ended */
737                 while (run_again) {
738                         run_again = 0;
739
740                         /* the first base */
741                         if (fase == F_START) {
742                                 *base = (*scene)->base.first;
743                                 if (*base) {
744                                         *ob = (*base)->object;
745                                         fase = F_SCENE;
746                                 }
747                                 else {
748                                         /* exception: empty scene */
749                                         while ((*scene)->set) {
750                                                 (*scene) = (*scene)->set;
751                                                 if ((*scene)->base.first) {
752                                                         *base = (*scene)->base.first;
753                                                         *ob = (*base)->object;
754                                                         fase = F_SCENE;
755                                                         break;
756                                                 }
757                                         }
758                                 }
759                         }
760                         else {
761                                 if (*base && fase != F_DUPLI) {
762                                         *base = (*base)->next;
763                                         if (*base) *ob = (*base)->object;
764                                         else {
765                                                 if (fase == F_SCENE) {
766                                                         /* (*scene) is finished, now do the set */
767                                                         while ((*scene)->set) {
768                                                                 (*scene) = (*scene)->set;
769                                                                 if ((*scene)->base.first) {
770                                                                         *base = (*scene)->base.first;
771                                                                         *ob = (*base)->object;
772                                                                         break;
773                                                                 }
774                                                         }
775                                                 }
776                                         }
777                                 }
778                         }
779                         
780                         if (*base == NULL) fase = F_START;
781                         else {
782                                 if (fase != F_DUPLI) {
783                                         if ( (*base)->object->transflag & OB_DUPLI) {
784                                                 /* groups cannot be duplicated for mballs yet, 
785                                                  * this enters eternal loop because of 
786                                                  * makeDispListMBall getting called inside of group_duplilist */
787                                                 if ((*base)->object->dup_group == NULL) {
788                                                         duplilist = object_duplilist((*scene), (*base)->object, FALSE);
789                                                         
790                                                         dupob = duplilist->first;
791
792                                                         if (!dupob)
793                                                                 free_object_duplilist(duplilist);
794                                                 }
795                                         }
796                                 }
797                                 /* handle dupli's */
798                                 if (dupob) {
799                                         
800                                         copy_m4_m4(dupob->ob->obmat, dupob->mat);
801                                         
802                                         (*base)->flag |= OB_FROMDUPLI;
803                                         *ob = dupob->ob;
804                                         fase = F_DUPLI;
805                                         
806                                         dupob = dupob->next;
807                                 }
808                                 else if (fase == F_DUPLI) {
809                                         fase = F_SCENE;
810                                         (*base)->flag &= ~OB_FROMDUPLI;
811                                         
812                                         for (dupob = duplilist->first; dupob; dupob = dupob->next) {
813                                                 copy_m4_m4(dupob->ob->obmat, dupob->omat);
814                                         }
815                                         
816                                         free_object_duplilist(duplilist);
817                                         duplilist = NULL;
818                                         run_again = 1;
819                                 }
820                         }
821                 }
822         }
823
824 #if 0
825         if (ob && *ob) {
826                 printf("Scene: '%s', '%s'\n", (*scene)->id.name + 2, (*ob)->id.name + 2);
827         }
828 #endif
829
830         /* reset recursion test */
831         in_next_object = 0;
832         
833         return fase;
834 }
835
836 Object *BKE_scene_camera_find(Scene *sc)
837 {
838         Base *base;
839         
840         for (base = sc->base.first; base; base = base->next)
841                 if (base->object->type == OB_CAMERA)
842                         return base->object;
843
844         return NULL;
845 }
846
847 #ifdef DURIAN_CAMERA_SWITCH
848 Object *BKE_scene_camera_switch_find(Scene *scene)
849 {
850         TimeMarker *m;
851         int cfra = scene->r.cfra;
852         int frame = -(MAXFRAME + 1);
853         Object *camera = NULL;
854
855         for (m = scene->markers.first; m; m = m->next) {
856                 if (m->camera && (m->camera->restrictflag & OB_RESTRICT_RENDER) == 0 && (m->frame <= cfra) && (m->frame > frame)) {
857                         camera = m->camera;
858                         frame = m->frame;
859
860                         if (frame == cfra)
861                                 break;
862
863                 }
864         }
865         return camera;
866 }
867 #endif
868
869 int BKE_scene_camera_switch_update(Scene *scene)
870 {
871 #ifdef DURIAN_CAMERA_SWITCH
872         Object *camera = BKE_scene_camera_switch_find(scene);
873         if (camera) {
874                 scene->camera = camera;
875                 return 1;
876         }
877 #else
878         (void)scene;
879 #endif
880         return 0;
881 }
882
883 char *BKE_scene_find_marker_name(Scene *scene, int frame)
884 {
885         ListBase *markers = &scene->markers;
886         TimeMarker *m1, *m2;
887
888         /* search through markers for match */
889         for (m1 = markers->first, m2 = markers->last; m1 && m2; m1 = m1->next, m2 = m2->prev) {
890                 if (m1->frame == frame)
891                         return m1->name;
892
893                 if (m1 == m2)
894                         break;
895
896                 if (m2->frame == frame)
897                         return m2->name;
898         }
899
900         return NULL;
901 }
902
903 /* return the current marker for this frame,
904  * we can have more then 1 marker per frame, this just returns the first :/ */
905 char *BKE_scene_find_last_marker_name(Scene *scene, int frame)
906 {
907         TimeMarker *marker, *best_marker = NULL;
908         int best_frame = -MAXFRAME * 2;
909         for (marker = scene->markers.first; marker; marker = marker->next) {
910                 if (marker->frame == frame) {
911                         return marker->name;
912                 }
913
914                 if (marker->frame > best_frame && marker->frame < frame) {
915                         best_marker = marker;
916                         best_frame = marker->frame;
917                 }
918         }
919
920         return best_marker ? best_marker->name : NULL;
921 }
922
923
924 Base *BKE_scene_base_add(Scene *sce, Object *ob)
925 {
926         Base *b = MEM_callocN(sizeof(*b), "BKE_scene_base_add");
927         BLI_addhead(&sce->base, b);
928
929         b->object = ob;
930         b->flag = ob->flag;
931         b->lay = ob->lay;
932
933         return b;
934 }
935
936 void BKE_scene_base_deselect_all(Scene *sce)
937 {
938         Base *b;
939
940         for (b = sce->base.first; b; b = b->next) {
941                 b->flag &= ~SELECT;
942                 b->object->flag = b->flag;
943         }
944 }
945
946 void BKE_scene_base_select(Scene *sce, Base *selbase)
947 {
948         selbase->flag |= SELECT;
949         selbase->object->flag = selbase->flag;
950
951         sce->basact = selbase;
952 }
953
954 /* checks for cycle, returns 1 if it's all OK */
955 int BKE_scene_validate_setscene(Main *bmain, Scene *sce)
956 {
957         Scene *scene;
958         int a, totscene;
959         
960         if (sce->set == NULL) return 1;
961         
962         totscene = 0;
963         for (scene = bmain->scene.first; scene; scene = scene->id.next)
964                 totscene++;
965         
966         for (a = 0, scene = sce; scene->set; scene = scene->set, a++) {
967                 /* more iterations than scenes means we have a cycle */
968                 if (a > totscene) {
969                         /* the tested scene gets zero'ed, that's typically current scene */
970                         sce->set = NULL;
971                         return 0;
972                 }
973         }
974
975         return 1;
976 }
977
978 /* This function is needed to cope with fractional frames - including two Blender rendering features
979  * mblur (motion blur that renders 'subframes' and blurs them together), and fields rendering. 
980  */
981 float BKE_scene_frame_get(Scene *scene)
982 {
983         return BKE_scene_frame_get_from_ctime(scene, scene->r.cfra);
984 }
985
986 /* This function is used to obtain arbitrary fractional frames */
987 float BKE_scene_frame_get_from_ctime(Scene *scene, const float frame)
988 {
989         float ctime = frame;
990         ctime += scene->r.subframe;
991         ctime *= scene->r.framelen;
992         
993         return ctime;
994 }
995
996 /* drivers support/hacks 
997  *  - this method is called from scene_update_tagged_recursive(), so gets included in viewport + render
998  *      - these are always run since the depsgraph can't handle non-object data
999  *      - these happen after objects are all done so that we can read in their final transform values,
1000  *        though this means that objects can't refer to scene info for guidance...
1001  */
1002 static void scene_update_drivers(Main *UNUSED(bmain), Scene *scene)
1003 {
1004         float ctime = BKE_scene_frame_get(scene);
1005         
1006         /* scene itself */
1007         if (scene->adt && scene->adt->drivers.first) {
1008                 BKE_animsys_evaluate_animdata(scene, &scene->id, scene->adt, ctime, ADT_RECALC_DRIVERS);
1009         }
1010
1011         /* world */
1012         /* TODO: what about world textures? but then those have nodes too... */
1013         if (scene->world) {
1014                 ID *wid = (ID *)scene->world;
1015                 AnimData *adt = BKE_animdata_from_id(wid);
1016                 
1017                 if (adt && adt->drivers.first)
1018                         BKE_animsys_evaluate_animdata(scene, wid, adt, ctime, ADT_RECALC_DRIVERS);
1019         }
1020         
1021         /* nodes */
1022         if (scene->nodetree) {
1023                 ID *nid = (ID *)scene->nodetree;
1024                 AnimData *adt = BKE_animdata_from_id(nid);
1025                 
1026                 if (adt && adt->drivers.first)
1027                         BKE_animsys_evaluate_animdata(scene, nid, adt, ctime, ADT_RECALC_DRIVERS);
1028         }
1029 }
1030
1031 static void scene_update_tagged_recursive(Main *bmain, Scene *scene, Scene *scene_parent)
1032 {
1033         Base *base;
1034         
1035         
1036         scene->customdata_mask = scene_parent->customdata_mask;
1037
1038         /* sets first, we allow per definition current scene to have
1039          * dependencies on sets, but not the other way around. */
1040         if (scene->set)
1041                 scene_update_tagged_recursive(bmain, scene->set, scene_parent);
1042         
1043         /* scene objects */
1044         for (base = scene->base.first; base; base = base->next) {
1045                 Object *ob = base->object;
1046                 
1047                 BKE_object_handle_update(scene_parent, ob);
1048                 
1049                 if (ob->dup_group && (ob->transflag & OB_DUPLIGROUP))
1050                         group_handle_recalc_and_update(scene_parent, ob, ob->dup_group);
1051                         
1052                 /* always update layer, so that animating layers works (joshua july 2010) */
1053                 /* XXX commented out, this has depsgraph issues anyway - and this breaks setting scenes
1054                  * (on scene-set, the base-lay is copied to ob-lay (ton nov 2012) */
1055                 // base->lay = ob->lay;
1056         }
1057         
1058         /* scene drivers... */
1059         scene_update_drivers(bmain, scene);
1060
1061         /* update sound system animation */
1062         sound_update_scene(scene);
1063
1064         /* update masking curves */
1065         BKE_mask_update_scene(bmain, scene, FALSE);
1066 }
1067
1068 /* this is called in main loop, doing tagged updates before redraw */
1069 void BKE_scene_update_tagged(Main *bmain, Scene *scene)
1070 {
1071         /* keep this first */
1072         BLI_callback_exec(bmain, &scene->id, BLI_CB_EVT_SCENE_UPDATE_PRE);
1073
1074         /* flush recalc flags to dependencies */
1075         DAG_ids_flush_tagged(bmain);
1076
1077         /* removed calls to quick_cache, see pointcache.c */
1078         
1079         /* clear "LIB_DOIT" flag from all materials, to prevent infinite recursion problems later 
1080          * when trying to find materials with drivers that need evaluating [#32017] 
1081          */
1082         tag_main_idcode(bmain, ID_MA, FALSE);
1083         tag_main_idcode(bmain, ID_LA, FALSE);
1084
1085         /* update all objects: drivers, matrices, displists, etc. flags set
1086          * by depgraph or manual, no layer check here, gets correct flushed
1087          *
1088          * in the future this should handle updates for all datablocks, not
1089          * only objects and scenes. - brecht */
1090         scene_update_tagged_recursive(bmain, scene, scene);
1091
1092         /* extra call here to recalc scene animation (for sequencer) */
1093         {
1094                 AnimData *adt = BKE_animdata_from_id(&scene->id);
1095                 float ctime = BKE_scene_frame_get(scene);
1096                 
1097                 if (adt && (adt->recalc & ADT_RECALC_ANIM))
1098                         BKE_animsys_evaluate_animdata(scene, &scene->id, adt, ctime, 0);
1099         }
1100         
1101         /* notify editors and python about recalc */
1102         BLI_callback_exec(bmain, &scene->id, BLI_CB_EVT_SCENE_UPDATE_POST);
1103         DAG_ids_check_recalc(bmain, scene, FALSE);
1104
1105         /* clear recalc flags */
1106         DAG_ids_clear_recalc(bmain);
1107 }
1108
1109 /* applies changes right away, does all sets too */
1110 void BKE_scene_update_for_newframe(Main *bmain, Scene *sce, unsigned int lay)
1111 {
1112         float ctime = BKE_scene_frame_get(sce);
1113         Scene *sce_iter;
1114
1115         /* keep this first */
1116         BLI_callback_exec(bmain, &sce->id, BLI_CB_EVT_FRAME_CHANGE_PRE);
1117         BLI_callback_exec(bmain, &sce->id, BLI_CB_EVT_SCENE_UPDATE_PRE);
1118
1119         sound_set_cfra(sce->r.cfra);
1120         
1121         /* clear animation overrides */
1122         /* XXX TODO... */
1123
1124         for (sce_iter = sce; sce_iter; sce_iter = sce_iter->set) {
1125                 if (sce_iter->theDag == NULL)
1126                         DAG_scene_sort(bmain, sce_iter);
1127         }
1128
1129         /* flush recalc flags to dependencies, if we were only changing a frame
1130          * this would not be necessary, but if a user or a script has modified
1131          * some datablock before BKE_scene_update_tagged was called, we need the flush */
1132         DAG_ids_flush_tagged(bmain);
1133
1134         /* Following 2 functions are recursive
1135          * so don't call within 'scene_update_tagged_recursive' */
1136         DAG_scene_update_flags(bmain, sce, lay, TRUE);   // only stuff that moves or needs display still
1137
1138         BKE_mask_evaluate_all_masks(bmain, ctime, TRUE);
1139
1140         /* All 'standard' (i.e. without any dependencies) animation is handled here,
1141          * with an 'local' to 'macro' order of evaluation. This should ensure that
1142          * settings stored nestled within a hierarchy (i.e. settings in a Texture block
1143          * can be overridden by settings from Scene, which owns the Texture through a hierarchy
1144          * such as Scene->World->MTex/Texture) can still get correctly overridden.
1145          */
1146         BKE_animsys_evaluate_all_animation(bmain, sce, ctime);
1147         /*...done with recusrive funcs */
1148
1149         /* clear "LIB_DOIT" flag from all materials, to prevent infinite recursion problems later 
1150          * when trying to find materials with drivers that need evaluating [#32017] 
1151          */
1152         tag_main_idcode(bmain, ID_MA, FALSE);
1153         tag_main_idcode(bmain, ID_LA, FALSE);
1154
1155         /* BKE_object_handle_update() on all objects, groups and sets */
1156         scene_update_tagged_recursive(bmain, sce, sce);
1157
1158         /* notify editors and python about recalc */
1159         BLI_callback_exec(bmain, &sce->id, BLI_CB_EVT_SCENE_UPDATE_POST);
1160         BLI_callback_exec(bmain, &sce->id, BLI_CB_EVT_FRAME_CHANGE_POST);
1161
1162         DAG_ids_check_recalc(bmain, sce, TRUE);
1163
1164         /* clear recalc flags */
1165         DAG_ids_clear_recalc(bmain);
1166 }
1167
1168 /* return default layer, also used to patch old files */
1169 SceneRenderLayer *BKE_scene_add_render_layer(Scene *sce, const char *name)
1170 {
1171         SceneRenderLayer *srl;
1172
1173         if (!name)
1174                 name = "RenderLayer";
1175
1176         srl = MEM_callocN(sizeof(SceneRenderLayer), "new render layer");
1177         BLI_strncpy(srl->name, name, sizeof(srl->name));
1178         BLI_uniquename(&sce->r.layers, srl, "RenderLayer", '.', offsetof(SceneRenderLayer, name), sizeof(srl->name));
1179         BLI_addtail(&sce->r.layers, srl);
1180
1181         /* note, this is also in render, pipeline.c, to make layer when scenedata doesnt have it */
1182         srl->lay = (1 << 20) - 1;
1183         srl->layflag = 0x7FFF;   /* solid ztra halo edge strand */
1184         srl->passflag = SCE_PASS_COMBINED | SCE_PASS_Z;
1185
1186         return srl;
1187 }
1188
1189 int BKE_scene_remove_render_layer(Main *bmain, Scene *scene, SceneRenderLayer *srl)
1190 {
1191         const int act = BLI_findindex(&scene->r.layers, srl);
1192         Scene *sce;
1193
1194         if (act == -1) {
1195                 return 0;
1196         }
1197         else if ( (scene->r.layers.first == scene->r.layers.last) &&
1198                   (scene->r.layers.first == srl))
1199         {
1200                 /* ensure 1 layer is kept */
1201                 return 0;
1202         }
1203
1204         BLI_remlink(&scene->r.layers, srl);
1205         MEM_freeN(srl);
1206
1207         scene->r.actlay = 0;
1208
1209         for (sce = bmain->scene.first; sce; sce = sce->id.next) {
1210                 if (sce->nodetree) {
1211                         bNode *node;
1212                         for (node = sce->nodetree->nodes.first; node; node = node->next) {
1213                                 if (node->type == CMP_NODE_R_LAYERS && (Scene *)node->id == scene) {
1214                                         if (node->custom1 == act)
1215                                                 node->custom1 = 0;
1216                                         else if (node->custom1 > act)
1217                                                 node->custom1--;
1218                                 }
1219                         }
1220                 }
1221         }
1222
1223         return 1;
1224 }
1225
1226 /* render simplification */
1227
1228 int get_render_subsurf_level(RenderData *r, int lvl)
1229 {
1230         if (r->mode & R_SIMPLIFY)
1231                 return min_ii(r->simplify_subsurf, lvl);
1232         else
1233                 return lvl;
1234 }
1235
1236 int get_render_child_particle_number(RenderData *r, int num)
1237 {
1238         if (r->mode & R_SIMPLIFY)
1239                 return (int)(r->simplify_particles * num);
1240         else
1241                 return num;
1242 }
1243
1244 int get_render_shadow_samples(RenderData *r, int samples)
1245 {
1246         if ((r->mode & R_SIMPLIFY) && samples > 0)
1247                 return min_ii(r->simplify_shadowsamples, samples);
1248         else
1249                 return samples;
1250 }
1251
1252 float get_render_aosss_error(RenderData *r, float error)
1253 {
1254         if (r->mode & R_SIMPLIFY)
1255                 return ((1.0f - r->simplify_aosss) * 10.0f + 1.0f) * error;
1256         else
1257                 return error;
1258 }
1259
1260 /* helper function for the SETLOOPER macro */
1261 Base *_setlooper_base_step(Scene **sce_iter, Base *base)
1262 {
1263         if (base && base->next) {
1264                 /* common case, step to the next */
1265                 return base->next;
1266         }
1267         else if (base == NULL && (*sce_iter)->base.first) {
1268                 /* first time looping, return the scenes first base */
1269                 return (Base *)(*sce_iter)->base.first;
1270         }
1271         else {
1272                 /* reached the end, get the next base in the set */
1273                 while ((*sce_iter = (*sce_iter)->set)) {
1274                         base = (Base *)(*sce_iter)->base.first;
1275                         if (base) {
1276                                 return base;
1277                         }
1278                 }
1279         }
1280
1281         return NULL;
1282 }
1283
1284 int BKE_scene_use_new_shading_nodes(Scene *scene)
1285 {
1286         RenderEngineType *type = RE_engines_find(scene->r.engine);
1287         return (type && type->flag & RE_USE_SHADING_NODES);
1288 }
1289
1290 void BKE_scene_base_flag_to_objects(struct Scene *scene)
1291 {
1292         Base *base = scene->base.first;
1293
1294         while (base) {
1295                 base->object->flag = base->flag;
1296                 base = base->next;
1297         }
1298 }
1299
1300 void BKE_scene_base_flag_from_objects(struct Scene *scene)
1301 {
1302         Base *base = scene->base.first;
1303
1304         while (base) {
1305                 base->flag = base->object->flag;
1306                 base = base->next;
1307         }
1308 }
1309
1310 void BKE_scene_disable_color_management(Scene *scene)
1311 {
1312         ColorManagedDisplaySettings *display_settings = &scene->display_settings;
1313         ColorManagedViewSettings *view_settings = &scene->view_settings;
1314         const char *view;
1315         const char *none_display_name;
1316
1317         none_display_name = IMB_colormanagement_display_get_none_name();
1318
1319         BLI_strncpy(display_settings->display_device, none_display_name, sizeof(display_settings->display_device));
1320
1321         view = IMB_colormanagement_view_get_default_name(display_settings->display_device);
1322
1323         if (view) {
1324                 BLI_strncpy(view_settings->view_transform, view, sizeof(view_settings->view_transform));
1325         }
1326 }
1327
1328 int BKE_scene_check_color_management_enabled(const Scene *scene)
1329 {
1330         return strcmp(scene->display_settings.display_device, "None") != 0;
1331 }