8f3a99cc05134ed65ed7053ef962645dd29136d6
[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 #include "MEM_guardedalloc.h"
38
39 #include "DNA_anim_types.h"
40 #include "DNA_group_types.h"
41 #include "DNA_linestyle_types.h"
42 #include "DNA_mesh_types.h"
43 #include "DNA_node_types.h"
44 #include "DNA_object_types.h"
45 #include "DNA_rigidbody_types.h"
46 #include "DNA_scene_types.h"
47 #include "DNA_screen_types.h"
48 #include "DNA_sequence_types.h"
49 #include "DNA_space_types.h"
50 #include "DNA_view3d_types.h"
51 #include "DNA_windowmanager_types.h"
52
53 #include "BLI_math.h"
54 #include "BLI_blenlib.h"
55 #include "BLI_utildefines.h"
56 #include "BLI_callbacks.h"
57 #include "BLI_string.h"
58 #include "BLI_threads.h"
59 #include "BLI_task.h"
60
61 #include "BLF_translation.h"
62
63 #include "BKE_anim.h"
64 #include "BKE_animsys.h"
65 #include "BKE_action.h"
66 #include "BKE_armature.h"
67 #include "BKE_colortools.h"
68 #include "BKE_depsgraph.h"
69 #include "BKE_editmesh.h"
70 #include "BKE_fcurve.h"
71 #include "BKE_freestyle.h"
72 #include "BKE_global.h"
73 #include "BKE_gpencil.h"
74 #include "BKE_group.h"
75 #include "BKE_idprop.h"
76 #include "BKE_image.h"
77 #include "BKE_library.h"
78 #include "BKE_linestyle.h"
79 #include "BKE_main.h"
80 #include "BKE_mask.h"
81 #include "BKE_node.h"
82 #include "BKE_object.h"
83 #include "BKE_paint.h"
84 #include "BKE_rigidbody.h"
85 #include "BKE_scene.h"
86 #include "BKE_screen.h"
87 #include "BKE_sequencer.h"
88 #include "BKE_sound.h"
89 #include "BKE_unit.h"
90 #include "BKE_world.h"
91
92 #include "DEG_depsgraph.h"
93
94 #include "RE_engine.h"
95
96 #include "PIL_time.h"
97
98 #include "IMB_colormanagement.h"
99 #include "IMB_imbuf.h"
100
101 #include "bmesh.h"
102
103 #ifdef WIN32
104 #else
105 #  include <sys/time.h>
106 #endif
107
108 const char *RE_engine_id_BLENDER_RENDER = "BLENDER_RENDER";
109 const char *RE_engine_id_BLENDER_GAME = "BLENDER_GAME";
110 const char *RE_engine_id_CYCLES = "CYCLES";
111
112 void free_avicodecdata(AviCodecData *acd)
113 {
114         if (acd) {
115                 if (acd->lpFormat) {
116                         MEM_freeN(acd->lpFormat);
117                         acd->lpFormat = NULL;
118                         acd->cbFormat = 0;
119                 }
120                 if (acd->lpParms) {
121                         MEM_freeN(acd->lpParms);
122                         acd->lpParms = NULL;
123                         acd->cbParms = 0;
124                 }
125         }
126 }
127
128 void free_qtcodecdata(QuicktimeCodecData *qcd)
129 {
130         if (qcd) {
131                 if (qcd->cdParms) {
132                         MEM_freeN(qcd->cdParms);
133                         qcd->cdParms = NULL;
134                         qcd->cdSize = 0;
135                 }
136         }
137 }
138
139 static void remove_sequencer_fcurves(Scene *sce)
140 {
141         AnimData *adt = BKE_animdata_from_id(&sce->id);
142
143         if (adt && adt->action) {
144                 FCurve *fcu, *nextfcu;
145                 
146                 for (fcu = adt->action->curves.first; fcu; fcu = nextfcu) {
147                         nextfcu = fcu->next;
148                         
149                         if ((fcu->rna_path) && strstr(fcu->rna_path, "sequences_all")) {
150                                 action_groups_remove_channel(adt->action, fcu);
151                                 free_fcurve(fcu);
152                         }
153                 }
154         }
155 }
156
157 Scene *BKE_scene_copy(Scene *sce, int type)
158 {
159         Scene *scen;
160         SceneRenderLayer *srl, *new_srl;
161         FreestyleLineSet *lineset;
162         ToolSettings *ts;
163         Base *base, *obase;
164         
165         if (type == SCE_COPY_EMPTY) {
166                 ListBase rl, rv;
167                 /* XXX. main should become an arg */
168                 scen = BKE_scene_add(G.main, sce->id.name + 2);
169                 
170                 rl = scen->r.layers;
171                 rv = scen->r.views;
172                 scen->r = sce->r;
173                 scen->r.layers = rl;
174                 scen->r.actlay = 0;
175                 scen->r.views = rv;
176                 scen->unit = sce->unit;
177                 scen->physics_settings = sce->physics_settings;
178                 scen->gm = sce->gm;
179                 scen->audio = sce->audio;
180
181                 if (sce->id.properties)
182                         scen->id.properties = IDP_CopyProperty(sce->id.properties);
183
184                 MEM_freeN(scen->toolsettings);
185         }
186         else {
187                 scen = BKE_libblock_copy(&sce->id);
188                 BLI_duplicatelist(&(scen->base), &(sce->base));
189                 
190                 BKE_main_id_clear_newpoins(G.main);
191                 
192                 id_us_plus((ID *)scen->world);
193                 id_us_plus((ID *)scen->set);
194                 id_us_plus((ID *)scen->gm.dome.warptext);
195
196                 scen->ed = NULL;
197                 scen->theDag = NULL;
198                 scen->depsgraph = NULL;
199                 scen->obedit = NULL;
200                 scen->stats = NULL;
201                 scen->fps_info = NULL;
202
203                 if (sce->rigidbody_world)
204                         scen->rigidbody_world = BKE_rigidbody_world_copy(sce->rigidbody_world);
205
206                 BLI_duplicatelist(&(scen->markers), &(sce->markers));
207                 BLI_duplicatelist(&(scen->transform_spaces), &(sce->transform_spaces));
208                 BLI_duplicatelist(&(scen->r.layers), &(sce->r.layers));
209                 BLI_duplicatelist(&(scen->r.views), &(sce->r.views));
210                 BKE_keyingsets_copy(&(scen->keyingsets), &(sce->keyingsets));
211
212                 if (sce->nodetree) {
213                         /* ID's are managed on both copy and switch */
214                         scen->nodetree = ntreeCopyTree(sce->nodetree);
215                         ntreeSwitchID(scen->nodetree, &sce->id, &scen->id);
216                 }
217
218                 obase = sce->base.first;
219                 base = scen->base.first;
220                 while (base) {
221                         id_us_plus(&base->object->id);
222                         if (obase == sce->basact) scen->basact = base;
223         
224                         obase = obase->next;
225                         base = base->next;
226                 }
227
228                 /* copy color management settings */
229                 BKE_color_managed_display_settings_copy(&scen->display_settings, &sce->display_settings);
230                 BKE_color_managed_view_settings_copy(&scen->view_settings, &sce->view_settings);
231                 BKE_color_managed_view_settings_copy(&scen->r.im_format.view_settings, &sce->r.im_format.view_settings);
232
233                 BLI_strncpy(scen->sequencer_colorspace_settings.name, sce->sequencer_colorspace_settings.name,
234                             sizeof(scen->sequencer_colorspace_settings.name));
235
236                 /* copy action and remove animation used by sequencer */
237                 BKE_animdata_copy_id_action(&scen->id);
238
239                 if (type != SCE_COPY_FULL)
240                         remove_sequencer_fcurves(scen);
241
242                 /* copy Freestyle settings */
243                 new_srl = scen->r.layers.first;
244                 for (srl = sce->r.layers.first; srl; srl = srl->next) {
245                         BKE_freestyle_config_copy(&new_srl->freestyleConfig, &srl->freestyleConfig);
246                         if (type == SCE_COPY_FULL) {
247                                 for (lineset = new_srl->freestyleConfig.linesets.first; lineset; lineset = lineset->next) {
248                                         if (lineset->linestyle) {
249                                                 id_us_plus((ID *)lineset->linestyle);
250                                                 lineset->linestyle = BKE_linestyle_copy(G.main, lineset->linestyle);
251                                         }
252                                 }
253                         }
254                         new_srl = new_srl->next;
255                 }
256         }
257
258         /* tool settings */
259         scen->toolsettings = MEM_dupallocN(sce->toolsettings);
260
261         ts = scen->toolsettings;
262         if (ts) {
263                 if (ts->vpaint) {
264                         ts->vpaint = MEM_dupallocN(ts->vpaint);
265                         ts->vpaint->paintcursor = NULL;
266                         ts->vpaint->vpaint_prev = NULL;
267                         ts->vpaint->wpaint_prev = NULL;
268                         BKE_paint_copy(&ts->vpaint->paint, &ts->vpaint->paint);
269                 }
270                 if (ts->wpaint) {
271                         ts->wpaint = MEM_dupallocN(ts->wpaint);
272                         ts->wpaint->paintcursor = NULL;
273                         ts->wpaint->vpaint_prev = NULL;
274                         ts->wpaint->wpaint_prev = NULL;
275                         BKE_paint_copy(&ts->wpaint->paint, &ts->wpaint->paint);
276                 }
277                 if (ts->sculpt) {
278                         ts->sculpt = MEM_dupallocN(ts->sculpt);
279                         BKE_paint_copy(&ts->sculpt->paint, &ts->sculpt->paint);
280                 }
281
282                 BKE_paint_copy(&ts->imapaint.paint, &ts->imapaint.paint);
283                 ts->imapaint.paintcursor = NULL;
284                 id_us_plus((ID *)ts->imapaint.stencil);
285                 ts->particle.paintcursor = NULL;
286         }
287         
288         /* make a private copy of the avicodecdata */
289         if (sce->r.avicodecdata) {
290                 scen->r.avicodecdata = MEM_dupallocN(sce->r.avicodecdata);
291                 scen->r.avicodecdata->lpFormat = MEM_dupallocN(scen->r.avicodecdata->lpFormat);
292                 scen->r.avicodecdata->lpParms = MEM_dupallocN(scen->r.avicodecdata->lpParms);
293         }
294         
295         /* make a private copy of the qtcodecdata */
296         if (sce->r.qtcodecdata) {
297                 scen->r.qtcodecdata = MEM_dupallocN(sce->r.qtcodecdata);
298                 scen->r.qtcodecdata->cdParms = MEM_dupallocN(scen->r.qtcodecdata->cdParms);
299         }
300         
301         if (sce->r.ffcodecdata.properties) { /* intentionally check scen not sce. */
302                 scen->r.ffcodecdata.properties = IDP_CopyProperty(sce->r.ffcodecdata.properties);
303         }
304
305         /* NOTE: part of SCE_COPY_LINK_DATA and SCE_COPY_FULL operations
306          * are done outside of blenkernel with ED_objects_single_users! */
307
308         /*  camera */
309         if (type == SCE_COPY_LINK_DATA || type == SCE_COPY_FULL) {
310                 ID_NEW(scen->camera);
311         }
312         
313         /* before scene copy */
314         BKE_sound_create_scene(scen);
315
316         /* world */
317         if (type == SCE_COPY_FULL) {
318                 if (scen->world) {
319                         id_us_plus((ID *)scen->world);
320                         scen->world = BKE_world_copy(scen->world);
321                         BKE_animdata_copy_id_action((ID *)scen->world);
322                 }
323
324                 if (sce->ed) {
325                         scen->ed = MEM_callocN(sizeof(Editing), "addseq");
326                         scen->ed->seqbasep = &scen->ed->seqbase;
327                         BKE_sequence_base_dupli_recursive(sce, scen, &scen->ed->seqbase, &sce->ed->seqbase, SEQ_DUPE_ALL);
328                 }
329         }
330         
331         /* grease pencil */
332         if (scen->gpd) {
333                 if (type == SCE_COPY_FULL) {
334                         scen->gpd = gpencil_data_duplicate(scen->gpd, false);
335                 }
336                 else if (type == SCE_COPY_EMPTY) {
337                         scen->gpd = NULL;
338                 }
339                 else {
340                         id_us_plus((ID *)scen->gpd);
341                 }
342         }
343
344         return scen;
345 }
346
347 void BKE_scene_groups_relink(Scene *sce)
348 {
349         if (sce->rigidbody_world)
350                 BKE_rigidbody_world_groups_relink(sce->rigidbody_world);
351 }
352
353 /* do not free scene itself */
354 void BKE_scene_free(Scene *sce)
355 {
356         Base *base;
357         SceneRenderLayer *srl;
358
359         /* check all sequences */
360         BKE_sequencer_clear_scene_in_allseqs(G.main, sce);
361
362         base = sce->base.first;
363         while (base) {
364                 base->object->id.us--;
365                 base = base->next;
366         }
367         /* do not free objects! */
368         
369         if (sce->gpd) {
370 #if 0   /* removed since this can be invalid memory when freeing everything */
371                 /* since the grease pencil data is freed before the scene.
372                  * since grease pencil data is not (yet?), shared between objects
373                  * its probably safe not to do this, some save and reload will free this. */
374                 sce->gpd->id.us--;
375 #endif
376                 sce->gpd = NULL;
377         }
378
379         BLI_freelistN(&sce->base);
380         BKE_sequencer_editing_free(sce);
381
382         BKE_animdata_free((ID *)sce);
383         BKE_keyingsets_free(&sce->keyingsets);
384         
385         if (sce->rigidbody_world)
386                 BKE_rigidbody_free_world(sce->rigidbody_world);
387         
388         if (sce->r.avicodecdata) {
389                 free_avicodecdata(sce->r.avicodecdata);
390                 MEM_freeN(sce->r.avicodecdata);
391                 sce->r.avicodecdata = NULL;
392         }
393         if (sce->r.qtcodecdata) {
394                 free_qtcodecdata(sce->r.qtcodecdata);
395                 MEM_freeN(sce->r.qtcodecdata);
396                 sce->r.qtcodecdata = NULL;
397         }
398         if (sce->r.ffcodecdata.properties) {
399                 IDP_FreeProperty(sce->r.ffcodecdata.properties);
400                 MEM_freeN(sce->r.ffcodecdata.properties);
401                 sce->r.ffcodecdata.properties = NULL;
402         }
403         
404         for (srl = sce->r.layers.first; srl; srl = srl->next) {
405                 BKE_freestyle_config_free(&srl->freestyleConfig);
406         }
407         
408         BLI_freelistN(&sce->markers);
409         BLI_freelistN(&sce->transform_spaces);
410         BLI_freelistN(&sce->r.layers);
411         BLI_freelistN(&sce->r.views);
412         
413         if (sce->toolsettings) {
414                 if (sce->toolsettings->vpaint) {
415                         BKE_paint_free(&sce->toolsettings->vpaint->paint);
416                         MEM_freeN(sce->toolsettings->vpaint);
417                 }
418                 if (sce->toolsettings->wpaint) {
419                         BKE_paint_free(&sce->toolsettings->wpaint->paint);
420                         MEM_freeN(sce->toolsettings->wpaint);
421                 }
422                 if (sce->toolsettings->sculpt) {
423                         BKE_paint_free(&sce->toolsettings->sculpt->paint);
424                         MEM_freeN(sce->toolsettings->sculpt);
425                 }
426                 if (sce->toolsettings->uvsculpt) {
427                         BKE_paint_free(&sce->toolsettings->uvsculpt->paint);
428                         MEM_freeN(sce->toolsettings->uvsculpt);
429                 }
430                 BKE_paint_free(&sce->toolsettings->imapaint.paint);
431
432                 MEM_freeN(sce->toolsettings);
433                 sce->toolsettings = NULL;
434         }
435         
436         DAG_scene_free(sce);
437         if (sce->depsgraph)
438                 DEG_graph_free(sce->depsgraph);
439         
440         if (sce->nodetree) {
441                 ntreeFreeTree(sce->nodetree);
442                 MEM_freeN(sce->nodetree);
443         }
444
445         if (sce->stats)
446                 MEM_freeN(sce->stats);
447         if (sce->fps_info)
448                 MEM_freeN(sce->fps_info);
449
450         BKE_sound_destroy_scene(sce);
451
452         BKE_color_managed_view_settings_free(&sce->view_settings);
453 }
454
455 Scene *BKE_scene_add(Main *bmain, const char *name)
456 {
457         Scene *sce;
458         ParticleEditSettings *pset;
459         int a;
460         const char *colorspace_name;
461         SceneRenderView *srv;
462
463         sce = BKE_libblock_alloc(bmain, ID_SCE, name);
464         sce->lay = sce->layact = 1;
465         
466         sce->r.mode = R_GAMMA | R_OSA | R_SHADOW | R_SSS | R_ENVMAP | R_RAYTRACE;
467         sce->r.cfra = 1;
468         sce->r.sfra = 1;
469         sce->r.efra = 250;
470         sce->r.frame_step = 1;
471         sce->r.xsch = 1920;
472         sce->r.ysch = 1080;
473         sce->r.xasp = 1;
474         sce->r.yasp = 1;
475         sce->r.tilex = 256;
476         sce->r.tiley = 256;
477         sce->r.mblur_samples = 1;
478         sce->r.filtertype = R_FILTER_MITCH;
479         sce->r.size = 50;
480
481         sce->r.im_format.planes = R_IMF_PLANES_RGBA;
482         sce->r.im_format.imtype = R_IMF_IMTYPE_PNG;
483         sce->r.im_format.depth = R_IMF_CHAN_DEPTH_8;
484         sce->r.im_format.quality = 90;
485         sce->r.im_format.compress = 15;
486
487         sce->r.displaymode = R_OUTPUT_AREA;
488         sce->r.framapto = 100;
489         sce->r.images = 100;
490         sce->r.framelen = 1.0;
491         sce->r.blurfac = 0.5;
492         sce->r.frs_sec = 24;
493         sce->r.frs_sec_base = 1;
494         sce->r.edgeint = 10;
495         sce->r.ocres = 128;
496
497         /* OCIO_TODO: for forwards compatibility only, so if no tonecurve are used,
498          *            images would look in the same way as in current blender
499          *
500          *            perhaps at some point should be completely deprecated?
501          */
502         sce->r.color_mgt_flag |= R_COLOR_MANAGEMENT;
503
504         sce->r.gauss = 1.0;
505         
506         /* deprecated but keep for upwards compat */
507         sce->r.postgamma = 1.0;
508         sce->r.posthue = 0.0;
509         sce->r.postsat = 1.0;
510
511         sce->r.bake_mode = 1;    /* prevent to include render stuff here */
512         sce->r.bake_filter = 16;
513         sce->r.bake_osa = 5;
514         sce->r.bake_flag = R_BAKE_CLEAR;
515         sce->r.bake_normal_space = R_BAKE_SPACE_TANGENT;
516         sce->r.bake_samples = 256;
517         sce->r.bake_biasdist = 0.001;
518
519         sce->r.bake.flag = R_BAKE_CLEAR;
520         sce->r.bake.width = 512;
521         sce->r.bake.height = 512;
522         sce->r.bake.margin = 16;
523         sce->r.bake.normal_space = R_BAKE_SPACE_TANGENT;
524         sce->r.bake.normal_swizzle[0] = R_BAKE_POSX;
525         sce->r.bake.normal_swizzle[1] = R_BAKE_POSY;
526         sce->r.bake.normal_swizzle[2] = R_BAKE_POSZ;
527         BLI_strncpy(sce->r.bake.filepath, U.renderdir, sizeof(sce->r.bake.filepath));
528
529         sce->r.bake.im_format.planes = R_IMF_PLANES_RGBA;
530         sce->r.bake.im_format.imtype = R_IMF_IMTYPE_PNG;
531         sce->r.bake.im_format.depth = R_IMF_CHAN_DEPTH_8;
532         sce->r.bake.im_format.quality = 90;
533         sce->r.bake.im_format.compress = 15;
534
535         sce->r.scemode = R_DOCOMP | R_DOSEQ | R_EXTENSION;
536         sce->r.stamp = R_STAMP_TIME | R_STAMP_FRAME | R_STAMP_DATE | R_STAMP_CAMERA | R_STAMP_SCENE | R_STAMP_FILENAME | R_STAMP_RENDERTIME;
537         sce->r.stamp_font_id = 12;
538         sce->r.fg_stamp[0] = sce->r.fg_stamp[1] = sce->r.fg_stamp[2] = 0.8f;
539         sce->r.fg_stamp[3] = 1.0f;
540         sce->r.bg_stamp[0] = sce->r.bg_stamp[1] = sce->r.bg_stamp[2] = 0.0f;
541         sce->r.bg_stamp[3] = 0.25f;
542         sce->r.raytrace_options = R_RAYTRACE_USE_INSTANCES;
543
544         sce->r.seq_prev_type = OB_SOLID;
545         sce->r.seq_rend_type = OB_SOLID;
546         sce->r.seq_flag = R_SEQ_GL_PREV;
547
548         sce->r.threads = 1;
549
550         sce->r.simplify_subsurf = 6;
551         sce->r.simplify_particles = 1.0f;
552         sce->r.simplify_shadowsamples = 16;
553         sce->r.simplify_aosss = 1.0f;
554
555         sce->r.border.xmin = 0.0f;
556         sce->r.border.ymin = 0.0f;
557         sce->r.border.xmax = 1.0f;
558         sce->r.border.ymax = 1.0f;
559
560         sce->r.preview_start_resolution = 64;
561         
562         sce->r.line_thickness_mode = R_LINE_THICKNESS_ABSOLUTE;
563         sce->r.unit_line_thickness = 1.0f;
564
565         sce->toolsettings = MEM_callocN(sizeof(struct ToolSettings), "Tool Settings Struct");
566         sce->toolsettings->doublimit = 0.001;
567         sce->toolsettings->vgroup_weight = 1.0f;
568         sce->toolsettings->uvcalc_margin = 0.001f;
569         sce->toolsettings->unwrapper = 1;
570         sce->toolsettings->select_thresh = 0.01f;
571
572         sce->toolsettings->selectmode = SCE_SELECT_VERTEX;
573         sce->toolsettings->uv_selectmode = UV_SELECT_VERTEX;
574         sce->toolsettings->normalsize = 0.1;
575         sce->toolsettings->autokey_mode = U.autokey_mode;
576
577         sce->toolsettings->snap_node_mode = SCE_SNAP_MODE_GRID;
578
579         sce->toolsettings->skgen_resolution = 100;
580         sce->toolsettings->skgen_threshold_internal     = 0.01f;
581         sce->toolsettings->skgen_threshold_external     = 0.01f;
582         sce->toolsettings->skgen_angle_limit            = 45.0f;
583         sce->toolsettings->skgen_length_ratio           = 1.3f;
584         sce->toolsettings->skgen_length_limit           = 1.5f;
585         sce->toolsettings->skgen_correlation_limit      = 0.98f;
586         sce->toolsettings->skgen_symmetry_limit         = 0.1f;
587         sce->toolsettings->skgen_postpro = SKGEN_SMOOTH;
588         sce->toolsettings->skgen_postpro_passes = 1;
589         sce->toolsettings->skgen_options = SKGEN_FILTER_INTERNAL | SKGEN_FILTER_EXTERNAL | SKGEN_FILTER_SMART | SKGEN_HARMONIC | SKGEN_SUB_CORRELATION | SKGEN_STICK_TO_EMBEDDING;
590         sce->toolsettings->skgen_subdivisions[0] = SKGEN_SUB_CORRELATION;
591         sce->toolsettings->skgen_subdivisions[1] = SKGEN_SUB_LENGTH;
592         sce->toolsettings->skgen_subdivisions[2] = SKGEN_SUB_ANGLE;
593
594         sce->toolsettings->statvis.overhang_axis = OB_NEGZ;
595         sce->toolsettings->statvis.overhang_min = 0;
596         sce->toolsettings->statvis.overhang_max = DEG2RADF(45.0f);
597         sce->toolsettings->statvis.thickness_max = 0.1f;
598         sce->toolsettings->statvis.thickness_samples = 1;
599         sce->toolsettings->statvis.distort_min = DEG2RADF(5.0f);
600         sce->toolsettings->statvis.distort_max = DEG2RADF(45.0f);
601
602         sce->toolsettings->statvis.sharp_min = DEG2RADF(90.0f);
603         sce->toolsettings->statvis.sharp_max = DEG2RADF(180.0f);
604
605         sce->toolsettings->proportional_size = 1.0f;
606
607         sce->toolsettings->imapaint.paint.flags |= PAINT_SHOW_BRUSH;
608         sce->toolsettings->imapaint.normal_angle = 80;
609         sce->toolsettings->imapaint.seam_bleed = 2;
610
611         sce->physics_settings.gravity[0] = 0.0f;
612         sce->physics_settings.gravity[1] = 0.0f;
613         sce->physics_settings.gravity[2] = -9.81f;
614         sce->physics_settings.flag = PHYS_GLOBAL_GRAVITY;
615
616         sce->unit.scale_length = 1.0f;
617
618         pset = &sce->toolsettings->particle;
619         pset->flag = PE_KEEP_LENGTHS | PE_LOCK_FIRST | PE_DEFLECT_EMITTER | PE_AUTO_VELOCITY;
620         pset->emitterdist = 0.25f;
621         pset->totrekey = 5;
622         pset->totaddkey = 5;
623         pset->brushtype = PE_BRUSH_NONE;
624         pset->draw_step = 2;
625         pset->fade_frames = 2;
626         pset->selectmode = SCE_SELECT_PATH;
627         for (a = 0; a < PE_TOT_BRUSH; a++) {
628                 pset->brush[a].strength = 0.5;
629                 pset->brush[a].size = 50;
630                 pset->brush[a].step = 10;
631                 pset->brush[a].count = 10;
632         }
633         pset->brush[PE_BRUSH_CUT].strength = 100;
634
635         sce->r.ffcodecdata.audio_mixrate = 44100;
636         sce->r.ffcodecdata.audio_volume = 1.0f;
637         sce->r.ffcodecdata.audio_bitrate = 192;
638         sce->r.ffcodecdata.audio_channels = 2;
639
640         BLI_strncpy(sce->r.engine, RE_engine_id_BLENDER_RENDER, sizeof(sce->r.engine));
641
642         sce->audio.distance_model = 2.0f;
643         sce->audio.doppler_factor = 1.0f;
644         sce->audio.speed_of_sound = 343.3f;
645         sce->audio.volume = 1.0f;
646
647         BLI_strncpy(sce->r.pic, U.renderdir, sizeof(sce->r.pic));
648
649         BLI_rctf_init(&sce->r.safety, 0.1f, 0.9f, 0.1f, 0.9f);
650         sce->r.osa = 8;
651
652         /* note; in header_info.c the scene copy happens..., if you add more to renderdata it has to be checked there */
653         BKE_scene_add_render_layer(sce, NULL);
654
655         /* multiview - stereo */
656         BKE_scene_add_render_view(sce, STEREO_LEFT_NAME);
657         srv = sce->r.views.first;
658         BLI_strncpy(srv->suffix, STEREO_LEFT_SUFFIX, sizeof(srv->suffix));
659
660         BKE_scene_add_render_view(sce, STEREO_RIGHT_NAME);
661         srv = sce->r.views.last;
662         BLI_strncpy(srv->suffix, STEREO_RIGHT_SUFFIX, sizeof(srv->suffix));
663
664         /* game data */
665         sce->gm.stereoflag = STEREO_NOSTEREO;
666         sce->gm.stereomode = STEREO_ANAGLYPH;
667         sce->gm.eyeseparation = 0.10;
668
669         sce->gm.dome.angle = 180;
670         sce->gm.dome.mode = DOME_FISHEYE;
671         sce->gm.dome.res = 4;
672         sce->gm.dome.resbuf = 1.0f;
673         sce->gm.dome.tilt = 0;
674
675         sce->gm.xplay = 640;
676         sce->gm.yplay = 480;
677         sce->gm.freqplay = 60;
678         sce->gm.depth = 32;
679
680         sce->gm.gravity = 9.8f;
681         sce->gm.physicsEngine = WOPHY_BULLET;
682         sce->gm.mode = 32; //XXX ugly harcoding, still not sure we should drop mode. 32 == 1 << 5 == use_occlusion_culling 
683         sce->gm.occlusionRes = 128;
684         sce->gm.ticrate = 60;
685         sce->gm.maxlogicstep = 5;
686         sce->gm.physubstep = 1;
687         sce->gm.maxphystep = 5;
688         sce->gm.lineardeactthreshold = 0.8f;
689         sce->gm.angulardeactthreshold = 1.0f;
690         sce->gm.deactivationtime = 0.0f;
691
692         sce->gm.flag = GAME_DISPLAY_LISTS;
693         sce->gm.matmode = GAME_MAT_MULTITEX;
694
695         sce->gm.obstacleSimulation = OBSTSIMULATION_NONE;
696         sce->gm.levelHeight = 2.f;
697
698         sce->gm.recastData.cellsize = 0.3f;
699         sce->gm.recastData.cellheight = 0.2f;
700         sce->gm.recastData.agentmaxslope = M_PI_4;
701         sce->gm.recastData.agentmaxclimb = 0.9f;
702         sce->gm.recastData.agentheight = 2.0f;
703         sce->gm.recastData.agentradius = 0.6f;
704         sce->gm.recastData.edgemaxlen = 12.0f;
705         sce->gm.recastData.edgemaxerror = 1.3f;
706         sce->gm.recastData.regionminsize = 8.f;
707         sce->gm.recastData.regionmergesize = 20.f;
708         sce->gm.recastData.vertsperpoly = 6;
709         sce->gm.recastData.detailsampledist = 6.0f;
710         sce->gm.recastData.detailsamplemaxerror = 1.0f;
711
712         sce->gm.lodflag = SCE_LOD_USE_HYST;
713         sce->gm.scehysteresis = 10;
714
715         sce->gm.exitkey = 218; // Blender key code for ESC
716
717         BKE_sound_create_scene(sce);
718
719         /* color management */
720         colorspace_name = IMB_colormanagement_role_colorspace_name_get(COLOR_ROLE_DEFAULT_SEQUENCER);
721
722         BKE_color_managed_display_settings_init(&sce->display_settings);
723         BKE_color_managed_view_settings_init(&sce->view_settings);
724         BLI_strncpy(sce->sequencer_colorspace_settings.name, colorspace_name,
725                     sizeof(sce->sequencer_colorspace_settings.name));
726
727         /* Safe Areas */
728         copy_v2_fl2(sce->safe_areas.title, 3.5f / 100.0f, 3.5f / 100.0f);
729         copy_v2_fl2(sce->safe_areas.action, 10.0f / 100.0f, 5.0f / 100.0f);
730         copy_v2_fl2(sce->safe_areas.title_center, 17.5f / 100.0f, 5.0f / 100.0f);
731         copy_v2_fl2(sce->safe_areas.action_center, 15.0f / 100.0f, 5.0f / 100.0f);
732
733         return sce;
734 }
735
736 Base *BKE_scene_base_find_by_name(struct Scene *scene, const char *name)
737 {
738         Base *base;
739
740         for (base = scene->base.first; base; base = base->next) {
741                 if (STREQ(base->object->id.name + 2, name)) {
742                         break;
743                 }
744         }
745
746         return base;
747 }
748
749 Base *BKE_scene_base_find(Scene *scene, Object *ob)
750 {
751         return BLI_findptr(&scene->base, ob, offsetof(Base, object));
752 }
753
754 void BKE_scene_set_background(Main *bmain, Scene *scene)
755 {
756         Scene *sce;
757         Base *base;
758         Object *ob;
759         Group *group;
760         GroupObject *go;
761         int flag;
762         
763         /* check for cyclic sets, for reading old files but also for definite security (py?) */
764         BKE_scene_validate_setscene(bmain, scene);
765         
766         /* can happen when switching modes in other scenes */
767         if (scene->obedit && !(scene->obedit->mode & OB_MODE_EDIT))
768                 scene->obedit = NULL;
769
770         /* deselect objects (for dataselect) */
771         for (ob = bmain->object.first; ob; ob = ob->id.next)
772                 ob->flag &= ~(SELECT | OB_FROMGROUP);
773
774         /* group flags again */
775         for (group = bmain->group.first; group; group = group->id.next) {
776                 for (go = group->gobject.first; go; go = go->next) {
777                         if (go->ob) {
778                                 go->ob->flag |= OB_FROMGROUP;
779                         }
780                 }
781         }
782
783         /* sort baselist for scene and sets */
784         for (sce = scene; sce; sce = sce->set)
785                 DAG_scene_relations_rebuild(bmain, sce);
786
787         /* copy layers and flags from bases to objects */
788         for (base = scene->base.first; base; base = base->next) {
789                 ob = base->object;
790                 ob->lay = base->lay;
791                 
792                 /* group patch... */
793                 base->flag &= ~(OB_FROMGROUP);
794                 flag = ob->flag & (OB_FROMGROUP);
795                 base->flag |= flag;
796                 
797                 /* not too nice... for recovering objects with lost data */
798                 //if (ob->pose == NULL) base->flag &= ~OB_POSEMODE;
799                 ob->flag = base->flag;
800         }
801         /* no full animation update, this to enable render code to work (render code calls own animation updates) */
802 }
803
804 /* called from creator.c */
805 Scene *BKE_scene_set_name(Main *bmain, const char *name)
806 {
807         Scene *sce = (Scene *)BKE_libblock_find_name_ex(bmain, ID_SCE, name);
808         if (sce) {
809                 BKE_scene_set_background(bmain, sce);
810                 printf("Scene switch: '%s' in file: '%s'\n", name, bmain->name);
811                 return sce;
812         }
813
814         printf("Can't find scene: '%s' in file: '%s'\n", name, bmain->name);
815         return NULL;
816 }
817
818 void BKE_scene_unlink(Main *bmain, Scene *sce, Scene *newsce)
819 {
820         Scene *sce1;
821         bScreen *screen;
822
823         /* check all sets */
824         for (sce1 = bmain->scene.first; sce1; sce1 = sce1->id.next)
825                 if (sce1->set == sce)
826                         sce1->set = NULL;
827         
828         for (sce1 = bmain->scene.first; sce1; sce1 = sce1->id.next) {
829                 bNode *node;
830                 
831                 if (sce1 == sce || !sce1->nodetree)
832                         continue;
833                 
834                 for (node = sce1->nodetree->nodes.first; node; node = node->next) {
835                         if (node->id == &sce->id)
836                                 node->id = NULL;
837                 }
838         }
839         
840         /* all screens */
841         for (screen = bmain->screen.first; screen; screen = screen->id.next) {
842                 if (screen->scene == sce) {
843                         screen->scene = newsce;
844                 }
845
846                 /* editors are handled by WM_main_remove_editor_id_reference */
847         }
848
849         BKE_libblock_free(bmain, sce);
850 }
851
852 /* Used by metaballs, return *all* objects (including duplis) existing in the scene (including scene's sets) */
853 int BKE_scene_base_iter_next(EvaluationContext *eval_ctx, SceneBaseIter *iter,
854                              Scene **scene, int val, Base **base, Object **ob)
855 {
856         bool run_again = true;
857         
858         /* init */
859         if (val == 0) {
860                 iter->phase = F_START;
861                 iter->dupob = NULL;
862                 iter->duplilist = NULL;
863                 iter->dupli_refob = NULL;
864         }
865         else {
866                 /* run_again is set when a duplilist has been ended */
867                 while (run_again) {
868                         run_again = false;
869
870                         /* the first base */
871                         if (iter->phase == F_START) {
872                                 *base = (*scene)->base.first;
873                                 if (*base) {
874                                         *ob = (*base)->object;
875                                         iter->phase = F_SCENE;
876                                 }
877                                 else {
878                                         /* exception: empty scene */
879                                         while ((*scene)->set) {
880                                                 (*scene) = (*scene)->set;
881                                                 if ((*scene)->base.first) {
882                                                         *base = (*scene)->base.first;
883                                                         *ob = (*base)->object;
884                                                         iter->phase = F_SCENE;
885                                                         break;
886                                                 }
887                                         }
888                                 }
889                         }
890                         else {
891                                 if (*base && iter->phase != F_DUPLI) {
892                                         *base = (*base)->next;
893                                         if (*base) {
894                                                 *ob = (*base)->object;
895                                         }
896                                         else {
897                                                 if (iter->phase == F_SCENE) {
898                                                         /* (*scene) is finished, now do the set */
899                                                         while ((*scene)->set) {
900                                                                 (*scene) = (*scene)->set;
901                                                                 if ((*scene)->base.first) {
902                                                                         *base = (*scene)->base.first;
903                                                                         *ob = (*base)->object;
904                                                                         break;
905                                                                 }
906                                                         }
907                                                 }
908                                         }
909                                 }
910                         }
911                         
912                         if (*base == NULL) {
913                                 iter->phase = F_START;
914                         }
915                         else {
916                                 if (iter->phase != F_DUPLI) {
917                                         if ( (*base)->object->transflag & OB_DUPLI) {
918                                                 /* groups cannot be duplicated for mballs yet, 
919                                                  * this enters eternal loop because of 
920                                                  * makeDispListMBall getting called inside of group_duplilist */
921                                                 if ((*base)->object->dup_group == NULL) {
922                                                         iter->duplilist = object_duplilist_ex(eval_ctx, (*scene), (*base)->object, false);
923                                                         
924                                                         iter->dupob = iter->duplilist->first;
925
926                                                         if (!iter->dupob) {
927                                                                 free_object_duplilist(iter->duplilist);
928                                                                 iter->duplilist = NULL;
929                                                         }
930                                                         iter->dupli_refob = NULL;
931                                                 }
932                                         }
933                                 }
934                                 /* handle dupli's */
935                                 if (iter->dupob) {
936                                         (*base)->flag |= OB_FROMDUPLI;
937                                         *ob = iter->dupob->ob;
938                                         iter->phase = F_DUPLI;
939
940                                         if (iter->dupli_refob != *ob) {
941                                                 if (iter->dupli_refob) {
942                                                         /* Restore previous object's real matrix. */
943                                                         copy_m4_m4(iter->dupli_refob->obmat, iter->omat);
944                                                 }
945                                                 /* Backup new object's real matrix. */
946                                                 iter->dupli_refob = *ob;
947                                                 copy_m4_m4(iter->omat, iter->dupli_refob->obmat);
948                                         }
949                                         copy_m4_m4((*ob)->obmat, iter->dupob->mat);
950
951                                         iter->dupob = iter->dupob->next;
952                                 }
953                                 else if (iter->phase == F_DUPLI) {
954                                         iter->phase = F_SCENE;
955                                         (*base)->flag &= ~OB_FROMDUPLI;
956                                         
957                                         if (iter->dupli_refob) {
958                                                 /* Restore last object's real matrix. */
959                                                 copy_m4_m4(iter->dupli_refob->obmat, iter->omat);
960                                                 iter->dupli_refob = NULL;
961                                         }
962                                         
963                                         free_object_duplilist(iter->duplilist);
964                                         iter->duplilist = NULL;
965                                         run_again = true;
966                                 }
967                         }
968                 }
969         }
970
971 #if 0
972         if (ob && *ob) {
973                 printf("Scene: '%s', '%s'\n", (*scene)->id.name + 2, (*ob)->id.name + 2);
974         }
975 #endif
976
977         return iter->phase;
978 }
979
980 Object *BKE_scene_camera_find(Scene *sc)
981 {
982         Base *base;
983         
984         for (base = sc->base.first; base; base = base->next)
985                 if (base->object->type == OB_CAMERA)
986                         return base->object;
987
988         return NULL;
989 }
990
991 #ifdef DURIAN_CAMERA_SWITCH
992 Object *BKE_scene_camera_switch_find(Scene *scene)
993 {
994         TimeMarker *m;
995         int cfra = scene->r.cfra;
996         int frame = -(MAXFRAME + 1);
997         int min_frame = MAXFRAME + 1;
998         Object *camera = NULL;
999         Object *first_camera = NULL;
1000
1001         for (m = scene->markers.first; m; m = m->next) {
1002                 if (m->camera && (m->camera->restrictflag & OB_RESTRICT_RENDER) == 0) {
1003                         if ((m->frame <= cfra) && (m->frame > frame)) {
1004                                 camera = m->camera;
1005                                 frame = m->frame;
1006
1007                                 if (frame == cfra)
1008                                         break;
1009                         }
1010
1011                         if (m->frame < min_frame) {
1012                                 first_camera = m->camera;
1013                                 min_frame = m->frame;
1014                         }
1015                 }
1016         }
1017
1018         if (camera == NULL) {
1019                 /* If there's no marker to the left of current frame,
1020                  * use camera from left-most marker to solve all sort
1021                  * of Schrodinger uncertainties.
1022                  */
1023                 return first_camera;
1024         }
1025
1026         return camera;
1027 }
1028 #endif
1029
1030 int BKE_scene_camera_switch_update(Scene *scene)
1031 {
1032 #ifdef DURIAN_CAMERA_SWITCH
1033         Object *camera = BKE_scene_camera_switch_find(scene);
1034         if (camera) {
1035                 scene->camera = camera;
1036                 return 1;
1037         }
1038 #else
1039         (void)scene;
1040 #endif
1041         return 0;
1042 }
1043
1044 char *BKE_scene_find_marker_name(Scene *scene, int frame)
1045 {
1046         ListBase *markers = &scene->markers;
1047         TimeMarker *m1, *m2;
1048
1049         /* search through markers for match */
1050         for (m1 = markers->first, m2 = markers->last; m1 && m2; m1 = m1->next, m2 = m2->prev) {
1051                 if (m1->frame == frame)
1052                         return m1->name;
1053
1054                 if (m1 == m2)
1055                         break;
1056
1057                 if (m2->frame == frame)
1058                         return m2->name;
1059         }
1060
1061         return NULL;
1062 }
1063
1064 /* return the current marker for this frame,
1065  * we can have more than 1 marker per frame, this just returns the first :/ */
1066 char *BKE_scene_find_last_marker_name(Scene *scene, int frame)
1067 {
1068         TimeMarker *marker, *best_marker = NULL;
1069         int best_frame = -MAXFRAME * 2;
1070         for (marker = scene->markers.first; marker; marker = marker->next) {
1071                 if (marker->frame == frame) {
1072                         return marker->name;
1073                 }
1074
1075                 if (marker->frame > best_frame && marker->frame < frame) {
1076                         best_marker = marker;
1077                         best_frame = marker->frame;
1078                 }
1079         }
1080
1081         return best_marker ? best_marker->name : NULL;
1082 }
1083
1084
1085 Base *BKE_scene_base_add(Scene *sce, Object *ob)
1086 {
1087         Base *b = MEM_callocN(sizeof(*b), "BKE_scene_base_add");
1088         BLI_addhead(&sce->base, b);
1089
1090         b->object = ob;
1091         b->flag = ob->flag;
1092         b->lay = ob->lay;
1093
1094         return b;
1095 }
1096
1097 void BKE_scene_base_unlink(Scene *sce, Base *base)
1098 {
1099         /* remove rigid body constraint from world before removing object */
1100         if (base->object->rigidbody_constraint)
1101                 BKE_rigidbody_remove_constraint(sce, base->object);
1102         /* remove rigid body object from world before removing object */
1103         if (base->object->rigidbody_object)
1104                 BKE_rigidbody_remove_object(sce, base->object);
1105         
1106         BLI_remlink(&sce->base, base);
1107 }
1108
1109 void BKE_scene_base_deselect_all(Scene *sce)
1110 {
1111         Base *b;
1112
1113         for (b = sce->base.first; b; b = b->next) {
1114                 b->flag &= ~SELECT;
1115                 b->object->flag = b->flag;
1116         }
1117 }
1118
1119 void BKE_scene_base_select(Scene *sce, Base *selbase)
1120 {
1121         selbase->flag |= SELECT;
1122         selbase->object->flag = selbase->flag;
1123
1124         sce->basact = selbase;
1125 }
1126
1127 /* checks for cycle, returns 1 if it's all OK */
1128 bool BKE_scene_validate_setscene(Main *bmain, Scene *sce)
1129 {
1130         Scene *sce_iter;
1131         int a, totscene;
1132
1133         if (sce->set == NULL) return true;
1134         totscene = BLI_listbase_count(&bmain->scene);
1135         
1136         for (a = 0, sce_iter = sce; sce_iter->set; sce_iter = sce_iter->set, a++) {
1137                 /* more iterations than scenes means we have a cycle */
1138                 if (a > totscene) {
1139                         /* the tested scene gets zero'ed, that's typically current scene */
1140                         sce->set = NULL;
1141                         return false;
1142                 }
1143         }
1144
1145         return true;
1146 }
1147
1148 /* This function is needed to cope with fractional frames - including two Blender rendering features
1149  * mblur (motion blur that renders 'subframes' and blurs them together), and fields rendering. 
1150  */
1151 float BKE_scene_frame_get(const Scene *scene)
1152 {
1153         return BKE_scene_frame_get_from_ctime(scene, scene->r.cfra);
1154 }
1155
1156 /* This function is used to obtain arbitrary fractional frames */
1157 float BKE_scene_frame_get_from_ctime(const Scene *scene, const float frame)
1158 {
1159         float ctime = frame;
1160         ctime += scene->r.subframe;
1161         ctime *= scene->r.framelen;
1162         
1163         return ctime;
1164 }
1165
1166 /**
1167  * Sets the frame int/float components.
1168  */
1169 void BKE_scene_frame_set(struct Scene *scene, double cfra)
1170 {
1171         double intpart;
1172         scene->r.subframe = modf(cfra, &intpart);
1173         scene->r.cfra = (int)intpart;
1174 }
1175
1176 #ifdef WITH_LEGACY_DEPSGRAPH
1177 /* drivers support/hacks 
1178  *  - this method is called from scene_update_tagged_recursive(), so gets included in viewport + render
1179  *      - these are always run since the depsgraph can't handle non-object data
1180  *      - these happen after objects are all done so that we can read in their final transform values,
1181  *        though this means that objects can't refer to scene info for guidance...
1182  */
1183 static void scene_update_drivers(Main *UNUSED(bmain), Scene *scene)
1184 {
1185         SceneRenderLayer *srl;
1186         float ctime = BKE_scene_frame_get(scene);
1187         
1188         /* scene itself */
1189         if (scene->adt && scene->adt->drivers.first) {
1190                 BKE_animsys_evaluate_animdata(scene, &scene->id, scene->adt, ctime, ADT_RECALC_DRIVERS);
1191         }
1192
1193         /* world */
1194         /* TODO: what about world textures? but then those have nodes too... */
1195         if (scene->world) {
1196                 ID *wid = (ID *)scene->world;
1197                 AnimData *adt = BKE_animdata_from_id(wid);
1198                 
1199                 if (adt && adt->drivers.first)
1200                         BKE_animsys_evaluate_animdata(scene, wid, adt, ctime, ADT_RECALC_DRIVERS);
1201         }
1202         
1203         /* nodes */
1204         if (scene->nodetree) {
1205                 ID *nid = (ID *)scene->nodetree;
1206                 AnimData *adt = BKE_animdata_from_id(nid);
1207                 
1208                 if (adt && adt->drivers.first)
1209                         BKE_animsys_evaluate_animdata(scene, nid, adt, ctime, ADT_RECALC_DRIVERS);
1210         }
1211
1212         /* world nodes */
1213         if (scene->world && scene->world->nodetree) {
1214                 ID *nid = (ID *)scene->world->nodetree;
1215                 AnimData *adt = BKE_animdata_from_id(nid);
1216                 
1217                 if (adt && adt->drivers.first)
1218                         BKE_animsys_evaluate_animdata(scene, nid, adt, ctime, ADT_RECALC_DRIVERS);
1219         }
1220
1221         /* freestyle */
1222         for (srl = scene->r.layers.first; srl; srl = srl->next) {
1223                 FreestyleConfig *config = &srl->freestyleConfig;
1224                 FreestyleLineSet *lineset;
1225
1226                 for (lineset = config->linesets.first; lineset; lineset = lineset->next) {
1227                         if (lineset->linestyle) {
1228                                 ID *lid = &lineset->linestyle->id;
1229                                 AnimData *adt = BKE_animdata_from_id(lid);
1230
1231                                 if (adt && adt->drivers.first)
1232                                         BKE_animsys_evaluate_animdata(scene, lid, adt, ctime, ADT_RECALC_DRIVERS);
1233                         }
1234                 }
1235         }
1236 }
1237
1238 /* deps hack - do extra recalcs at end */
1239 static void scene_depsgraph_hack(EvaluationContext *eval_ctx, Scene *scene, Scene *scene_parent)
1240 {
1241         Base *base;
1242                 
1243         scene->customdata_mask = scene_parent->customdata_mask;
1244         
1245         /* sets first, we allow per definition current scene to have
1246          * dependencies on sets, but not the other way around. */
1247         if (scene->set)
1248                 scene_depsgraph_hack(eval_ctx, scene->set, scene_parent);
1249         
1250         for (base = scene->base.first; base; base = base->next) {
1251                 Object *ob = base->object;
1252                 
1253                 if (ob->depsflag) {
1254                         int recalc = 0;
1255                         // printf("depshack %s\n", ob->id.name + 2);
1256                         
1257                         if (ob->depsflag & OB_DEPS_EXTRA_OB_RECALC)
1258                                 recalc |= OB_RECALC_OB;
1259                         if (ob->depsflag & OB_DEPS_EXTRA_DATA_RECALC)
1260                                 recalc |= OB_RECALC_DATA;
1261                         
1262                         ob->recalc |= recalc;
1263                         BKE_object_handle_update(eval_ctx, scene_parent, ob);
1264                         
1265                         if (ob->dup_group && (ob->transflag & OB_DUPLIGROUP)) {
1266                                 GroupObject *go;
1267                                 
1268                                 for (go = ob->dup_group->gobject.first; go; go = go->next) {
1269                                         if (go->ob)
1270                                                 go->ob->recalc |= recalc;
1271                                 }
1272                                 BKE_group_handle_recalc_and_update(eval_ctx, scene_parent, ob, ob->dup_group);
1273                         }
1274                 }
1275         }
1276 }
1277 #endif  /* WITH_LEGACY_DEPSGRAPH */
1278
1279 /* That's like really a bummer, because currently animation data for armatures
1280  * might want to use pose, and pose might be missing on the object.
1281  * This happens when changing visible layers, which leads to situations when
1282  * pose is missing or marked for recalc, animation will change it and then
1283  * object update will restore the pose.
1284  *
1285  * This could be solved by the new dependency graph, but for until then we'll
1286  * do an extra pass on the objects to ensure it's all fine.
1287  */
1288 #define POSE_ANIMATION_WORKAROUND
1289
1290 #ifdef POSE_ANIMATION_WORKAROUND
1291 static void scene_armature_depsgraph_workaround(Main *bmain)
1292 {
1293         Object *ob;
1294         if (BLI_listbase_is_empty(&bmain->armature) || !DAG_id_type_tagged(bmain, ID_OB)) {
1295                 return;
1296         }
1297         for (ob = bmain->object.first; ob; ob = ob->id.next) {
1298                 if (ob->type == OB_ARMATURE && ob->adt && ob->adt->recalc & ADT_RECALC_ANIM) {
1299                         if (ob->pose == NULL || (ob->pose->flag & POSE_RECALC)) {
1300                                 BKE_pose_rebuild(ob, ob->data);
1301                         }
1302                 }
1303         }
1304 }
1305 #endif
1306
1307 #ifdef WITH_LEGACY_DEPSGRAPH
1308 static void scene_rebuild_rbw_recursive(Scene *scene, float ctime)
1309 {
1310         if (scene->set)
1311                 scene_rebuild_rbw_recursive(scene->set, ctime);
1312
1313         if (BKE_scene_check_rigidbody_active(scene))
1314                 BKE_rigidbody_rebuild_world(scene, ctime);
1315 }
1316
1317 static void scene_do_rb_simulation_recursive(Scene *scene, float ctime)
1318 {
1319         if (scene->set)
1320                 scene_do_rb_simulation_recursive(scene->set, ctime);
1321
1322         if (BKE_scene_check_rigidbody_active(scene))
1323                 BKE_rigidbody_do_simulation(scene, ctime);
1324 }
1325 #endif
1326
1327 /* Used to visualize CPU threads activity during threaded object update,
1328  * would pollute STDERR with whole bunch of timing information which then
1329  * could be parsed and nicely visualized.
1330  */
1331 #ifdef WITH_LEGACY_DEPSGRAPH
1332 #  undef DETAILED_ANALYSIS_OUTPUT
1333 #else
1334 /* ALWAYS KEEY DISABLED! */
1335 #  undef DETAILED_ANALYSIS_OUTPUT
1336 #endif
1337
1338 /* Mballs evaluation uses BKE_scene_base_iter_next which calls
1339  * duplilist for all objects in the scene. This leads to conflict
1340  * accessing and writing same data from multiple threads.
1341  *
1342  * Ideally Mballs shouldn't do such an iteration and use DAG
1343  * queries instead. For the time being we've got new DAG
1344  * let's keep it simple and update mballs in a ingle thread.
1345  */
1346 #define MBALL_SINGLETHREAD_HACK
1347
1348 #ifdef WITH_LEGACY_DEPSGRAPH
1349 typedef struct StatisicsEntry {
1350         struct StatisicsEntry *next, *prev;
1351         Object *object;
1352         double start_time;
1353         double duration;
1354 } StatisicsEntry;
1355
1356 typedef struct ThreadedObjectUpdateState {
1357         /* TODO(sergey): We might want this to be per-thread object. */
1358         EvaluationContext *eval_ctx;
1359         Scene *scene;
1360         Scene *scene_parent;
1361         double base_time;
1362
1363 #ifdef MBALL_SINGLETHREAD_HACK
1364         bool has_mballs;
1365 #endif
1366
1367         /* Execution statistics */
1368         bool has_updated_objects;
1369         ListBase *statistics;
1370 } ThreadedObjectUpdateState;
1371
1372 static void scene_update_object_add_task(void *node, void *user_data);
1373
1374 static void scene_update_all_bases(EvaluationContext *eval_ctx, Scene *scene, Scene *scene_parent)
1375 {
1376         Base *base;
1377
1378         for (base = scene->base.first; base; base = base->next) {
1379                 Object *object = base->object;
1380
1381                 BKE_object_handle_update_ex(eval_ctx, scene_parent, object, scene->rigidbody_world, true);
1382
1383                 if (object->dup_group && (object->transflag & OB_DUPLIGROUP))
1384                         BKE_group_handle_recalc_and_update(eval_ctx, scene_parent, object, object->dup_group);
1385
1386                 /* always update layer, so that animating layers works (joshua july 2010) */
1387                 /* XXX commented out, this has depsgraph issues anyway - and this breaks setting scenes
1388                  * (on scene-set, the base-lay is copied to ob-lay (ton nov 2012) */
1389                 // base->lay = ob->lay;
1390         }
1391 }
1392
1393 static void scene_update_object_func(TaskPool *pool, void *taskdata, int threadid)
1394 {
1395 /* Disable print for now in favor of summary statistics at the end of update. */
1396 #define PRINT if (false) printf
1397
1398         ThreadedObjectUpdateState *state = (ThreadedObjectUpdateState *) BLI_task_pool_userdata(pool);
1399         void *node = taskdata;
1400         Object *object = DAG_get_node_object(node);
1401         EvaluationContext *eval_ctx = state->eval_ctx;
1402         Scene *scene = state->scene;
1403         Scene *scene_parent = state->scene_parent;
1404
1405 #ifdef MBALL_SINGLETHREAD_HACK
1406         if (object && object->type == OB_MBALL) {
1407                 state->has_mballs = true;
1408         }
1409         else
1410 #endif
1411         if (object) {
1412                 double start_time = 0.0;
1413                 bool add_to_stats = false;
1414
1415                 if (G.debug & G_DEBUG_DEPSGRAPH) {
1416                         if (object->recalc & OB_RECALC_ALL) {
1417                                 printf("Thread %d: update object %s\n", threadid, object->id.name);
1418                         }
1419
1420                         start_time = PIL_check_seconds_timer();
1421
1422                         if (object->recalc & OB_RECALC_ALL) {
1423                                 state->has_updated_objects = true;
1424                                 add_to_stats = true;
1425                         }
1426                 }
1427
1428                 /* We only update object itself here, dupli-group will be updated
1429                  * separately from main thread because of we've got no idea about
1430                  * dependencies inside the group.
1431                  */
1432                 BKE_object_handle_update_ex(eval_ctx, scene_parent, object, scene->rigidbody_world, false);
1433
1434                 /* Calculate statistics. */
1435                 if (add_to_stats) {
1436                         StatisicsEntry *entry;
1437
1438                         BLI_assert(threadid < BLI_pool_get_num_threads(pool));
1439
1440                         entry = MEM_mallocN(sizeof(StatisicsEntry), "update thread statistics");
1441                         entry->object = object;
1442                         entry->start_time = start_time;
1443                         entry->duration = PIL_check_seconds_timer() - start_time;
1444
1445                         BLI_addtail(&state->statistics[threadid], entry);
1446                 }
1447         }
1448         else {
1449                 PRINT("Threda %d: update node %s\n", threadid,
1450                       DAG_get_node_name(scene, node));
1451         }
1452
1453         /* Update will decrease child's valency and schedule child with zero valency. */
1454         DAG_threaded_update_handle_node_updated(node, scene_update_object_add_task, pool);
1455
1456 #undef PRINT
1457 }
1458
1459 static void scene_update_object_add_task(void *node, void *user_data)
1460 {
1461         TaskPool *task_pool = user_data;
1462
1463         BLI_task_pool_push(task_pool, scene_update_object_func, node, false, TASK_PRIORITY_LOW);
1464 }
1465
1466 static void print_threads_statistics(ThreadedObjectUpdateState *state)
1467 {
1468         int i, tot_thread;
1469         double finish_time;
1470
1471         if ((G.debug & G_DEBUG_DEPSGRAPH) == 0) {
1472                 return;
1473         }
1474
1475 #ifdef DETAILED_ANALYSIS_OUTPUT
1476         if (state->has_updated_objects) {
1477                 tot_thread = BLI_system_thread_count();
1478
1479                 fprintf(stderr, "objects update base time %f\n", state->base_time);
1480
1481                 for (i = 0; i < tot_thread; i++) {
1482                         StatisicsEntry *entry;
1483                         for (entry = state->statistics[i].first;
1484                              entry;
1485                              entry = entry->next)
1486                         {
1487                                 fprintf(stderr, "thread %d object %s start_time %f duration %f\n",
1488                                         i, entry->object->id.name + 2,
1489                                         entry->start_time, entry->duration);
1490                         }
1491                         BLI_freelistN(&state->statistics[i]);
1492                 }
1493         }
1494 #else
1495         finish_time = PIL_check_seconds_timer();
1496         tot_thread = BLI_system_thread_count();
1497
1498         for (i = 0; i < tot_thread; i++) {
1499                 int total_objects = 0;
1500                 double total_time = 0.0;
1501                 StatisicsEntry *entry;
1502
1503                 if (state->has_updated_objects) {
1504                         /* Don't pollute output if no objects were updated. */
1505                         for (entry = state->statistics[i].first;
1506                              entry;
1507                              entry = entry->next)
1508                         {
1509                                 total_objects++;
1510                                 total_time += entry->duration;
1511                         }
1512
1513                         printf("Thread %d: total %d objects in %f sec.\n", i, total_objects, total_time);
1514
1515                         for (entry = state->statistics[i].first;
1516                              entry;
1517                              entry = entry->next)
1518                         {
1519                                 printf("  %s in %f sec\n", entry->object->id.name + 2, entry->duration);
1520                         }
1521                 }
1522
1523                 BLI_freelistN(&state->statistics[i]);
1524         }
1525         if (state->has_updated_objects) {
1526                 printf("Scene update in %f sec\n", finish_time - state->base_time);
1527         }
1528 #endif
1529 }
1530
1531 static bool scene_need_update_objects(Main *bmain)
1532 {
1533         return
1534                 /* Object datablocks themselves (for OB_RECALC_OB) */
1535                 DAG_id_type_tagged(bmain, ID_OB) ||
1536
1537                 /* Objects data datablocks (for OB_RECALC_DATA) */
1538                 DAG_id_type_tagged(bmain, ID_ME)  ||  /* Mesh */
1539                 DAG_id_type_tagged(bmain, ID_CU)  ||  /* Curve */
1540                 DAG_id_type_tagged(bmain, ID_MB)  ||  /* MetaBall */
1541                 DAG_id_type_tagged(bmain, ID_LA)  ||  /* Lamp */
1542                 DAG_id_type_tagged(bmain, ID_LT)  ||  /* Lattice */
1543                 DAG_id_type_tagged(bmain, ID_CA)  ||  /* Camera */
1544                 DAG_id_type_tagged(bmain, ID_KE)  ||  /* KE */
1545                 DAG_id_type_tagged(bmain, ID_SPK) ||  /* Speaker */
1546                 DAG_id_type_tagged(bmain, ID_AR);     /* Armature */
1547 }
1548
1549 static void scene_update_objects(EvaluationContext *eval_ctx, Main *bmain, Scene *scene, Scene *scene_parent)
1550 {
1551         TaskScheduler *task_scheduler = BLI_task_scheduler_get();
1552         TaskPool *task_pool;
1553         ThreadedObjectUpdateState state;
1554         bool need_singlethread_pass;
1555
1556         /* Early check for whether we need to invoke all the task-based
1557          * things (spawn new ppol, traverse dependency graph and so on).
1558          *
1559          * Basically if there's no ID datablocks tagged for update which
1560          * corresponds to object->recalc flags (which are checked in
1561          * BKE_object_handle_update() then we do nothing here.
1562          */
1563         if (!scene_need_update_objects(bmain)) {
1564                 return;
1565         }
1566
1567         state.eval_ctx = eval_ctx;
1568         state.scene = scene;
1569         state.scene_parent = scene_parent;
1570
1571         /* Those are only needed when blender is run with --debug argument. */
1572         if (G.debug & G_DEBUG_DEPSGRAPH) {
1573                 const int tot_thread = BLI_task_scheduler_num_threads(task_scheduler);
1574                 state.statistics = MEM_callocN(tot_thread * sizeof(*state.statistics),
1575                                                "scene update objects stats");
1576                 state.has_updated_objects = false;
1577                 state.base_time = PIL_check_seconds_timer();
1578         }
1579
1580 #ifdef MBALL_SINGLETHREAD_HACK
1581         state.has_mballs = false;
1582 #endif
1583
1584         task_pool = BLI_task_pool_create(task_scheduler, &state);
1585         if (G.debug & G_DEBUG_DEPSGRAPH_NO_THREADS) {
1586                 BLI_pool_set_num_threads(task_pool, 1);
1587         }
1588
1589         DAG_threaded_update_begin(scene, scene_update_object_add_task, task_pool);
1590         BLI_task_pool_work_and_wait(task_pool);
1591         BLI_task_pool_free(task_pool);
1592
1593         if (G.debug & G_DEBUG_DEPSGRAPH) {
1594                 print_threads_statistics(&state);
1595                 MEM_freeN(state.statistics);
1596         }
1597
1598         /* We do single thread pass to update all the objects which are in cyclic dependency.
1599          * Such objects can not be handled by a generic DAG traverse and it's really tricky
1600          * to detect whether cycle could be solved or not.
1601          *
1602          * In this situation we simply update all remaining objects in a single thread and
1603          * it'll happen in the same exact order as it was in single-threaded DAG.
1604          *
1605          * We couldn't use threaded update for objects which are in cycle because they might
1606          * access data of each other which is being re-evaluated.
1607          *
1608          * Also, as was explained above, for now we also update all the mballs in single thread.
1609          *
1610          *                                                                   - sergey -
1611          */
1612         need_singlethread_pass = DAG_is_acyclic(scene) == false;
1613 #ifdef MBALL_SINGLETHREAD_HACK
1614         need_singlethread_pass |= state.has_mballs;
1615 #endif
1616
1617         if (need_singlethread_pass) {
1618                 scene_update_all_bases(eval_ctx, scene, scene_parent);
1619         }
1620 }
1621
1622 static void scene_update_tagged_recursive(EvaluationContext *eval_ctx, Main *bmain, Scene *scene, Scene *scene_parent)
1623 {
1624         scene->customdata_mask = scene_parent->customdata_mask;
1625
1626         /* sets first, we allow per definition current scene to have
1627          * dependencies on sets, but not the other way around. */
1628         if (scene->set)
1629                 scene_update_tagged_recursive(eval_ctx, bmain, scene->set, scene_parent);
1630
1631         /* scene objects */
1632         scene_update_objects(eval_ctx, bmain, scene, scene_parent);
1633
1634         /* scene drivers... */
1635         scene_update_drivers(bmain, scene);
1636
1637         /* update masking curves */
1638         BKE_mask_update_scene(bmain, scene);
1639         
1640 }
1641 #endif  /* WITH_LEGACY_DEPSGRAPH */
1642
1643 static bool check_rendered_viewport_visible(Main *bmain)
1644 {
1645         wmWindowManager *wm = bmain->wm.first;
1646         wmWindow *window;
1647         for (window = wm->windows.first; window != NULL; window = window->next) {
1648                 bScreen *screen = window->screen;
1649                 ScrArea *area;
1650                 for (area = screen->areabase.first; area != NULL; area = area->next) {
1651                         View3D *v3d = area->spacedata.first;
1652                         if (area->spacetype != SPACE_VIEW3D) {
1653                                 continue;
1654                         }
1655                         if (v3d->drawtype == OB_RENDER) {
1656                                 return true;
1657                         }
1658                 }
1659         }
1660         return false;
1661 }
1662
1663 static void prepare_mesh_for_viewport_render(Main *bmain, Scene *scene)
1664 {
1665         /* This is needed to prepare mesh to be used by the render
1666          * engine from the viewport rendering. We do loading here
1667          * so all the objects which shares the same mesh datablock
1668          * are nicely tagged for update and updated.
1669          *
1670          * This makes it so viewport render engine doesn't need to
1671          * call loading of the edit data for the mesh objects.
1672          */
1673
1674         Object *obedit = scene->obedit;
1675         if (obedit) {
1676                 Mesh *mesh = obedit->data;
1677                 if ((obedit->type == OB_MESH) &&
1678                     ((obedit->id.flag & LIB_ID_RECALC_ALL) ||
1679                      (mesh->id.flag & LIB_ID_RECALC_ALL)))
1680                 {
1681                         if (check_rendered_viewport_visible(bmain)) {
1682                                 BMesh *bm = mesh->edit_btmesh->bm;
1683                                 BM_mesh_bm_to_me(bm, mesh, false);
1684                                 DAG_id_tag_update(&mesh->id, 0);
1685                         }
1686                 }
1687         }
1688 }
1689
1690 void BKE_scene_update_tagged(EvaluationContext *eval_ctx, Main *bmain, Scene *scene)
1691 {
1692         Scene *sce_iter;
1693 #ifdef WITH_LEGACY_DEPSGRAPH
1694         bool use_new_eval = !DEG_depsgraph_use_legacy();
1695 #endif
1696
1697         /* keep this first */
1698         BLI_callback_exec(bmain, &scene->id, BLI_CB_EVT_SCENE_UPDATE_PRE);
1699
1700         /* (re-)build dependency graph if needed */
1701         for (sce_iter = scene; sce_iter; sce_iter = sce_iter->set) {
1702                 DAG_scene_relations_update(bmain, sce_iter);
1703                 /* Uncomment this to check if graph was properly tagged for update. */
1704 #if 0
1705 #ifdef WITH_LEGACY_DEPSGRAPH
1706                 if (use_new_eval)
1707 #endif
1708                 {
1709                         DAG_scene_relations_validate(bmain, sce_iter);
1710                 }
1711 #endif
1712         }
1713
1714         /* flush editing data if needed */
1715         prepare_mesh_for_viewport_render(bmain, scene);
1716
1717         /* flush recalc flags to dependencies */
1718         DAG_ids_flush_tagged(bmain);
1719
1720         /* removed calls to quick_cache, see pointcache.c */
1721         
1722         /* clear "LIB_DOIT" flag from all materials, to prevent infinite recursion problems later 
1723          * when trying to find materials with drivers that need evaluating [#32017] 
1724          */
1725         BKE_main_id_tag_idcode(bmain, ID_MA, false);
1726         BKE_main_id_tag_idcode(bmain, ID_LA, false);
1727
1728         /* update all objects: drivers, matrices, displists, etc. flags set
1729          * by depgraph or manual, no layer check here, gets correct flushed
1730          *
1731          * in the future this should handle updates for all datablocks, not
1732          * only objects and scenes. - brecht */
1733 #ifdef WITH_LEGACY_DEPSGRAPH
1734         if (!use_new_eval) {
1735                 scene_update_tagged_recursive(eval_ctx, bmain, scene, scene);
1736         }
1737         else
1738 #endif
1739         {
1740                 DEG_evaluate_on_refresh(eval_ctx, scene->depsgraph, scene);
1741                 /* TODO(sergey): This is to beocme a node in new depsgraph. */
1742                 BKE_mask_update_scene(bmain, scene);
1743         }
1744
1745         /* update sound system animation (TODO, move to depsgraph) */
1746         BKE_sound_update_scene(bmain, scene);
1747
1748         /* extra call here to recalc scene animation (for sequencer) */
1749         {
1750                 AnimData *adt = BKE_animdata_from_id(&scene->id);
1751                 float ctime = BKE_scene_frame_get(scene);
1752                 
1753                 if (adt && (adt->recalc & ADT_RECALC_ANIM))
1754                         BKE_animsys_evaluate_animdata(scene, &scene->id, adt, ctime, 0);
1755         }
1756
1757         /* Extra call here to recalc material animation.
1758          *
1759          * Need to do this so changing material settings from the graph/dopesheet
1760          * will update stuff in the viewport.
1761          */
1762 #ifdef WITH_LEGACY_DEPSGRAPH
1763         if (!use_new_eval && DAG_id_type_tagged(bmain, ID_MA)) {
1764                 Material *material;
1765                 float ctime = BKE_scene_frame_get(scene);
1766
1767                 for (material = bmain->mat.first;
1768                      material;
1769                      material = material->id.next)
1770                 {
1771                         AnimData *adt = BKE_animdata_from_id(&material->id);
1772                         if (adt && (adt->recalc & ADT_RECALC_ANIM))
1773                                 BKE_animsys_evaluate_animdata(scene, &material->id, adt, ctime, 0);
1774                 }
1775         }
1776
1777         /* Also do the same for node trees. */
1778         if (!use_new_eval && DAG_id_type_tagged(bmain, ID_NT)) {
1779                 float ctime = BKE_scene_frame_get(scene);
1780
1781                 FOREACH_NODETREE(bmain, ntree, id)
1782                 {
1783                         AnimData *adt = BKE_animdata_from_id(&ntree->id);
1784                         if (adt && (adt->recalc & ADT_RECALC_ANIM))
1785                                 BKE_animsys_evaluate_animdata(scene, &ntree->id, adt, ctime, 0);
1786                 }
1787                 FOREACH_NODETREE_END
1788         }
1789 #endif
1790
1791         /* notify editors and python about recalc */
1792         BLI_callback_exec(bmain, &scene->id, BLI_CB_EVT_SCENE_UPDATE_POST);
1793
1794         /* Inform editors about possible changes. */
1795         DAG_ids_check_recalc(bmain, scene, false);
1796
1797         /* clear recalc flags */
1798         DAG_ids_clear_recalc(bmain);
1799 }
1800
1801 /* applies changes right away, does all sets too */
1802 void BKE_scene_update_for_newframe(EvaluationContext *eval_ctx, Main *bmain, Scene *sce, unsigned int lay)
1803 {
1804         BKE_scene_update_for_newframe_ex(eval_ctx, bmain, sce, lay, false);
1805 }
1806
1807 void BKE_scene_update_for_newframe_ex(EvaluationContext *eval_ctx, Main *bmain, Scene *sce, unsigned int lay, bool do_invisible_flush)
1808 {
1809         float ctime = BKE_scene_frame_get(sce);
1810         Scene *sce_iter;
1811 #ifdef DETAILED_ANALYSIS_OUTPUT
1812         double start_time = PIL_check_seconds_timer();
1813 #endif
1814 #ifdef WITH_LEGACY_DEPSGRAPH
1815         bool use_new_eval = !DEG_depsgraph_use_legacy();
1816 #else
1817         /* TODO(sergey): Pass to evaluation routines instead of storing layer in the graph? */
1818         (void) do_invisible_flush;
1819 #endif
1820
1821         /* keep this first */
1822         BLI_callback_exec(bmain, &sce->id, BLI_CB_EVT_FRAME_CHANGE_PRE);
1823         BLI_callback_exec(bmain, &sce->id, BLI_CB_EVT_SCENE_UPDATE_PRE);
1824
1825         /* update animated image textures for particles, modifiers, gpu, etc,
1826          * call this at the start so modifiers with textures don't lag 1 frame */
1827         BKE_image_update_frame(bmain, sce->r.cfra);
1828         
1829 #ifdef WITH_LEGACY_DEPSGRAPH
1830         /* rebuild rigid body worlds before doing the actual frame update
1831          * this needs to be done on start frame but animation playback usually starts one frame later
1832          * we need to do it here to avoid rebuilding the world on every simulation change, which can be very expensive
1833          */
1834         if (!use_new_eval) {
1835                 scene_rebuild_rbw_recursive(sce, ctime);
1836         }
1837 #endif
1838         
1839         BKE_sound_set_cfra(sce->r.cfra);
1840         
1841         /* clear animation overrides */
1842         /* XXX TODO... */
1843
1844         for (sce_iter = sce; sce_iter; sce_iter = sce_iter->set)
1845                 DAG_scene_relations_update(bmain, sce_iter);
1846
1847 #ifdef WITH_LEGACY_DEPSGRAPH
1848         if (!use_new_eval) {
1849                 /* flush recalc flags to dependencies, if we were only changing a frame
1850                  * this would not be necessary, but if a user or a script has modified
1851                  * some datablock before BKE_scene_update_tagged was called, we need the flush */
1852                 DAG_ids_flush_tagged(bmain);
1853
1854                 /* Following 2 functions are recursive
1855                  * so don't call within 'scene_update_tagged_recursive' */
1856                 DAG_scene_update_flags(bmain, sce, lay, true, do_invisible_flush);   // only stuff that moves or needs display still
1857         }
1858 #endif
1859
1860         BKE_mask_evaluate_all_masks(bmain, ctime, true);
1861
1862 #ifdef POSE_ANIMATION_WORKAROUND
1863         scene_armature_depsgraph_workaround(bmain);
1864 #endif
1865
1866         /* All 'standard' (i.e. without any dependencies) animation is handled here,
1867          * with an 'local' to 'macro' order of evaluation. This should ensure that
1868          * settings stored nestled within a hierarchy (i.e. settings in a Texture block
1869          * can be overridden by settings from Scene, which owns the Texture through a hierarchy
1870          * such as Scene->World->MTex/Texture) can still get correctly overridden.
1871          */
1872 #ifdef WITH_LEGACY_DEPSGRAPH
1873         if (!use_new_eval) {
1874                 BKE_animsys_evaluate_all_animation(bmain, sce, ctime);
1875                 /*...done with recursive funcs */
1876         }
1877 #endif
1878
1879         /* clear "LIB_DOIT" flag from all materials, to prevent infinite recursion problems later 
1880          * when trying to find materials with drivers that need evaluating [#32017] 
1881          */
1882         BKE_main_id_tag_idcode(bmain, ID_MA, false);
1883         BKE_main_id_tag_idcode(bmain, ID_LA, false);
1884
1885         /* run rigidbody sim */
1886         /* NOTE: current position is so that rigidbody sim affects other objects, might change in the future */
1887 #ifdef WITH_LEGACY_DEPSGRAPH
1888         if (!use_new_eval) {
1889                 scene_do_rb_simulation_recursive(sce, ctime);
1890         }
1891 #endif
1892         
1893         /* BKE_object_handle_update() on all objects, groups and sets */
1894 #ifdef WITH_LEGACY_DEPSGRAPH
1895         if (use_new_eval) {
1896                 DEG_evaluate_on_framechange(eval_ctx, bmain, sce->depsgraph, ctime, lay);
1897         }
1898         else {
1899                 scene_update_tagged_recursive(eval_ctx, bmain, sce, sce);
1900         }
1901 #else
1902         DEG_evaluate_on_framechange(eval_ctx, bmain, sce->depsgraph, ctime, lay);
1903 #endif
1904
1905         /* update sound system animation (TODO, move to depsgraph) */
1906         BKE_sound_update_scene(bmain, sce);
1907
1908 #ifdef WITH_LEGACY_DEPSGRAPH
1909         if (!use_new_eval) {
1910                 scene_depsgraph_hack(eval_ctx, sce, sce);
1911         }
1912 #endif
1913
1914         /* notify editors and python about recalc */
1915         BLI_callback_exec(bmain, &sce->id, BLI_CB_EVT_SCENE_UPDATE_POST);
1916         BLI_callback_exec(bmain, &sce->id, BLI_CB_EVT_FRAME_CHANGE_POST);
1917
1918         /* Inform editors about possible changes. */
1919         DAG_ids_check_recalc(bmain, sce, true);
1920
1921         /* clear recalc flags */
1922         DAG_ids_clear_recalc(bmain);
1923
1924 #ifdef DETAILED_ANALYSIS_OUTPUT
1925         fprintf(stderr, "frame update start_time %f duration %f\n", start_time, PIL_check_seconds_timer() - start_time);
1926 #endif
1927 }
1928
1929 /* return default layer, also used to patch old files */
1930 SceneRenderLayer *BKE_scene_add_render_layer(Scene *sce, const char *name)
1931 {
1932         SceneRenderLayer *srl;
1933
1934         if (!name)
1935                 name = DATA_("RenderLayer");
1936
1937         srl = MEM_callocN(sizeof(SceneRenderLayer), "new render layer");
1938         BLI_strncpy(srl->name, name, sizeof(srl->name));
1939         BLI_uniquename(&sce->r.layers, srl, DATA_("RenderLayer"), '.', offsetof(SceneRenderLayer, name), sizeof(srl->name));
1940         BLI_addtail(&sce->r.layers, srl);
1941
1942         /* note, this is also in render, pipeline.c, to make layer when scenedata doesnt have it */
1943         srl->lay = (1 << 20) - 1;
1944         srl->layflag = 0x7FFF;   /* solid ztra halo edge strand */
1945         srl->passflag = SCE_PASS_COMBINED | SCE_PASS_Z;
1946         srl->pass_alpha_threshold = 0.5f;
1947         BKE_freestyle_config_init(&srl->freestyleConfig);
1948
1949         return srl;
1950 }
1951
1952 bool BKE_scene_remove_render_layer(Main *bmain, Scene *scene, SceneRenderLayer *srl)
1953 {
1954         const int act = BLI_findindex(&scene->r.layers, srl);
1955         Scene *sce;
1956
1957         if (act == -1) {
1958                 return false;
1959         }
1960         else if ( (scene->r.layers.first == scene->r.layers.last) &&
1961                   (scene->r.layers.first == srl))
1962         {
1963                 /* ensure 1 layer is kept */
1964                 return false;
1965         }
1966
1967         BLI_remlink(&scene->r.layers, srl);
1968         MEM_freeN(srl);
1969
1970         scene->r.actlay = 0;
1971
1972         for (sce = bmain->scene.first; sce; sce = sce->id.next) {
1973                 if (sce->nodetree) {
1974                         bNode *node;
1975                         for (node = sce->nodetree->nodes.first; node; node = node->next) {
1976                                 if (node->type == CMP_NODE_R_LAYERS && (Scene *)node->id == scene) {
1977                                         if (node->custom1 == act)
1978                                                 node->custom1 = 0;
1979                                         else if (node->custom1 > act)
1980                                                 node->custom1--;
1981                                 }
1982                         }
1983                 }
1984         }
1985
1986         return true;
1987 }
1988
1989 /* return default view */
1990 SceneRenderView *BKE_scene_add_render_view(Scene *sce, const char *name)
1991 {
1992         SceneRenderView *srv;
1993
1994         if (!name)
1995                 name = DATA_("RenderView");
1996
1997         srv = MEM_callocN(sizeof(SceneRenderView), "new render view");
1998         BLI_strncpy(srv->name, name, sizeof(srv->name));
1999         BLI_uniquename(&sce->r.views, srv, DATA_("RenderView"), '.', offsetof(SceneRenderView, name), sizeof(srv->name));
2000         BLI_addtail(&sce->r.views, srv);
2001
2002         return srv;
2003 }
2004
2005 bool BKE_scene_remove_render_view(Scene *scene, SceneRenderView *srv)
2006 {
2007         const int act = BLI_findindex(&scene->r.views, srv);
2008
2009         if (act == -1) {
2010                 return false;
2011         }
2012         else if (scene->r.views.first == scene->r.views.last) {
2013                 /* ensure 1 view is kept */
2014                 return false;
2015         }
2016
2017         BLI_remlink(&scene->r.views, srv);
2018         MEM_freeN(srv);
2019
2020         scene->r.actview = 0;
2021
2022         return true;
2023 }
2024
2025 /* render simplification */
2026
2027 int get_render_subsurf_level(const RenderData *r, int lvl, bool for_render)
2028 {
2029         if (r->mode & R_SIMPLIFY)  {
2030                 if (for_render)
2031                         return min_ii(r->simplify_subsurf_render, lvl);
2032                 else
2033                         return min_ii(r->simplify_subsurf, lvl);
2034         }
2035         else {
2036                 return lvl;
2037         }
2038 }
2039
2040 int get_render_child_particle_number(const RenderData *r, int num, bool for_render)
2041 {
2042         if (r->mode & R_SIMPLIFY) {
2043                 if (for_render)
2044                         return (int)(r->simplify_particles_render * num);
2045                 else
2046                         return (int)(r->simplify_particles * num);
2047         }
2048         else {
2049                 return num;
2050         }
2051 }
2052
2053 int get_render_shadow_samples(const RenderData *r, int samples)
2054 {
2055         if ((r->mode & R_SIMPLIFY) && samples > 0)
2056                 return min_ii(r->simplify_shadowsamples, samples);
2057         else
2058                 return samples;
2059 }
2060
2061 float get_render_aosss_error(const RenderData *r, float error)
2062 {
2063         if (r->mode & R_SIMPLIFY)
2064                 return ((1.0f - r->simplify_aosss) * 10.0f + 1.0f) * error;
2065         else
2066                 return error;
2067 }
2068
2069 /* helper function for the SETLOOPER macro */
2070 Base *_setlooper_base_step(Scene **sce_iter, Base *base)
2071 {
2072         if (base && base->next) {
2073                 /* common case, step to the next */
2074                 return base->next;
2075         }
2076         else if (base == NULL && (*sce_iter)->base.first) {
2077                 /* first time looping, return the scenes first base */
2078                 return (Base *)(*sce_iter)->base.first;
2079         }
2080         else {
2081                 /* reached the end, get the next base in the set */
2082                 while ((*sce_iter = (*sce_iter)->set)) {
2083                         base = (Base *)(*sce_iter)->base.first;
2084                         if (base) {
2085                                 return base;
2086                         }
2087                 }
2088         }
2089
2090         return NULL;
2091 }
2092
2093 bool BKE_scene_use_new_shading_nodes(const Scene *scene)
2094 {
2095         const RenderEngineType *type = RE_engines_find(scene->r.engine);
2096         return (type && type->flag & RE_USE_SHADING_NODES);
2097 }
2098
2099 bool BKE_scene_use_shading_nodes_custom(Scene *scene)
2100 {
2101            RenderEngineType *type = RE_engines_find(scene->r.engine);
2102            return (type && type->flag & RE_USE_SHADING_NODES_CUSTOM);
2103 }
2104
2105 bool BKE_scene_uses_blender_internal(const  Scene *scene)
2106 {
2107         return STREQ(scene->r.engine, RE_engine_id_BLENDER_RENDER);
2108 }
2109
2110 bool BKE_scene_uses_blender_game(const Scene *scene)
2111 {
2112         return STREQ(scene->r.engine, RE_engine_id_BLENDER_GAME);
2113 }
2114
2115 void BKE_scene_base_flag_to_objects(struct Scene *scene)
2116 {
2117         Base *base = scene->base.first;
2118
2119         while (base) {
2120                 base->object->flag = base->flag;
2121                 base = base->next;
2122         }
2123 }
2124
2125 void BKE_scene_base_flag_from_objects(struct Scene *scene)
2126 {
2127         Base *base = scene->base.first;
2128
2129         while (base) {
2130                 base->flag = base->object->flag;
2131                 base = base->next;
2132         }
2133 }
2134
2135 void BKE_scene_disable_color_management(Scene *scene)
2136 {
2137         ColorManagedDisplaySettings *display_settings = &scene->display_settings;
2138         ColorManagedViewSettings *view_settings = &scene->view_settings;
2139         const char *view;
2140         const char *none_display_name;
2141
2142         none_display_name = IMB_colormanagement_display_get_none_name();
2143
2144         BLI_strncpy(display_settings->display_device, none_display_name, sizeof(display_settings->display_device));
2145
2146         view = IMB_colormanagement_view_get_default_name(display_settings->display_device);
2147
2148         if (view) {
2149                 BLI_strncpy(view_settings->view_transform, view, sizeof(view_settings->view_transform));
2150         }
2151 }
2152
2153 bool BKE_scene_check_color_management_enabled(const Scene *scene)
2154 {
2155         return !STREQ(scene->display_settings.display_device, "None");
2156 }
2157
2158 bool BKE_scene_check_rigidbody_active(const Scene *scene)
2159 {
2160         return scene && scene->rigidbody_world && scene->rigidbody_world->group && !(scene->rigidbody_world->flag & RBW_FLAG_MUTED);
2161 }
2162
2163 int BKE_render_num_threads(const RenderData *rd)
2164 {
2165         int threads;
2166
2167         /* override set from command line? */
2168         threads = BLI_system_num_threads_override_get();
2169
2170         if (threads > 0)
2171                 return threads;
2172
2173         /* fixed number of threads specified in scene? */
2174         if (rd->mode & R_FIXED_THREADS)
2175                 threads = rd->threads;
2176         else
2177                 threads = BLI_system_thread_count();
2178         
2179         return max_ii(threads, 1);
2180 }
2181
2182 int BKE_scene_num_threads(const Scene *scene)
2183 {
2184         return BKE_render_num_threads(&scene->r);
2185 }
2186
2187 /* Apply the needed correction factor to value, based on unit_type (only length-related are affected currently)
2188  * and unit->scale_length.
2189  */
2190 double BKE_scene_unit_scale(const UnitSettings *unit, const int unit_type, double value)
2191 {
2192         if (unit->system == USER_UNIT_NONE) {
2193                 /* Never apply scale_length when not using a unit setting! */
2194                 return value;
2195         }
2196
2197         switch (unit_type) {
2198                 case B_UNIT_LENGTH:
2199                         return value * (double)unit->scale_length;
2200                 case B_UNIT_AREA:
2201                         return value * pow(unit->scale_length, 2);
2202                 case B_UNIT_VOLUME:
2203                         return value * pow(unit->scale_length, 3);
2204                 case B_UNIT_MASS:
2205                         return value * pow(unit->scale_length, 3);
2206                 case B_UNIT_CAMERA:  /* *Do not* use scene's unit scale for camera focal lens! See T42026. */
2207                 default:
2208                         return value;
2209         }
2210 }
2211
2212 /******************** multiview *************************/
2213
2214 size_t BKE_scene_multiview_num_views_get(const RenderData *rd)
2215 {
2216         SceneRenderView *srv;
2217         size_t totviews = 0;
2218
2219         if ((rd->scemode & R_MULTIVIEW) == 0)
2220                 return 1;
2221
2222         if (rd->views_format == SCE_VIEWS_FORMAT_STEREO_3D) {
2223                 srv = BLI_findstring(&rd->views, STEREO_LEFT_NAME, offsetof(SceneRenderView, name));
2224                 if ((srv && srv->viewflag & SCE_VIEW_DISABLE) == 0) {
2225                         totviews++;
2226                 }
2227
2228                 srv = BLI_findstring(&rd->views, STEREO_RIGHT_NAME, offsetof(SceneRenderView, name));
2229                 if ((srv && srv->viewflag & SCE_VIEW_DISABLE) == 0) {
2230                         totviews++;
2231                 }
2232         }
2233         else {
2234                 for (srv = rd->views.first; srv; srv = srv->next) {
2235                         if ((srv->viewflag & SCE_VIEW_DISABLE) == 0) {
2236                                 totviews++;
2237                         }
2238                 }
2239         }
2240         return totviews;
2241 }
2242
2243 bool BKE_scene_multiview_is_stereo3d(const RenderData *rd)
2244 {
2245         SceneRenderView *srv[2];
2246
2247         if ((rd->scemode & R_MULTIVIEW) == 0)
2248                 return false;
2249
2250         srv[0] = (SceneRenderView *)BLI_findstring(&rd->views, STEREO_LEFT_NAME, offsetof(SceneRenderView, name));
2251         srv[1] = (SceneRenderView *)BLI_findstring(&rd->views, STEREO_RIGHT_NAME, offsetof(SceneRenderView, name));
2252
2253         return (srv[0] && ((srv[0]->viewflag & SCE_VIEW_DISABLE) == 0) &&
2254                 srv[1] && ((srv[1]->viewflag & SCE_VIEW_DISABLE) == 0));
2255 }
2256
2257 /* return whether to render this SceneRenderView */
2258 bool BKE_scene_multiview_is_render_view_active(const RenderData *rd, const SceneRenderView *srv)
2259 {
2260         if (srv == NULL)
2261                 return false;
2262
2263         if ((rd->scemode & R_MULTIVIEW) == 0)
2264                 return false;
2265
2266         if ((srv->viewflag & SCE_VIEW_DISABLE))
2267                 return false;
2268
2269         if (rd->views_format == SCE_VIEWS_FORMAT_MULTIVIEW)
2270                 return true;
2271
2272         /* SCE_VIEWS_SETUP_BASIC */
2273         if (STREQ(srv->name, STEREO_LEFT_NAME) ||
2274             STREQ(srv->name, STEREO_RIGHT_NAME))
2275         {
2276                 return true;
2277         }
2278
2279         return false;
2280 }
2281
2282 /* return true if viewname is the first or if the name is NULL or not found */
2283 bool BKE_scene_multiview_is_render_view_first(const RenderData *rd, const char *viewname)
2284 {
2285         SceneRenderView *srv;
2286
2287         if ((rd->scemode & R_MULTIVIEW) == 0)
2288                 return true;
2289
2290         if ((!viewname) || (!viewname[0]))
2291                 return true;
2292
2293         for (srv = rd->views.first; srv; srv = srv->next) {
2294                 if (BKE_scene_multiview_is_render_view_active(rd, srv)) {
2295                         return STREQ(viewname, srv->name);
2296                 }
2297         }
2298
2299         return true;
2300 }
2301
2302 /* return true if viewname is the last or if the name is NULL or not found */
2303 bool BKE_scene_multiview_is_render_view_last(const RenderData *rd, const char *viewname)
2304 {
2305         SceneRenderView *srv;
2306
2307         if ((rd->scemode & R_MULTIVIEW) == 0)
2308                 return true;
2309
2310         if ((!viewname) || (!viewname[0]))
2311                 return true;
2312
2313         for (srv = rd->views.last; srv; srv = srv->prev) {
2314                 if (BKE_scene_multiview_is_render_view_active(rd, srv)) {
2315                         return STREQ(viewname, srv->name);
2316                 }
2317         }
2318
2319         return true;
2320 }
2321
2322 SceneRenderView *BKE_scene_multiview_render_view_findindex(const RenderData *rd, const int view_id)
2323 {
2324         SceneRenderView *srv;
2325         size_t nr;
2326
2327         if ((rd->scemode & R_MULTIVIEW) == 0)
2328                 return NULL;
2329
2330         nr = 0;
2331         for (srv = rd->views.first, nr = 0; srv; srv = srv->next) {
2332                 if (BKE_scene_multiview_is_render_view_active(rd, srv)) {
2333                         if (nr++ == view_id)
2334                                 return srv;
2335                 }
2336         }
2337         return srv;
2338 }
2339
2340 const char *BKE_scene_multiview_render_view_name_get(const RenderData *rd, const int view_id)
2341 {
2342         SceneRenderView *srv = BKE_scene_multiview_render_view_findindex(rd, view_id);
2343
2344         if (srv)
2345                 return srv->name;
2346         else
2347                 return "";
2348 }
2349
2350 size_t BKE_scene_multiview_view_id_get(const RenderData *rd, const char *viewname)
2351 {
2352         SceneRenderView *srv;
2353         size_t nr;
2354
2355         if ((!rd) || ((rd->scemode & R_MULTIVIEW) == 0))
2356                 return 0;
2357
2358         if ((!viewname) || (!viewname[0]))
2359                 return 0;
2360
2361         nr = 0;
2362         for (srv = rd->views.first, nr = 0; srv; srv = srv->next) {
2363                 if (BKE_scene_multiview_is_render_view_active(rd, srv)) {
2364                         if (STREQ(viewname, srv->name)) {
2365                                 return nr;
2366                         }
2367                         else {
2368                                 nr += 1;
2369                         }
2370                 }
2371         }
2372
2373         return 0;
2374 }
2375
2376 void BKE_scene_multiview_filepath_get(
2377         SceneRenderView *srv, const char *filepath,
2378         char *r_filepath)
2379 {
2380         BLI_strncpy(r_filepath, filepath, FILE_MAX);
2381         BLI_path_suffix(r_filepath, FILE_MAX, srv->suffix, "");
2382 }
2383
2384 /**
2385  * When multiview is not used the filepath is as usual (e.g., ``Image.jpg``).
2386  * When multiview is on, even if only one view is enabled the view is incorporated
2387  * into the file name (e.g., ``Image_L.jpg``). That allows for the user to re-render
2388  * individual views.
2389  */
2390 void BKE_scene_multiview_view_filepath_get(
2391         const RenderData *rd, const char *filepath, const char *viewname,
2392         char *r_filepath)
2393 {
2394         SceneRenderView *srv;
2395         char suffix[FILE_MAX];
2396
2397         srv = BLI_findstring(&rd->views, viewname, offsetof(SceneRenderView, name));
2398         if (srv)
2399                 BLI_strncpy(suffix, srv->suffix, sizeof(suffix));
2400         else
2401                 BLI_strncpy(suffix, viewname, sizeof(suffix));
2402
2403         BLI_strncpy(r_filepath, filepath, FILE_MAX);
2404         BLI_path_suffix(r_filepath, FILE_MAX, suffix, "");
2405 }
2406
2407 const char *BKE_scene_multiview_view_suffix_get(const RenderData *rd, const char *viewname)
2408 {
2409         SceneRenderView *srv;
2410
2411         if ((viewname == NULL) || (viewname[0] == '\0'))
2412                 return viewname;
2413
2414         srv = BLI_findstring(&rd->views, viewname, offsetof(SceneRenderView, name));
2415         if (srv)
2416                 return srv->suffix;
2417         else
2418                 return viewname;
2419 }
2420
2421 const char *BKE_scene_multiview_view_id_suffix_get(const RenderData *rd, const size_t view_id)
2422 {
2423         if ((rd->scemode & R_MULTIVIEW) == 0) {
2424                 return "";
2425         }
2426         else {
2427                 const char *viewname = BKE_scene_multiview_render_view_name_get(rd, view_id);
2428                 return BKE_scene_multiview_view_suffix_get(rd, viewname);
2429         }
2430 }
2431
2432 void BKE_scene_multiview_view_prefix_get(Scene *scene, const char *name, char *rprefix, const char **rext)
2433 {
2434         SceneRenderView *srv;
2435         size_t index_act;
2436         const char *suf_act;
2437         const char delims[] = {'.', '\0'};
2438
2439         rprefix[0] = '\0';
2440
2441         /* begin of extension */
2442         index_act = BLI_str_rpartition(name, delims, rext, &suf_act);
2443         BLI_assert(index_act > 0);
2444         UNUSED_VARS_NDEBUG(index_act);
2445
2446         for (srv = scene->r.views.first; srv; srv = srv->next) {
2447                 if (BKE_scene_multiview_is_render_view_active(&scene->r, srv)) {
2448                         size_t len = strlen(srv->suffix);
2449                         if (STREQLEN(*rext - len, srv->suffix, len)) {
2450                                 BLI_strncpy(rprefix, name, strlen(name) - strlen(*rext) - len + 1);
2451                                 break;
2452                         }
2453                 }
2454         }
2455 }
2456
2457 void BKE_scene_multiview_videos_dimensions_get(
2458         const RenderData *rd, const size_t width, const size_t height,
2459         size_t *r_width, size_t *r_height)
2460 {
2461         if ((rd->scemode & R_MULTIVIEW) &&
2462             rd->im_format.views_format == R_IMF_VIEWS_STEREO_3D)
2463         {
2464                 IMB_stereo3d_write_dimensions(
2465                         rd->im_format.stereo3d_format.display_mode,
2466                         (rd->im_format.stereo3d_format.flag & S3D_SQUEEZED_FRAME) != 0,
2467                         width, height,
2468                         r_width, r_height);
2469         }
2470         else {
2471                 *r_width = width;
2472                 *r_height = height;
2473         }
2474 }
2475
2476 size_t BKE_scene_multiview_num_videos_get(const RenderData *rd)
2477 {
2478         if (BKE_imtype_is_movie(rd->im_format.imtype) == false)
2479                 return 0;
2480
2481         if ((rd->scemode & R_MULTIVIEW) == 0)
2482                 return 1;
2483
2484         if (rd->im_format.views_format == R_IMF_VIEWS_STEREO_3D) {
2485                 return 1;
2486         }
2487         else {
2488                 /* R_IMF_VIEWS_INDIVIDUAL */
2489                 return BKE_scene_multiview_num_views_get(rd);
2490         }
2491 }