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