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