Sound:
[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 <stdio.h>
33 #include <string.h>
34
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #ifndef WIN32 
40 #include <unistd.h>
41 #else
42 #include <io.h>
43 #endif
44 #include "MEM_guardedalloc.h"
45
46 #include "DNA_anim_types.h"
47 #include "DNA_armature_types.h" 
48 #include "DNA_color_types.h"
49 #include "DNA_constraint_types.h"
50 #include "DNA_curve_types.h"
51 #include "DNA_group_types.h"
52 #include "DNA_lamp_types.h"
53 #include "DNA_material_types.h"
54 #include "DNA_meta_types.h"
55 #include "DNA_object_types.h"
56 #include "DNA_scene_types.h"
57 #include "DNA_screen_types.h"
58 #include "DNA_texture_types.h"
59 #include "DNA_userdef_types.h"
60
61 #include "BKE_action.h"                 
62 #include "BKE_anim.h"
63 #include "BKE_animsys.h"
64 #include "BKE_armature.h"               
65 #include "BKE_colortools.h"
66 #include "BKE_colortools.h"
67 #include "BKE_constraint.h"
68 #include "BKE_depsgraph.h"
69 #include "BKE_global.h"
70 #include "BKE_group.h"
71 #include "BKE_ipo.h"
72 #include "BKE_idprop.h"
73 #include "BKE_image.h"
74 #include "BKE_key.h"
75 #include "BKE_library.h"
76 #include "BKE_main.h"
77 #include "BKE_node.h"
78 #include "BKE_object.h"
79 #include "BKE_paint.h"
80 #include "BKE_scene.h"
81 #include "BKE_sequence.h"
82 #include "BKE_world.h"
83 #include "BKE_utildefines.h"
84
85 //XXX #include "BIF_previewrender.h"
86 //XXX #include "BIF_editseq.h"
87
88 #ifndef DISABLE_PYTHON
89 #include "BPY_extern.h"
90 #endif
91
92 #include "BLI_arithb.h"
93 #include "BLI_blenlib.h"
94
95 //XXX #include "nla.h"
96
97 #ifdef WIN32
98 #else
99 #include <sys/time.h>
100 #endif
101
102 void free_avicodecdata(AviCodecData *acd)
103 {
104         if (acd) {
105                 if (acd->lpFormat){
106                         MEM_freeN(acd->lpFormat);
107                         acd->lpFormat = NULL;
108                         acd->cbFormat = 0;
109                 }
110                 if (acd->lpParms){
111                         MEM_freeN(acd->lpParms);
112                         acd->lpParms = NULL;
113                         acd->cbParms = 0;
114                 }
115         }
116 }
117
118 void free_qtcodecdata(QuicktimeCodecData *qcd)
119 {
120         if (qcd) {
121                 if (qcd->cdParms){
122                         MEM_freeN(qcd->cdParms);
123                         qcd->cdParms = NULL;
124                         qcd->cdSize = 0;
125                 }
126         }
127 }
128
129 Scene *copy_scene(Main *bmain, Scene *sce, int type)
130 {
131         Scene *scen;
132         ToolSettings *ts;
133         Base *base, *obase;
134         
135         if(type == SCE_COPY_EMPTY) {
136                 ListBase lb;
137                 scen= add_scene(sce->id.name+2);
138                 
139                 lb= scen->r.layers;
140                 scen->r= sce->r;
141                 scen->r.layers= lb;
142         }
143         else {
144                 scen= copy_libblock(sce);
145                 BLI_duplicatelist(&(scen->base), &(sce->base));
146                 
147                 clear_id_newpoins();
148                 
149                 id_us_plus((ID *)scen->world);
150                 id_us_plus((ID *)scen->set);
151                 id_us_plus((ID *)scen->ima);
152                 id_us_plus((ID *)scen->gm.dome.warptext);
153
154                 scen->ed= NULL;
155                 scen->theDag= NULL;
156                 scen->obedit= NULL;
157                 scen->toolsettings= MEM_dupallocN(sce->toolsettings);
158                 scen->stats= NULL;
159
160                 ts= scen->toolsettings;
161                 if(ts) {
162                         if(ts->vpaint) {
163                                 ts->vpaint= MEM_dupallocN(ts->vpaint);
164                                 ts->vpaint->paintcursor= NULL;
165                                 ts->vpaint->vpaint_prev= NULL;
166                                 ts->vpaint->wpaint_prev= NULL;
167                                 copy_paint(&ts->vpaint->paint, &ts->vpaint->paint);
168                         }
169                         if(ts->wpaint) {
170                                 ts->wpaint= MEM_dupallocN(ts->wpaint);
171                                 ts->wpaint->paintcursor= NULL;
172                                 ts->wpaint->vpaint_prev= NULL;
173                                 ts->wpaint->wpaint_prev= NULL;
174                                 copy_paint(&ts->wpaint->paint, &ts->wpaint->paint);
175                         }
176                         if(ts->sculpt) {
177                                 ts->sculpt= MEM_dupallocN(ts->sculpt);
178                                 copy_paint(&ts->sculpt->paint, &ts->sculpt->paint);
179                         }
180
181                         copy_paint(&ts->imapaint.paint, &ts->imapaint.paint);
182                         ts->imapaint.paintcursor= NULL;
183
184                         ts->particle.paintcursor= NULL;
185                 }
186                 
187                 BLI_duplicatelist(&(scen->markers), &(sce->markers));
188                 BLI_duplicatelist(&(scen->transform_spaces), &(sce->transform_spaces));
189                 BLI_duplicatelist(&(scen->r.layers), &(sce->r.layers));
190                 BKE_keyingsets_copy(&(scen->keyingsets), &(sce->keyingsets));
191                 
192                 scen->nodetree= ntreeCopyTree(sce->nodetree, 0);
193                 
194                 obase= sce->base.first;
195                 base= scen->base.first;
196                 while(base) {
197                         id_us_plus(&base->object->id);
198                         if(obase==sce->basact) scen->basact= base;
199         
200                         obase= obase->next;
201                         base= base->next;
202                 }
203         }
204         
205         /* make a private copy of the avicodecdata */
206         if(sce->r.avicodecdata) {
207                 scen->r.avicodecdata = MEM_dupallocN(sce->r.avicodecdata);
208                 scen->r.avicodecdata->lpFormat = MEM_dupallocN(scen->r.avicodecdata->lpFormat);
209                 scen->r.avicodecdata->lpParms = MEM_dupallocN(scen->r.avicodecdata->lpParms);
210         }
211         
212         /* make a private copy of the qtcodecdata */
213         if(sce->r.qtcodecdata) {
214                 scen->r.qtcodecdata = MEM_dupallocN(sce->r.qtcodecdata);
215                 scen->r.qtcodecdata->cdParms = MEM_dupallocN(scen->r.qtcodecdata->cdParms);
216         }
217         
218         /* NOTE: part of SCE_COPY_LINK_DATA and SCE_COPY_FULL operations
219          * are done outside of blenkernel with ED_objects_single_users! */
220
221     /*  camera */
222         if(type == SCE_COPY_LINK_DATA || type == SCE_COPY_FULL) {
223             ID_NEW(scen->camera);
224         }
225
226         /* world */
227         if(type == SCE_COPY_FULL) {
228         if(scen->world) {
229             id_us_plus((ID *)scen->world);
230             scen->world= copy_world(scen->world);
231         }
232         }
233
234         return scen;
235 }
236
237 /* do not free scene itself */
238 void free_scene(Scene *sce)
239 {
240         Base *base;
241
242         base= sce->base.first;
243         while(base) {
244                 base->object->id.us--;
245                 base= base->next;
246         }
247         /* do not free objects! */
248         
249         if(sce->gpd) {
250                 sce->gpd->id.us--;
251                 sce->gpd= NULL;
252         }
253
254         BLI_freelistN(&sce->base);
255         seq_free_editing(sce);
256
257         BKE_free_animdata((ID *)sce);
258         BKE_keyingsets_free(&sce->keyingsets);
259         
260         if (sce->r.avicodecdata) {
261                 free_avicodecdata(sce->r.avicodecdata);
262                 MEM_freeN(sce->r.avicodecdata);
263                 sce->r.avicodecdata = NULL;
264         }
265         if (sce->r.qtcodecdata) {
266                 free_qtcodecdata(sce->r.qtcodecdata);
267                 MEM_freeN(sce->r.qtcodecdata);
268                 sce->r.qtcodecdata = NULL;
269         }
270         if (sce->r.ffcodecdata.properties) {
271                 IDP_FreeProperty(sce->r.ffcodecdata.properties);
272                 MEM_freeN(sce->r.ffcodecdata.properties);
273                 sce->r.ffcodecdata.properties = NULL;
274         }
275         
276         BLI_freelistN(&sce->markers);
277         BLI_freelistN(&sce->transform_spaces);
278         BLI_freelistN(&sce->r.layers);
279         
280         if(sce->toolsettings) {
281                 if(sce->toolsettings->vpaint) {
282                         free_paint(&sce->toolsettings->vpaint->paint);
283                         MEM_freeN(sce->toolsettings->vpaint);
284                 }
285                 if(sce->toolsettings->wpaint) {
286                         free_paint(&sce->toolsettings->wpaint->paint);
287                         MEM_freeN(sce->toolsettings->wpaint);
288                 }
289                 if(sce->toolsettings->sculpt) {
290                         free_paint(&sce->toolsettings->sculpt->paint);
291                         MEM_freeN(sce->toolsettings->sculpt);
292                 }
293                 free_paint(&sce->toolsettings->imapaint.paint);
294
295                 MEM_freeN(sce->toolsettings);
296                 sce->toolsettings = NULL;       
297         }
298         
299         if (sce->theDag) {
300                 free_forest(sce->theDag);
301                 MEM_freeN(sce->theDag);
302         }
303         
304         if(sce->nodetree) {
305                 ntreeFreeTree(sce->nodetree);
306                 MEM_freeN(sce->nodetree);
307         }
308
309         if(sce->stats)
310                 MEM_freeN(sce->stats);
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= 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.xsch= 1920;
327         sce->r.ysch= 1080;
328         sce->r.xasp= 1;
329         sce->r.yasp= 1;
330         sce->r.xparts= 8;
331         sce->r.yparts= 8;
332         sce->r.size= 25;
333         sce->r.planes= 24;
334         sce->r.quality= 90;
335         sce->r.framapto= 100;
336         sce->r.images= 100;
337         sce->r.framelen= 1.0;
338         sce->r.frs_sec= 25;
339         sce->r.frs_sec_base= 1;
340         sce->r.ocres = 128;
341         sce->r.color_mgt_flag |= R_COLOR_MANAGEMENT;
342         
343         sce->r.bake_mode= 1;    /* prevent to include render stuff here */
344         sce->r.bake_filter= 8;
345         sce->r.bake_osa= 5;
346         sce->r.bake_flag= R_BAKE_CLEAR;
347         sce->r.bake_normal_space= R_BAKE_SPACE_TANGENT;
348
349         sce->r.scemode= R_DOCOMP|R_DOSEQ|R_EXTENSION;
350         sce->r.stamp= R_STAMP_TIME|R_STAMP_FRAME|R_STAMP_DATE|R_STAMP_SCENE|R_STAMP_CAMERA;
351         
352         sce->r.threads= 1;
353
354         sce->r.simplify_subsurf= 6;
355         sce->r.simplify_particles= 1.0f;
356         sce->r.simplify_shadowsamples= 16;
357         sce->r.simplify_aosss= 1.0f;
358
359         sce->r.cineonblack= 95;
360         sce->r.cineonwhite= 685;
361         sce->r.cineongamma= 1.7f;
362         
363         sce->toolsettings = MEM_callocN(sizeof(struct ToolSettings),"Tool Settings Struct");
364         sce->toolsettings->cornertype=1;
365         sce->toolsettings->degr = 90; 
366         sce->toolsettings->step = 9;
367         sce->toolsettings->turn = 1;                            
368         sce->toolsettings->extr_offs = 1; 
369         sce->toolsettings->doublimit = 0.001;
370         sce->toolsettings->segments = 32;
371         sce->toolsettings->rings = 32;
372         sce->toolsettings->vertices = 32;
373         sce->toolsettings->editbutflag = 1;
374         sce->toolsettings->uvcalc_radius = 1.0f;
375         sce->toolsettings->uvcalc_cubesize = 1.0f;
376         sce->toolsettings->uvcalc_mapdir = 1;
377         sce->toolsettings->uvcalc_mapalign = 1;
378         sce->toolsettings->unwrapper = 1;
379         sce->toolsettings->select_thresh= 0.01f;
380         sce->toolsettings->jointrilimit = 0.8f;
381
382         sce->toolsettings->selectmode= SCE_SELECT_VERTEX;
383         sce->toolsettings->normalsize= 0.1;
384         sce->toolsettings->autokey_mode= U.autokey_mode;
385
386         sce->toolsettings->skgen_resolution = 100;
387         sce->toolsettings->skgen_threshold_internal     = 0.01f;
388         sce->toolsettings->skgen_threshold_external     = 0.01f;
389         sce->toolsettings->skgen_angle_limit                    = 45.0f;
390         sce->toolsettings->skgen_length_ratio                   = 1.3f;
391         sce->toolsettings->skgen_length_limit                   = 1.5f;
392         sce->toolsettings->skgen_correlation_limit              = 0.98f;
393         sce->toolsettings->skgen_symmetry_limit                 = 0.1f;
394         sce->toolsettings->skgen_postpro = SKGEN_SMOOTH;
395         sce->toolsettings->skgen_postpro_passes = 1;
396         sce->toolsettings->skgen_options = SKGEN_FILTER_INTERNAL|SKGEN_FILTER_EXTERNAL|SKGEN_FILTER_SMART|SKGEN_HARMONIC|SKGEN_SUB_CORRELATION|SKGEN_STICK_TO_EMBEDDING;
397         sce->toolsettings->skgen_subdivisions[0] = SKGEN_SUB_CORRELATION;
398         sce->toolsettings->skgen_subdivisions[1] = SKGEN_SUB_LENGTH;
399         sce->toolsettings->skgen_subdivisions[2] = SKGEN_SUB_ANGLE;
400
401         sce->toolsettings->proportional_size = 1.0f;
402
403
404         sce->unit.scale_length = 1.0f;
405
406         pset= &sce->toolsettings->particle;
407         pset->flag= PE_KEEP_LENGTHS|PE_LOCK_FIRST|PE_DEFLECT_EMITTER|PE_AUTO_VELOCITY;
408         pset->emitterdist= 0.25f;
409         pset->totrekey= 5;
410         pset->totaddkey= 5;
411         pset->brushtype= PE_BRUSH_NONE;
412         pset->draw_step= 2;
413         pset->fade_frames= 2;
414         for(a=0; a<PE_TOT_BRUSH; a++) {
415                 pset->brush[a].strength= 50;
416                 pset->brush[a].size= 50;
417                 pset->brush[a].step= 10;
418         }
419         pset->brush[PE_BRUSH_CUT].strength= 100;
420         
421         sce->jumpframe = 10;
422         sce->r.ffcodecdata.audio_mixrate = 44100;
423
424         strcpy(sce->r.backbuf, "//backbuf");
425         strcpy(sce->r.pic, U.renderdir);
426
427         BLI_init_rctf(&sce->r.safety, 0.1f, 0.9f, 0.1f, 0.9f);
428         sce->r.osa= 8;
429
430         /* note; in header_info.c the scene copy happens..., if you add more to renderdata it has to be checked there */
431         scene_add_render_layer(sce);
432         
433         /* game data */
434         sce->gm.stereoflag = STEREO_NOSTEREO;
435         sce->gm.stereomode = STEREO_ANAGLYPH;
436         sce->gm.dome.angle = 180;
437         sce->gm.dome.mode = DOME_FISHEYE;
438         sce->gm.dome.res = 4;
439         sce->gm.dome.resbuf = 1.0f;
440         sce->gm.dome.tilt = 0;
441
442         sce->gm.xplay= 800;
443         sce->gm.yplay= 600;
444         sce->gm.freqplay= 60;
445         sce->gm.depth= 32;
446
447         sce->gm.gravity= 9.8f;
448         sce->gm.physicsEngine= WOPHY_BULLET;
449         sce->gm.mode = 32; //XXX ugly harcoding, still not sure we should drop mode. 32 == 1 << 5 == use_occlusion_culling 
450         sce->gm.occlusionRes = 128;
451         sce->gm.ticrate = 60;
452         sce->gm.maxlogicstep = 5;
453         sce->gm.physubstep = 1;
454         sce->gm.maxphystep = 5;
455
456         sce->gm.flag = GAME_DISPLAY_LISTS;
457         sce->gm.matmode = GAME_MAT_MULTITEX;
458
459         return sce;
460 }
461
462 Base *object_in_scene(Object *ob, Scene *sce)
463 {
464         Base *base;
465         
466         base= sce->base.first;
467         while(base) {
468                 if(base->object == ob) return base;
469                 base= base->next;
470         }
471         return NULL;
472 }
473
474 void set_scene_bg(Scene *scene)
475 {
476         Scene *sce;
477         Base *base;
478         Object *ob;
479         Group *group;
480         GroupObject *go;
481         int flag;
482         
483         /* check for cyclic sets, for reading old files but also for definite security (py?) */
484         scene_check_setscene(scene);
485         
486         /* deselect objects (for dataselect) */
487         for(ob= G.main->object.first; ob; ob= ob->id.next)
488                 ob->flag &= ~(SELECT|OB_FROMGROUP);
489
490         /* group flags again */
491         for(group= G.main->group.first; group; group= group->id.next) {
492                 go= group->gobject.first;
493                 while(go) {
494                         if(go->ob) go->ob->flag |= OB_FROMGROUP;
495                         go= go->next;
496                 }
497         }
498
499         /* sort baselist */
500         DAG_scene_sort(scene);
501         
502         /* ensure dags are built for sets */
503         for(sce= scene->set; sce; sce= sce->set)
504                 if(sce->theDag==NULL)
505                         DAG_scene_sort(sce);
506
507         /* copy layers and flags from bases to objects */
508         for(base= scene->base.first; base; base= base->next) {
509                 ob= base->object;
510                 ob->lay= base->lay;
511                 
512                 /* group patch... */
513                 base->flag &= ~(OB_FROMGROUP);
514                 flag= ob->flag & (OB_FROMGROUP);
515                 base->flag |= flag;
516                 
517                 /* not too nice... for recovering objects with lost data */
518                 //if(ob->pose==NULL) base->flag &= ~OB_POSEMODE;
519                 ob->flag= base->flag;
520                 
521                 ob->ctime= -1234567.0;  /* force ipo to be calculated later */
522         }
523         /* no full animation update, this to enable render code to work (render code calls own animation updates) */
524 }
525
526 /* called from creator.c */
527 void set_scene_name(char *name)
528 {
529         Scene *sce;
530
531         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
532                 if (BLI_streq(name, sce->id.name+2)) {
533                         set_scene_bg(sce);
534                         return;
535                 }
536         }
537         
538         //XXX error("Can't find scene: %s", name);
539 }
540
541 void unlink_scene(Main *bmain, Scene *sce, Scene *newsce)
542 {
543         Scene *sce1;
544         bScreen *sc;
545
546         /* check all sets */
547         for(sce1= bmain->scene.first; sce1; sce1= sce1->id.next)
548                 if(sce1->set == sce)
549                         sce1->set= NULL;
550         
551         /* check all sequences */
552         clear_scene_in_allseqs(sce);
553
554         /* check render layer nodes in other scenes */
555         clear_scene_in_nodes(bmain, sce);
556         
557         /* al screens */
558         for(sc= bmain->screen.first; sc; sc= sc->id.next)
559                 if(sc->scene == sce)
560                         sc->scene= newsce;
561
562         free_libblock(&bmain->scene, sce);
563 }
564
565 /* used by metaballs
566  * doesnt return the original duplicated object, only dupli's
567  */
568 int next_object(Scene *scene, int val, Base **base, Object **ob)
569 {
570         static ListBase *duplilist= NULL;
571         static DupliObject *dupob;
572         static int fase= F_START, in_next_object= 0;
573         int run_again=1;
574         
575         /* init */
576         if(val==0) {
577                 fase= F_START;
578                 dupob= NULL;
579                 
580                 /* XXX particle systems with metas+dupligroups call this recursively */
581                 /* see bug #18725 */
582                 if(in_next_object) {
583                         printf("ERROR: MetaBall generation called recursively, not supported\n");
584                         
585                         return F_ERROR;
586                 }
587         }
588         else {
589                 in_next_object= 1;
590                 
591                 /* run_again is set when a duplilist has been ended */
592                 while(run_again) {
593                         run_again= 0;
594
595                         /* the first base */
596                         if(fase==F_START) {
597                                 *base= scene->base.first;
598                                 if(*base) {
599                                         *ob= (*base)->object;
600                                         fase= F_SCENE;
601                                 }
602                                 else {
603                                     /* exception: empty scene */
604                                         if(scene->set && scene->set->base.first) {
605                                                 *base= scene->set->base.first;
606                                                 *ob= (*base)->object;
607                                                 fase= F_SET;
608                                         }
609                                 }
610                         }
611                         else {
612                                 if(*base && fase!=F_DUPLI) {
613                                         *base= (*base)->next;
614                                         if(*base) *ob= (*base)->object;
615                                         else {
616                                                 if(fase==F_SCENE) {
617                                                         /* scene is finished, now do the set */
618                                                         if(scene->set && scene->set->base.first) {
619                                                                 *base= scene->set->base.first;
620                                                                 *ob= (*base)->object;
621                                                                 fase= F_SET;
622                                                         }
623                                                 }
624                                         }
625                                 }
626                         }
627                         
628                         if(*base == NULL) fase= F_START;
629                         else {
630                                 if(fase!=F_DUPLI) {
631                                         if( (*base)->object->transflag & OB_DUPLI) {
632                                                 /* groups cannot be duplicated for mballs yet, 
633                                                 this enters eternal loop because of 
634                                                 makeDispListMBall getting called inside of group_duplilist */
635                                                 if((*base)->object->dup_group == NULL) {
636                                                         duplilist= object_duplilist(scene, (*base)->object);
637                                                         
638                                                         dupob= duplilist->first;
639
640                                                         if(!dupob)
641                                                                 free_object_duplilist(duplilist);
642                                                 }
643                                         }
644                                 }
645                                 /* handle dupli's */
646                                 if(dupob) {
647                                         
648                                         Mat4CpyMat4(dupob->ob->obmat, dupob->mat);
649                                         
650                                         (*base)->flag |= OB_FROMDUPLI;
651                                         *ob= dupob->ob;
652                                         fase= F_DUPLI;
653                                         
654                                         dupob= dupob->next;
655                                 }
656                                 else if(fase==F_DUPLI) {
657                                         fase= F_SCENE;
658                                         (*base)->flag &= ~OB_FROMDUPLI;
659                                         
660                                         for(dupob= duplilist->first; dupob; dupob= dupob->next) {
661                                                 Mat4CpyMat4(dupob->ob->obmat, dupob->omat);
662                                         }
663                                         
664                                         free_object_duplilist(duplilist);
665                                         duplilist= NULL;
666                                         run_again= 1;
667                                 }
668                         }
669                 }
670         }
671         
672         /* reset recursion test */
673         in_next_object= 0;
674         
675         return fase;
676 }
677
678 Object *scene_find_camera(Scene *sc)
679 {
680         Base *base;
681         
682         for (base= sc->base.first; base; base= base->next)
683                 if (base->object->type==OB_CAMERA)
684                         return base->object;
685
686         return NULL;
687 }
688
689
690 Base *scene_add_base(Scene *sce, Object *ob)
691 {
692         Base *b= MEM_callocN(sizeof(*b), "scene_add_base");
693         BLI_addhead(&sce->base, b);
694
695         b->object= ob;
696         b->flag= ob->flag;
697         b->lay= ob->lay;
698
699         return b;
700 }
701
702 void scene_deselect_all(Scene *sce)
703 {
704         Base *b;
705
706         for (b= sce->base.first; b; b= b->next) {
707                 b->flag&= ~SELECT;
708                 b->object->flag= b->flag;
709         }
710 }
711
712 void scene_select_base(Scene *sce, Base *selbase)
713 {
714         scene_deselect_all(sce);
715
716         selbase->flag |= SELECT;
717         selbase->object->flag= selbase->flag;
718
719         sce->basact= selbase;
720 }
721
722 /* checks for cycle, returns 1 if it's all OK */
723 int scene_check_setscene(Scene *sce)
724 {
725         Scene *scene;
726         int a, totscene;
727         
728         if(sce->set==NULL) return 1;
729         
730         totscene= 0;
731         for(scene= G.main->scene.first; scene; scene= scene->id.next)
732                 totscene++;
733         
734         for(a=0, scene=sce; scene->set; scene=scene->set, a++) {
735                 /* more iterations than scenes means we have a cycle */
736                 if(a > totscene) {
737                         /* the tested scene gets zero'ed, that's typically current scene */
738                         sce->set= NULL;
739                         return 0;
740                 }
741         }
742
743         return 1;
744 }
745
746 /* This (evil) function is needed to cope with two legacy Blender rendering features
747 * mblur (motion blur that renders 'subframes' and blurs them together), and fields 
748 * rendering. Thus, the use of ugly globals from object.c
749 */
750 // BAD... EVIL... JUJU...!!!!
751 // XXX moved here temporarily
752 float frame_to_float (Scene *scene, int cfra)           /* see also bsystem_time in object.c */
753 {
754         extern float bluroffs;  /* bad stuff borrowed from object.c */
755         extern float fieldoffs;
756         float ctime;
757         
758         ctime= (float)cfra;
759         ctime+= bluroffs+fieldoffs;
760         ctime*= scene->r.framelen;
761         
762         return ctime;
763 }
764
765 static void scene_update(Scene *sce, unsigned int lay)
766 {
767         Base *base;
768         Object *ob;
769         float ctime = frame_to_float(sce, sce->r.cfra); 
770         
771         if(sce->theDag==NULL)
772                 DAG_scene_sort(sce);
773         
774         DAG_scene_update_flags(sce, lay);   // only stuff that moves or needs display still
775         
776         /* All 'standard' (i.e. without any dependencies) animation is handled here,
777                 * with an 'local' to 'macro' order of evaluation. This should ensure that
778                 * settings stored nestled within a hierarchy (i.e. settings in a Texture block
779             * can be overridden by settings from Scene, which owns the Texture through a hierarchy 
780             * such as Scene->World->MTex/Texture) can still get correctly overridden.
781                 */
782         BKE_animsys_evaluate_all_animation(G.main, ctime);
783         
784         for(base= sce->base.first; base; base= base->next) {
785                 ob= base->object;
786                 
787                 object_handle_update(sce, ob);   // bke_object.h
788                 
789                 /* only update layer when an ipo */
790                         // XXX old animation system
791                 //if(ob->ipo && has_ipo_code(ob->ipo, OB_LAY) ) {
792                 //      base->lay= ob->lay;
793                 //}
794         }
795 }
796
797
798 /* applies changes right away, does all sets too */
799 void scene_update_for_newframe(Scene *sce, unsigned int lay)
800 {
801         Scene *scene= sce;
802         
803         /* clear animation overrides */
804         // XXX TODO...
805         
806         /* sets first, we allow per definition current scene to have dependencies on sets */
807         for(sce= sce->set; sce; sce= sce->set)
808                 scene_update(sce, lay);
809
810         scene_update(scene, lay);
811 }
812
813 /* return default layer, also used to patch old files */
814 void scene_add_render_layer(Scene *sce)
815 {
816         SceneRenderLayer *srl;
817         int tot= 1 + BLI_countlist(&sce->r.layers);
818         
819         srl= MEM_callocN(sizeof(SceneRenderLayer), "new render layer");
820         sprintf(srl->name, "%d RenderLayer", tot);
821         BLI_addtail(&sce->r.layers, srl);
822
823         /* note, this is also in render, pipeline.c, to make layer when scenedata doesnt have it */
824         srl->lay= (1<<20) -1;
825         srl->layflag= 0x7FFF;   /* solid ztra halo edge strand */
826         srl->passflag= SCE_PASS_COMBINED|SCE_PASS_Z;
827 }
828
829 /* render simplification */
830
831 int get_render_subsurf_level(RenderData *r, int lvl)
832 {
833         if(G.rt == 1 && (r->mode & R_SIMPLIFY))
834                 return MIN2(r->simplify_subsurf, lvl);
835         else
836                 return lvl;
837 }
838
839 int get_render_child_particle_number(RenderData *r, int num)
840 {
841         if(G.rt == 1 && (r->mode & R_SIMPLIFY))
842                 return (int)(r->simplify_particles*num);
843         else
844                 return num;
845 }
846
847 int get_render_shadow_samples(RenderData *r, int samples)
848 {
849         if(G.rt == 1 && (r->mode & R_SIMPLIFY) && samples > 0)
850                 return MIN2(r->simplify_shadowsamples, samples);
851         else
852                 return samples;
853 }
854
855 float get_render_aosss_error(RenderData *r, float error)
856 {
857         if(G.rt == 1 && (r->mode & R_SIMPLIFY))
858                 return ((1.0f-r->simplify_aosss)*10.0f + 1.0f)*error;
859         else
860                 return error;
861 }
862