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