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