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