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