remove references to BKE_utildefines where its not needed.
[blender-staging.git] / source / blender / blenkernel / intern / scene.c
1 /*  scene.c
2  *  
3  * 
4  * $Id$
5  *
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 <stddef.h>
33 #include <stdio.h>
34 #include <string.h>
35
36 #ifndef WIN32 
37 #include <unistd.h>
38 #else
39 #include <io.h>
40 #endif
41
42 #include "MEM_guardedalloc.h"
43
44 #include "DNA_anim_types.h"
45 #include "DNA_group_types.h"
46 #include "DNA_object_types.h"
47 #include "DNA_scene_types.h"
48 #include "DNA_screen_types.h"
49 #include "DNA_sequence_types.h"
50
51 #include "BLI_math.h"
52 #include "BLI_blenlib.h"
53 #include "BLI_utildefines.h"
54
55 #include "BKE_anim.h"
56 #include "BKE_animsys.h"
57 #include "BKE_depsgraph.h"
58 #include "BKE_global.h"
59 #include "BKE_group.h"
60 #include "BKE_idprop.h"
61 #include "BKE_library.h"
62 #include "BKE_main.h"
63 #include "BKE_node.h"
64 #include "BKE_object.h"
65 #include "BKE_paint.h"
66 #include "BKE_pointcache.h"
67 #include "BKE_scene.h"
68 #include "BKE_sequencer.h"
69 #include "BKE_world.h"
70
71 #include "BKE_sound.h"
72
73 //XXX #include "BIF_previewrender.h"
74 //XXX #include "BIF_editseq.h"
75
76 //XXX #include "nla.h"
77
78 #ifdef WIN32
79 #else
80 #include <sys/time.h>
81 #endif
82
83 void free_avicodecdata(AviCodecData *acd)
84 {
85         if (acd) {
86                 if (acd->lpFormat){
87                         MEM_freeN(acd->lpFormat);
88                         acd->lpFormat = NULL;
89                         acd->cbFormat = 0;
90                 }
91                 if (acd->lpParms){
92                         MEM_freeN(acd->lpParms);
93                         acd->lpParms = NULL;
94                         acd->cbParms = 0;
95                 }
96         }
97 }
98
99 void free_qtcodecdata(QuicktimeCodecData *qcd)
100 {
101         if (qcd) {
102                 if (qcd->cdParms){
103                         MEM_freeN(qcd->cdParms);
104                         qcd->cdParms = NULL;
105                         qcd->cdSize = 0;
106                 }
107         }
108 }
109
110 Scene *copy_scene(Scene *sce, int type)
111 {
112         Scene *scen;
113         ToolSettings *ts;
114         Base *base, *obase;
115         
116         if(type == SCE_COPY_EMPTY) {
117                 ListBase lb;
118                 scen= add_scene(sce->id.name+2);
119                 
120                 lb= scen->r.layers;
121                 scen->r= sce->r;
122                 scen->r.layers= lb;
123         }
124         else {
125                 scen= copy_libblock(sce);
126                 BLI_duplicatelist(&(scen->base), &(sce->base));
127                 
128                 clear_id_newpoins();
129                 
130                 id_us_plus((ID *)scen->world);
131                 id_us_plus((ID *)scen->set);
132                 id_us_plus((ID *)scen->gm.dome.warptext);
133
134                 scen->ed= NULL;
135                 scen->theDag= NULL;
136                 scen->obedit= NULL;
137                 scen->toolsettings= MEM_dupallocN(sce->toolsettings);
138                 scen->stats= NULL;
139                 scen->fps_info= NULL;
140
141                 ts= scen->toolsettings;
142                 if(ts) {
143                         if(ts->vpaint) {
144                                 ts->vpaint= MEM_dupallocN(ts->vpaint);
145                                 ts->vpaint->paintcursor= NULL;
146                                 ts->vpaint->vpaint_prev= NULL;
147                                 ts->vpaint->wpaint_prev= NULL;
148                                 copy_paint(&ts->vpaint->paint, &ts->vpaint->paint);
149                         }
150                         if(ts->wpaint) {
151                                 ts->wpaint= MEM_dupallocN(ts->wpaint);
152                                 ts->wpaint->paintcursor= NULL;
153                                 ts->wpaint->vpaint_prev= NULL;
154                                 ts->wpaint->wpaint_prev= NULL;
155                                 copy_paint(&ts->wpaint->paint, &ts->wpaint->paint);
156                         }
157                         if(ts->sculpt) {
158                                 ts->sculpt= MEM_dupallocN(ts->sculpt);
159                                 copy_paint(&ts->sculpt->paint, &ts->sculpt->paint);
160                         }
161
162                         copy_paint(&ts->imapaint.paint, &ts->imapaint.paint);
163                         ts->imapaint.paintcursor= NULL;
164
165                         ts->particle.paintcursor= NULL;
166                 }
167                 
168                 BLI_duplicatelist(&(scen->markers), &(sce->markers));
169                 BLI_duplicatelist(&(scen->transform_spaces), &(sce->transform_spaces));
170                 BLI_duplicatelist(&(scen->r.layers), &(sce->r.layers));
171                 BKE_keyingsets_copy(&(scen->keyingsets), &(sce->keyingsets));
172
173                 if(sce->nodetree) {
174                         scen->nodetree= ntreeCopyTree(sce->nodetree, 0); /* copies actions */
175                         ntreeSwitchID(scen->nodetree, &sce->id, &scen->id);
176                 }
177
178                 obase= sce->base.first;
179                 base= scen->base.first;
180                 while(base) {
181                         id_us_plus(&base->object->id);
182                         if(obase==sce->basact) scen->basact= base;
183         
184                         obase= obase->next;
185                         base= base->next;
186                 }
187         }
188         
189         /* make a private copy of the avicodecdata */
190         if(sce->r.avicodecdata) {
191                 scen->r.avicodecdata = MEM_dupallocN(sce->r.avicodecdata);
192                 scen->r.avicodecdata->lpFormat = MEM_dupallocN(scen->r.avicodecdata->lpFormat);
193                 scen->r.avicodecdata->lpParms = MEM_dupallocN(scen->r.avicodecdata->lpParms);
194         }
195         
196         /* make a private copy of the qtcodecdata */
197         if(sce->r.qtcodecdata) {
198                 scen->r.qtcodecdata = MEM_dupallocN(sce->r.qtcodecdata);
199                 scen->r.qtcodecdata->cdParms = MEM_dupallocN(scen->r.qtcodecdata->cdParms);
200         }
201         
202         if(sce->r.ffcodecdata.properties) { /* intentionally check scen not sce. */
203                 scen->r.ffcodecdata.properties= IDP_CopyProperty(sce->r.ffcodecdata.properties);
204         }
205
206         /* NOTE: part of SCE_COPY_LINK_DATA and SCE_COPY_FULL operations
207          * are done outside of blenkernel with ED_objects_single_users! */
208
209         /*  camera */
210         if(type == SCE_COPY_LINK_DATA || type == SCE_COPY_FULL) {
211                 ID_NEW(scen->camera);
212         }
213         
214         /* before scene copy */
215         sound_create_scene(scen);
216
217         /* world */
218         if(type == SCE_COPY_FULL) {
219                 BKE_copy_animdata_id_action((ID *)scen);
220                 if(scen->world) {
221                         id_us_plus((ID *)scen->world);
222                         scen->world= copy_world(scen->world);
223                         BKE_copy_animdata_id_action((ID *)scen->world);
224                 }
225
226                 if(sce->ed) {
227                         scen->ed= MEM_callocN( sizeof(Editing), "addseq");
228                         scen->ed->seqbasep= &scen->ed->seqbase;
229                         seqbase_dupli_recursive(sce, scen, &scen->ed->seqbase, &sce->ed->seqbase, SEQ_DUPE_ALL);
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         if(sce->gpd) {
249 #if 0   // removed since this can be invalid memory when freeing everything
250                 // since the grease pencil data is free'd before the scene.
251                 // since grease pencil data is not (yet?), shared between objects
252                 // its probably safe not to do this, some save and reload will free this.
253                 sce->gpd->id.us--;
254 #endif
255                 sce->gpd= NULL;
256         }
257
258         BLI_freelistN(&sce->base);
259         seq_free_editing(sce);
260
261         BKE_free_animdata((ID *)sce);
262         BKE_keyingsets_free(&sce->keyingsets);
263         
264         if (sce->r.avicodecdata) {
265                 free_avicodecdata(sce->r.avicodecdata);
266                 MEM_freeN(sce->r.avicodecdata);
267                 sce->r.avicodecdata = NULL;
268         }
269         if (sce->r.qtcodecdata) {
270                 free_qtcodecdata(sce->r.qtcodecdata);
271                 MEM_freeN(sce->r.qtcodecdata);
272                 sce->r.qtcodecdata = NULL;
273         }
274         if (sce->r.ffcodecdata.properties) {
275                 IDP_FreeProperty(sce->r.ffcodecdata.properties);
276                 MEM_freeN(sce->r.ffcodecdata.properties);
277                 sce->r.ffcodecdata.properties = NULL;
278         }
279         
280         BLI_freelistN(&sce->markers);
281         BLI_freelistN(&sce->transform_spaces);
282         BLI_freelistN(&sce->r.layers);
283         
284         if(sce->toolsettings) {
285                 if(sce->toolsettings->vpaint) {
286                         free_paint(&sce->toolsettings->vpaint->paint);
287                         MEM_freeN(sce->toolsettings->vpaint);
288                 }
289                 if(sce->toolsettings->wpaint) {
290                         free_paint(&sce->toolsettings->wpaint->paint);
291                         MEM_freeN(sce->toolsettings->wpaint);
292                 }
293                 if(sce->toolsettings->sculpt) {
294                         free_paint(&sce->toolsettings->sculpt->paint);
295                         MEM_freeN(sce->toolsettings->sculpt);
296                 }
297                 free_paint(&sce->toolsettings->imapaint.paint);
298
299                 MEM_freeN(sce->toolsettings);
300                 sce->toolsettings = NULL;       
301         }
302         
303         if (sce->theDag) {
304                 free_forest(sce->theDag);
305                 MEM_freeN(sce->theDag);
306         }
307         
308         if(sce->nodetree) {
309                 ntreeFreeTree(sce->nodetree);
310                 MEM_freeN(sce->nodetree);
311         }
312
313         if(sce->stats)
314                 MEM_freeN(sce->stats);
315         if(sce->fps_info)
316                 MEM_freeN(sce->fps_info);
317
318         sound_destroy_scene(sce);
319 }
320
321 Scene *add_scene(const char *name)
322 {
323         Main *bmain= G.main;
324         Scene *sce;
325         ParticleEditSettings *pset;
326         int a;
327
328         sce= alloc_libblock(&bmain->scene, ID_SCE, name);
329         sce->lay= sce->layact= 1;
330         
331         sce->r.mode= R_GAMMA|R_OSA|R_SHADOW|R_SSS|R_ENVMAP|R_RAYTRACE;
332         sce->r.cfra= 1;
333         sce->r.sfra= 1;
334         sce->r.efra= 250;
335         sce->r.frame_step= 1;
336         sce->r.xsch= 1920;
337         sce->r.ysch= 1080;
338         sce->r.xasp= 1;
339         sce->r.yasp= 1;
340         sce->r.xparts= 8;
341         sce->r.yparts= 8;
342         sce->r.mblur_samples= 1;
343         sce->r.filtertype= R_FILTER_MITCH;
344         sce->r.size= 50;
345         sce->r.planes= 24;
346         sce->r.imtype= R_PNG;
347         sce->r.quality= 90;
348         sce->r.displaymode= R_OUTPUT_AREA;
349         sce->r.framapto= 100;
350         sce->r.images= 100;
351         sce->r.framelen= 1.0;
352         sce->r.blurfac= 0.5;
353         sce->r.frs_sec= 24;
354         sce->r.frs_sec_base= 1;
355         sce->r.edgeint= 10;
356         sce->r.ocres = 128;
357         sce->r.color_mgt_flag |= R_COLOR_MANAGEMENT;
358         sce->r.gauss= 1.0;
359         
360         /* deprecated but keep for upwards compat */
361         sce->r.postgamma= 1.0;
362         sce->r.posthue= 0.0;
363         sce->r.postsat= 1.0;
364         
365         sce->r.bake_mode= 1;    /* prevent to include render stuff here */
366         sce->r.bake_filter= 2;
367         sce->r.bake_osa= 5;
368         sce->r.bake_flag= R_BAKE_CLEAR;
369         sce->r.bake_normal_space= R_BAKE_SPACE_TANGENT;
370         sce->r.scemode= R_DOCOMP|R_DOSEQ|R_EXTENSION;
371         sce->r.stamp= R_STAMP_TIME|R_STAMP_FRAME|R_STAMP_DATE|R_STAMP_CAMERA|R_STAMP_SCENE|R_STAMP_FILENAME|R_STAMP_RENDERTIME;
372         sce->r.stamp_font_id= 12;
373         sce->r.fg_stamp[0]= sce->r.fg_stamp[1]= sce->r.fg_stamp[2]= 0.8f;
374         sce->r.fg_stamp[3]= 1.0f;
375         sce->r.bg_stamp[0]= sce->r.bg_stamp[1]= sce->r.bg_stamp[2]= 0.0f;
376         sce->r.bg_stamp[3]= 0.25f;
377
378         sce->r.seq_prev_type= OB_SOLID;
379         sce->r.seq_rend_type= OB_SOLID;
380         sce->r.seq_flag= R_SEQ_GL_PREV;
381
382         sce->r.threads= 1;
383
384         sce->r.simplify_subsurf= 6;
385         sce->r.simplify_particles= 1.0f;
386         sce->r.simplify_shadowsamples= 16;
387         sce->r.simplify_aosss= 1.0f;
388
389         sce->r.cineonblack= 95;
390         sce->r.cineonwhite= 685;
391         sce->r.cineongamma= 1.7f;
392
393         sce->r.border.xmin= 0.0f;
394         sce->r.border.ymin= 0.0f;
395         sce->r.border.xmax= 1.0f;
396         sce->r.border.ymax= 1.0f;
397         
398         sce->toolsettings = MEM_callocN(sizeof(struct ToolSettings),"Tool Settings Struct");
399         sce->toolsettings->cornertype=1;
400         sce->toolsettings->degr = 90; 
401         sce->toolsettings->step = 9;
402         sce->toolsettings->turn = 1;                            
403         sce->toolsettings->extr_offs = 1; 
404         sce->toolsettings->doublimit = 0.001;
405         sce->toolsettings->segments = 32;
406         sce->toolsettings->rings = 32;
407         sce->toolsettings->vertices = 32;
408         sce->toolsettings->editbutflag = 1;
409         sce->toolsettings->uvcalc_radius = 1.0f;
410         sce->toolsettings->uvcalc_cubesize = 1.0f;
411         sce->toolsettings->uvcalc_mapdir = 1;
412         sce->toolsettings->uvcalc_mapalign = 1;
413         sce->toolsettings->unwrapper = 1;
414         sce->toolsettings->select_thresh= 0.01f;
415         sce->toolsettings->jointrilimit = 0.8f;
416
417         sce->toolsettings->selectmode= SCE_SELECT_VERTEX;
418         sce->toolsettings->uv_selectmode= UV_SELECT_VERTEX;
419         sce->toolsettings->normalsize= 0.1;
420         sce->toolsettings->autokey_mode= U.autokey_mode;
421
422         sce->toolsettings->skgen_resolution = 100;
423         sce->toolsettings->skgen_threshold_internal     = 0.01f;
424         sce->toolsettings->skgen_threshold_external     = 0.01f;
425         sce->toolsettings->skgen_angle_limit                    = 45.0f;
426         sce->toolsettings->skgen_length_ratio                   = 1.3f;
427         sce->toolsettings->skgen_length_limit                   = 1.5f;
428         sce->toolsettings->skgen_correlation_limit              = 0.98f;
429         sce->toolsettings->skgen_symmetry_limit                 = 0.1f;
430         sce->toolsettings->skgen_postpro = SKGEN_SMOOTH;
431         sce->toolsettings->skgen_postpro_passes = 1;
432         sce->toolsettings->skgen_options = SKGEN_FILTER_INTERNAL|SKGEN_FILTER_EXTERNAL|SKGEN_FILTER_SMART|SKGEN_HARMONIC|SKGEN_SUB_CORRELATION|SKGEN_STICK_TO_EMBEDDING;
433         sce->toolsettings->skgen_subdivisions[0] = SKGEN_SUB_CORRELATION;
434         sce->toolsettings->skgen_subdivisions[1] = SKGEN_SUB_LENGTH;
435         sce->toolsettings->skgen_subdivisions[2] = SKGEN_SUB_ANGLE;
436
437         sce->toolsettings->proportional_size = 1.0f;
438
439         sce->physics_settings.gravity[0] = 0.0f;
440         sce->physics_settings.gravity[1] = 0.0f;
441         sce->physics_settings.gravity[2] = -9.81f;
442         sce->physics_settings.flag = PHYS_GLOBAL_GRAVITY;
443
444         sce->unit.scale_length = 1.0f;
445
446         pset= &sce->toolsettings->particle;
447         pset->flag= PE_KEEP_LENGTHS|PE_LOCK_FIRST|PE_DEFLECT_EMITTER|PE_AUTO_VELOCITY;
448         pset->emitterdist= 0.25f;
449         pset->totrekey= 5;
450         pset->totaddkey= 5;
451         pset->brushtype= PE_BRUSH_NONE;
452         pset->draw_step= 2;
453         pset->fade_frames= 2;
454         pset->selectmode= SCE_SELECT_PATH;
455         for(a=0; a<PE_TOT_BRUSH; a++) {
456                 pset->brush[a].strength= 0.5;
457                 pset->brush[a].size= 50;
458                 pset->brush[a].step= 10;
459                 pset->brush[a].count= 10;
460         }
461         pset->brush[PE_BRUSH_CUT].strength= 100;
462
463         sce->r.ffcodecdata.audio_mixrate = 44100;
464         sce->r.ffcodecdata.audio_volume = 1.0f;
465
466         BLI_strncpy(sce->r.engine, "BLENDER_RENDER", sizeof(sce->r.engine));
467
468         sce->audio.distance_model = 2.0;
469         sce->audio.doppler_factor = 1.0;
470         sce->audio.speed_of_sound = 343.3;
471
472         strcpy(sce->r.backbuf, "//backbuf");
473         strcpy(sce->r.pic, U.renderdir);
474
475         BLI_init_rctf(&sce->r.safety, 0.1f, 0.9f, 0.1f, 0.9f);
476         sce->r.osa= 8;
477
478         /* note; in header_info.c the scene copy happens..., if you add more to renderdata it has to be checked there */
479         scene_add_render_layer(sce);
480         
481         /* game data */
482         sce->gm.stereoflag = STEREO_NOSTEREO;
483         sce->gm.stereomode = STEREO_ANAGLYPH;
484         sce->gm.eyeseparation = 0.10;
485
486         sce->gm.dome.angle = 180;
487         sce->gm.dome.mode = DOME_FISHEYE;
488         sce->gm.dome.res = 4;
489         sce->gm.dome.resbuf = 1.0f;
490         sce->gm.dome.tilt = 0;
491
492         sce->gm.xplay= 640;
493         sce->gm.yplay= 480;
494         sce->gm.freqplay= 60;
495         sce->gm.depth= 32;
496
497         sce->gm.gravity= 9.8f;
498         sce->gm.physicsEngine= WOPHY_BULLET;
499         sce->gm.mode = 32; //XXX ugly harcoding, still not sure we should drop mode. 32 == 1 << 5 == use_occlusion_culling 
500         sce->gm.occlusionRes = 128;
501         sce->gm.ticrate = 60;
502         sce->gm.maxlogicstep = 5;
503         sce->gm.physubstep = 1;
504         sce->gm.maxphystep = 5;
505
506         sce->gm.flag = GAME_DISPLAY_LISTS;
507         sce->gm.matmode = GAME_MAT_MULTITEX;
508
509         sound_create_scene(sce);
510
511         return sce;
512 }
513
514 Base *object_in_scene(Object *ob, Scene *sce)
515 {
516         Base *base;
517         
518         base= sce->base.first;
519         while(base) {
520                 if(base->object == ob) return base;
521                 base= base->next;
522         }
523         return NULL;
524 }
525
526 void set_scene_bg(Main *bmain, Scene *scene)
527 {
528         Scene *sce;
529         Base *base;
530         Object *ob;
531         Group *group;
532         GroupObject *go;
533         int flag;
534         
535         /* check for cyclic sets, for reading old files but also for definite security (py?) */
536         scene_check_setscene(bmain, scene);
537         
538         /* can happen when switching modes in other scenes */
539         if(scene->obedit && !(scene->obedit->mode & OB_MODE_EDIT))
540                 scene->obedit= NULL;
541
542         /* deselect objects (for dataselect) */
543         for(ob= bmain->object.first; ob; ob= ob->id.next)
544                 ob->flag &= ~(SELECT|OB_FROMGROUP);
545
546         /* group flags again */
547         for(group= bmain->group.first; group; group= group->id.next) {
548                 go= group->gobject.first;
549                 while(go) {
550                         if(go->ob) go->ob->flag |= OB_FROMGROUP;
551                         go= go->next;
552                 }
553         }
554
555         /* sort baselist */
556         DAG_scene_sort(bmain, scene);
557         
558         /* ensure dags are built for sets */
559         for(sce= scene->set; sce; sce= sce->set)
560                 if(sce->theDag==NULL)
561                         DAG_scene_sort(bmain, sce);
562
563         /* copy layers and flags from bases to objects */
564         for(base= scene->base.first; base; base= base->next) {
565                 ob= base->object;
566                 ob->lay= base->lay;
567                 
568                 /* group patch... */
569                 base->flag &= ~(OB_FROMGROUP);
570                 flag= ob->flag & (OB_FROMGROUP);
571                 base->flag |= flag;
572                 
573                 /* not too nice... for recovering objects with lost data */
574                 //if(ob->pose==NULL) base->flag &= ~OB_POSEMODE;
575                 ob->flag= base->flag;
576                 
577                 ob->ctime= -1234567.0;  /* force ipo to be calculated later */
578         }
579         /* no full animation update, this to enable render code to work (render code calls own animation updates) */
580 }
581
582 /* called from creator.c */
583 Scene *set_scene_name(Main *bmain, char *name)
584 {
585         Scene *sce= (Scene *)find_id("SC", name);
586         if(sce) {
587                 set_scene_bg(bmain, sce);
588                 printf("Scene switch: '%s' in file: '%s'\n", name, G.main->name);
589                 return sce;
590         }
591
592         printf("Can't find scene: '%s' in file: '%s'\n", name, G.main->name);
593         return NULL;
594 }
595
596 void unlink_scene(Main *bmain, Scene *sce, Scene *newsce)
597 {
598         Scene *sce1;
599         bScreen *sc;
600
601         /* check all sets */
602         for(sce1= bmain->scene.first; sce1; sce1= sce1->id.next)
603                 if(sce1->set == sce)
604                         sce1->set= NULL;
605         
606         /* check all sequences */
607         clear_scene_in_allseqs(bmain, sce);
608
609         /* check render layer nodes in other scenes */
610         clear_scene_in_nodes(bmain, sce);
611         
612         /* al screens */
613         for(sc= bmain->screen.first; sc; sc= sc->id.next)
614                 if(sc->scene == sce)
615                         sc->scene= newsce;
616
617         free_libblock(&bmain->scene, sce);
618 }
619
620 /* used by metaballs
621  * doesnt return the original duplicated object, only dupli's
622  */
623 int next_object(Scene **scene, int val, Base **base, Object **ob)
624 {
625         static ListBase *duplilist= NULL;
626         static DupliObject *dupob;
627         static int fase= F_START, in_next_object= 0;
628         int run_again=1;
629         
630         /* init */
631         if(val==0) {
632                 fase= F_START;
633                 dupob= NULL;
634                 
635                 /* XXX particle systems with metas+dupligroups call this recursively */
636                 /* see bug #18725 */
637                 if(in_next_object) {
638                         printf("ERROR: MetaBall generation called recursively, not supported\n");
639                         
640                         return F_ERROR;
641                 }
642         }
643         else {
644                 in_next_object= 1;
645                 
646                 /* run_again is set when a duplilist has been ended */
647                 while(run_again) {
648                         run_again= 0;
649
650                         /* the first base */
651                         if(fase==F_START) {
652                                 *base= (*scene)->base.first;
653                                 if(*base) {
654                                         *ob= (*base)->object;
655                                         fase= F_SCENE;
656                                 }
657                                 else {
658                                         /* exception: empty scene */
659                                         while((*scene)->set) {
660                                                 (*scene)= (*scene)->set;
661                                                 if((*scene)->base.first) {
662                                                         *base= (*scene)->base.first;
663                                                         *ob= (*base)->object;
664                                                         fase= F_SCENE;
665                                                         break;
666                                                 }
667                                         }
668                                 }
669                         }
670                         else {
671                                 if(*base && fase!=F_DUPLI) {
672                                         *base= (*base)->next;
673                                         if(*base) *ob= (*base)->object;
674                                         else {
675                                                 if(fase==F_SCENE) {
676                                                         /* (*scene) is finished, now do the set */
677                                                         while((*scene)->set) {
678                                                                 (*scene)= (*scene)->set;
679                                                                 if((*scene)->base.first) {
680                                                                         *base= (*scene)->base.first;
681                                                                         *ob= (*base)->object;
682                                                                         break;
683                                                                 }
684                                                         }
685                                                 }
686                                         }
687                                 }
688                         }
689                         
690                         if(*base == NULL) fase= F_START;
691                         else {
692                                 if(fase!=F_DUPLI) {
693                                         if( (*base)->object->transflag & OB_DUPLI) {
694                                                 /* groups cannot be duplicated for mballs yet, 
695                                                 this enters eternal loop because of 
696                                                 makeDispListMBall getting called inside of group_duplilist */
697                                                 if((*base)->object->dup_group == NULL) {
698                                                         duplilist= object_duplilist((*scene), (*base)->object);
699                                                         
700                                                         dupob= duplilist->first;
701
702                                                         if(!dupob)
703                                                                 free_object_duplilist(duplilist);
704                                                 }
705                                         }
706                                 }
707                                 /* handle dupli's */
708                                 if(dupob) {
709                                         
710                                         copy_m4_m4(dupob->ob->obmat, dupob->mat);
711                                         
712                                         (*base)->flag |= OB_FROMDUPLI;
713                                         *ob= dupob->ob;
714                                         fase= F_DUPLI;
715                                         
716                                         dupob= dupob->next;
717                                 }
718                                 else if(fase==F_DUPLI) {
719                                         fase= F_SCENE;
720                                         (*base)->flag &= ~OB_FROMDUPLI;
721                                         
722                                         for(dupob= duplilist->first; dupob; dupob= dupob->next) {
723                                                 copy_m4_m4(dupob->ob->obmat, dupob->omat);
724                                         }
725                                         
726                                         free_object_duplilist(duplilist);
727                                         duplilist= NULL;
728                                         run_again= 1;
729                                 }
730                         }
731                 }
732         }
733         
734         /* if(ob && *ob) {
735                 printf("Scene: '%s', '%s'\n", (*scene)->id.name+2, (*ob)->id.name+2);
736         } */
737
738         /* reset recursion test */
739         in_next_object= 0;
740         
741         return fase;
742 }
743
744 Object *scene_find_camera(Scene *sc)
745 {
746         Base *base;
747         
748         for (base= sc->base.first; base; base= base->next)
749                 if (base->object->type==OB_CAMERA)
750                         return base->object;
751
752         return NULL;
753 }
754
755 #ifdef DURIAN_CAMERA_SWITCH
756 Object *scene_camera_switch_find(Scene *scene)
757 {
758         TimeMarker *m;
759         int cfra = scene->r.cfra;
760         int frame = -(MAXFRAME + 1);
761         Object *camera= NULL;
762
763         for (m= scene->markers.first; m; m= m->next) {
764                 if(m->camera && (m->camera->restrictflag & OB_RESTRICT_RENDER)==0 && (m->frame <= cfra) && (m->frame > frame)) {
765                         camera= m->camera;
766                         frame= m->frame;
767
768                         if(frame == cfra)
769                                 break;
770
771                 }
772         }
773         return camera;
774 }
775 #endif
776
777 int scene_camera_switch_update(Scene *scene)
778 {
779 #ifdef DURIAN_CAMERA_SWITCH
780         Object *camera= scene_camera_switch_find(scene);
781         if(camera) {
782                 scene->camera= camera;
783                 return 1;
784         }
785 #endif
786         return 0;
787 }
788
789 char *scene_find_marker_name(Scene *scene, int frame)
790 {
791         ListBase *markers= &scene->markers;
792         TimeMarker *m1, *m2;
793
794         /* search through markers for match */
795         for (m1=markers->first, m2=markers->last; m1 && m2; m1=m1->next, m2=m2->prev) {
796                 if (m1->frame==frame)
797                         return m1->name;
798
799                 if (m1 == m2)
800                         break;
801
802                 if (m2->frame==frame)
803                         return m2->name;
804         }
805
806         return NULL;
807 }
808
809 /* return the current marker for this frame,
810 we can have more then 1 marker per frame, this just returns the first :/ */
811 char *scene_find_last_marker_name(Scene *scene, int frame)
812 {
813         TimeMarker *marker, *best_marker = NULL;
814         int best_frame = -MAXFRAME*2;
815         for (marker= scene->markers.first; marker; marker= marker->next) {
816                 if (marker->frame==frame) {
817                         return marker->name;
818                 }
819
820                 if ( marker->frame > best_frame && marker->frame < frame) {
821                         best_marker = marker;
822                         best_frame = marker->frame;
823                 }
824         }
825
826         return best_marker ? best_marker->name : NULL;
827 }
828
829 /* markers need transforming from different parts of the code so have
830  * a generic function to do this */
831 int scene_marker_tfm_translate(Scene *scene, int delta, int flag)
832 {
833         TimeMarker *marker;
834         int tot= 0;
835
836         for (marker= scene->markers.first; marker; marker= marker->next) {
837                 if ((marker->flag & flag) == flag) {
838                         marker->frame += delta;
839                         tot++;
840                 }
841         }
842
843         return tot;
844 }
845
846 int scene_marker_tfm_extend(Scene *scene, int delta, int flag, int frame, char side)
847 {
848         TimeMarker *marker;
849         int tot= 0;
850
851         for (marker= scene->markers.first; marker; marker= marker->next) {
852                 if ((marker->flag & flag) == flag) {
853                         if((side=='L' && marker->frame < frame) || (side=='R' && marker->frame >= frame)) {
854                                 marker->frame += delta;
855                                 tot++;
856                         }
857                 }
858         }
859
860         return tot;
861 }
862
863 int scene_marker_tfm_scale(struct Scene *scene, float value, int flag)
864 {
865         TimeMarker *marker;
866         int tot= 0;
867
868         for (marker= scene->markers.first; marker; marker= marker->next) {
869                 if ((marker->flag & flag) == flag) {
870                         marker->frame= CFRA + (int)floorf(((float)(marker->frame - CFRA) * value) + 0.5f);
871                         tot++;
872                 }
873         }
874
875         return tot;
876 }
877
878 Base *scene_add_base(Scene *sce, Object *ob)
879 {
880         Base *b= MEM_callocN(sizeof(*b), "scene_add_base");
881         BLI_addhead(&sce->base, b);
882
883         b->object= ob;
884         b->flag= ob->flag;
885         b->lay= ob->lay;
886
887         return b;
888 }
889
890 void scene_deselect_all(Scene *sce)
891 {
892         Base *b;
893
894         for (b= sce->base.first; b; b= b->next) {
895                 b->flag&= ~SELECT;
896                 b->object->flag= b->flag;
897         }
898 }
899
900 void scene_select_base(Scene *sce, Base *selbase)
901 {
902         scene_deselect_all(sce);
903
904         selbase->flag |= SELECT;
905         selbase->object->flag= selbase->flag;
906
907         sce->basact= selbase;
908 }
909
910 /* checks for cycle, returns 1 if it's all OK */
911 int scene_check_setscene(Main *bmain, Scene *sce)
912 {
913         Scene *scene;
914         int a, totscene;
915         
916         if(sce->set==NULL) return 1;
917         
918         totscene= 0;
919         for(scene= bmain->scene.first; scene; scene= scene->id.next)
920                 totscene++;
921         
922         for(a=0, scene=sce; scene->set; scene=scene->set, a++) {
923                 /* more iterations than scenes means we have a cycle */
924                 if(a > totscene) {
925                         /* the tested scene gets zero'ed, that's typically current scene */
926                         sce->set= NULL;
927                         return 0;
928                 }
929         }
930
931         return 1;
932 }
933
934 /* This function is needed to cope with fractional frames - including two Blender rendering features
935 * mblur (motion blur that renders 'subframes' and blurs them together), and fields rendering. */
936
937 /* see also bsystem_time in object.c */
938 float BKE_curframe(Scene *scene)
939 {
940         float ctime = scene->r.cfra;
941         ctime+= scene->r.subframe;
942         ctime*= scene->r.framelen;      
943
944         return ctime;
945 }
946
947 static void scene_update_tagged_recursive(Main *bmain, Scene *scene, Scene *scene_parent)
948 {
949         Base *base;
950         scene->customdata_mask= scene_parent->customdata_mask;
951
952         /* sets first, we allow per definition current scene to have
953            dependencies on sets, but not the other way around. */
954         if(scene->set)
955                 scene_update_tagged_recursive(bmain, scene->set, scene_parent);
956
957         for(base= scene->base.first; base; base= base->next) {
958                 Object *ob= base->object;
959
960                 object_handle_update(scene_parent, ob);
961
962                 if(ob->dup_group && (ob->transflag & OB_DUPLIGROUP))
963                         group_handle_recalc_and_update(scene_parent, ob, ob->dup_group);
964                         
965                 /* always update layer, so that animating layers works */
966                 base->lay= ob->lay;
967         }
968 }
969
970 /* this is called in main loop, doing tagged updates before redraw */
971 void scene_update_tagged(Main *bmain, Scene *scene)
972 {
973         DAG_ids_flush_tagged(bmain);
974
975         scene->physics_settings.quick_cache_step= 0;
976
977         /* update all objects: drivers, matrices, displists, etc. flags set
978            by depgraph or manual, no layer check here, gets correct flushed */
979
980         scene_update_tagged_recursive(bmain, scene, scene);
981
982         /* recalc scene animation data here (for sequencer) */
983         {
984                 float ctime = BKE_curframe(scene); 
985                 AnimData *adt= BKE_animdata_from_id(&scene->id);
986
987                 if(adt && (adt->recalc & ADT_RECALC_ANIM))
988                         BKE_animsys_evaluate_animdata(&scene->id, adt, ctime, 0);
989         }
990
991         if(scene->physics_settings.quick_cache_step)
992                 BKE_ptcache_quick_cache_all(bmain, scene);
993
994         /* in the future this should handle updates for all datablocks, not
995            only objects and scenes. - brecht */
996 }
997
998 /* applies changes right away, does all sets too */
999 void scene_update_for_newframe(Main *bmain, Scene *sce, unsigned int lay)
1000 {
1001         float ctime = BKE_curframe(sce);
1002         Scene *sce_iter;
1003         
1004         /* clear animation overrides */
1005         // XXX TODO...
1006
1007         for(sce_iter= sce; sce_iter; sce_iter= sce_iter->set) {
1008                 if(sce_iter->theDag==NULL)
1009                         DAG_scene_sort(bmain, sce_iter);
1010         }
1011
1012
1013         /* Following 2 functions are recursive
1014          * so dont call within 'scene_update_tagged_recursive' */
1015         DAG_scene_update_flags(bmain, sce, lay, TRUE);   // only stuff that moves or needs display still
1016
1017         /* All 'standard' (i.e. without any dependencies) animation is handled here,
1018          * with an 'local' to 'macro' order of evaluation. This should ensure that
1019          * settings stored nestled within a hierarchy (i.e. settings in a Texture block
1020          * can be overridden by settings from Scene, which owns the Texture through a hierarchy
1021          * such as Scene->World->MTex/Texture) can still get correctly overridden.
1022          */
1023         BKE_animsys_evaluate_all_animation(bmain, ctime);
1024         /*...done with recusrive funcs */
1025
1026         /* object_handle_update() on all objects, groups and sets */
1027         scene_update_tagged_recursive(bmain, sce, sce);
1028 }
1029
1030 /* return default layer, also used to patch old files */
1031 void scene_add_render_layer(Scene *sce)
1032 {
1033         SceneRenderLayer *srl;
1034 //      int tot= 1 + BLI_countlist(&sce->r.layers);
1035         
1036         srl= MEM_callocN(sizeof(SceneRenderLayer), "new render layer");
1037         strcpy(srl->name, "RenderLayer");
1038         BLI_uniquename(&sce->r.layers, srl, "RenderLayer", '.', offsetof(SceneRenderLayer, name), sizeof(srl->name));
1039         BLI_addtail(&sce->r.layers, srl);
1040
1041         /* note, this is also in render, pipeline.c, to make layer when scenedata doesnt have it */
1042         srl->lay= (1<<20) -1;
1043         srl->layflag= 0x7FFF;   /* solid ztra halo edge strand */
1044         srl->passflag= SCE_PASS_COMBINED|SCE_PASS_Z;
1045 }
1046
1047 /* render simplification */
1048
1049 int get_render_subsurf_level(RenderData *r, int lvl)
1050 {
1051         if(r->mode & R_SIMPLIFY)
1052                 return MIN2(r->simplify_subsurf, lvl);
1053         else
1054                 return lvl;
1055 }
1056
1057 int get_render_child_particle_number(RenderData *r, int num)
1058 {
1059         if(r->mode & R_SIMPLIFY)
1060                 return (int)(r->simplify_particles*num);
1061         else
1062                 return num;
1063 }
1064
1065 int get_render_shadow_samples(RenderData *r, int samples)
1066 {
1067         if((r->mode & R_SIMPLIFY) && samples > 0)
1068                 return MIN2(r->simplify_shadowsamples, samples);
1069         else
1070                 return samples;
1071 }
1072
1073 float get_render_aosss_error(RenderData *r, float error)
1074 {
1075         if(r->mode & R_SIMPLIFY)
1076                 return ((1.0f-r->simplify_aosss)*10.0f + 1.0f)*error;
1077         else
1078                 return error;
1079 }
1080
1081 /* helper function for the SETLOOPER macro */
1082 Base *_setlooper_base_step(Scene **sce_iter, Base *base)
1083 {
1084     if(base && base->next) {
1085         /* common case, step to the next */
1086         return base->next;
1087     }
1088         else if(base==NULL && (*sce_iter)->base.first) {
1089         /* first time looping, return the scenes first base */
1090                 return (Base *)(*sce_iter)->base.first;
1091     }
1092     else {
1093         /* reached the end, get the next base in the set */
1094                 while((*sce_iter= (*sce_iter)->set)) {
1095                         base= (Base *)(*sce_iter)->base.first;
1096             if(base) {
1097                 return base;
1098             }
1099         }
1100     }
1101
1102     return NULL;
1103 }