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