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