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