Color Management, Stage 2: Switch color pipeline to use OpenColorIO
[blender.git] / source / blender / blenkernel / intern / scene.c
index 9e21538b5ca3ba4030ed8ccc5cebecee3684e113..2dec72560a228f0d69bbbb539ce00b34cf8a9450 100644 (file)
 
 #include "BKE_anim.h"
 #include "BKE_animsys.h"
+#include "BKE_colortools.h"
 #include "BKE_depsgraph.h"
 #include "BKE_global.h"
 #include "BKE_group.h"
 #include "BKE_idprop.h"
 #include "BKE_library.h"
 #include "BKE_main.h"
+#include "BKE_mask.h"
 #include "BKE_node.h"
 #include "BKE_object.h"
 #include "BKE_paint.h"
@@ -75,6 +77,8 @@
 
 #include "RE_engine.h"
 
+#include "IMB_colormanagement.h"
+
 //XXX #include "BIF_previewrender.h"
 //XXX #include "BIF_editseq.h"
 
@@ -118,20 +122,22 @@ Scene *BKE_scene_copy(Scene *sce, int type)
        
        if (type == SCE_COPY_EMPTY) {
                ListBase lb;
-               scen= BKE_scene_add(sce->id.name+2);
+               scen = BKE_scene_add(sce->id.name + 2);
                
-               lb= scen->r.layers;
-               scen->r= sce->r;
-               scen->r.layers= lb;
-               scen->unit= sce->unit;
-               scen->physics_settings= sce->physics_settings;
-               scen->gm= sce->gm;
-               scen->audio= sce->audio;
+               lb = scen->r.layers;
+               scen->r = sce->r;
+               scen->r.layers = lb;
+               scen->unit = sce->unit;
+               scen->physics_settings = sce->physics_settings;
+               scen->gm = sce->gm;
+               scen->audio = sce->audio;
 
                MEM_freeN(scen->toolsettings);
        }
        else {
-               scen= BKE_libblock_copy(&sce->id);
+               ImageFormatData *im_format, *im_formatn;
+
+               scen = BKE_libblock_copy(&sce->id);
                BLI_duplicatelist(&(scen->base), &(sce->base));
                
                clear_id_newpoins();
@@ -140,11 +146,11 @@ Scene *BKE_scene_copy(Scene *sce, int type)
                id_us_plus((ID *)scen->set);
                id_us_plus((ID *)scen->gm.dome.warptext);
 
-               scen->ed= NULL;
-               scen->theDag= NULL;
-               scen->obedit= NULL;
-               scen->stats= NULL;
-               scen->fps_info= NULL;
+               scen->ed = NULL;
+               scen->theDag = NULL;
+               scen->obedit = NULL;
+               scen->stats = NULL;
+               scen->fps_info = NULL;
 
                BLI_duplicatelist(&(scen->markers), &(sce->markers));
                BLI_duplicatelist(&(scen->transform_spaces), &(sce->transform_spaces));
@@ -152,48 +158,56 @@ Scene *BKE_scene_copy(Scene *sce, int type)
                BKE_keyingsets_copy(&(scen->keyingsets), &(sce->keyingsets));
 
                if (sce->nodetree) {
-                       scen->nodetree= ntreeCopyTree(sce->nodetree); /* copies actions */
+                       /* ID's are managed on both copy and switch */
+                       scen->nodetree = ntreeCopyTree(sce->nodetree);
                        ntreeSwitchID(scen->nodetree, &sce->id, &scen->id);
                }
 
-               obase= sce->base.first;
-               base= scen->base.first;
+               obase = sce->base.first;
+               base = scen->base.first;
                while (base) {
                        id_us_plus(&base->object->id);
-                       if (obase==sce->basact) scen->basact= base;
+                       if (obase == sce->basact) scen->basact = base;
        
-                       obase= obase->next;
-                       base= base->next;
+                       obase = obase->next;
+                       base = base->next;
                }
+
+               /* copy color management settings */
+               im_format = &sce->r.im_format;
+               im_formatn = &scen->r.im_format;
+
+               BKE_color_managed_display_settings_copy(&scen->display_settings, &sce->display_settings);
+               BKE_color_managed_view_settings_copy(&scen->view_settings, &sce->view_settings);
        }
 
        /* tool settings */
-       scen->toolsettings= MEM_dupallocN(sce->toolsettings);
+       scen->toolsettings = MEM_dupallocN(sce->toolsettings);
 
-       ts= scen->toolsettings;
+       ts = scen->toolsettings;
        if (ts) {
                if (ts->vpaint) {
-                       ts->vpaint= MEM_dupallocN(ts->vpaint);
-                       ts->vpaint->paintcursor= NULL;
-                       ts->vpaint->vpaint_prev= NULL;
-                       ts->vpaint->wpaint_prev= NULL;
-                       copy_paint(&ts->vpaint->paint, &ts->vpaint->paint);
+                       ts->vpaint = MEM_dupallocN(ts->vpaint);
+                       ts->vpaint->paintcursor = NULL;
+                       ts->vpaint->vpaint_prev = NULL;
+                       ts->vpaint->wpaint_prev = NULL;
+                       BKE_paint_copy(&ts->vpaint->paint, &ts->vpaint->paint);
                }
                if (ts->wpaint) {
-                       ts->wpaint= MEM_dupallocN(ts->wpaint);
-                       ts->wpaint->paintcursor= NULL;
-                       ts->wpaint->vpaint_prev= NULL;
-                       ts->wpaint->wpaint_prev= NULL;
-                       copy_paint(&ts->wpaint->paint, &ts->wpaint->paint);
+                       ts->wpaint = MEM_dupallocN(ts->wpaint);
+                       ts->wpaint->paintcursor = NULL;
+                       ts->wpaint->vpaint_prev = NULL;
+                       ts->wpaint->wpaint_prev = NULL;
+                       BKE_paint_copy(&ts->wpaint->paint, &ts->wpaint->paint);
                }
                if (ts->sculpt) {
-                       ts->sculpt= MEM_dupallocN(ts->sculpt);
-                       copy_paint(&ts->sculpt->paint, &ts->sculpt->paint);
+                       ts->sculpt = MEM_dupallocN(ts->sculpt);
+                       BKE_paint_copy(&ts->sculpt->paint, &ts->sculpt->paint);
                }
 
-               copy_paint(&ts->imapaint.paint, &ts->imapaint.paint);
-               ts->imapaint.paintcursor= NULL;
-               ts->particle.paintcursor= NULL;
+               BKE_paint_copy(&ts->imapaint.paint, &ts->imapaint.paint);
+               ts->imapaint.paintcursor = NULL;
+               ts->particle.paintcursor = NULL;
        }
        
        /* make a private copy of the avicodecdata */
