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