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