correct fsf address
[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         
368         sce->r.threads= 1;
369
370         sce->r.simplify_subsurf= 6;
371         sce->r.simplify_particles= 1.0f;
372         sce->r.simplify_shadowsamples= 16;
373         sce->r.simplify_aosss= 1.0f;
374
375         sce->r.cineonblack= 95;
376         sce->r.cineonwhite= 685;
377         sce->r.cineongamma= 1.7f;
378
379         sce->r.border.xmin= 0.0f;
380         sce->r.border.ymin= 0.0f;
381         sce->r.border.xmax= 1.0f;
382         sce->r.border.ymax= 1.0f;
383         
384         sce->toolsettings = MEM_callocN(sizeof(struct ToolSettings),"Tool Settings Struct");
385         sce->toolsettings->cornertype=1;
386         sce->toolsettings->degr = 90; 
387         sce->toolsettings->step = 9;
388         sce->toolsettings->turn = 1;                            
389         sce->toolsettings->extr_offs = 1; 
390         sce->toolsettings->doublimit = 0.001;
391         sce->toolsettings->segments = 32;
392         sce->toolsettings->rings = 32;
393         sce->toolsettings->vertices = 32;
394         sce->toolsettings->editbutflag = 1;
395         sce->toolsettings->uvcalc_radius = 1.0f;
396         sce->toolsettings->uvcalc_cubesize = 1.0f;
397         sce->toolsettings->uvcalc_mapdir = 1;
398         sce->toolsettings->uvcalc_mapalign = 1;
399         sce->toolsettings->unwrapper = 1;
400         sce->toolsettings->select_thresh= 0.01f;
401         sce->toolsettings->jointrilimit = 0.8f;
402
403         sce->toolsettings->selectmode= SCE_SELECT_VERTEX;
404         sce->toolsettings->normalsize= 0.1;
405         sce->toolsettings->autokey_mode= U.autokey_mode;
406
407         sce->toolsettings->skgen_resolution = 100;
408         sce->toolsettings->skgen_threshold_internal     = 0.01f;
409         sce->toolsettings->skgen_threshold_external     = 0.01f;
410         sce->toolsettings->skgen_angle_limit                    = 45.0f;
411         sce->toolsettings->skgen_length_ratio                   = 1.3f;
412         sce->toolsettings->skgen_length_limit                   = 1.5f;
413         sce->toolsettings->skgen_correlation_limit              = 0.98f;
414         sce->toolsettings->skgen_symmetry_limit                 = 0.1f;
415         sce->toolsettings->skgen_postpro = SKGEN_SMOOTH;
416         sce->toolsettings->skgen_postpro_passes = 1;
417         sce->toolsettings->skgen_options = SKGEN_FILTER_INTERNAL|SKGEN_FILTER_EXTERNAL|SKGEN_FILTER_SMART|SKGEN_HARMONIC|SKGEN_SUB_CORRELATION|SKGEN_STICK_TO_EMBEDDING;
418         sce->toolsettings->skgen_subdivisions[0] = SKGEN_SUB_CORRELATION;
419         sce->toolsettings->skgen_subdivisions[1] = SKGEN_SUB_LENGTH;
420         sce->toolsettings->skgen_subdivisions[2] = SKGEN_SUB_ANGLE;
421
422         sce->toolsettings->proportional_size = 1.0f;
423
424         sce->physics_settings.gravity[0] = 0.0f;
425         sce->physics_settings.gravity[1] = 0.0f;
426         sce->physics_settings.gravity[2] = -9.81f;
427         sce->physics_settings.flag = PHYS_GLOBAL_GRAVITY;
428
429         sce->unit.scale_length = 1.0f;
430
431         pset= &sce->toolsettings->particle;
432         pset->flag= PE_KEEP_LENGTHS|PE_LOCK_FIRST|PE_DEFLECT_EMITTER|PE_AUTO_VELOCITY;
433         pset->emitterdist= 0.25f;
434         pset->totrekey= 5;
435         pset->totaddkey= 5;
436         pset->brushtype= PE_BRUSH_NONE;
437         pset->draw_step= 2;
438         pset->fade_frames= 2;
439         pset->selectmode= SCE_SELECT_PATH;
440         for(a=0; a<PE_TOT_BRUSH; a++) {
441                 pset->brush[a].strength= 50;
442                 pset->brush[a].size= 50;
443                 pset->brush[a].step= 10;
444         }
445         pset->brush[PE_BRUSH_CUT].strength= 100;
446         
447         sce->jumpframe = 10;
448         sce->r.ffcodecdata.audio_mixrate = 44100;
449
450         sce->audio.distance_model = 2.0;
451         sce->audio.doppler_factor = 1.0;
452         sce->audio.speed_of_sound = 343.3;
453
454         strcpy(sce->r.backbuf, "//backbuf");
455         strcpy(sce->r.pic, U.renderdir);
456
457         BLI_init_rctf(&sce->r.safety, 0.1f, 0.9f, 0.1f, 0.9f);
458         sce->r.osa= 8;
459
460         /* note; in header_info.c the scene copy happens..., if you add more to renderdata it has to be checked there */
461         scene_add_render_layer(sce);
462         
463         /* game data */
464         sce->gm.stereoflag = STEREO_NOSTEREO;
465         sce->gm.stereomode = STEREO_ANAGLYPH;
466         sce->gm.eyeseparation = 0.10;
467
468         sce->gm.dome.angle = 180;
469         sce->gm.dome.mode = DOME_FISHEYE;
470         sce->gm.dome.res = 4;
471         sce->gm.dome.resbuf = 1.0f;
472         sce->gm.dome.tilt = 0;
473
474         sce->gm.xplay= 800;
475         sce->gm.yplay= 600;
476         sce->gm.freqplay= 60;
477         sce->gm.depth= 32;
478
479         sce->gm.gravity= 9.8f;
480         sce->gm.physicsEngine= WOPHY_BULLET;
481         sce->gm.mode = 32; //XXX ugly harcoding, still not sure we should drop mode. 32 == 1 << 5 == use_occlusion_culling 
482         sce->gm.occlusionRes = 128;
483         sce->gm.ticrate = 60;
484         sce->gm.maxlogicstep = 5;
485         sce->gm.physubstep = 1;
486         sce->gm.maxphystep = 5;
487
488         sce->gm.flag = GAME_DISPLAY_LISTS;
489         sce->gm.matmode = GAME_MAT_MULTITEX;
490
491         sound_create_scene(sce);
492
493         return sce;
494 }
495
496 Base *object_in_scene(Object *ob, Scene *sce)
497 {
498         Base *base;
499         
500         base= sce->base.first;
501         while(base) {
502                 if(base->object == ob) return base;
503                 base= base->next;
504         }
505         return NULL;
506 }
507
508 void set_scene_bg(Scene *scene)
509 {
510         Scene *sce;
511         Base *base;
512         Object *ob;
513         Group *group;
514         GroupObject *go;
515         int flag;
516         
517         /* check for cyclic sets, for reading old files but also for definite security (py?) */
518         scene_check_setscene(scene);
519         
520         /* deselect objects (for dataselect) */
521         for(ob= G.main->object.first; ob; ob= ob->id.next)
522                 ob->flag &= ~(SELECT|OB_FROMGROUP);
523
524         /* group flags again */
525         for(group= G.main->group.first; group; group= group->id.next) {
526                 go= group->gobject.first;
527                 while(go) {
528                         if(go->ob) go->ob->flag |= OB_FROMGROUP;
529                         go= go->next;
530                 }
531         }
532
533         /* sort baselist */
534         DAG_scene_sort(scene);
535         
536         /* ensure dags are built for sets */
537         for(sce= scene->set; sce; sce= sce->set)
538                 if(sce->theDag==NULL)
539                         DAG_scene_sort(sce);
540
541         /* copy layers and flags from bases to objects */
542         for(base= scene->base.first; base; base= base->next) {
543                 ob= base->object;
544                 ob->lay= base->lay;
545                 
546                 /* group patch... */
547                 base->flag &= ~(OB_FROMGROUP);
548                 flag= ob->flag & (OB_FROMGROUP);
549                 base->flag |= flag;
550                 
551                 /* not too nice... for recovering objects with lost data */
552                 //if(ob->pose==NULL) base->flag &= ~OB_POSEMODE;
553                 ob->flag= base->flag;
554                 
555                 ob->ctime= -1234567.0;  /* force ipo to be calculated later */
556         }
557         /* no full animation update, this to enable render code to work (render code calls own animation updates) */
558 }
559
560 /* called from creator.c */
561 void set_scene_name(char *name)
562 {
563         Scene *sce;
564
565         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
566                 if (BLI_streq(name, sce->id.name+2)) {
567                         set_scene_bg(sce);
568                         return;
569                 }
570         }
571         
572         //XXX error("Can't find scene: %s", name);
573 }
574
575 void unlink_scene(Main *bmain, Scene *sce, Scene *newsce)
576 {
577         Scene *sce1;
578         bScreen *sc;
579
580         /* check all sets */
581         for(sce1= bmain->scene.first; sce1; sce1= sce1->id.next)
582                 if(sce1->set == sce)
583                         sce1->set= NULL;
584         
585         /* check all sequences */
586         clear_scene_in_allseqs(sce);
587
588         /* check render layer nodes in other scenes */
589         clear_scene_in_nodes(bmain, sce);
590         
591         /* al screens */
592         for(sc= bmain->screen.first; sc; sc= sc->id.next)
593                 if(sc->scene == sce)
594                         sc->scene= newsce;
595
596         free_libblock(&bmain->scene, sce);
597 }
598
599 /* used by metaballs
600  * doesnt return the original duplicated object, only dupli's
601  */
602 int next_object(Scene *scene, int val, Base **base, Object **ob)
603 {
604         static ListBase *duplilist= NULL;
605         static DupliObject *dupob;
606         static int fase= F_START, in_next_object= 0;
607         int run_again=1;
608         
609         /* init */
610         if(val==0) {
611                 fase= F_START;
612                 dupob= NULL;
613                 
614                 /* XXX particle systems with metas+dupligroups call this recursively */
615                 /* see bug #18725 */
616                 if(in_next_object) {
617                         printf("ERROR: MetaBall generation called recursively, not supported\n");
618                         
619                         return F_ERROR;
620                 }
621         }
622         else {
623                 in_next_object= 1;
624                 
625                 /* run_again is set when a duplilist has been ended */
626                 while(run_again) {
627                         run_again= 0;
628
629                         /* the first base */
630                         if(fase==F_START) {
631                                 *base= scene->base.first;
632                                 if(*base) {
633                                         *ob= (*base)->object;
634                                         fase= F_SCENE;
635                                 }
636                                 else {
637                                     /* exception: empty scene */
638                                         if(scene->set && scene->set->base.first) {
639                                                 *base= scene->set->base.first;
640                                                 *ob= (*base)->object;
641                                                 fase= F_SET;
642                                         }
643                                 }
644                         }
645                         else {
646                                 if(*base && fase!=F_DUPLI) {
647                                         *base= (*base)->next;
648                                         if(*base) *ob= (*base)->object;
649                                         else {
650                                                 if(fase==F_SCENE) {
651                                                         /* scene is finished, now do the set */
652                                                         if(scene->set && scene->set->base.first) {
653                                                                 *base= scene->set->base.first;
654                                                                 *ob= (*base)->object;
655                                                                 fase= F_SET;
656                                                         }
657                                                 }
658                                         }
659                                 }
660                         }
661                         
662                         if(*base == NULL) fase= F_START;
663                         else {
664                                 if(fase!=F_DUPLI) {
665                                         if( (*base)->object->transflag & OB_DUPLI) {
666                                                 /* groups cannot be duplicated for mballs yet, 
667                                                 this enters eternal loop because of 
668                                                 makeDispListMBall getting called inside of group_duplilist */
669                                                 if((*base)->object->dup_group == NULL) {
670                                                         duplilist= object_duplilist(scene, (*base)->object);
671                                                         
672                                                         dupob= duplilist->first;
673
674                                                         if(!dupob)
675                                                                 free_object_duplilist(duplilist);
676                                                 }
677                                         }
678                                 }
679                                 /* handle dupli's */
680                                 if(dupob) {
681                                         
682                                         copy_m4_m4(dupob->ob->obmat, dupob->mat);
683                                         
684                                         (*base)->flag |= OB_FROMDUPLI;
685                                         *ob= dupob->ob;
686                                         fase= F_DUPLI;
687                                         
688                                         dupob= dupob->next;
689                                 }
690                                 else if(fase==F_DUPLI) {
691                                         fase= F_SCENE;
692                                         (*base)->flag &= ~OB_FROMDUPLI;
693                                         
694                                         for(dupob= duplilist->first; dupob; dupob= dupob->next) {
695                                                 copy_m4_m4(dupob->ob->obmat, dupob->omat);
696                                         }
697                                         
698                                         free_object_duplilist(duplilist);
699                                         duplilist= NULL;
700                                         run_again= 1;
701                                 }
702                         }
703                 }
704         }
705         
706         /* reset recursion test */
707         in_next_object= 0;
708         
709         return fase;
710 }
711
712 Object *scene_find_camera(Scene *sc)
713 {
714         Base *base;
715         
716         for (base= sc->base.first; base; base= base->next)
717                 if (base->object->type==OB_CAMERA)
718                         return base->object;
719
720         return NULL;
721 }
722
723 #ifdef DURIAN_CAMERA_SWITCH
724 Object *scene_find_camera_switch(Scene *scene)
725 {
726         TimeMarker *m;
727         int cfra = scene->r.cfra;
728         int frame = -(MAXFRAME + 1);
729         Object *camera= NULL;
730
731         for (m= scene->markers.first; m; m= m->next) {
732                 if(m->camera && (m->camera->restrictflag & OB_RESTRICT_RENDER)==0 && (m->frame <= cfra) && (m->frame > frame)) {
733                         camera= m->camera;
734                         frame= m->frame;
735
736                         if(frame == cfra)
737                                 break;
738
739                 }
740         }
741         return camera;
742 }
743 #endif
744
745 char *scene_find_marker_name(Scene *scene, int frame)
746 {
747         ListBase *markers= &scene->markers;
748         TimeMarker *m1, *m2;
749
750         /* search through markers for match */
751         for (m1=markers->first, m2=markers->last; m1 && m2; m1=m1->next, m2=m2->prev) {
752                 if (m1->frame==frame)
753                         return m1->name;
754
755                 if (m1 == m2)
756                         break;
757
758                 if (m2->frame==frame)
759                         return m2->name;
760         }
761
762         return NULL;
763 }
764
765 /* return the current marker for this frame,
766 we can have more then 1 marker per frame, this just returns the first :/ */
767 char *scene_find_last_marker_name(Scene *scene, int frame)
768 {
769         TimeMarker *marker, *best_marker = NULL;
770         int best_frame = -MAXFRAME*2;
771         for (marker= scene->markers.first; marker; marker= marker->next) {
772                 if (marker->frame==frame) {
773                         return marker->name;
774                 }
775
776                 if ( marker->frame > best_frame && marker->frame < frame) {
777                         best_marker = marker;
778                         best_frame = marker->frame;
779                 }
780         }
781
782         return best_marker ? best_marker->name : NULL;
783 }
784
785
786 Base *scene_add_base(Scene *sce, Object *ob)
787 {
788         Base *b= MEM_callocN(sizeof(*b), "scene_add_base");
789         BLI_addhead(&sce->base, b);
790
791         b->object= ob;
792         b->flag= ob->flag;
793         b->lay= ob->lay;
794
795         return b;
796 }
797
798 void scene_deselect_all(Scene *sce)
799 {
800         Base *b;
801
802         for (b= sce->base.first; b; b= b->next) {
803                 b->flag&= ~SELECT;
804                 b->object->flag= b->flag;
805         }
806 }
807
808 void scene_select_base(Scene *sce, Base *selbase)
809 {
810         scene_deselect_all(sce);
811
812         selbase->flag |= SELECT;
813         selbase->object->flag= selbase->flag;
814
815         sce->basact= selbase;
816 }
817
818 /* checks for cycle, returns 1 if it's all OK */
819 int scene_check_setscene(Scene *sce)
820 {
821         Scene *scene;
822         int a, totscene;
823         
824         if(sce->set==NULL) return 1;
825         
826         totscene= 0;
827         for(scene= G.main->scene.first; scene; scene= scene->id.next)
828                 totscene++;
829         
830         for(a=0, scene=sce; scene->set; scene=scene->set, a++) {
831                 /* more iterations than scenes means we have a cycle */
832                 if(a > totscene) {
833                         /* the tested scene gets zero'ed, that's typically current scene */
834                         sce->set= NULL;
835                         return 0;
836                 }
837         }
838
839         return 1;
840 }
841
842 /* This (evil) function is needed to cope with two legacy Blender rendering features
843 * mblur (motion blur that renders 'subframes' and blurs them together), and fields 
844 * rendering. Thus, the use of ugly globals from object.c
845 */
846 // BAD... EVIL... JUJU...!!!!
847 // XXX moved here temporarily
848 float frame_to_float (Scene *scene, int cfra)           /* see also bsystem_time in object.c */
849 {
850         extern float bluroffs;  /* bad stuff borrowed from object.c */
851         extern float fieldoffs;
852         float ctime;
853         
854         ctime= (float)cfra;
855         ctime+= bluroffs+fieldoffs;
856         ctime*= scene->r.framelen;
857         
858         return ctime;
859 }
860
861 static void scene_update_newframe(Scene *sce, unsigned int lay)
862 {
863         Base *base;
864         Object *ob;
865         float ctime = frame_to_float(sce, sce->r.cfra); 
866         
867         if(sce->theDag==NULL)
868                 DAG_scene_sort(sce);
869         
870         DAG_scene_update_flags(sce, lay);   // only stuff that moves or needs display still
871         
872         /* All 'standard' (i.e. without any dependencies) animation is handled here,
873          * with an 'local' to 'macro' order of evaluation. This should ensure that
874          * settings stored nestled within a hierarchy (i.e. settings in a Texture block
875          * can be overridden by settings from Scene, which owns the Texture through a hierarchy 
876          * such as Scene->World->MTex/Texture) can still get correctly overridden.
877          */
878         BKE_animsys_evaluate_all_animation(G.main, ctime);
879         
880         for(base= sce->base.first; base; base= base->next) {
881                 ob= base->object;
882                 
883                 object_handle_update(sce, ob);   // bke_object.h
884                 
885                 /* only update layer when an ipo */
886                         // XXX old animation system
887                 //if(ob->ipo && has_ipo_code(ob->ipo, OB_LAY) ) {
888                 //      base->lay= ob->lay;
889                 //}
890         }
891 }
892
893 /* this is called in main loop, doing tagged updates before redraw */
894 void scene_update_tagged(Scene *scene)
895 {
896         Scene *sce;
897         Base *base;
898         float ctime = frame_to_float(scene, scene->r.cfra); 
899
900         /* update all objects: drivers, matrices, displists, etc. flags set
901            by depgraph or manual, no layer check here, gets correct flushed */
902
903         /* sets first, we allow per definition current scene to have
904            dependencies on sets, but not the other way around. */
905         if(scene->set) {
906                 for(SETLOOPER(scene->set, base))
907                         object_handle_update(scene, base->object);
908         }
909         
910         for(base= scene->base.first; base; base= base->next) {
911                 object_handle_update(scene, base->object);
912         }
913
914         /* recalc scene animation data here (for sequencer) */
915         {
916                 AnimData *adt= BKE_animdata_from_id(&scene->id);
917
918                 if(adt && (adt->recalc & ADT_RECALC_ANIM))
919                         BKE_animsys_evaluate_animdata(&scene->id, adt, ctime, 0);
920         }
921
922         BKE_ptcache_quick_cache_all(scene);
923
924         /* in the future this should handle updates for all datablocks, not
925            only objects and scenes. - brecht */
926 }
927
928 /* applies changes right away, does all sets too */
929 void scene_update_for_newframe(Scene *sce, unsigned int lay)
930 {
931         Scene *scene= sce;
932         
933         /* clear animation overrides */
934         // XXX TODO...
935         
936         /* sets first, we allow per definition current scene to have dependencies on sets */
937         for(sce= sce->set; sce; sce= sce->set)
938                 scene_update_newframe(sce, lay);
939
940         scene_update_newframe(scene, lay);
941 }
942
943 /* return default layer, also used to patch old files */
944 void scene_add_render_layer(Scene *sce)
945 {
946         SceneRenderLayer *srl;
947 //      int tot= 1 + BLI_countlist(&sce->r.layers);
948         
949         srl= MEM_callocN(sizeof(SceneRenderLayer), "new render layer");
950         sprintf(srl->name, "RenderLayer");
951         BLI_uniquename(&sce->r.layers, srl, "RenderLayer", '.', offsetof(SceneRenderLayer, name), 32);
952         BLI_addtail(&sce->r.layers, srl);
953
954         /* note, this is also in render, pipeline.c, to make layer when scenedata doesnt have it */
955         srl->lay= (1<<20) -1;
956         srl->layflag= 0x7FFF;   /* solid ztra halo edge strand */
957         srl->passflag= SCE_PASS_COMBINED|SCE_PASS_Z;
958 }
959
960 /* render simplification */
961
962 int get_render_subsurf_level(RenderData *r, int lvl)
963 {
964         if(r->mode & R_SIMPLIFY)
965                 return MIN2(r->simplify_subsurf, lvl);
966         else
967                 return lvl;
968 }
969
970 int get_render_child_particle_number(RenderData *r, int num)
971 {
972         if(r->mode & R_SIMPLIFY)
973                 return (int)(r->simplify_particles*num);
974         else
975                 return num;
976 }
977
978 int get_render_shadow_samples(RenderData *r, int samples)
979 {
980         if((r->mode & R_SIMPLIFY) && samples > 0)
981                 return MIN2(r->simplify_shadowsamples, samples);
982         else
983                 return samples;
984 }
985
986 float get_render_aosss_error(RenderData *r, float error)
987 {
988         if(r->mode & R_SIMPLIFY)
989                 return ((1.0f-r->simplify_aosss)*10.0f + 1.0f)*error;
990         else
991                 return error;
992 }
993