@@ -210,7 +224,7 @@ Scene *BKE_scene_copy(Scene *sce, int type)
        }
        
        if (sce->r.ffcodecdata.properties) { /* intentionally check scen not sce. */
-               scen->r.ffcodecdata.properties= IDP_CopyProperty(sce->r.ffcodecdata.properties);
+               scen->r.ffcodecdata.properties = IDP_CopyProperty(sce->r.ffcodecdata.properties);
        }
 
        /* NOTE: part of SCE_COPY_LINK_DATA and SCE_COPY_FULL operations
@@ -229,14 +243,14 @@ Scene *BKE_scene_copy(Scene *sce, int type)
                BKE_copy_animdata_id_action((ID *)scen);
                if (scen->world) {
                        id_us_plus((ID *)scen->world);
-                       scen->world= BKE_world_copy(scen->world);
+                       scen->world = BKE_world_copy(scen->world);
                        BKE_copy_animdata_id_action((ID *)scen->world);
                }
 
                if (sce->ed) {
-                       scen->ed= MEM_callocN(sizeof(Editing), "addseq");
-                       scen->ed->seqbasep= &scen->ed->seqbase;
-                       seqbase_dupli_recursive(sce, scen, &scen->ed->seqbase, &sce->ed->seqbase, SEQ_DUPE_ALL);
+                       scen->ed = MEM_callocN(sizeof(Editing), "addseq");
+                       scen->ed->seqbasep = &scen->ed->seqbase;
+                       BKE_sequence_base_dupli_recursive(sce, scen, &scen->ed->seqbase, &sce->ed->seqbase, SEQ_DUPE_ALL);
                }
        }
 
@@ -248,25 +262,25 @@ void BKE_scene_free(Scene *sce)
 {
        Base *base;
 
-       base= sce->base.first;
+       base = sce->base.first;
        while (base) {
                base->object->id.us--;
-               base= base->next;
+               base = base->next;
        }
        /* do not free objects! */
        
        if (sce->gpd) {
-#if 0   // removed since this can be invalid memory when freeing everything
-               // since the grease pencil data is freed before the scene.
-               // since grease pencil data is not (yet?), shared between objects
-               // its probably safe not to do this, some save and reload will free this.
+#if 0   /* removed since this can be invalid memory when freeing everything */
+               /* since the grease pencil data is freed before the scene.
+                * since grease pencil data is not (yet?), shared between objects
+                * its probably safe not to do this, some save and reload will free this. */
                sce->gpd->id.us--;
 #endif
-               sce->gpd= NULL;
+               sce->gpd = NULL;
        }
 
        BLI_freelistN(&sce->base);
-       seq_free_editing(sce);
+       BKE_sequencer_editing_free(sce);
 
        BKE_free_animdata((ID *)sce);
        BKE_keyingsets_free(&sce->keyingsets);
