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