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