hack, we need a way not to switch some cameras quickly.
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #ifndef WIN32 
41 #include <unistd.h>
42 #else
43 #include <io.h>
44 #endif
45 #include "MEM_guardedalloc.h"
46
47 #include "DNA_anim_types.h"
48 #include "DNA_armature_types.h" 
49 #include "DNA_color_types.h"
50 #include "DNA_constraint_types.h"
51 #include "DNA_curve_types.h"
52 #include "DNA_group_types.h"
53 #include "DNA_lamp_types.h"
54 #include "DNA_material_types.h"
55 #include "DNA_meta_types.h"
56 #include "DNA_object_types.h"
57 #include "DNA_scene_types.h"
58 #include "DNA_screen_types.h"
59 #include "DNA_texture_types.h"
60 #include "DNA_userdef_types.h"
61
62 #include "BKE_action.h"                 
63 #include "BKE_anim.h"
64 #include "BKE_animsys.h"
65 #include "BKE_armature.h"               
66 #include "BKE_colortools.h"
67 #include "BKE_colortools.h"
68 #include "BKE_constraint.h"
69 #include "BKE_depsgraph.h"
70 #include "BKE_global.h"
71 #include "BKE_group.h"
72 #include "BKE_ipo.h"
73 #include "BKE_idprop.h"
74 #include "BKE_image.h"
75 #include "BKE_key.h"
76 #include "BKE_library.h"
77 #include "BKE_main.h"
78 #include "BKE_node.h"
79 #include "BKE_object.h"
80 #include "BKE_paint.h"
81 #include "BKE_pointcache.h"
82 #include "BKE_scene.h"
83 #include "BKE_sequencer.h"
84 #include "BKE_world.h"
85 #include "BKE_utildefines.h"
86 #include "BKE_sound.h"
87
88 //XXX #include "BIF_previewrender.h"
89 //XXX #include "BIF_editseq.h"
90
91 #ifndef DISABLE_PYTHON
92 #include "BPY_extern.h"
93 #endif
94
95 #include "BLI_math.h"
96 #include "BLI_blenlib.h"
97
98 //XXX #include "nla.h"
99
100 #ifdef WIN32
101 #else
102 #include <sys/time.h>
103 #endif
104
105 void free_avicodecdata(AviCodecData *acd)
106 {
107         if (acd) {
108                 if (acd->lpFormat){
109                         MEM_freeN(acd->lpFormat);
110                         acd->lpFormat = NULL;
111                         acd->cbFormat = 0;
112                 }
113                 if (acd->lpParms){
114                         MEM_freeN(acd->lpParms);
115                         acd->lpParms = NULL;
116                         acd->cbParms = 0;
117                 }
118         }
119 }
120
121 void free_qtcodecdata(QuicktimeCodecData *qcd)
122 {
123         if (qcd) {
124                 if (qcd->cdParms){
125                         MEM_freeN(qcd->cdParms);
126                         qcd->cdParms = NULL;
127                         qcd->cdSize = 0;
128                 }
129         }
130 }
131
132 Scene *copy_scene(Main *bmain, Scene *sce, int type)
133 {
134         Scene *scen;
135         ToolSettings *ts;
136         Base *base, *obase;
137         
138         if(type == SCE_COPY_EMPTY) {
139                 ListBase lb;
140                 scen= add_scene(sce->id.name+2);
141                 
142                 lb= scen->r.layers;
143                 scen->r= sce->r;
144                 scen->r.layers= lb;
145         }
146         else {
147                 scen= copy_libblock(sce);
148                 BLI_duplicatelist(&(scen->base), &(sce->base));
149                 
150                 clear_id_newpoins();
151                 
152                 id_us_plus((ID *)scen->world);
153                 id_us_plus((ID *)scen->set);
154                 id_us_plus((ID *)scen->ima);
155                 id_us_plus((ID *)scen->gm.dome.warptext);
156
157                 scen->ed= NULL;
158                 scen->theDag= NULL;
159                 scen->obedit= NULL;
160                 scen->toolsettings= MEM_dupallocN(sce->toolsettings);
161                 scen->stats= NULL;
162
163                 ts= scen->toolsettings;
164                 if(ts) {
165                         if(ts->vpaint) {
166                                 ts->vpaint= MEM_dupallocN(ts->vpaint);
167                                 ts->vpaint->paintcursor= NULL;
168                                 ts->vpaint->vpaint_prev= NULL;
169                                 ts->vpaint->wpaint_prev= NULL;
170                                 copy_paint(&ts->vpaint->paint, &ts->vpaint->paint);
171                         }
172                         if(ts->wpaint) {
173                                 ts->wpaint= MEM_dupallocN(ts->wpaint);
174                                 ts->wpaint->paintcursor= NULL;
175                                 ts->wpaint->vpaint_prev= NULL;
176                                 ts->wpaint->wpaint_prev= NULL;
177                                 copy_paint(&ts->wpaint->paint, &ts->wpaint->paint);
178                         }
179                         if(ts->sculpt) {
180                                 ts->sculpt= MEM_dupallocN(ts->sculpt);
181                                 copy_paint(&ts->sculpt->paint, &ts->sculpt->paint);
182                         }
183
184                         copy_paint(&ts->imapaint.paint, &ts->imapaint.paint);
185                         ts->imapaint.paintcursor= NULL;
186
187                         ts->particle.paintcursor= NULL;
188                 }
189                 
190                 BLI_duplicatelist(&(scen->markers), &(sce->markers));
191                 BLI_duplicatelist(&(scen->transform_spaces), &(sce->transform_spaces));
192                 BLI_duplicatelist(&(scen->r.layers), &(sce->r.layers));
193                 BKE_keyingsets_copy(&(scen->keyingsets), &(sce->keyingsets));
194                 
195                 scen->nodetree= ntreeCopyTree(sce->nodetree, 0);
196                 
197                 obase= sce->base.first;
198                 base= scen->base.first;
199                 while(base) {
200                         id_us_plus(&base->object->id);
201                         if(obase==sce->basact) scen->basact= base;
202         
203                         obase= obase->next;
204                         base= base->next;
205                 }
206         }
207         
208         /* make a private copy of the avicodecdata */
209         if(sce->r.avicodecdata) {
210                 scen->r.avicodecdata = MEM_dupallocN(sce->r.avicodecdata);
211                 scen->r.avicodecdata->lpFormat = MEM_dupallocN(scen->r.avicodecdata->lpFormat);
212                 scen->r.avicodecdata->lpParms = MEM_dupallocN(scen->r.avicodecdata->lpParms);
213         }
214         
215         /* make a private copy of the qtcodecdata */
216         if(sce->r.qtcodecdata) {
217                 scen->r.qtcodecdata = MEM_dupallocN(sce->r.qtcodecdata);
218                 scen->r.qtcodecdata->cdParms = MEM_dupallocN(scen->r.qtcodecdata->cdParms);
219         }
220         
221         /* NOTE: part of SCE_COPY_LINK_DATA and SCE_COPY_FULL operations
222          * are done outside of blenkernel with ED_objects_single_users! */
223
224     /*  camera */
225         if(type == SCE_COPY_LINK_DATA || type == SCE_COPY_FULL) {
226             ID_NEW(scen->camera);
227         }
228
229         /* world */
230         if(type == SCE_COPY_FULL) {
231         if(scen->world) {
232             id_us_plus((ID *)scen->world);
233             scen->world= copy_world(scen->world);
234         }
235         }
236
237         sound_create_scene(scen);
238
239         return scen;
240 }
241
242 /* do not free scene itself */
243 void free_scene(Scene *sce)
244 {
245         Base *base;
246
247         base= sce->base.first;
248         while(base) {
249                 base->object->id.us--;
250                 base= base->next;
251         }
252         /* do not free objects! */
253         
254         if(sce->gpd) {
255 #if 0   // removed since this can be invalid memory when freeing everything
256         // since the grease pencil data is free'd before the scene.
257         // since grease pencil data is not (yet?), shared between objects
258         // its probably safe not to do this, some save and reload will free this.
259                 sce->gpd->id.us--;
260 #endif
261                 sce->gpd= NULL;
262         }
263
264         BLI_freelistN(&sce->base);
265         seq_free_editing(sce);
266
267         BKE_free_animdata((ID *)sce);
268         BKE_keyingsets_free(&sce->keyingsets);
269         
270         if (sce->r.avicodecdata) {
271                 free_avicodecdata(sce->r.avicodecdata);
272                 MEM_freeN(sce->r.avicodecdata);
273                 sce->r.avicodecdata = NULL;
274         }
275         if (sce->r.qtcodecdata) {
276                 free_qtcodecdata(sce->r.qtcodecdata);
277                 MEM_freeN(sce->r.qtcodecdata);
278                 sce->r.qtcodecdata = NULL;
279         }
280         if (sce->r.ffcodecdata.properties) {
281                 IDP_FreeProperty(sce->r.ffcodecdata.properties);
282                 MEM_freeN(sce->r.ffcodecdata.properties);
283                 sce->r.ffcodecdata.properties = NULL;
284         }
285         
286         BLI_freelistN(&sce->markers);
287         BLI_freelistN(&sce->transform_spaces);
288         BLI_freelistN(&sce->r.layers);
289         
290         if(sce->toolsettings) {
291                 if(sce->toolsettings->vpaint) {
292                         free_paint(&sce->toolsettings->vpaint->paint);
293                         MEM_freeN(sce->toolsettings->vpaint);
294                 }
295                 if(sce->toolsettings->wpaint) {
296                         free_paint(&sce->toolsettings->wpaint->paint);
297                         MEM_freeN(sce->toolsettings->wpaint);
298                 }
299                 if(sce->toolsettings->sculpt) {
300                         free_paint(&sce->toolsettings->sculpt->paint);
301                         MEM_freeN(sce->toolsettings->sculpt);
302                 }
303                 free_paint(&sce->toolsettings->imapaint.paint);
304
305                 MEM_freeN(sce->toolsettings);
306                 sce->toolsettings = NULL;       
307         }
308         
309         if (sce->theDag) {
310                 free_forest(sce->theDag);
311                 MEM_freeN(sce->theDag);
312         }
313         
314         if(sce->nodetree) {
315                 ntreeFreeTree(sce->nodetree);
316                 MEM_freeN(sce->nodetree);
317         }
318
319         if(sce->stats)
320                 MEM_freeN(sce->stats);
321
322         sound_destroy_scene(sce);
323 }
324
325 Scene *add_scene(char *name)
326 {
327         Scene *sce;
328         ParticleEditSettings *pset;
329         int a;
330
331         sce= alloc_libblock(&G.main->scene, ID_SCE, name);
332         sce->lay= 1;
333         
334         sce->r.mode= R_GAMMA|R_OSA|R_SHADOW|R_SSS|R_ENVMAP|R_RAYTRACE;
335         sce->r.cfra= 1;
336         sce->r.sfra= 1;
337         sce->r.efra= 250;
338         sce->r.frame_step= 1;
339         sce->r.xsch= 1920;
340         sce->r.ysch= 1080;
341         sce->r.xasp= 1;
342         sce->r.yasp= 1;
343         sce->r.xparts= 8;
344         sce->r.yparts= 8;
345         sce->r.size= 25;
346         sce->r.planes= 24;
347         sce->r.quality= 90;
348         sce->r.framapto= 100;
349         sce->r.images= 100;
350         sce->r.framelen= 1.0;
351         sce->r.frs_sec= 25;
352         sce->r.frs_sec_base= 1;
353         sce->r.ocres = 128;
354         sce->r.color_mgt_flag |= R_COLOR_MANAGEMENT;
355         
356         sce->r.bake_mode= 1;    /* prevent to include render stuff here */
357         sce->r.bake_filter= 8;
358         sce->r.bake_osa= 5;
359         sce->r.bake_flag= R_BAKE_CLEAR;
360         sce->r.bake_normal_space= R_BAKE_SPACE_TANGENT;
361
362         sce->r.scemode= R_DOCOMP|R_DOSEQ|R_EXTENSION;
363         sce->r.stamp= R_STAMP_TIME|R_STAMP_FRAME|R_STAMP_DATE|R_STAMP_SCENE|R_STAMP_CAMERA|R_STAMP_RENDERTIME;
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->normalsize= 0.1;
402         sce->toolsettings->autokey_mode= U.autokey_mode;
403
404         sce->toolsettings->skgen_resolution = 100;
405         sce->toolsettings->skgen_threshold_internal     = 0.01f;
406         sce->toolsettings->skgen_threshold_external     = 0.01f;
407         sce->toolsettings->skgen_angle_limit                    = 45.0f;
408         sce->toolsettings->skgen_length_ratio                   = 1.3f;
409         sce->toolsettings->skgen_length_limit                   = 1.5f;
410         sce->toolsettings->skgen_correlation_limit              = 0.98f;
411         sce->toolsettings->skgen_symmetry_limit                 = 0.1f;
412         sce->toolsettings->skgen_postpro = SKGEN_SMOOTH;
413         sce->toolsettings->skgen_postpro_passes = 1;
414         sce->toolsettings->skgen_options = SKGEN_FILTER_INTERNAL|SKGEN_FILTER_EXTERNAL|SKGEN_FILTER_SMART|SKGEN_HARMONIC|SKGEN_SUB_CORRELATION|SKGEN_STICK_TO_EMBEDDING;
415         sce->toolsettings->skgen_subdivisions[0] = SKGEN_SUB_CORRELATION;
416         sce->toolsettings->skgen_subdivisions[1] = SKGEN_SUB_LENGTH;
417         sce->toolsettings->skgen_subdivisions[2] = SKGEN_SUB_ANGLE;
418
419         sce->toolsettings->proportional_size = 1.0f;
420
421         sce->physics_settings.gravity[0] = 0.0f;
422         sce->physics_settings.gravity[1] = 0.0f;
423         sce->physics_settings.gravity[2] = -9.81f;
424         sce->physics_settings.flag = PHYS_GLOBAL_GRAVITY;
425
426         sce->unit.scale_length = 1.0f;
427
428         pset= &sce->toolsettings->particle;
429         pset->flag= PE_KEEP_LENGTHS|PE_LOCK_FIRST|PE_DEFLECT_EMITTER|PE_AUTO_VELOCITY;
430         pset->emitterdist= 0.25f;
431         pset->totrekey= 5;
432         pset->totaddkey= 5;
433         pset->brushtype= PE_BRUSH_NONE;
434         pset->draw_step= 2;
435         pset->fade_frames= 2;
436         pset->selectmode= SCE_SELECT_PATH;
437         for(a=0; a<PE_TOT_BRUSH; a++) {
438                 pset->brush[a].strength= 50;
439                 pset->brush[a].size= 50;
440                 pset->brush[a].step= 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(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(scene);
516         
517         /* deselect objects (for dataselect) */
518         for(ob= G.main->object.first; ob; ob= ob->id.next)
519                 ob->flag &= ~(SELECT|OB_FROMGROUP);
520
521         /* group flags again */
522         for(group= G.main->group.first; group; group= group->id.next) {
523                 go= group->gobject.first;
524                 while(go) {
525                         if(go->ob) go->ob->flag |= OB_FROMGROUP;
526                         go= go->next;
527                 }
528         }
529
530         /* sort baselist */
531         DAG_scene_sort(scene);
532         
533         /* ensure dags are built for sets */
534         for(sce= scene->set; sce; sce= sce->set)
535                 if(sce->theDag==NULL)
536                         DAG_scene_sort(sce);
537
538         /* copy layers and flags from bases to objects */
539         for(base= scene->base.first; base; base= base->next) {
540                 ob= base->object;
541                 ob->lay= base->lay;
542                 
543                 /* group patch... */
544                 base->flag &= ~(OB_FROMGROUP);
545                 flag= ob->flag & (OB_FROMGROUP);
546                 base->flag |= flag;
547                 
548                 /* not too nice... for recovering objects with lost data */
549                 //if(ob->pose==NULL) base->flag &= ~OB_POSEMODE;
550                 ob->flag= base->flag;
551                 
552                 ob->ctime= -1234567.0;  /* force ipo to be calculated later */
553         }
554         /* no full animation update, this to enable render code to work (render code calls own animation updates) */
555 }
556
557 /* called from creator.c */
558 void set_scene_name(char *name)
559 {
560         Scene *sce;
561
562         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
563                 if (BLI_streq(name, sce->id.name+2)) {
564                         set_scene_bg(sce);
565                         return;
566                 }
567         }
568         
569         //XXX error("Can't find scene: %s", name);
570 }
571
572 void unlink_scene(Main *bmain, Scene *sce, Scene *newsce)
573 {
574         Scene *sce1;
575         bScreen *sc;
576
577         /* check all sets */
578         for(sce1= bmain->scene.first; sce1; sce1= sce1->id.next)
579                 if(sce1->set == sce)
580                         sce1->set= NULL;
581         
582         /* check all sequences */
583         clear_scene_in_allseqs(sce);
584
585         /* check render layer nodes in other scenes */
586         clear_scene_in_nodes(bmain, sce);
587         
588         /* al screens */
589         for(sc= bmain->screen.first; sc; sc= sc->id.next)
590                 if(sc->scene == sce)
591                         sc->scene= newsce;
592
593         free_libblock(&bmain->scene, sce);
594 }
595
596 /* used by metaballs
597  * doesnt return the original duplicated object, only dupli's
598  */
599 int next_object(Scene *scene, int val, Base **base, Object **ob)
600 {
601         static ListBase *duplilist= NULL;
602         static DupliObject *dupob;
603         static int fase= F_START, in_next_object= 0;
604         int run_again=1;
605         
606         /* init */
607         if(val==0) {
608                 fase= F_START;
609                 dupob= NULL;
610                 
611                 /* XXX particle systems with metas+dupligroups call this recursively */
612                 /* see bug #18725 */
613                 if(in_next_object) {
614                         printf("ERROR: MetaBall generation called recursively, not supported\n");
615                         
616                         return F_ERROR;
617                 }
618         }
619         else {
620                 in_next_object= 1;
621                 
622                 /* run_again is set when a duplilist has been ended */
623                 while(run_again) {
624                         run_again= 0;
625
626                         /* the first base */
627                         if(fase==F_START) {
628                                 *base= scene->base.first;
629                                 if(*base) {
630                                         *ob= (*base)->object;
631                                         fase= F_SCENE;
632                                 }
633                                 else {
634                                     /* exception: empty scene */
635                                         if(scene->set && scene->set->base.first) {
636                                                 *base= scene->set->base.first;
637                                                 *ob= (*base)->object;
638                                                 fase= F_SET;
639                                         }
640                                 }
641                         }
642                         else {
643                                 if(*base && fase!=F_DUPLI) {
644                                         *base= (*base)->next;
645                                         if(*base) *ob= (*base)->object;
646                                         else {
647                                                 if(fase==F_SCENE) {
648                                                         /* scene is finished, now do the set */
649                                                         if(scene->set && scene->set->base.first) {
650                                                                 *base= scene->set->base.first;
651                                                                 *ob= (*base)->object;
652                                                                 fase= F_SET;
653                                                         }
654                                                 }
655                                         }
656                                 }
657                         }
658                         
659                         if(*base == NULL) fase= F_START;
660                         else {
661                                 if(fase!=F_DUPLI) {
662                                         if( (*base)->object->transflag & OB_DUPLI) {
663                                                 /* groups cannot be duplicated for mballs yet, 
664                                                 this enters eternal loop because of 
665                                                 makeDispListMBall getting called inside of group_duplilist */
666                                                 if((*base)->object->dup_group == NULL) {
667                                                         duplilist= object_duplilist(scene, (*base)->object);
668                                                         
669                                                         dupob= duplilist->first;
670
671                                                         if(!dupob)
672                                                                 free_object_duplilist(duplilist);
673                                                 }
674                                         }
675                                 }
676                                 /* handle dupli's */
677                                 if(dupob) {
678                                         
679                                         copy_m4_m4(dupob->ob->obmat, dupob->mat);
680                                         
681                                         (*base)->flag |= OB_FROMDUPLI;
682                                         *ob= dupob->ob;
683                                         fase= F_DUPLI;
684                                         
685                                         dupob= dupob->next;
686                                 }
687                                 else if(fase==F_DUPLI) {
688                                         fase= F_SCENE;
689                                         (*base)->flag &= ~OB_FROMDUPLI;
690                                         
691                                         for(dupob= duplilist->first; dupob; dupob= dupob->next) {
692                                                 copy_m4_m4(dupob->ob->obmat, dupob->omat);
693                                         }
694                                         
695                                         free_object_duplilist(duplilist);
696                                         duplilist= NULL;
697                                         run_again= 1;
698                                 }
699                         }
700                 }
701         }
702         
703         /* reset recursion test */
704         in_next_object= 0;
705         
706         return fase;
707 }
708
709 Object *scene_find_camera(Scene *sc)
710 {
711         Base *base;
712         
713         for (base= sc->base.first; base; base= base->next)
714                 if (base->object->type==OB_CAMERA)
715                         return base->object;
716
717         return NULL;
718 }
719
720 #ifdef DURIAN_CAMERA_SWITCH
721 Object *scene_find_camera_switch(Scene *scene)
722 {
723         TimeMarker *m;
724         int cfra = scene->r.cfra;
725         int frame = -(MAXFRAME + 1);
726         Object *camera= NULL;
727
728         for (m= scene->markers.first; m; m= m->next) {
729                 if(m->camera && (m->camera->restrictflag & OB_RESTRICT_RENDER)==0 && (m->frame <= cfra) && (m->frame > frame)) {
730                         camera= m->camera;
731                         frame= m->frame;
732
733                         if(frame == cfra)
734                                 break;
735
736                 }
737         }
738         return camera;
739 }
740 #endif
741
742 char *scene_find_marker_name(Scene *scene, int frame)
743 {
744         ListBase *markers= &scene->markers;
745         TimeMarker *m1, *m2;
746
747         /* search through markers for match */
748         for (m1=markers->first, m2=markers->last; m1 && m2; m1=m1->next, m2=m2->prev) {
749                 if (m1->frame==frame)
750                         return m1->name;
751
752                 if (m1 == m2)
753                         break;
754
755                 if (m2->frame==frame)
756                         return m2->name;
757         }
758
759         return NULL;
760 }
761
762 /* return the current marker for this frame,
763 we can have more then 1 marker per frame, this just returns the first :/ */
764 char *scene_find_last_marker_name(Scene *scene, int frame)
765 {
766         TimeMarker *marker, *best_marker = NULL;
767         int best_frame = -MAXFRAME*2;
768         for (marker= scene->markers.first; marker; marker= marker->next) {
769                 if (marker->frame==frame) {
770                         return marker->name;
771                 }
772
773                 if ( marker->frame > best_frame && marker->frame < frame) {
774                         best_marker = marker;
775                         best_frame = marker->frame;
776                 }
777         }
778
779         return best_marker ? best_marker->name : NULL;
780 }
781
782
783 Base *scene_add_base(Scene *sce, Object *ob)
784 {
785         Base *b= MEM_callocN(sizeof(*b), "scene_add_base");
786         BLI_addhead(&sce->base, b);
787
788         b->object= ob;
789         b->flag= ob->flag;
790         b->lay= ob->lay;
791
792         return b;
793 }
794
795 void scene_deselect_all(Scene *sce)
796 {
797         Base *b;
798
799         for (b= sce->base.first; b; b= b->next) {
800                 b->flag&= ~SELECT;
801                 b->object->flag= b->flag;
802         }
803 }
804
805 void scene_select_base(Scene *sce, Base *selbase)
806 {
807         scene_deselect_all(sce);
808
809         selbase->flag |= SELECT;
810         selbase->object->flag= selbase->flag;
811
812         sce->basact= selbase;
813 }
814
815 /* checks for cycle, returns 1 if it's all OK */
816 int scene_check_setscene(Scene *sce)
817 {
818         Scene *scene;
819         int a, totscene;
820         
821         if(sce->set==NULL) return 1;
822         
823         totscene= 0;
824         for(scene= G.main->scene.first; scene; scene= scene->id.next)
825                 totscene++;
826         
827         for(a=0, scene=sce; scene->set; scene=scene->set, a++) {
828                 /* more iterations than scenes means we have a cycle */
829                 if(a > totscene) {
830                         /* the tested scene gets zero'ed, that's typically current scene */
831                         sce->set= NULL;
832                         return 0;
833                 }
834         }
835
836         return 1;
837 }
838
839 /* This (evil) function is needed to cope with two legacy Blender rendering features
840 * mblur (motion blur that renders 'subframes' and blurs them together), and fields 
841 * rendering. Thus, the use of ugly globals from object.c
842 */
843 // BAD... EVIL... JUJU...!!!!
844 // XXX moved here temporarily
845 float frame_to_float (Scene *scene, int cfra)           /* see also bsystem_time in object.c */
846 {
847         extern float bluroffs;  /* bad stuff borrowed from object.c */
848         extern float fieldoffs;
849         float ctime;
850         
851         ctime= (float)cfra;
852         ctime+= bluroffs+fieldoffs;
853         ctime*= scene->r.framelen;
854         
855         return ctime;
856 }
857
858 static void scene_update_newframe(Scene *sce, unsigned int lay)
859 {
860         Base *base;
861         Object *ob;
862         float ctime = frame_to_float(sce, sce->r.cfra); 
863         
864         if(sce->theDag==NULL)
865                 DAG_scene_sort(sce);
866         
867         DAG_scene_update_flags(sce, lay);   // only stuff that moves or needs display still
868         
869         /* All 'standard' (i.e. without any dependencies) animation is handled here,
870          * with an 'local' to 'macro' order of evaluation. This should ensure that
871          * settings stored nestled within a hierarchy (i.e. settings in a Texture block
872          * can be overridden by settings from Scene, which owns the Texture through a hierarchy 
873          * such as Scene->World->MTex/Texture) can still get correctly overridden.
874          */
875         BKE_animsys_evaluate_all_animation(G.main, ctime);
876         
877         for(base= sce->base.first; base; base= base->next) {
878                 ob= base->object;
879                 
880                 object_handle_update(sce, ob);   // bke_object.h
881                 
882                 /* only update layer when an ipo */
883                         // XXX old animation system
884                 //if(ob->ipo && has_ipo_code(ob->ipo, OB_LAY) ) {
885                 //      base->lay= ob->lay;
886                 //}
887         }
888 }
889
890 /* this is called in main loop, doing tagged updates before redraw */
891 void scene_update_tagged(Scene *scene)
892 {
893         Scene *sce;
894         Base *base;
895         float ctime = frame_to_float(scene, scene->r.cfra); 
896
897         /* update all objects: drivers, matrices, displists, etc. flags set
898            by depgraph or manual, no layer check here, gets correct flushed */
899
900         /* sets first, we allow per definition current scene to have
901            dependencies on sets, but not the other way around. */
902         if(scene->set) {
903                 for(SETLOOPER(scene->set, base))
904                         object_handle_update(scene, base->object);
905         }
906         
907         for(base= scene->base.first; base; base= base->next) {
908                 object_handle_update(scene, base->object);
909         }
910
911         /* recalc scene animation data here (for sequencer) */
912         {
913                 AnimData *adt= BKE_animdata_from_id(&scene->id);
914
915                 if(adt && (adt->recalc & ADT_RECALC_ANIM))
916                         BKE_animsys_evaluate_animdata(&scene->id, adt, ctime, 0);
917         }
918
919         BKE_ptcache_quick_cache_all(scene);
920
921         /* in the future this should handle updates for all datablocks, not
922            only objects and scenes. - brecht */
923 }
924
925 /* applies changes right away, does all sets too */
926 void scene_update_for_newframe(Scene *sce, unsigned int lay)
927 {
928         Scene *scene= sce;
929         
930         /* clear animation overrides */
931         // XXX TODO...
932         
933         /* sets first, we allow per definition current scene to have dependencies on sets */
934         for(sce= sce->set; sce; sce= sce->set)
935                 scene_update_newframe(sce, lay);
936
937         scene_update_newframe(scene, lay);
938 }
939
940 /* return default layer, also used to patch old files */
941 void scene_add_render_layer(Scene *sce)
942 {
943         SceneRenderLayer *srl;
944 //      int tot= 1 + BLI_countlist(&sce->r.layers);
945         
946         srl= MEM_callocN(sizeof(SceneRenderLayer), "new render layer");
947         sprintf(srl->name, "RenderLayer");
948         BLI_uniquename(&sce->r.layers, srl, "RenderLayer", '.', offsetof(SceneRenderLayer, name), 32);
949         BLI_addtail(&sce->r.layers, srl);
950
951         /* note, this is also in render, pipeline.c, to make layer when scenedata doesnt have it */
952         srl->lay= (1<<20) -1;
953         srl->layflag= 0x7FFF;   /* solid ztra halo edge strand */
954         srl->passflag= SCE_PASS_COMBINED|SCE_PASS_Z;
955 }
956
957 /* render simplification */
958
959 int get_render_subsurf_level(RenderData *r, int lvl)
960 {
961         if(r->mode & R_SIMPLIFY)
962                 return MIN2(r->simplify_subsurf, lvl);
963         else
964                 return lvl;
965 }
966
967 int get_render_child_particle_number(RenderData *r, int num)
968 {
969         if(r->mode & R_SIMPLIFY)
970                 return (int)(r->simplify_particles*num);
971         else
972                 return num;
973 }
974
975 int get_render_shadow_samples(RenderData *r, int samples)
976 {
977         if((r->mode & R_SIMPLIFY) && samples > 0)
978                 return MIN2(r->simplify_shadowsamples, samples);
979         else
980                 return samples;
981 }
982
983 float get_render_aosss_error(RenderData *r, float error)
984 {
985         if(r->mode & R_SIMPLIFY)
986                 return ((1.0f-r->simplify_aosss)*10.0f + 1.0f)*error;
987         else
988                 return error;
989 }
990