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