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