@@ -293,22 +307,22 @@ void BKE_scene_free(Scene *sce)
        
        if (sce->toolsettings) {
                if (sce->toolsettings->vpaint) {
-                       free_paint(&sce->toolsettings->vpaint->paint);
+                       BKE_paint_free(&sce->toolsettings->vpaint->paint);
                        MEM_freeN(sce->toolsettings->vpaint);
                }
                if (sce->toolsettings->wpaint) {
-                       free_paint(&sce->toolsettings->wpaint->paint);
+                       BKE_paint_free(&sce->toolsettings->wpaint->paint);
                        MEM_freeN(sce->toolsettings->wpaint);
                }
                if (sce->toolsettings->sculpt) {
-                       free_paint(&sce->toolsettings->sculpt->paint);
+                       BKE_paint_free(&sce->toolsettings->sculpt->paint);
                        MEM_freeN(sce->toolsettings->sculpt);
                }
                if (sce->toolsettings->uvsculpt) {
-                       free_paint(&sce->toolsettings->uvsculpt->paint);
+                       BKE_paint_free(&sce->toolsettings->uvsculpt->paint);
                        MEM_freeN(sce->toolsettings->uvsculpt);
                }
-               free_paint(&sce->toolsettings->imapaint.paint);
+               BKE_paint_free(&sce->toolsettings->imapaint.paint);
 
                MEM_freeN(sce->toolsettings);
                sce->toolsettings = NULL;       
@@ -330,78 +344,87 @@ void BKE_scene_free(Scene *sce)
                MEM_freeN(sce->fps_info);
 
        sound_destroy_scene(sce);
+
+       BKE_color_managed_view_settings_free(&sce->view_settings);
 }
 
 Scene *BKE_scene_add(const char *name)
 {
-       Main *bmain= G.main;
+       Main *bmain = G.main;
        Scene *sce;
        ParticleEditSettings *pset;
        int a;
 
-       sce= BKE_libblock_alloc(&bmain->scene, ID_SCE, name);
-       sce->lay= sce->layact= 1;
+       sce = BKE_libblock_alloc(&bmain->scene, ID_SCE, name);
+       sce->lay = sce->layact = 1;
        
-       sce->r.mode= R_GAMMA|R_OSA|R_SHADOW|R_SSS|R_ENVMAP|R_RAYTRACE;
-       sce->r.cfra= 1;
-       sce->r.sfra= 1;
-       sce->r.efra= 250;
-       sce->r.frame_step= 1;
-       sce->r.xsch= 1920;
-       sce->r.ysch= 1080;
-       sce->r.xasp= 1;
-       sce->r.yasp= 1;
-       sce->r.xparts= 8;
-       sce->r.yparts= 8;
-       sce->r.mblur_samples= 1;
-       sce->r.filtertype= R_FILTER_MITCH;
-       sce->r.size= 50;
-
-       sce->r.im_format.planes= R_IMF_PLANES_RGB;
-       sce->r.im_format.imtype= R_IMF_IMTYPE_PNG;
-       sce->r.im_format.quality= 90;
-
-       sce->r.displaymode= R_OUTPUT_AREA;
-       sce->r.framapto= 100;
-       sce->r.images= 100;
-       sce->r.framelen= 1.0;
-       sce->r.blurfac= 0.5;
-       sce->r.frs_sec= 24;
-       sce->r.frs_sec_base= 1;
-       sce->r.edgeint= 10;
+       sce->r.mode = R_GAMMA | R_OSA | R_SHADOW | R_SSS | R_ENVMAP | R_RAYTRACE;
+       sce->r.cfra = 1;
+       sce->r.sfra = 1;
+       sce->r.efra = 250;
+       sce->r.frame_step = 1;
+       sce->r.xsch = 1920;
+       sce->r.ysch = 1080;
+       sce->r.xasp = 1;
+       sce->r.yasp = 1;
+       sce->r.xparts = 8;
+       sce->r.yparts = 8;
+       sce->r.mblur_samples = 1;
+       sce->r.filtertype = R_FILTER_MITCH;
+       sce->r.size = 50;
+
+       sce->r.im_format.planes = R_IMF_PLANES_RGB;
+       sce->r.im_format.imtype = R_IMF_IMTYPE_PNG;
+       sce->r.im_format.quality = 90;
+
+       sce->r.displaymode = R_OUTPUT_AREA;
+       sce->r.framapto = 100;
+       sce->r.images = 100;
+       sce->r.framelen = 1.0;
+       sce->r.blurfac = 0.5;
+       sce->r.frs_sec = 24;
+       sce->r.frs_sec_base = 1;
+       sce->r.edgeint = 10;
        sce->r.ocres = 128;
+
+       /* OCIO_TODO: for forwards compatibiliy only, so if no tonecurve are used,
+        *            images would look in the same way as in current blender
+        *
+        *            perhaps at some point should be completely deprecated?
+        */
        sce->r.color_mgt_flag |= R_COLOR_MANAGEMENT;
-       sce->r.gauss= 1.0;
+
+       sce->r.gauss = 1.0;
        
        /* deprecated but keep for upwards compat */
-       sce->r.postgamma= 1.0;
-       sce->r.posthue= 0.0;
-       sce->r.postsat= 1.0;
-       
-       sce->r.bake_mode= 1;    /* prevent to include render stuff here */
-       sce->r.bake_filter= 2;
-       sce->r.bake_osa= 5;
-       sce->r.bake_flag= R_BAKE_CLEAR;
-       sce->r.bake_normal_space= R_BAKE_SPACE_TANGENT;
-       sce->r.scemode= R_DOCOMP|R_DOSEQ|R_EXTENSION;
-       sce->r.stamp= R_STAMP_TIME|R_STAMP_FRAME|R_STAMP_DATE|R_STAMP_CAMERA|R_STAMP_SCENE|R_STAMP_FILENAME|R_STAMP_RENDERTIME;
-       sce->r.stamp_font_id= 12;
-       sce->r.fg_stamp[0]= sce->r.fg_stamp[1]= sce->r.fg_stamp[2]= 0.8f;
-       sce->r.fg_stamp[3]= 1.0f;
-       sce->r.bg_stamp[0]= sce->r.bg_stamp[1]= sce->r.bg_stamp[2]= 0.0f;
-       sce->r.bg_stamp[3]= 0.25f;
+       sce->r.postgamma = 1.0;
+       sce->r.posthue = 0.0;
+       sce->r.postsat = 1.0;
+
+       sce->r.bake_mode = 1;    /* prevent to include render stuff here */
+       sce->r.bake_filter = 2;
+       sce->r.bake_osa = 5;
+       sce->r.bake_flag = R_BAKE_CLEAR;
+       sce->r.bake_normal_space = R_BAKE_SPACE_TANGENT;
+       sce->r.scemode = R_DOCOMP | R_DOSEQ | R_EXTENSION;
+       sce->r.stamp = R_STAMP_TIME | R_STAMP_FRAME | R_STAMP_DATE | R_STAMP_CAMERA | R_STAMP_SCENE | R_STAMP_FILENAME | R_STAMP_RENDERTIME;
+       sce->r.stamp_font_id = 12;
+       sce->r.fg_stamp[0] = sce->r.fg_stamp[1] = sce->r.fg_stamp[2] = 0.8f;
+       sce->r.fg_stamp[3] = 1.0f;
+       sce->r.bg_stamp[0] = sce->r.bg_stamp[1] = sce->r.bg_stamp[2] = 0.0f;
+       sce->r.bg_stamp[3] = 0.25f;
        sce->r.raytrace_options = R_RAYTRACE_USE_INSTANCES;
 
-       sce->r.seq_prev_type= OB_SOLID;
-       sce->r.seq_rend_type= OB_SOLID;
-       sce->r.seq_flag= R_SEQ_GL_PREV;
+       sce->r.seq_prev_type = OB_SOLID;
+       sce->r.seq_rend_type = OB_SOLID;
+       sce->r.seq_flag = R_SEQ_GL_PREV;
 
-       sce->r.threads= 1;
+       sce->r.threads = 1;
 
-       sce->r.simplify_subsurf= 6;
-       sce->r.simplify_particles= 1.0f;
-       sce->r.simplify_shadowsamples= 16;
-       sce->r.simplify_aosss= 1.0f;
+       sce->r.simplify_subsurf = 6;
+       sce->r.simplify_particles = 1.0f;
+       sce->r.simplify_shadowsamples = 16;
+       sce->r.simplify_aosss = 1.0f;
 
        sce->r.border.xmin = 0.0f;
        sce->r.border.ymin = 0.0f;
@@ -409,7 +432,7 @@ Scene *BKE_scene_add(const char *name)
        sce->r.border.ymax = 1.0f;
        
        sce->toolsettings = MEM_callocN(sizeof(struct ToolSettings), "Tool Settings Struct");
-       sce->toolsettings->cornertype=1;
+       sce->toolsettings->cornertype = 1;
        sce->toolsettings->degr = 90; 
        sce->toolsettings->step = 9;
        sce->toolsettings->turn = 1;                            
@@ -423,25 +446,25 @@ Scene *BKE_scene_add(const char *name)
        sce->toolsettings->uvcalc_mapdir = 1;
        sce->toolsettings->uvcalc_mapalign = 1;
        sce->toolsettings->unwrapper = 1;
-       sce->toolsettings->select_thresh= 0.01f;
+       sce->toolsettings->select_thresh = 0.01f;
        sce->toolsettings->jointrilimit = 0.8f;
 
-       sce->toolsettings->selectmode= SCE_SELECT_VERTEX;
-       sce->toolsettings->uv_selectmode= UV_SELECT_VERTEX;
-       sce->toolsettings->normalsize= 0.1;
-       sce->toolsettings->autokey_mode= U.autokey_mode;
+       sce->toolsettings->selectmode = SCE_SELECT_VERTEX;
+       sce->toolsettings->uv_selectmode = UV_SELECT_VERTEX;
+       sce->toolsettings->normalsize = 0.1;
+       sce->toolsettings->autokey_mode = U.autokey_mode;
 
        sce->toolsettings->skgen_resolution = 100;
-       sce->toolsettings->skgen_threshold_internal     = 0.01f;
-       sce->toolsettings->skgen_threshold_external     = 0.01f;
-       sce->toolsettings->skgen_angle_limit                    = 45.0f;
-       sce->toolsettings->skgen_length_ratio                   = 1.3f;
-       sce->toolsettings->skgen_length_limit                   = 1.5f;
-       sce->toolsettings->skgen_correlation_limit              = 0.98f;
-       sce->toolsettings->skgen_symmetry_limit                 = 0.1f;
+       sce->toolsettings->skgen_threshold_internal     = 0.01f;
+       sce->toolsettings->skgen_threshold_external     = 0.01f;
+       sce->toolsettings->skgen_angle_limit            = 45.0f;
+       sce->toolsettings->skgen_length_ratio           = 1.3f;
+       sce->toolsettings->skgen_length_limit           = 1.5f;
+       sce->toolsettings->skgen_correlation_limit      = 0.98f;
+       sce->toolsettings->skgen_symmetry_limit         = 0.1f;
        sce->toolsettings->skgen_postpro = SKGEN_SMOOTH;
        sce->toolsettings->skgen_postpro_passes = 1;
-       sce->toolsettings->skgen_options = SKGEN_FILTER_INTERNAL|SKGEN_FILTER_EXTERNAL|SKGEN_FILTER_SMART|SKGEN_HARMONIC|SKGEN_SUB_CORRELATION|SKGEN_STICK_TO_EMBEDDING;
+       sce->toolsettings->skgen_options = SKGEN_FILTER_INTERNAL | SKGEN_FILTER_EXTERNAL | SKGEN_FILTER_SMART | SKGEN_HARMONIC | SKGEN_SUB_CORRELATION | SKGEN_STICK_TO_EMBEDDING;
        sce->toolsettings->skgen_subdivisions[0] = SKGEN_SUB_CORRELATION;
        sce->toolsettings->skgen_subdivisions[1] = SKGEN_SUB_LENGTH;
        sce->toolsettings->skgen_subdivisions[2] = SKGEN_SUB_ANGLE;
@@ -455,22 +478,22 @@ Scene *BKE_scene_add(const char *name)
 
        sce->unit.scale_length = 1.0f;
 
-       pset= &sce->toolsettings->particle;
-       pset->flag= PE_KEEP_LENGTHS|PE_LOCK_FIRST|PE_DEFLECT_EMITTER|PE_AUTO_VELOCITY;
-       pset->emitterdist= 0.25f;
-       pset->totrekey= 5;
-       pset->totaddkey= 5;
-       pset->brushtype= PE_BRUSH_NONE;
-       pset->draw_step= 2;
-       pset->fade_frames= 2;
-       pset->selectmode= SCE_SELECT_PATH;
-       for (a=0; a<PE_TOT_BRUSH; a++) {
-               pset->brush[a].strength= 0.5;
-               pset->brush[a].size= 50;
-               pset->brush[a].step= 10;
-               pset->brush[a].count= 10;
+       pset = &sce->toolsettings->particle;
+       pset->flag = PE_KEEP_LENGTHS | PE_LOCK_FIRST | PE_DEFLECT_EMITTER | PE_AUTO_VELOCITY;
+       pset->emitterdist = 0.25f;
+       pset->totrekey = 5;
+       pset->totaddkey = 5;
+       pset->brushtype = PE_BRUSH_NONE;
+       pset->draw_step = 2;
+       pset->fade_frames = 2;
+       pset->selectmode = SCE_SELECT_PATH;
+       for (a = 0; a < PE_TOT_BRUSH; a++) {
+               pset->brush[a].strength = 0.5;
+               pset->brush[a].size = 50;
+               pset->brush[a].step = 10;
+               pset->brush[a].count = 10;
        }
-       pset->brush[PE_BRUSH_CUT].strength= 100;
+       pset->brush[PE_BRUSH_CUT].strength = 100;
 
        sce->r.ffcodecdata.audio_mixrate = 44100;
        sce->r.ffcodecdata.audio_volume = 1.0f;
@@ -486,8 +509,8 @@ Scene *BKE_scene_add(const char *name)
 
        BLI_strncpy(sce->r.pic, U.renderdir, sizeof(sce->r.pic));
 
-       BLI_init_rctf(&sce->r.safety, 0.1f, 0.9f, 0.1f, 0.9f);
-       sce->r.osa= 8;
+       BLI_rctf_init(&sce->r.safety, 0.1f, 0.9f, 0.1f, 0.9f);
+       sce->r.osa = 8;
 
        /* note; in header_info.c the scene copy happens..., if you add more to renderdata it has to be checked there */
        BKE_scene_add_render_layer(sce, NULL);
@@ -503,29 +526,32 @@ Scene *BKE_scene_add(const char *name)
        sce->gm.dome.resbuf = 1.0f;
        sce->gm.dome.tilt = 0;
 
-       sce->gm.xplay= 640;
-       sce->gm.yplay= 480;
-       sce->gm.freqplay= 60;
-       sce->gm.depth= 32;
+       sce->gm.xplay = 640;
+       sce->gm.yplay = 480;
+       sce->gm.freqplay = 60;
+       sce->gm.depth = 32;
 
-       sce->gm.gravity= 9.8f;
-       sce->gm.physicsEngine= WOPHY_BULLET;
+       sce->gm.gravity = 9.8f;
+       sce->gm.physicsEngine = WOPHY_BULLET;
        sce->gm.mode = 32; //XXX ugly harcoding, still not sure we should drop mode. 32 == 1 << 5 == use_occlusion_culling 
        sce->gm.occlusionRes = 128;
        sce->gm.ticrate = 60;
        sce->gm.maxlogicstep = 5;
        sce->gm.physubstep = 1;
        sce->gm.maxphystep = 5;
+       sce->gm.lineardeactthreshold = 0.8f;
+       sce->gm.angulardeactthreshold = 1.0f;
+       sce->gm.deactivationtime = 0.0f;
 
        sce->gm.flag = GAME_DISPLAY_LISTS;
        sce->gm.matmode = GAME_MAT_MULTITEX;
 
-       sce->gm.obstacleSimulation= OBSTSIMULATION_NONE;
+       sce->gm.obstacleSimulation = OBSTSIMULATION_NONE;
        sce->gm.levelHeight = 2.f;
 
        sce->gm.recastData.cellsize = 0.3f;
        sce->gm.recastData.cellheight = 0.2f;
-       sce->gm.recastData.agentmaxslope = M_PI/2;
+       sce->gm.recastData.agentmaxslope = M_PI / 2;
        sce->gm.recastData.agentmaxclimb = 0.9f;
        sce->gm.recastData.agentheight = 2.0f;
        sce->gm.recastData.agentradius = 0.6f;
@@ -541,19 +567,15 @@ Scene *BKE_scene_add(const char *name)
 
        sound_create_scene(sce);
 
+       BKE_color_managed_display_settings_init(&sce->display_settings);
+       BKE_color_managed_view_settings_init(&sce->view_settings);
+
        return sce;
 }
 
 Base *BKE_scene_base_find(Scene *scene, Object *ob)
 {
-       Base *base;
-       
-       base= scene->base.first;
-       while (base) {
-               if (base->object == ob) return base;
-               base= base->next;
-       }
-       return NULL;
+       return BLI_findptr(&scene->base, ob, offsetof(Base, object));
 }
 
 void BKE_scene_set_background(Main *bmain, Scene *scene)
@@ -570,18 +592,18 @@ void BKE_scene_set_background(Main *bmain, Scene *scene)
        
        /* can happen when switching modes in other scenes */
        if (scene->obedit && !(scene->obedit->mode & OB_MODE_EDIT))
-               scene->obedit= NULL;
+               scene->obedit = NULL;
 
        /* deselect objects (for dataselect) */
-       for (ob= bmain->object.first; ob; ob= ob->id.next)
-               ob->flag &= ~(SELECT|OB_FROMGROUP);
+       for (ob = bmain->object.first; ob; ob = ob->id.next)
+               ob->flag &= ~(SELECT | OB_FROMGROUP);
 
        /* group flags again */
-       for (group= bmain->group.first; group; group= group->id.next) {
-               go= group->gobject.first;
-               while (go) {
-                       if (go->ob) go->ob->flag |= OB_FROMGROUP;
-                       go= go->next;
+       for (group = bmain->group.first; group; group = group->id.next) {
+               for (go = group->gobject.first; go; go = go->next) {
+                       if (go->ob) {
+                               go->ob->flag |= OB_FROMGROUP;
+                       }
                }
        }
 
@@ -589,25 +611,23 @@ void BKE_scene_set_background(Main *bmain, Scene *scene)
        DAG_scene_sort(bmain, scene);
        
        /* ensure dags are built for sets */
-       for (sce= scene->set; sce; sce= sce->set)
-               if (sce->theDag==NULL)
+       for (sce = scene->set; sce; sce = sce->set)
+               if (sce->theDag == NULL)
                        DAG_scene_sort(bmain, sce);
 
        /* copy layers and flags from bases to objects */
-       for (base= scene->base.first; base; base= base->next) {
-               ob= base->object;
-               ob->lay= base->lay;
+       for (base = scene->base.first; base; base = base->next) {
+               ob = base->object;
+               ob->lay = base->lay;
                
                /* group patch... */
                base->flag &= ~(OB_FROMGROUP);
-               flag= ob->flag & (OB_FROMGROUP);
+               flag = ob->flag & (OB_FROMGROUP);
                base->flag |= flag;
                
                /* not too nice... for recovering objects with lost data */
-               //if (ob->pose==NULL) base->flag &= ~OB_POSEMODE;
-               ob->flag= base->flag;
-               
-               ob->ctime= -1234567.0;  /* force ipo to be calculated later */
+               //if (ob->pose == NULL) base->flag &= ~OB_POSEMODE;
+               ob->flag = base->flag;
        }
        /* no full animation update, this to enable render code to work (render code calls own animation updates) */
 }
@@ -615,7 +635,7 @@ void BKE_scene_set_background(Main *bmain, Scene *scene)
 /* called from creator.c */
 Scene *BKE_scene_set_name(Main *bmain, const char *name)
 {
-       Scene *sce= (Scene *)find_id("SC", name);
+       Scene *sce = (Scene *)BKE_libblock_find_name(ID_SCE, name);
        if (sce) {
                BKE_scene_set_background(bmain, sce);
                printf("Scene switch: '%s' in file: '%s'\n", name, G.main->name);
@@ -632,20 +652,20 @@ void BKE_scene_unlink(Main *bmain, Scene *sce, Scene *newsce)
        bScreen *sc;
 
        /* check all sets */
-       for (sce1= bmain->scene.first; sce1; sce1= sce1->id.next)
+       for (sce1 = bmain->scene.first; sce1; sce1 = sce1->id.next)
                if (sce1->set == sce)
-                       sce1->set= NULL;
+                       sce1->set = NULL;
        
        /* check all sequences */
-       clear_scene_in_allseqs(bmain, sce);
+       BKE_sequencer_clear_scene_in_allseqs(bmain, sce);
 
        /* check render layer nodes in other scenes */
        clear_scene_in_nodes(bmain, sce);
        
        /* al screens */
-       for (sc= bmain->screen.first; sc; sc= sc->id.next)
+       for (sc = bmain->screen.first; sc; sc = sc->id.next)
                if (sc->scene == sce)
-                       sc->scene= newsce;
+                       sc->scene = newsce;
 
        BKE_libblock_free(&bmain->scene, sce);
 }
@@ -655,15 +675,15 @@ void BKE_scene_unlink(Main *bmain, Scene *sce, Scene *newsce)
  */
 int BKE_scene_base_iter_next(Scene **scene, int val, Base **base, Object **ob)
 {
-       static ListBase *duplilist= NULL;
+       static ListBase *duplilist = NULL;
        static DupliObject *dupob;
-       static int fase= F_START, in_next_object= 0;
-       int run_again=1;
+       static int fase = F_START, in_next_object = 0;
+       int run_again = 1;
        
        /* init */
-       if (val==0) {
-               fase= F_START;
-               dupob= NULL;
+       if (val == 0) {
+               fase = F_START;
+               dupob = NULL;
                
                /* XXX particle systems with metas+dupligroups call this recursively */
                /* see bug #18725 */
@@ -674,44 +694,44 @@ int BKE_scene_base_iter_next(Scene **scene, int val, Base **base, Object **ob)
                }
        }
        else {
-               in_next_object= 1;
+               in_next_object = 1;
                
                /* run_again is set when a duplilist has been ended */
                while (run_again) {
-                       run_again= 0;
+                       run_again = 0;
 
                        /* the first base */
-                       if (fase==F_START) {
-                               *base= (*scene)->base.first;
+                       if (fase == F_START) {
+                               *base = (*scene)->base.first;
                                if (*base) {
-                                       *ob= (*base)->object;
-                                       fase= F_SCENE;
+                                       *ob = (*base)->object;
+                                       fase = F_SCENE;
                                }
                                else {
                                        /* exception: empty scene */
                                        while ((*scene)->set) {
-                                               (*scene)= (*scene)->set;
+                                               (*scene) = (*scene)->set;
                                                if ((*scene)->base.first) {
-                                                       *base= (*scene)->base.first;
-                                                       *ob= (*base)->object;
-                                                       fase= F_SCENE;
+                                                       *base = (*scene)->base.first;
+                                                       *ob = (*base)->object;
+                                                       fase = F_SCENE;
                                                        break;
                                                }
                                        }
                                }
                        }
                        else {
-                               if (*base && fase!=F_DUPLI) {
-                                       *base= (*base)->next;
-                                       if (*base) *ob= (*base)->object;
+                               if (*base && fase != F_DUPLI) {
+                                       *base = (*base)->next;
+                                       if (*base) *ob = (*base)->object;
                                        else {
-                                               if (fase==F_SCENE) {
+                                               if (fase == F_SCENE) {
                                                        /* (*scene) is finished, now do the set */
                                                        while ((*scene)->set) {
-                                                               (*scene)= (*scene)->set;
+                                                               (*scene) = (*scene)->set;
                                                                if ((*scene)->base.first) {
-                                                                       *base= (*scene)->base.first;
-                                                                       *ob= (*base)->object;
+                                                                       *base = (*scene)->base.first;
+                                                                       *ob = (*base)->object;
                                                                        break;
                                                                }
                                                        }
@@ -720,17 +740,17 @@ int BKE_scene_base_iter_next(Scene **scene, int val, Base **base, Object **ob)
                                }
                        }
                        
-                       if (*base == NULL) fase= F_START;
+                       if (*base == NULL) fase = F_START;
                        else {
-                               if (fase!=F_DUPLI) {
+                               if (fase != F_DUPLI) {
                                        if ( (*base)->object->transflag & OB_DUPLI) {
                                                /* groups cannot be duplicated for mballs yet, 
                                                 * this enters eternal loop because of 
                                                 * makeDispListMBall getting called inside of group_duplilist */
                                                if ((*base)->object->dup_group == NULL) {
-                                                       duplilist= object_duplilist((*scene), (*base)->object);
+                                                       duplilist = object_duplilist((*scene), (*base)->object);
                                                        
-                                                       dupob= duplilist->first;
+                                                       dupob = duplilist->first;
 
                                                        if (!dupob)
                                                                free_object_duplilist(duplilist);
@@ -743,22 +763,22 @@ int BKE_scene_base_iter_next(Scene **scene, int val, Base **base, Object **ob)
                                        copy_m4_m4(dupob->ob->obmat, dupob->mat);
                                        
                                        (*base)->flag |= OB_FROMDUPLI;
-                                       *ob= dupob->ob;
-                                       fase= F_DUPLI;
+                                       *ob = dupob->ob;
+                                       fase = F_DUPLI;
                                        
-                                       dupob= dupob->next;
+                                       dupob = dupob->next;
                                }
-                               else if (fase==F_DUPLI) {
-                                       fase= F_SCENE;
+                               else if (fase == F_DUPLI) {
+                                       fase = F_SCENE;
                                        (*base)->flag &= ~OB_FROMDUPLI;
                                        
-                                       for (dupob= duplilist->first; dupob; dupob= dupob->next) {
+                                       for (dupob = duplilist->first; dupob; dupob = dupob->next) {
                                                copy_m4_m4(dupob->ob->obmat, dupob->omat);
                                        }
                                        
                                        free_object_duplilist(duplilist);
-                                       duplilist= NULL;
-                                       run_again= 1;
+                                       duplilist = NULL;
+                                       run_again = 1;
                                }
                        }
                }
@@ -766,12 +786,12 @@ int BKE_scene_base_iter_next(Scene **scene, int val, Base **base, Object **ob)
 
 #if 0
        if (ob && *ob) {
-               printf("Scene: '%s', '%s'\n", (*scene)->id.name+2, (*ob)->id.name+2);
+               printf("Scene: '%s', '%s'\n", (*scene)->id.name + 2, (*ob)->id.name + 2);
        }
 #endif
 
        /* reset recursion test */
-       in_next_object= 0;
+       in_next_object = 0;
        
        return fase;
 }
@@ -780,8 +800,8 @@ Object *BKE_scene_camera_find(Scene *sc)
 {
        Base *base;
        
-       for (base= sc->base.first; base; base= base->next)
-               if (base->object->type==OB_CAMERA)
+       for (base = sc->base.first; base; base = base->next)
+               if (base->object->type == OB_CAMERA)
                        return base->object;
 
        return NULL;
@@ -793,12 +813,12 @@ Object *BKE_scene_camera_switch_find(Scene *scene)
        TimeMarker *m;
        int cfra = scene->r.cfra;
        int frame = -(MAXFRAME + 1);
-       Object *camera= NULL;
+       Object *camera = NULL;
 
-       for (m= scene->markers.first; m; m= m->next) {
-               if (m->camera && (m->camera->restrictflag & OB_RESTRICT_RENDER)==0 && (m->frame <= cfra) && (m->frame > frame)) {
-                       camera= m->camera;
-                       frame= m->frame;
+       for (m = scene->markers.first; m; m = m->next) {
+               if (m->camera && (m->camera->restrictflag & OB_RESTRICT_RENDER) == 0 && (m->frame <= cfra) && (m->frame > frame)) {
+                       camera = m->camera;
+                       frame = m->frame;
 
                        if (frame == cfra)
                                break;
@@ -812,9 +832,9 @@ Object *BKE_scene_camera_switch_find(Scene *scene)
 int BKE_scene_camera_switch_update(Scene *scene)
 {
 #ifdef DURIAN_CAMERA_SWITCH
-       Object *camera= BKE_scene_camera_switch_find(scene);
+       Object *camera = BKE_scene_camera_switch_find(scene);
        if (camera) {
-               scene->camera= camera;
+               scene->camera = camera;
                return 1;
        }
 #else
@@ -825,18 +845,18 @@ int BKE_scene_camera_switch_update(Scene *scene)
 
 char *BKE_scene_find_marker_name(Scene *scene, int frame)
 {
-       ListBase *markers= &scene->markers;
+       ListBase *markers = &scene->markers;
        TimeMarker *m1, *m2;
 
        /* search through markers for match */
-       for (m1=markers->first, m2=markers->last; m1 && m2; m1=m1->next, m2=m2->prev) {
-               if (m1->frame==frame)
+       for (m1 = markers->first, m2 = markers->last; m1 && m2; m1 = m1->next, m2 = m2->prev) {
+               if (m1->frame == frame)
                        return m1->name;
 
                if (m1 == m2)
                        break;
 
-               if (m2->frame==frame)
+               if (m2->frame == frame)
                        return m2->name;
        }
 
@@ -848,13 +868,13 @@ char *BKE_scene_find_marker_name(Scene *scene, int frame)
 char *BKE_scene_find_last_marker_name(Scene *scene, int frame)
 {
        TimeMarker *marker, *best_marker = NULL;
-       int best_frame = -MAXFRAME*2;
-       for (marker= scene->markers.first; marker; marker= marker->next) {
-               if (marker->frame==frame) {
+       int best_frame = -MAXFRAME * 2;
+       for (marker = scene->markers.first; marker; marker = marker->next) {
+               if (marker->frame == frame) {
                        return marker->name;
                }
 
-               if ( marker->frame > best_frame && marker->frame < frame) {
+               if (marker->frame > best_frame && marker->frame < frame) {
                        best_marker = marker;
                        best_frame = marker->frame;
                }
@@ -866,12 +886,12 @@ char *BKE_scene_find_last_marker_name(Scene *scene, int frame)
 
 Base *BKE_scene_base_add(Scene *sce, Object *ob)
 {
-       Base *b= MEM_callocN(sizeof(*b), "BKE_scene_base_add");
+       Base *b = MEM_callocN(sizeof(*b), "BKE_scene_base_add");
        BLI_addhead(&sce->base, b);
 
-       b->object= ob;
-       b->flag= ob->flag;
-       b->lay= ob->lay;
+       b->object = ob;
+       b->flag = ob->flag;
+       b->lay = ob->lay;
 
        return b;
 }
@@ -880,18 +900,18 @@ void BKE_scene_base_deselect_all(Scene *sce)
 {
        Base *b;
 
-       for (b= sce->base.first; b; b= b->next) {
-               b->flag&= ~SELECT;
-               b->object->flag= b->flag;
+       for (b = sce->base.first; b; b = b->next) {
+               b->flag &= ~SELECT;
+               b->object->flag = b->flag;
        }
 }
 
 void BKE_scene_base_select(Scene *sce, Base *selbase)
 {
        selbase->flag |= SELECT;
-       selbase->object->flag= selbase->flag;
+       selbase->object->flag = selbase->flag;
 
-       sce->basact= selbase;
+       sce->basact = selbase;
 }
 
 /* checks for cycle, returns 1 if it's all OK */
@@ -900,17 +920,17 @@ int BKE_scene_validate_setscene(Main *bmain, Scene *sce)
        Scene *scene;
        int a, totscene;
        
-       if (sce->set==NULL) return 1;
+       if (sce->set == NULL) return 1;
        
-       totscene= 0;
-       for (scene= bmain->scene.first; scene; scene= scene->id.next)
+       totscene = 0;
+       for (scene = bmain->scene.first; scene; scene = scene->id.next)
                totscene++;
        
-       for (a=0, scene=sce; scene->set; scene=scene->set, a++) {
+       for (a = 0, scene = sce; scene->set; scene = scene->set, a++) {
                /* more iterations than scenes means we have a cycle */
                if (a > totscene) {
                        /* the tested scene gets zero'ed, that's typically current scene */
-                       sce->set= NULL;
+                       sce->set = NULL;
                        return 0;
                }
        }
@@ -937,7 +957,7 @@ float BKE_scene_frame_get_from_ctime(Scene *scene, const float frame)
 }
 
 /* drivers support/hacks 
- *     - this method is called from scene_update_tagged_recursive(), so gets included in viewport + render
+ *  - this method is called from scene_update_tagged_recursive(), so gets included in viewport + render
  *     - these are always run since the depsgraph can't handle non-object data
  *     - these happen after objects are all done so that we can read in their final transform values,
  *       though this means that objects can't refer to scene info for guidance...
@@ -950,12 +970,12 @@ static void scene_update_drivers(Main *UNUSED(bmain), Scene *scene)
        if (scene->adt && scene->adt->drivers.first) {
                BKE_animsys_evaluate_animdata(scene, &scene->id, scene->adt, ctime, ADT_RECALC_DRIVERS);
        }
-       
+
        /* world */
-       // TODO: what about world textures? but then those have nodes too...
+       /* TODO: what about world textures? but then those have nodes too... */
        if (scene->world) {
                ID *wid = (ID *)scene->world;
-               AnimData *adt= BKE_animdata_from_id(wid);
+               AnimData *adt = BKE_animdata_from_id(wid);
                
                if (adt && adt->drivers.first)
                        BKE_animsys_evaluate_animdata(scene, wid, adt, ctime, ADT_RECALC_DRIVERS);
@@ -964,7 +984,7 @@ static void scene_update_drivers(Main *UNUSED(bmain), Scene *scene)
        /* nodes */
        if (scene->nodetree) {
                ID *nid = (ID *)scene->nodetree;
-               AnimData *adt= BKE_animdata_from_id(nid);
+               AnimData *adt = BKE_animdata_from_id(nid);
                
                if (adt && adt->drivers.first)
                        BKE_animsys_evaluate_animdata(scene, nid, adt, ctime, ADT_RECALC_DRIVERS);
@@ -976,7 +996,7 @@ static void scene_update_tagged_recursive(Main *bmain, Scene *scene, Scene *scen
        Base *base;
        
        
-       scene->customdata_mask= scene_parent->customdata_mask;
+       scene->customdata_mask = scene_parent->customdata_mask;
 
        /* sets first, we allow per definition current scene to have
         * dependencies on sets, but not the other way around. */
@@ -984,8 +1004,8 @@ static void scene_update_tagged_recursive(Main *bmain, Scene *scene, Scene *scen
                scene_update_tagged_recursive(bmain, scene->set, scene_parent);
        
        /* scene objects */
-       for (base= scene->base.first; base; base= base->next) {
-               Object *ob= base->object;
+       for (base = scene->base.first; base; base = base->next) {
+               Object *ob = base->object;
                
                BKE_object_handle_update(scene_parent, ob);
                
@@ -993,7 +1013,7 @@ static void scene_update_tagged_recursive(Main *bmain, Scene *scene, Scene *scen
                        group_handle_recalc_and_update(scene_parent, ob, ob->dup_group);
                        
                /* always update layer, so that animating layers works */
-               base->lay= ob->lay;
+               base->lay = ob->lay;
        }
        
        /* scene drivers... */
@@ -1001,6 +1021,9 @@ static void scene_update_tagged_recursive(Main *bmain, Scene *scene, Scene *scen
 
        /* update sound system animation */
        sound_update_scene(scene);
+
+       /* update masking curves */
+       BKE_mask_update_scene(bmain, scene, FALSE);
 }
 
 /* this is called in main loop, doing tagged updates before redraw */
@@ -1012,7 +1035,12 @@ void BKE_scene_update_tagged(Main *bmain, Scene *scene)
        /* flush recalc flags to dependencies */
        DAG_ids_flush_tagged(bmain);
 
-       scene->physics_settings.quick_cache_step= 0;
+       scene->physics_settings.quick_cache_step = 0;
+       
+       /* clear "LIB_DOIT" flag from all materials, to prevent infinite recursion problems later 
+        * when trying to find materials with drivers that need evaluating [#32017] 
+        */
+       tag_main_idcode(bmain, ID_MA, FALSE);
 
        /* update all objects: drivers, matrices, displists, etc. flags set
         * by depgraph or manual, no layer check here, gets correct flushed
@@ -1023,7 +1051,7 @@ void BKE_scene_update_tagged(Main *bmain, Scene *scene)
 
        /* extra call here to recalc scene animation (for sequencer) */
        {
-               AnimData *adt= BKE_animdata_from_id(&scene->id);
+               AnimData *adt = BKE_animdata_from_id(&scene->id);
                float ctime = BKE_scene_frame_get(scene);
                
                if (adt && (adt->recalc & ADT_RECALC_ANIM))
@@ -1055,10 +1083,10 @@ void BKE_scene_update_for_newframe(Main *bmain, Scene *sce, unsigned int lay)
        sound_set_cfra(sce->r.cfra);
        
        /* clear animation overrides */
-       // XXX TODO...
+       /* XXX TODO... */
 
-       for (sce_iter= sce; sce_iter; sce_iter= sce_iter->set) {
-               if (sce_iter->theDag==NULL)
+       for (sce_iter = sce; sce_iter; sce_iter = sce_iter->set) {
+               if (sce_iter->theDag == NULL)
                        DAG_scene_sort(bmain, sce_iter);
        }
 
@@ -1071,6 +1099,8 @@ void BKE_scene_update_for_newframe(Main *bmain, Scene *sce, unsigned int lay)
         * so don't call within 'scene_update_tagged_recursive' */
        DAG_scene_update_flags(bmain, sce, lay, TRUE);   // only stuff that moves or needs display still
 
+       BKE_mask_evaluate_all_masks(bmain, ctime, TRUE);
+
        /* All 'standard' (i.e. without any dependencies) animation is handled here,
         * with an 'local' to 'macro' order of evaluation. This should ensure that
         * settings stored nestled within a hierarchy (i.e. settings in a Texture block
@@ -1099,24 +1129,24 @@ SceneRenderLayer *BKE_scene_add_render_layer(Scene *sce, const char *name)
        SceneRenderLayer *srl;
 
        if (!name)
-               name= "RenderLayer";
+               name = "RenderLayer";
 
-       srl= MEM_callocN(sizeof(SceneRenderLayer), "new render layer");
+       srl = MEM_callocN(sizeof(SceneRenderLayer), "new render layer");
        BLI_strncpy(srl->name, name, sizeof(srl->name));
        BLI_uniquename(&sce->r.layers, srl, "RenderLayer", '.', offsetof(SceneRenderLayer, name), sizeof(srl->name));
        BLI_addtail(&sce->r.layers, srl);
 
        /* note, this is also in render, pipeline.c, to make layer when scenedata doesnt have it */
-       srl->lay= (1<<20) -1;
-       srl->layflag= 0x7FFF;   /* solid ztra halo edge strand */
-       srl->passflag= SCE_PASS_COMBINED|SCE_PASS_Z;
+       srl->lay = (1 << 20) - 1;
+       srl->layflag = 0x7FFF;   /* solid ztra halo edge strand */
+       srl->passflag = SCE_PASS_COMBINED | SCE_PASS_Z;
 
        return srl;
 }
 
 int BKE_scene_remove_render_layer(Main *bmain, Scene *scene, SceneRenderLayer *srl)
 {
-       const int act= BLI_findindex(&scene->r.layers, srl);
+       const int act = BLI_findindex(&scene->r.layers, srl);
        Scene *sce;
 
        if (act == -1) {
@@ -1132,16 +1162,16 @@ int BKE_scene_remove_render_layer(Main *bmain, Scene *scene, SceneRenderLayer *s
        BLI_remlink(&scene->r.layers, srl);
        MEM_freeN(srl);
 
-       scene->r.actlay= 0;
+       scene->r.actlay = 0;
 
        for (sce = bmain->scene.first; sce; sce = sce->id.next) {
                if (sce->nodetree) {
                        bNode *node;
                        for (node = sce->nodetree->nodes.first; node; node = node->next) {
-                               if (node->type==CMP_NODE_R_LAYERS && (Scene*)node->id==scene) {
-                                       if (node->custom1==act)
-                                               node->custom1= 0;
-                                       else if (node->custom1>act)
+                               if (node->type == CMP_NODE_R_LAYERS && (Scene *)node->id == scene) {
+                                       if (node->custom1 == act)
+                                               node->custom1 = 0;
+                                       else if (node->custom1 > act)
                                                node->custom1--;
                                }
                        }
@@ -1164,7 +1194,7 @@ int get_render_subsurf_level(RenderData *r, int lvl)
 int get_render_child_particle_number(RenderData *r, int num)
 {
        if (r->mode & R_SIMPLIFY)
-               return (int)(r->simplify_particles*num);
+               return (int)(r->simplify_particles * num);
        else
                return num;
 }
@@ -1180,7 +1210,7 @@ int get_render_shadow_samples(RenderData *r, int samples)
 float get_render_aosss_error(RenderData *r, float error)
 {
        if (r->mode & R_SIMPLIFY)
-               return ((1.0f-r->simplify_aosss)*10.0f + 1.0f)*error;
+               return ((1.0f - r->simplify_aosss) * 10.0f + 1.0f) * error;
        else
                return error;
 }
@@ -1192,14 +1222,14 @@ Base *_setlooper_base_step(Scene **sce_iter, Base *base)
                /* common case, step to the next */
                return base->next;
        }
-       else if (base==NULL && (*sce_iter)->base.first) {
+       else if (base == NULL && (*sce_iter)->base.first) {
                /* first time looping, return the scenes first base */
                return (Base *)(*sce_iter)->base.first;
        }
        else {
                /* reached the end, get the next base in the set */
-               while ((*sce_iter= (*sce_iter)->set)) {
-                       base= (Base *)(*sce_iter)->base.first;
+               while ((*sce_iter = (*sce_iter)->set)) {
+                       base = (Base *)(*sce_iter)->base.first;
                        if (base) {
                                return base;
                        }
@@ -1211,26 +1241,42 @@ Base *_setlooper_base_step(Scene **sce_iter, Base *base)
 
 int BKE_scene_use_new_shading_nodes(Scene *scene)
 {
-       RenderEngineType *type= RE_engines_find(scene->r.engine);
+       RenderEngineType *type = RE_engines_find(scene->r.engine);
        return (type && type->flag & RE_USE_SHADING_NODES);
 }
 
 void BKE_scene_base_flag_to_objects(struct Scene *scene)
 {
-       Base *base= scene->base.first;
+       Base *base = scene->base.first;
 
        while (base) {
-               base->object->flag= base->flag;
-               base= base->next;
+               base->object->flag = base->flag;
+               base = base->next;
        }
 }
 
 void BKE_scene_base_flag_from_objects(struct Scene *scene)
 {
-       Base *base= scene->base.first;
+       Base *base = scene->base.first;
 
        while (base) {
-               base->flag= base->object->flag;
-               base= base->next;
+               base->flag = base->object->flag;
+               base = base->next;
+       }
+}
+
+void BKE_scene_disable_color_management(Scene *scene)
+{
+       ColorManagedDisplaySettings *display_settings = &scene->display_settings;
+       ColorManagedViewSettings *view_settings = &scene->view_settings;
+       const char *view;
+
+       /* NOTE: None display with Default view should always exist in OCIO configuration, otherwise it wouldn't work as expected */
+       BLI_strncpy(display_settings->display_device, "None", sizeof(display_settings->display_device));
+
+       view = IMB_colormanagement_view_get_default_name(display_settings->display_device);
+
+       if (view) {
+               BLI_strncpy(view_settings->view_transform, view, sizeof(view_settings->view_transform));
        }
 }