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