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