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