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