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