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