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