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