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