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