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