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