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