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