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