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