Cycles: svn merge -r41205:41225 ^/trunk/blender
[blender.git] / source / blender / blenkernel / intern / scene.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 /** \file blender/blenkernel/intern/scene.c
31  *  \ingroup bke
32  */
33
34
35 #include <stddef.h>
36 #include <stdio.h>
37 #include <string.h>
38
39 #ifndef WIN32 
40 #include <unistd.h>
41 #else
42 #include <io.h>
43 #endif
44
45 #include "MEM_guardedalloc.h"
46
47 #include "DNA_anim_types.h"
48 #include "DNA_group_types.h"
49 #include "DNA_object_types.h"
50 #include "DNA_scene_types.h"
51 #include "DNA_screen_types.h"
52 #include "DNA_sequence_types.h"
53
54 #include "BLI_math.h"
55 #include "BLI_blenlib.h"
56 #include "BLI_utildefines.h"
57 #include "BLI_callbacks.h"
58
59 #include "BKE_anim.h"
60 #include "BKE_animsys.h"
61 #include "BKE_depsgraph.h"
62 #include "BKE_global.h"
63 #include "BKE_group.h"
64 #include "BKE_idprop.h"
65 #include "BKE_library.h"
66 #include "BKE_main.h"
67 #include "BKE_node.h"
68 #include "BKE_object.h"
69 #include "BKE_paint.h"
70 #include "BKE_pointcache.h"
71 #include "BKE_scene.h"
72 #include "BKE_sequencer.h"
73 #include "BKE_world.h"
74
75 #include "BKE_sound.h"
76
77 #include "RE_engine.h"
78
79 //XXX #include "BIF_previewrender.h"
80 //XXX #include "BIF_editseq.h"
81
82 //XXX #include "nla.h"
83
84 #ifdef WIN32
85 #else
86 #include <sys/time.h>
87 #endif
88
89 void free_avicodecdata(AviCodecData *acd)
90 {
91         if (acd) {
92                 if (acd->lpFormat){
93                         MEM_freeN(acd->lpFormat);
94                         acd->lpFormat = NULL;
95                         acd->cbFormat = 0;
96                 }
97                 if (acd->lpParms){
98                         MEM_freeN(acd->lpParms);
99                         acd->lpParms = NULL;
100                         acd->cbParms = 0;
101                 }
102         }
103 }
104
105 void free_qtcodecdata(QuicktimeCodecData *qcd)
106 {
107         if (qcd) {
108                 if (qcd->cdParms){
109                         MEM_freeN(qcd->cdParms);
110                         qcd->cdParms = NULL;
111                         qcd->cdSize = 0;
112                 }
113         }
114 }
115
116 Scene *copy_scene(Scene *sce, int type)
117 {
118         Scene *scen;
119         ToolSettings *ts;
120         Base *base, *obase;
121         
122         if(type == SCE_COPY_EMPTY) {
123                 ListBase lb;
124                 scen= add_scene(sce->id.name+2);
125                 
126                 lb= scen->r.layers;
127                 scen->r= sce->r;
128                 scen->r.layers= lb;
129         }
130         else {
131                 scen= copy_libblock(sce);
132                 BLI_duplicatelist(&(scen->base), &(sce->base));
133                 
134                 clear_id_newpoins();
135                 
136                 id_us_plus((ID *)scen->world);
137                 id_us_plus((ID *)scen->set);
138                 id_us_plus((ID *)scen->gm.dome.warptext);
139
140                 scen->ed= NULL;
141                 scen->theDag= NULL;
142                 scen->obedit= NULL;
143                 scen->toolsettings= MEM_dupallocN(sce->toolsettings);
144                 scen->stats= NULL;
145                 scen->fps_info= NULL;
146
147                 ts= scen->toolsettings;
148                 if(ts) {
149                         if(ts->vpaint) {
150                                 ts->vpaint= MEM_dupallocN(ts->vpaint);
151                                 ts->vpaint->paintcursor= NULL;
152                                 ts->vpaint->vpaint_prev= NULL;
153                                 ts->vpaint->wpaint_prev= NULL;
154                                 copy_paint(&ts->vpaint->paint, &ts->vpaint->paint);
155                         }
156                         if(ts->wpaint) {
157                                 ts->wpaint= MEM_dupallocN(ts->wpaint);
158                                 ts->wpaint->paintcursor= NULL;
159                                 ts->wpaint->vpaint_prev= NULL;
160                                 ts->wpaint->wpaint_prev= NULL;
161                                 copy_paint(&ts->wpaint->paint, &ts->wpaint->paint);
162                         }
163                         if(ts->sculpt) {
164                                 ts->sculpt= MEM_dupallocN(ts->sculpt);
165                                 copy_paint(&ts->sculpt->paint, &ts->sculpt->paint);
166                         }
167
168                         copy_paint(&ts->imapaint.paint, &ts->imapaint.paint);
169                         ts->imapaint.paintcursor= NULL;
170
171                         ts->particle.paintcursor= NULL;
172                 }
173                 
174                 BLI_duplicatelist(&(scen->markers), &(sce->markers));
175                 BLI_duplicatelist(&(scen->transform_spaces), &(sce->transform_spaces));
176                 BLI_duplicatelist(&(scen->r.layers), &(sce->r.layers));
177                 BKE_keyingsets_copy(&(scen->keyingsets), &(sce->keyingsets));
178
179                 if(sce->nodetree) {
180                         scen->nodetree= ntreeCopyTree(sce->nodetree); /* copies actions */
181                         ntreeSwitchID(scen->nodetree, &sce->id, &scen->id);
182                 }
183
184                 obase= sce->base.first;
185                 base= scen->base.first;
186                 while(base) {
187                         id_us_plus(&base->object->id);
188                         if(obase==sce->basact) scen->basact= base;
189         
190                         obase= obase->next;
191                         base= base->next;
192                 }
193         }
194         
195         /* make a private copy of the avicodecdata */
196         if(sce->r.avicodecdata) {
197                 scen->r.avicodecdata = MEM_dupallocN(sce->r.avicodecdata);
198                 scen->r.avicodecdata->lpFormat = MEM_dupallocN(scen->r.avicodecdata->lpFormat);
199                 scen->r.avicodecdata->lpParms = MEM_dupallocN(scen->r.avicodecdata->lpParms);
200         }
201         
202         /* make a private copy of the qtcodecdata */
203         if(sce->r.qtcodecdata) {
204                 scen->r.qtcodecdata = MEM_dupallocN(sce->r.qtcodecdata);
205                 scen->r.qtcodecdata->cdParms = MEM_dupallocN(scen->r.qtcodecdata->cdParms);
206         }
207         
208         if(sce->r.ffcodecdata.properties) { /* intentionally check scen not sce. */
209                 scen->r.ffcodecdata.properties= IDP_CopyProperty(sce->r.ffcodecdata.properties);
210         }
211
212         /* NOTE: part of SCE_COPY_LINK_DATA and SCE_COPY_FULL operations
213          * are done outside of blenkernel with ED_objects_single_users! */
214
215         /*  camera */
216         if(type == SCE_COPY_LINK_DATA || type == SCE_COPY_FULL) {
217                 ID_NEW(scen->camera);
218         }
219         
220         /* before scene copy */
221         sound_create_scene(scen);
222
223         /* world */
224         if(type == SCE_COPY_FULL) {
225                 BKE_copy_animdata_id_action((ID *)scen);
226                 if(scen->world) {
227                         id_us_plus((ID *)scen->world);
228                         scen->world= copy_world(scen->world);
229                         BKE_copy_animdata_id_action((ID *)scen->world);
230                 }
231
232                 if(sce->ed) {
233                         scen->ed= MEM_callocN( sizeof(Editing), "addseq");
234                         scen->ed->seqbasep= &scen->ed->seqbase;
235                         seqbase_dupli_recursive(sce, scen, &scen->ed->seqbase, &sce->ed->seqbase, SEQ_DUPE_ALL);
236                 }
237         }
238
239         return scen;
240 }
241
242 /* do not free scene itself */
243 void free_scene(Scene *sce)
244 {
245         Base *base;
246
247         base= sce->base.first;
248         while(base) {
249                 base->object->id.us--;
250                 base= base->next;
251         }
252         /* do not free objects! */
253         
254         if(sce->gpd) {
255 #if 0   // removed since this can be invalid memory when freeing everything
256                 // since the grease pencil data is free'd before the scene.
257                 // since grease pencil data is not (yet?), shared between objects
258                 // its probably safe not to do this, some save and reload will free this.
259                 sce->gpd->id.us--;
260 #endif
261                 sce->gpd= NULL;
262         }
263
264         BLI_freelistN(&sce->base);
265         seq_free_editing(sce);
266
267         BKE_free_animdata((ID *)sce);
268         BKE_keyingsets_free(&sce->keyingsets);
269         
270         if (sce->r.avicodecdata) {
271                 free_avicodecdata(sce->r.avicodecdata);
272                 MEM_freeN(sce->r.avicodecdata);
273                 sce->r.avicodecdata = NULL;
274         }
275         if (sce->r.qtcodecdata) {
276                 free_qtcodecdata(sce->r.qtcodecdata);
277                 MEM_freeN(sce->r.qtcodecdata);
278                 sce->r.qtcodecdata = NULL;
279         }
280         if (sce->r.ffcodecdata.properties) {
281                 IDP_FreeProperty(sce->r.ffcodecdata.properties);
282                 MEM_freeN(sce->r.ffcodecdata.properties);
283                 sce->r.ffcodecdata.properties = NULL;
284         }
285         
286         BLI_freelistN(&sce->markers);
287         BLI_freelistN(&sce->transform_spaces);
288         BLI_freelistN(&sce->r.layers);
289         
290         if(sce->toolsettings) {
291                 if(sce->toolsettings->vpaint) {
292                         free_paint(&sce->toolsettings->vpaint->paint);
293                         MEM_freeN(sce->toolsettings->vpaint);
294                 }
295                 if(sce->toolsettings->wpaint) {
296                         free_paint(&sce->toolsettings->wpaint->paint);
297                         MEM_freeN(sce->toolsettings->wpaint);
298                 }
299                 if(sce->toolsettings->sculpt) {
300                         free_paint(&sce->toolsettings->sculpt->paint);
301                         MEM_freeN(sce->toolsettings->sculpt);
302                 }
303                 free_paint(&sce->toolsettings->imapaint.paint);
304
305                 MEM_freeN(sce->toolsettings);
306                 sce->toolsettings = NULL;       
307         }
308         
309         if (sce->theDag) {
310                 free_forest(sce->theDag);
311                 MEM_freeN(sce->theDag);
312         }
313         
314         if(sce->nodetree) {
315                 ntreeFreeTree(sce->nodetree);
316                 MEM_freeN(sce->nodetree);
317         }
318
319         if(sce->stats)
320                 MEM_freeN(sce->stats);
321         if(sce->fps_info)
322                 MEM_freeN(sce->fps_info);
323
324         sound_destroy_scene(sce);
325 }
326
327 Scene *add_scene(const char *name)
328 {
329         Main *bmain= G.main;
330         Scene *sce;
331         ParticleEditSettings *pset;
332         int a;
333
334         sce= alloc_libblock(&bmain->scene, ID_SCE, name);
335         sce->lay= sce->layact= 1;
336         
337         sce->r.mode= R_GAMMA|R_OSA|R_SHADOW|R_SSS|R_ENVMAP|R_RAYTRACE;
338         sce->r.cfra= 1;
339         sce->r.sfra= 1;
340         sce->r.efra= 250;
341         sce->r.frame_step= 1;
342         sce->r.xsch= 1920;
343         sce->r.ysch= 1080;
344         sce->r.xasp= 1;
345         sce->r.yasp= 1;
346         sce->r.xparts= 8;
347         sce->r.yparts= 8;
348         sce->r.mblur_samples= 1;
349         sce->r.filtertype= R_FILTER_MITCH;
350         sce->r.size= 50;
351         sce->r.planes= 24;
352         sce->r.imtype= R_PNG;
353         sce->r.quality= 90;
354         sce->r.displaymode= R_OUTPUT_AREA;
355         sce->r.framapto= 100;
356         sce->r.images= 100;
357         sce->r.framelen= 1.0;
358         sce->r.blurfac= 0.5;
359         sce->r.frs_sec= 24;
360         sce->r.frs_sec_base= 1;
361         sce->r.edgeint= 10;
362         sce->r.ocres = 128;
363         sce->r.color_mgt_flag |= R_COLOR_MANAGEMENT;
364         sce->r.gauss= 1.0;
365         
366         /* deprecated but keep for upwards compat */
367         sce->r.postgamma= 1.0;
368         sce->r.posthue= 0.0;
369         sce->r.postsat= 1.0;
370         
371         sce->r.bake_mode= 1;    /* prevent to include render stuff here */
372         sce->r.bake_filter= 2;
373         sce->r.bake_osa= 5;
374         sce->r.bake_flag= R_BAKE_CLEAR;
375         sce->r.bake_normal_space= R_BAKE_SPACE_TANGENT;
376         sce->r.scemode= R_DOCOMP|R_DOSEQ|R_EXTENSION;
377         sce->r.stamp= R_STAMP_TIME|R_STAMP_FRAME|R_STAMP_DATE|R_STAMP_CAMERA|R_STAMP_SCENE|R_STAMP_FILENAME|R_STAMP_RENDERTIME;
378         sce->r.stamp_font_id= 12;
379         sce->r.fg_stamp[0]= sce->r.fg_stamp[1]= sce->r.fg_stamp[2]= 0.8f;
380         sce->r.fg_stamp[3]= 1.0f;
381         sce->r.bg_stamp[0]= sce->r.bg_stamp[1]= sce->r.bg_stamp[2]= 0.0f;
382         sce->r.bg_stamp[3]= 0.25f;
383         sce->r.raytrace_options = R_RAYTRACE_USE_INSTANCES;
384
385         sce->r.seq_prev_type= OB_SOLID;
386         sce->r.seq_rend_type= OB_SOLID;
387         sce->r.seq_flag= R_SEQ_GL_PREV;
388
389         sce->r.threads= 1;
390
391         sce->r.simplify_subsurf= 6;
392         sce->r.simplify_particles= 1.0f;
393         sce->r.simplify_shadowsamples= 16;
394         sce->r.simplify_aosss= 1.0f;
395
396         sce->r.cineonblack= 95;
397         sce->r.cineonwhite= 685;
398         sce->r.cineongamma= 1.7f;
399
400         sce->r.border.xmin= 0.0f;
401         sce->r.border.ymin= 0.0f;
402         sce->r.border.xmax= 1.0f;
403         sce->r.border.ymax= 1.0f;
404         
405         sce->toolsettings = MEM_callocN(sizeof(struct ToolSettings),"Tool Settings Struct");
406         sce->toolsettings->cornertype=1;
407         sce->toolsettings->degr = 90; 
408         sce->toolsettings->step = 9;
409         sce->toolsettings->turn = 1;                            
410         sce->toolsettings->extr_offs = 1; 
411         sce->toolsettings->doublimit = 0.001;
412         sce->toolsettings->segments = 32;
413         sce->toolsettings->rings = 32;
414         sce->toolsettings->vertices = 32;
415         sce->toolsettings->editbutflag = 1;
416         sce->toolsettings->uvcalc_radius = 1.0f;
417         sce->toolsettings->uvcalc_cubesize = 1.0f;
418         sce->toolsettings->uvcalc_mapdir = 1;
419         sce->toolsettings->uvcalc_mapalign = 1;
420         sce->toolsettings->unwrapper = 1;
421         sce->toolsettings->select_thresh= 0.01f;
422         sce->toolsettings->jointrilimit = 0.8f;
423
424         sce->toolsettings->selectmode= SCE_SELECT_VERTEX;
425         sce->toolsettings->uv_selectmode= UV_SELECT_VERTEX;
426         sce->toolsettings->normalsize= 0.1;
427         sce->toolsettings->autokey_mode= U.autokey_mode;
428
429         sce->toolsettings->skgen_resolution = 100;
430         sce->toolsettings->skgen_threshold_internal     = 0.01f;
431         sce->toolsettings->skgen_threshold_external     = 0.01f;
432         sce->toolsettings->skgen_angle_limit                    = 45.0f;
433         sce->toolsettings->skgen_length_ratio                   = 1.3f;
434         sce->toolsettings->skgen_length_limit                   = 1.5f;
435         sce->toolsettings->skgen_correlation_limit              = 0.98f;
436         sce->toolsettings->skgen_symmetry_limit                 = 0.1f;
437         sce->toolsettings->skgen_postpro = SKGEN_SMOOTH;
438         sce->toolsettings->skgen_postpro_passes = 1;
439         sce->toolsettings->skgen_options = SKGEN_FILTER_INTERNAL|SKGEN_FILTER_EXTERNAL|SKGEN_FILTER_SMART|SKGEN_HARMONIC|SKGEN_SUB_CORRELATION|SKGEN_STICK_TO_EMBEDDING;
440         sce->toolsettings->skgen_subdivisions[0] = SKGEN_SUB_CORRELATION;
441         sce->toolsettings->skgen_subdivisions[1] = SKGEN_SUB_LENGTH;
442         sce->toolsettings->skgen_subdivisions[2] = SKGEN_SUB_ANGLE;
443
444         sce->toolsettings->proportional_size = 1.0f;
445
446         sce->physics_settings.gravity[0] = 0.0f;
447         sce->physics_settings.gravity[1] = 0.0f;
448         sce->physics_settings.gravity[2] = -9.81f;
449         sce->physics_settings.flag = PHYS_GLOBAL_GRAVITY;
450
451         sce->unit.scale_length = 1.0f;
452
453         pset= &sce->toolsettings->particle;
454         pset->flag= PE_KEEP_LENGTHS|PE_LOCK_FIRST|PE_DEFLECT_EMITTER|PE_AUTO_VELOCITY;
455         pset->emitterdist= 0.25f;
456         pset->totrekey= 5;
457         pset->totaddkey= 5;
458         pset->brushtype= PE_BRUSH_NONE;
459         pset->draw_step= 2;
460         pset->fade_frames= 2;
461         pset->selectmode= SCE_SELECT_PATH;
462         for(a=0; a<PE_TOT_BRUSH; a++) {
463                 pset->brush[a].strength= 0.5;
464                 pset->brush[a].size= 50;
465                 pset->brush[a].step= 10;
466                 pset->brush[a].count= 10;
467         }
468         pset->brush[PE_BRUSH_CUT].strength= 100;
469
470         sce->r.ffcodecdata.audio_mixrate = 44100;
471         sce->r.ffcodecdata.audio_volume = 1.0f;
472         sce->r.ffcodecdata.audio_bitrate = 192;
473         sce->r.ffcodecdata.audio_channels = 2;
474
475         BLI_strncpy(sce->r.engine, "BLENDER_RENDER", sizeof(sce->r.engine));
476
477         sce->audio.distance_model = 2.0f;
478         sce->audio.doppler_factor = 1.0f;
479         sce->audio.speed_of_sound = 343.3f;
480         sce->audio.volume = 1.0f;
481
482         BLI_strncpy(sce->r.pic, U.renderdir, sizeof(sce->r.pic));
483
484         BLI_init_rctf(&sce->r.safety, 0.1f, 0.9f, 0.1f, 0.9f);
485         sce->r.osa= 8;
486
487         /* note; in header_info.c the scene copy happens..., if you add more to renderdata it has to be checked there */
488         scene_add_render_layer(sce);
489         
490         /* game data */
491         sce->gm.stereoflag = STEREO_NOSTEREO;
492         sce->gm.stereomode = STEREO_ANAGLYPH;
493         sce->gm.eyeseparation = 0.10;
494
495         sce->gm.dome.angle = 180;
496         sce->gm.dome.mode = DOME_FISHEYE;
497         sce->gm.dome.res = 4;
498         sce->gm.dome.resbuf = 1.0f;
499         sce->gm.dome.tilt = 0;
500
501         sce->gm.xplay= 640;
502         sce->gm.yplay= 480;
503         sce->gm.freqplay= 60;
504         sce->gm.depth= 32;
505
506         sce->gm.gravity= 9.8f;
507         sce->gm.physicsEngine= WOPHY_BULLET;
508         sce->gm.mode = 32; //XXX ugly harcoding, still not sure we should drop mode. 32 == 1 << 5 == use_occlusion_culling 
509         sce->gm.occlusionRes = 128;
510         sce->gm.ticrate = 60;
511         sce->gm.maxlogicstep = 5;
512         sce->gm.physubstep = 1;
513         sce->gm.maxphystep = 5;
514
515         sce->gm.flag = GAME_DISPLAY_LISTS;
516         sce->gm.matmode = GAME_MAT_MULTITEX;
517
518         sce->gm.obstacleSimulation= OBSTSIMULATION_NONE;
519         sce->gm.levelHeight = 2.f;
520
521         sce->gm.recastData.cellsize = 0.3f;
522         sce->gm.recastData.cellheight = 0.2f;
523         sce->gm.recastData.agentmaxslope = M_PI/2;
524         sce->gm.recastData.agentmaxclimb = 0.9f;
525         sce->gm.recastData.agentheight = 2.0f;
526         sce->gm.recastData.agentradius = 0.6f;
527         sce->gm.recastData.edgemaxlen = 12.0f;
528         sce->gm.recastData.edgemaxerror = 1.3f;
529         sce->gm.recastData.regionminsize = 8.f;
530         sce->gm.recastData.regionmergesize = 20.f;
531         sce->gm.recastData.vertsperpoly = 6;
532         sce->gm.recastData.detailsampledist = 6.0f;
533         sce->gm.recastData.detailsamplemaxerror = 1.0f;
534
535         sound_create_scene(sce);
536
537         return sce;
538 }
539
540 Base *object_in_scene(Object *ob, Scene *sce)
541 {
542         Base *base;
543         
544         base= sce->base.first;
545         while(base) {
546                 if(base->object == ob) return base;
547                 base= base->next;
548         }
549         return NULL;
550 }
551
552 void set_scene_bg(Main *bmain, Scene *scene)
553 {
554         Scene *sce;
555         Base *base;
556         Object *ob;
557         Group *group;
558         GroupObject *go;
559         int flag;
560         
561         /* check for cyclic sets, for reading old files but also for definite security (py?) */
562         scene_check_setscene(bmain, scene);
563         
564         /* can happen when switching modes in other scenes */
565         if(scene->obedit && !(scene->obedit->mode & OB_MODE_EDIT))
566                 scene->obedit= NULL;
567
568         /* deselect objects (for dataselect) */
569         for(ob= bmain->object.first; ob; ob= ob->id.next)
570                 ob->flag &= ~(SELECT|OB_FROMGROUP);
571
572         /* group flags again */
573         for(group= bmain->group.first; group; group= group->id.next) {
574                 go= group->gobject.first;
575                 while(go) {
576                         if(go->ob) go->ob->flag |= OB_FROMGROUP;
577                         go= go->next;
578                 }
579         }
580
581         /* sort baselist */
582         DAG_scene_sort(bmain, scene);
583         
584         /* ensure dags are built for sets */
585         for(sce= scene->set; sce; sce= sce->set)
586                 if(sce->theDag==NULL)
587                         DAG_scene_sort(bmain, sce);
588
589         /* copy layers and flags from bases to objects */
590         for(base= scene->base.first; base; base= base->next) {
591                 ob= base->object;
592                 ob->lay= base->lay;
593                 
594                 /* group patch... */
595                 base->flag &= ~(OB_FROMGROUP);
596                 flag= ob->flag & (OB_FROMGROUP);
597                 base->flag |= flag;
598                 
599                 /* not too nice... for recovering objects with lost data */
600                 //if(ob->pose==NULL) base->flag &= ~OB_POSEMODE;
601                 ob->flag= base->flag;
602                 
603                 ob->ctime= -1234567.0;  /* force ipo to be calculated later */
604         }
605         /* no full animation update, this to enable render code to work (render code calls own animation updates) */
606 }
607
608 /* called from creator.c */
609 Scene *set_scene_name(Main *bmain, const char *name)
610 {
611         Scene *sce= (Scene *)find_id("SC", name);
612         if(sce) {
613                 set_scene_bg(bmain, sce);
614                 printf("Scene switch: '%s' in file: '%s'\n", name, G.main->name);
615                 return sce;
616         }
617
618         printf("Can't find scene: '%s' in file: '%s'\n", name, G.main->name);
619         return NULL;
620 }
621
622 void unlink_scene(Main *bmain, Scene *sce, Scene *newsce)
623 {
624         Scene *sce1;
625         bScreen *sc;
626
627         /* check all sets */
628         for(sce1= bmain->scene.first; sce1; sce1= sce1->id.next)
629                 if(sce1->set == sce)
630                         sce1->set= NULL;
631         
632         /* check all sequences */
633         clear_scene_in_allseqs(bmain, sce);
634
635         /* check render layer nodes in other scenes */
636         clear_scene_in_nodes(bmain, sce);
637         
638         /* al screens */
639         for(sc= bmain->screen.first; sc; sc= sc->id.next)
640                 if(sc->scene == sce)
641                         sc->scene= newsce;
642
643         free_libblock(&bmain->scene, sce);
644 }
645
646 /* used by metaballs
647  * doesnt return the original duplicated object, only dupli's
648  */
649 int next_object(Scene **scene, int val, Base **base, Object **ob)
650 {
651         static ListBase *duplilist= NULL;
652         static DupliObject *dupob;
653         static int fase= F_START, in_next_object= 0;
654         int run_again=1;
655         
656         /* init */
657         if(val==0) {
658                 fase= F_START;
659                 dupob= NULL;
660                 
661                 /* XXX particle systems with metas+dupligroups call this recursively */
662                 /* see bug #18725 */
663                 if(in_next_object) {
664                         printf("ERROR: MetaBall generation called recursively, not supported\n");
665                         
666                         return F_ERROR;
667                 }
668         }
669         else {
670                 in_next_object= 1;
671                 
672                 /* run_again is set when a duplilist has been ended */
673                 while(run_again) {
674                         run_again= 0;
675
676                         /* the first base */
677                         if(fase==F_START) {
678                                 *base= (*scene)->base.first;
679                                 if(*base) {
680                                         *ob= (*base)->object;
681                                         fase= F_SCENE;
682                                 }
683                                 else {
684                                         /* exception: empty scene */
685                                         while((*scene)->set) {
686                                                 (*scene)= (*scene)->set;
687                                                 if((*scene)->base.first) {
688                                                         *base= (*scene)->base.first;
689                                                         *ob= (*base)->object;
690                                                         fase= F_SCENE;
691                                                         break;
692                                                 }
693                                         }
694                                 }
695                         }
696                         else {
697                                 if(*base && fase!=F_DUPLI) {
698                                         *base= (*base)->next;
699                                         if(*base) *ob= (*base)->object;
700                                         else {
701                                                 if(fase==F_SCENE) {
702                                                         /* (*scene) is finished, now do the set */
703                                                         while((*scene)->set) {
704                                                                 (*scene)= (*scene)->set;
705                                                                 if((*scene)->base.first) {
706                                                                         *base= (*scene)->base.first;
707                                                                         *ob= (*base)->object;
708                                                                         break;
709                                                                 }
710                                                         }
711                                                 }
712                                         }
713                                 }
714                         }
715                         
716                         if(*base == NULL) fase= F_START;
717                         else {
718                                 if(fase!=F_DUPLI) {
719                                         if( (*base)->object->transflag & OB_DUPLI) {
720                                                 /* groups cannot be duplicated for mballs yet, 
721                                                 this enters eternal loop because of 
722                                                 makeDispListMBall getting called inside of group_duplilist */
723                                                 if((*base)->object->dup_group == NULL) {
724                                                         duplilist= object_duplilist((*scene), (*base)->object);
725                                                         
726                                                         dupob= duplilist->first;
727
728                                                         if(!dupob)
729                                                                 free_object_duplilist(duplilist);
730                                                 }
731                                         }
732                                 }
733                                 /* handle dupli's */
734                                 if(dupob) {
735                                         
736                                         copy_m4_m4(dupob->ob->obmat, dupob->mat);
737                                         
738                                         (*base)->flag |= OB_FROMDUPLI;
739                                         *ob= dupob->ob;
740                                         fase= F_DUPLI;
741                                         
742                                         dupob= dupob->next;
743                                 }
744                                 else if(fase==F_DUPLI) {
745                                         fase= F_SCENE;
746                                         (*base)->flag &= ~OB_FROMDUPLI;
747                                         
748                                         for(dupob= duplilist->first; dupob; dupob= dupob->next) {
749                                                 copy_m4_m4(dupob->ob->obmat, dupob->omat);
750                                         }
751                                         
752                                         free_object_duplilist(duplilist);
753                                         duplilist= NULL;
754                                         run_again= 1;
755                                 }
756                         }
757                 }
758         }
759         
760         /* if(ob && *ob) {
761                 printf("Scene: '%s', '%s'\n", (*scene)->id.name+2, (*ob)->id.name+2);
762         } */
763
764         /* reset recursion test */
765         in_next_object= 0;
766         
767         return fase;
768 }
769
770 Object *scene_find_camera(Scene *sc)
771 {
772         Base *base;
773         
774         for (base= sc->base.first; base; base= base->next)
775                 if (base->object->type==OB_CAMERA)
776                         return base->object;
777
778         return NULL;
779 }
780
781 #ifdef DURIAN_CAMERA_SWITCH
782 Object *scene_camera_switch_find(Scene *scene)
783 {
784         TimeMarker *m;
785         int cfra = scene->r.cfra;
786         int frame = -(MAXFRAME + 1);
787         Object *camera= NULL;
788
789         for (m= scene->markers.first; m; m= m->next) {
790                 if(m->camera && (m->camera->restrictflag & OB_RESTRICT_RENDER)==0 && (m->frame <= cfra) && (m->frame > frame)) {
791                         camera= m->camera;
792                         frame= m->frame;
793
794                         if(frame == cfra)
795                                 break;
796
797                 }
798         }
799         return camera;
800 }
801 #endif
802
803 int scene_camera_switch_update(Scene *scene)
804 {
805 #ifdef DURIAN_CAMERA_SWITCH
806         Object *camera= scene_camera_switch_find(scene);
807         if(camera) {
808                 scene->camera= camera;
809                 return 1;
810         }
811 #endif
812         return 0;
813 }
814
815 char *scene_find_marker_name(Scene *scene, int frame)
816 {
817         ListBase *markers= &scene->markers;
818         TimeMarker *m1, *m2;
819
820         /* search through markers for match */
821         for (m1=markers->first, m2=markers->last; m1 && m2; m1=m1->next, m2=m2->prev) {
822                 if (m1->frame==frame)
823                         return m1->name;
824
825                 if (m1 == m2)
826                         break;
827
828                 if (m2->frame==frame)
829                         return m2->name;
830         }
831
832         return NULL;
833 }
834
835 /* return the current marker for this frame,
836 we can have more then 1 marker per frame, this just returns the first :/ */
837 char *scene_find_last_marker_name(Scene *scene, int frame)
838 {
839         TimeMarker *marker, *best_marker = NULL;
840         int best_frame = -MAXFRAME*2;
841         for (marker= scene->markers.first; marker; marker= marker->next) {
842                 if (marker->frame==frame) {
843                         return marker->name;
844                 }
845
846                 if ( marker->frame > best_frame && marker->frame < frame) {
847                         best_marker = marker;
848                         best_frame = marker->frame;
849                 }
850         }
851
852         return best_marker ? best_marker->name : NULL;
853 }
854
855
856 Base *scene_add_base(Scene *sce, Object *ob)
857 {
858         Base *b= MEM_callocN(sizeof(*b), "scene_add_base");
859         BLI_addhead(&sce->base, b);
860
861         b->object= ob;
862         b->flag= ob->flag;
863         b->lay= ob->lay;
864
865         return b;
866 }
867
868 void scene_deselect_all(Scene *sce)
869 {
870         Base *b;
871
872         for (b= sce->base.first; b; b= b->next) {
873                 b->flag&= ~SELECT;
874                 b->object->flag= b->flag;
875         }
876 }
877
878 void scene_select_base(Scene *sce, Base *selbase)
879 {
880         scene_deselect_all(sce);
881
882         selbase->flag |= SELECT;
883         selbase->object->flag= selbase->flag;
884
885         sce->basact= selbase;
886 }
887
888 /* checks for cycle, returns 1 if it's all OK */
889 int scene_check_setscene(Main *bmain, Scene *sce)
890 {
891         Scene *scene;
892         int a, totscene;
893         
894         if(sce->set==NULL) return 1;
895         
896         totscene= 0;
897         for(scene= bmain->scene.first; scene; scene= scene->id.next)
898                 totscene++;
899         
900         for(a=0, scene=sce; scene->set; scene=scene->set, a++) {
901                 /* more iterations than scenes means we have a cycle */
902                 if(a > totscene) {
903                         /* the tested scene gets zero'ed, that's typically current scene */
904                         sce->set= NULL;
905                         return 0;
906                 }
907         }
908
909         return 1;
910 }
911
912 /* This function is needed to cope with fractional frames - including two Blender rendering features
913 * mblur (motion blur that renders 'subframes' and blurs them together), and fields rendering. */
914
915 /* see also bsystem_time in object.c */
916 float BKE_curframe(Scene *scene)
917 {
918         float ctime = scene->r.cfra;
919         ctime+= scene->r.subframe;
920         ctime*= scene->r.framelen;      
921
922         return ctime;
923 }
924
925 /* drivers support/hacks 
926  *      - this method is called from scene_update_tagged_recursive(), so gets included in viewport + render
927  *      - these are always run since the depsgraph can't handle non-object data
928  *      - these happen after objects are all done so that we can read in their final transform values,
929  *        though this means that objects can't refer to scene info for guidance...
930  */
931 static void scene_update_drivers(Main *UNUSED(bmain), Scene *scene)
932 {
933         float ctime = BKE_curframe(scene);
934         
935         /* scene itself */
936         if (scene->adt && scene->adt->drivers.first) {
937                 BKE_animsys_evaluate_animdata(scene, &scene->id, scene->adt, ctime, ADT_RECALC_DRIVERS);
938         }
939         
940         /* world */
941         // TODO: what about world textures? but then those have nodes too...
942         if (scene->world) {
943                 ID *wid = (ID *)scene->world;
944                 AnimData *adt= BKE_animdata_from_id(wid);
945                 
946                 if (adt && adt->drivers.first)
947                         BKE_animsys_evaluate_animdata(scene, wid, adt, ctime, ADT_RECALC_DRIVERS);
948         }
949         
950         /* nodes */
951         if (scene->nodetree) {
952                 ID *nid = (ID *)scene->nodetree;
953                 AnimData *adt= BKE_animdata_from_id(nid);
954                 
955                 if (adt && adt->drivers.first)
956                         BKE_animsys_evaluate_animdata(scene, nid, adt, ctime, ADT_RECALC_DRIVERS);
957         }
958 }
959
960 static void scene_update_tagged_recursive(Main *bmain, Scene *scene, Scene *scene_parent)
961 {
962         Base *base;
963         
964         
965         scene->customdata_mask= scene_parent->customdata_mask;
966
967         /* sets first, we allow per definition current scene to have
968            dependencies on sets, but not the other way around. */
969         if (scene->set)
970                 scene_update_tagged_recursive(bmain, scene->set, scene_parent);
971         
972         /* scene objects */
973         for (base= scene->base.first; base; base= base->next) {
974                 Object *ob= base->object;
975                 
976                 object_handle_update(scene_parent, ob);
977                 
978                 if(ob->dup_group && (ob->transflag & OB_DUPLIGROUP))
979                         group_handle_recalc_and_update(scene_parent, ob, ob->dup_group);
980                         
981                 /* always update layer, so that animating layers works */
982                 base->lay= ob->lay;
983         }
984         
985         /* scene drivers... */
986         scene_update_drivers(bmain, scene);
987
988         /* update sound system animation */
989         sound_update_scene(scene);
990 }
991
992 /* this is called in main loop, doing tagged updates before redraw */
993 void scene_update_tagged(Main *bmain, Scene *scene)
994 {
995         DAG_ids_flush_tagged(bmain);
996
997         scene->physics_settings.quick_cache_step= 0;
998
999         /* update all objects: drivers, matrices, displists, etc. flags set
1000            by depgraph or manual, no layer check here, gets correct flushed */
1001
1002         scene_update_tagged_recursive(bmain, scene, scene);
1003
1004         /* recalc scene animation data here (for sequencer) */
1005         {
1006                 AnimData *adt= BKE_animdata_from_id(&scene->id);
1007                 float ctime = BKE_curframe(scene);
1008                 
1009                 if (adt && (adt->recalc & ADT_RECALC_ANIM))
1010                         BKE_animsys_evaluate_animdata(scene, &scene->id, adt, ctime, 0);
1011         }
1012         
1013         if (scene->physics_settings.quick_cache_step)
1014                 BKE_ptcache_quick_cache_all(bmain, scene);
1015
1016         DAG_ids_check_recalc(bmain);
1017
1018         /* in the future this should handle updates for all datablocks, not
1019            only objects and scenes. - brecht */
1020 }
1021
1022 void scene_clear_tagged(Main *bmain, Scene *UNUSED(scene))
1023 {
1024         DAG_ids_clear_recalc(bmain);
1025 }
1026
1027 /* applies changes right away, does all sets too */
1028 void scene_update_for_newframe(Main *bmain, Scene *sce, unsigned int lay)
1029 {
1030         float ctime = BKE_curframe(sce);
1031         Scene *sce_iter;
1032
1033         /* keep this first */
1034         BLI_exec_cb(bmain, (ID *)sce, BLI_CB_EVT_FRAME_CHANGE_PRE);
1035
1036         sound_set_cfra(sce->r.cfra);
1037         
1038         /* clear animation overrides */
1039         // XXX TODO...
1040
1041         for(sce_iter= sce; sce_iter; sce_iter= sce_iter->set) {
1042                 if(sce_iter->theDag==NULL)
1043                         DAG_scene_sort(bmain, sce_iter);
1044         }
1045
1046
1047         /* Following 2 functions are recursive
1048          * so dont call within 'scene_update_tagged_recursive' */
1049         DAG_scene_update_flags(bmain, sce, lay, TRUE);   // only stuff that moves or needs display still
1050
1051         /* All 'standard' (i.e. without any dependencies) animation is handled here,
1052          * with an 'local' to 'macro' order of evaluation. This should ensure that
1053          * settings stored nestled within a hierarchy (i.e. settings in a Texture block
1054          * can be overridden by settings from Scene, which owns the Texture through a hierarchy
1055          * such as Scene->World->MTex/Texture) can still get correctly overridden.
1056          */
1057         BKE_animsys_evaluate_all_animation(bmain, sce, ctime);
1058         /*...done with recusrive funcs */
1059
1060         /* object_handle_update() on all objects, groups and sets */
1061         scene_update_tagged_recursive(bmain, sce, sce);
1062
1063         /* keep this last */
1064         BLI_exec_cb(bmain, (ID *)sce, BLI_CB_EVT_FRAME_CHANGE_POST);
1065 }
1066
1067 /* return default layer, also used to patch old files */
1068 void scene_add_render_layer(Scene *sce)
1069 {
1070         SceneRenderLayer *srl;
1071 //      int tot= 1 + BLI_countlist(&sce->r.layers);
1072         
1073         srl= MEM_callocN(sizeof(SceneRenderLayer), "new render layer");
1074         strcpy(srl->name, "RenderLayer");
1075         BLI_uniquename(&sce->r.layers, srl, "RenderLayer", '.', offsetof(SceneRenderLayer, name), sizeof(srl->name));
1076         BLI_addtail(&sce->r.layers, srl);
1077
1078         /* note, this is also in render, pipeline.c, to make layer when scenedata doesnt have it */
1079         srl->lay= (1<<20) -1;
1080         srl->layflag= 0x7FFF;   /* solid ztra halo edge strand */
1081         srl->passflag= SCE_PASS_COMBINED|SCE_PASS_Z;
1082 }
1083
1084 /* render simplification */
1085
1086 int get_render_subsurf_level(RenderData *r, int lvl)
1087 {
1088         if(r->mode & R_SIMPLIFY)
1089                 return MIN2(r->simplify_subsurf, lvl);
1090         else
1091                 return lvl;
1092 }
1093
1094 int get_render_child_particle_number(RenderData *r, int num)
1095 {
1096         if(r->mode & R_SIMPLIFY)
1097                 return (int)(r->simplify_particles*num);
1098         else
1099                 return num;
1100 }
1101
1102 int get_render_shadow_samples(RenderData *r, int samples)
1103 {
1104         if((r->mode & R_SIMPLIFY) && samples > 0)
1105                 return MIN2(r->simplify_shadowsamples, samples);
1106         else
1107                 return samples;
1108 }
1109
1110 float get_render_aosss_error(RenderData *r, float error)
1111 {
1112         if(r->mode & R_SIMPLIFY)
1113                 return ((1.0f-r->simplify_aosss)*10.0f + 1.0f)*error;
1114         else
1115                 return error;
1116 }
1117
1118 /* helper function for the SETLOOPER macro */
1119 Base *_setlooper_base_step(Scene **sce_iter, Base *base)
1120 {
1121         if(base && base->next) {
1122                 /* common case, step to the next */
1123                 return base->next;
1124         }
1125         else if(base==NULL && (*sce_iter)->base.first) {
1126                 /* first time looping, return the scenes first base */
1127                 return (Base *)(*sce_iter)->base.first;
1128         }
1129         else {
1130                 /* reached the end, get the next base in the set */
1131                 while((*sce_iter= (*sce_iter)->set)) {
1132                         base= (Base *)(*sce_iter)->base.first;
1133                         if(base) {
1134                                 return base;
1135                         }
1136                 }
1137         }
1138
1139         return NULL;
1140 }
1141
1142 int scene_use_new_shading_nodes(Scene *scene)
1143 {
1144         RenderEngineType *type= RE_engines_find(scene->r.engine);
1145         return (type->flag & RE_USE_SHADING_NODES);
1146 }
1147