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