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