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