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