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