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