Merged changes in the trunk up to revision 44288.
[blender-staging.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 #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         SceneRenderLayer *srl;
246         
247         base= sce->base.first;
248         while(base) {
249                 base->object->id.us--;
250                 base= base->next;
251         }
252         /* do not free objects! */
253         
254         if(sce->gpd) {
255 #if 0   // removed since this can be invalid memory when freeing everything
256                 // since the grease pencil data is free'd before the scene.
257                 // since grease pencil data is not (yet?), shared between objects
258                 // its probably safe not to do this, some save and reload will free this.
259                 sce->gpd->id.us--;
260 #endif
261                 sce->gpd= NULL;
262         }
263
264         BLI_freelistN(&sce->base);
265         seq_free_editing(sce);
266
267         BKE_free_animdata((ID *)sce);
268         BKE_keyingsets_free(&sce->keyingsets);
269         
270         if (sce->r.avicodecdata) {
271                 free_avicodecdata(sce->r.avicodecdata);
272                 MEM_freeN(sce->r.avicodecdata);
273                 sce->r.avicodecdata = NULL;
274         }
275         if (sce->r.qtcodecdata) {
276                 free_qtcodecdata(sce->r.qtcodecdata);
277                 MEM_freeN(sce->r.qtcodecdata);
278                 sce->r.qtcodecdata = NULL;
279         }
280         if (sce->r.ffcodecdata.properties) {
281                 IDP_FreeProperty(sce->r.ffcodecdata.properties);
282                 MEM_freeN(sce->r.ffcodecdata.properties);
283                 sce->r.ffcodecdata.properties = NULL;
284         }
285         
286         for(srl= sce->r.layers.first; srl; srl= srl->next) {
287                 FRS_free_freestyle_config(srl);
288         }
289         
290         BLI_freelistN(&sce->markers);
291         BLI_freelistN(&sce->transform_spaces);
292         BLI_freelistN(&sce->r.layers);
293         
294         if(sce->toolsettings) {
295                 if(sce->toolsettings->vpaint) {
296                         free_paint(&sce->toolsettings->vpaint->paint);
297                         MEM_freeN(sce->toolsettings->vpaint);
298                 }
299                 if(sce->toolsettings->wpaint) {
300                         free_paint(&sce->toolsettings->wpaint->paint);
301                         MEM_freeN(sce->toolsettings->wpaint);
302                 }
303                 if(sce->toolsettings->sculpt) {
304                         free_paint(&sce->toolsettings->sculpt->paint);
305                         MEM_freeN(sce->toolsettings->sculpt);
306                 }
307                 if(sce->toolsettings->uvsculpt) {
308                         free_paint(&sce->toolsettings->uvsculpt->paint);
309                         MEM_freeN(sce->toolsettings->uvsculpt);
310                 }
311                 free_paint(&sce->toolsettings->imapaint.paint);
312
313                 MEM_freeN(sce->toolsettings);
314                 sce->toolsettings = NULL;       
315         }
316         
317         if (sce->theDag) {
318                 free_forest(sce->theDag);
319                 MEM_freeN(sce->theDag);
320         }
321         
322         if(sce->nodetree) {
323                 ntreeFreeTree(sce->nodetree);
324                 MEM_freeN(sce->nodetree);
325         }
326
327         if(sce->stats)
328                 MEM_freeN(sce->stats);
329         if(sce->fps_info)
330                 MEM_freeN(sce->fps_info);
331
332         sound_destroy_scene(sce);
333 }
334
335 Scene *add_scene(const char *name)
336 {
337         Main *bmain= G.main;
338         Scene *sce;
339         ParticleEditSettings *pset;
340         int a;
341
342         sce= alloc_libblock(&bmain->scene, ID_SCE, name);
343         sce->lay= sce->layact= 1;
344         
345         sce->r.mode= R_GAMMA|R_OSA|R_SHADOW|R_SSS|R_ENVMAP|R_RAYTRACE;
346         sce->r.cfra= 1;
347         sce->r.sfra= 1;
348         sce->r.efra= 250;
349         sce->r.frame_step= 1;
350         sce->r.xsch= 1920;
351         sce->r.ysch= 1080;
352         sce->r.xasp= 1;
353         sce->r.yasp= 1;
354         sce->r.xparts= 8;
355         sce->r.yparts= 8;
356         sce->r.mblur_samples= 1;
357         sce->r.filtertype= R_FILTER_MITCH;
358         sce->r.size= 50;
359
360         sce->r.im_format.planes= R_IMF_PLANES_RGB;
361         sce->r.im_format.imtype= R_IMF_IMTYPE_PNG;
362         sce->r.im_format.quality= 90;
363
364         sce->r.displaymode= R_OUTPUT_AREA;
365         sce->r.framapto= 100;
366         sce->r.images= 100;
367         sce->r.framelen= 1.0;
368         sce->r.blurfac= 0.5;
369         sce->r.frs_sec= 24;
370         sce->r.frs_sec_base= 1;
371         sce->r.edgeint= 10;
372         sce->r.ocres = 128;
373         sce->r.color_mgt_flag |= R_COLOR_MANAGEMENT;
374         sce->r.gauss= 1.0;
375         
376         /* deprecated but keep for upwards compat */
377         sce->r.postgamma= 1.0;
378         sce->r.posthue= 0.0;
379         sce->r.postsat= 1.0;
380         
381         sce->r.bake_mode= 1;    /* prevent to include render stuff here */
382         sce->r.bake_filter= 2;
383         sce->r.bake_osa= 5;
384         sce->r.bake_flag= R_BAKE_CLEAR;
385         sce->r.bake_normal_space= R_BAKE_SPACE_TANGENT;
386         sce->r.scemode= R_DOCOMP|R_DOSEQ|R_EXTENSION;
387         sce->r.stamp= R_STAMP_TIME|R_STAMP_FRAME|R_STAMP_DATE|R_STAMP_CAMERA|R_STAMP_SCENE|R_STAMP_FILENAME|R_STAMP_RENDERTIME;
388         sce->r.stamp_font_id= 12;
389         sce->r.fg_stamp[0]= sce->r.fg_stamp[1]= sce->r.fg_stamp[2]= 0.8f;
390         sce->r.fg_stamp[3]= 1.0f;
391         sce->r.bg_stamp[0]= sce->r.bg_stamp[1]= sce->r.bg_stamp[2]= 0.0f;
392         sce->r.bg_stamp[3]= 0.25f;
393         sce->r.raytrace_options = R_RAYTRACE_USE_INSTANCES;
394
395         sce->r.seq_prev_type= OB_SOLID;
396         sce->r.seq_rend_type= OB_SOLID;
397         sce->r.seq_flag= R_SEQ_GL_PREV;
398
399         sce->r.threads= 1;
400
401         sce->r.simplify_subsurf= 6;
402         sce->r.simplify_particles= 1.0f;
403         sce->r.simplify_shadowsamples= 16;
404         sce->r.simplify_aosss= 1.0f;
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         sce->gm.exitkey = 218; // Blender key code for ESC
542
543         sound_create_scene(sce);
544
545         return sce;
546 }
547
548 Base *object_in_scene(Object *ob, Scene *sce)
549 {
550         Base *base;
551         
552         base= sce->base.first;
553         while(base) {
554                 if(base->object == ob) return base;
555                 base= base->next;
556         }
557         return NULL;
558 }
559
560 void set_scene_bg(Main *bmain, Scene *scene)
561 {
562         Scene *sce;
563         Base *base;
564         Object *ob;
565         Group *group;
566         GroupObject *go;
567         int flag;
568         
569         /* check for cyclic sets, for reading old files but also for definite security (py?) */
570         scene_check_setscene(bmain, scene);
571         
572         /* can happen when switching modes in other scenes */
573         if(scene->obedit && !(scene->obedit->mode & OB_MODE_EDIT))
574                 scene->obedit= NULL;
575
576         /* deselect objects (for dataselect) */
577         for(ob= bmain->object.first; ob; ob= ob->id.next)
578                 ob->flag &= ~(SELECT|OB_FROMGROUP);
579
580         /* group flags again */
581         for(group= bmain->group.first; group; group= group->id.next) {
582                 go= group->gobject.first;
583                 while(go) {
584                         if(go->ob) go->ob->flag |= OB_FROMGROUP;
585                         go= go->next;
586                 }
587         }
588
589         /* sort baselist */
590         DAG_scene_sort(bmain, scene);
591         
592         /* ensure dags are built for sets */
593         for(sce= scene->set; sce; sce= sce->set)
594                 if(sce->theDag==NULL)
595                         DAG_scene_sort(bmain, sce);
596
597         /* copy layers and flags from bases to objects */
598         for(base= scene->base.first; base; base= base->next) {
599                 ob= base->object;
600                 ob->lay= base->lay;
601                 
602                 /* group patch... */
603                 base->flag &= ~(OB_FROMGROUP);
604                 flag= ob->flag & (OB_FROMGROUP);
605                 base->flag |= flag;
606                 
607                 /* not too nice... for recovering objects with lost data */
608                 //if(ob->pose==NULL) base->flag &= ~OB_POSEMODE;
609                 ob->flag= base->flag;
610                 
611                 ob->ctime= -1234567.0;  /* force ipo to be calculated later */
612         }
613         /* no full animation update, this to enable render code to work (render code calls own animation updates) */
614 }
615
616 /* called from creator.c */
617 Scene *set_scene_name(Main *bmain, const char *name)
618 {
619         Scene *sce= (Scene *)find_id("SC", name);
620         if(sce) {
621                 set_scene_bg(bmain, sce);
622                 printf("Scene switch: '%s' in file: '%s'\n", name, G.main->name);
623                 return sce;
624         }
625
626         printf("Can't find scene: '%s' in file: '%s'\n", name, G.main->name);
627         return NULL;
628 }
629
630 void unlink_scene(Main *bmain, Scene *sce, Scene *newsce)
631 {
632         Scene *sce1;
633         bScreen *sc;
634
635         /* check all sets */
636         for(sce1= bmain->scene.first; sce1; sce1= sce1->id.next)
637                 if(sce1->set == sce)
638                         sce1->set= NULL;
639         
640         /* check all sequences */
641         clear_scene_in_allseqs(bmain, sce);
642
643         /* check render layer nodes in other scenes */
644         clear_scene_in_nodes(bmain, sce);
645         
646         /* al screens */
647         for(sc= bmain->screen.first; sc; sc= sc->id.next)
648                 if(sc->scene == sce)
649                         sc->scene= newsce;
650
651         free_libblock(&bmain->scene, sce);
652 }
653
654 /* used by metaballs
655  * doesnt return the original duplicated object, only dupli's
656  */
657 int next_object(Scene **scene, int val, Base **base, Object **ob)
658 {
659         static ListBase *duplilist= NULL;
660         static DupliObject *dupob;
661         static int fase= F_START, in_next_object= 0;
662         int run_again=1;
663         
664         /* init */
665         if(val==0) {
666                 fase= F_START;
667                 dupob= NULL;
668                 
669                 /* XXX particle systems with metas+dupligroups call this recursively */
670                 /* see bug #18725 */
671                 if(in_next_object) {
672                         printf("ERROR: MetaBall generation called recursively, not supported\n");
673                         
674                         return F_ERROR;
675                 }
676         }
677         else {
678                 in_next_object= 1;
679                 
680                 /* run_again is set when a duplilist has been ended */
681                 while(run_again) {
682                         run_again= 0;
683
684                         /* the first base */
685                         if(fase==F_START) {
686                                 *base= (*scene)->base.first;
687                                 if(*base) {
688                                         *ob= (*base)->object;
689                                         fase= F_SCENE;
690                                 }
691                                 else {
692                                         /* exception: empty scene */
693                                         while((*scene)->set) {
694                                                 (*scene)= (*scene)->set;
695                                                 if((*scene)->base.first) {
696                                                         *base= (*scene)->base.first;
697                                                         *ob= (*base)->object;
698                                                         fase= F_SCENE;
699                                                         break;
700                                                 }
701                                         }
702                                 }
703                         }
704                         else {
705                                 if(*base && fase!=F_DUPLI) {
706                                         *base= (*base)->next;
707                                         if(*base) *ob= (*base)->object;
708                                         else {
709                                                 if(fase==F_SCENE) {
710                                                         /* (*scene) is finished, now do the set */
711                                                         while((*scene)->set) {
712                                                                 (*scene)= (*scene)->set;
713                                                                 if((*scene)->base.first) {
714                                                                         *base= (*scene)->base.first;
715                                                                         *ob= (*base)->object;
716                                                                         break;
717                                                                 }
718                                                         }
719                                                 }
720                                         }
721                                 }
722                         }
723                         
724                         if(*base == NULL) fase= F_START;
725                         else {
726                                 if(fase!=F_DUPLI) {
727                                         if( (*base)->object->transflag & OB_DUPLI) {
728                                                 /* groups cannot be duplicated for mballs yet, 
729                                                 this enters eternal loop because of 
730                                                 makeDispListMBall getting called inside of group_duplilist */
731                                                 if((*base)->object->dup_group == NULL) {
732                                                         duplilist= object_duplilist((*scene), (*base)->object);
733                                                         
734                                                         dupob= duplilist->first;
735
736                                                         if(!dupob)
737                                                                 free_object_duplilist(duplilist);
738                                                 }
739                                         }
740                                 }
741                                 /* handle dupli's */
742                                 if(dupob) {
743                                         
744                                         copy_m4_m4(dupob->ob->obmat, dupob->mat);
745                                         
746                                         (*base)->flag |= OB_FROMDUPLI;
747                                         *ob= dupob->ob;
748                                         fase= F_DUPLI;
749                                         
750                                         dupob= dupob->next;
751                                 }
752                                 else if(fase==F_DUPLI) {
753                                         fase= F_SCENE;
754                                         (*base)->flag &= ~OB_FROMDUPLI;
755                                         
756                                         for(dupob= duplilist->first; dupob; dupob= dupob->next) {
757                                                 copy_m4_m4(dupob->ob->obmat, dupob->omat);
758                                         }
759                                         
760                                         free_object_duplilist(duplilist);
761                                         duplilist= NULL;
762                                         run_again= 1;
763                                 }
764                         }
765                 }
766         }
767         
768         /* if(ob && *ob) {
769                 printf("Scene: '%s', '%s'\n", (*scene)->id.name+2, (*ob)->id.name+2);
770         } */
771
772         /* reset recursion test */
773         in_next_object= 0;
774         
775         return fase;
776 }
777
778 Object *scene_find_camera(Scene *sc)
779 {
780         Base *base;
781         
782         for (base= sc->base.first; base; base= base->next)
783                 if (base->object->type==OB_CAMERA)
784                         return base->object;
785
786         return NULL;
787 }
788
789 #ifdef DURIAN_CAMERA_SWITCH
790 Object *scene_camera_switch_find(Scene *scene)
791 {
792         TimeMarker *m;
793         int cfra = scene->r.cfra;
794         int frame = -(MAXFRAME + 1);
795         Object *camera= NULL;
796
797         for (m= scene->markers.first; m; m= m->next) {
798                 if(m->camera && (m->camera->restrictflag & OB_RESTRICT_RENDER)==0 && (m->frame <= cfra) && (m->frame > frame)) {
799                         camera= m->camera;
800                         frame= m->frame;
801
802                         if(frame == cfra)
803                                 break;
804
805                 }
806         }
807         return camera;
808 }
809 #endif
810
811 int scene_camera_switch_update(Scene *scene)
812 {
813 #ifdef DURIAN_CAMERA_SWITCH
814         Object *camera= scene_camera_switch_find(scene);
815         if(camera) {
816                 scene->camera= camera;
817                 return 1;
818         }
819 #else
820         (void)scene;
821 #endif
822         return 0;
823 }
824
825 char *scene_find_marker_name(Scene *scene, int frame)
826 {
827         ListBase *markers= &scene->markers;
828         TimeMarker *m1, *m2;
829
830         /* search through markers for match */
831         for (m1=markers->first, m2=markers->last; m1 && m2; m1=m1->next, m2=m2->prev) {
832                 if (m1->frame==frame)
833                         return m1->name;
834
835                 if (m1 == m2)
836                         break;
837
838                 if (m2->frame==frame)
839                         return m2->name;
840         }
841
842         return NULL;
843 }
844
845 /* return the current marker for this frame,
846 we can have more then 1 marker per frame, this just returns the first :/ */
847 char *scene_find_last_marker_name(Scene *scene, int frame)
848 {
849         TimeMarker *marker, *best_marker = NULL;
850         int best_frame = -MAXFRAME*2;
851         for (marker= scene->markers.first; marker; marker= marker->next) {
852                 if (marker->frame==frame) {
853                         return marker->name;
854                 }
855
856                 if ( marker->frame > best_frame && marker->frame < frame) {
857                         best_marker = marker;
858                         best_frame = marker->frame;
859                 }
860         }
861
862         return best_marker ? best_marker->name : NULL;
863 }
864
865
866 Base *scene_add_base(Scene *sce, Object *ob)
867 {
868         Base *b= MEM_callocN(sizeof(*b), "scene_add_base");
869         BLI_addhead(&sce->base, b);
870
871         b->object= ob;
872         b->flag= ob->flag;
873         b->lay= ob->lay;
874
875         return b;
876 }
877
878 void scene_deselect_all(Scene *sce)
879 {
880         Base *b;
881
882         for (b= sce->base.first; b; b= b->next) {
883                 b->flag&= ~SELECT;
884                 b->object->flag= b->flag;
885         }
886 }
887
888 void scene_select_base(Scene *sce, Base *selbase)
889 {
890         scene_deselect_all(sce);
891
892         selbase->flag |= SELECT;
893         selbase->object->flag= selbase->flag;
894
895         sce->basact= selbase;
896 }
897
898 /* checks for cycle, returns 1 if it's all OK */
899 int scene_check_setscene(Main *bmain, Scene *sce)
900 {
901         Scene *scene;
902         int a, totscene;
903         
904         if(sce->set==NULL) return 1;
905         
906         totscene= 0;
907         for(scene= bmain->scene.first; scene; scene= scene->id.next)
908                 totscene++;
909         
910         for(a=0, scene=sce; scene->set; scene=scene->set, a++) {
911                 /* more iterations than scenes means we have a cycle */
912                 if(a > totscene) {
913                         /* the tested scene gets zero'ed, that's typically current scene */
914                         sce->set= NULL;
915                         return 0;
916                 }
917         }
918
919         return 1;
920 }
921
922 /* This function is needed to cope with fractional frames - including two Blender rendering features
923  * mblur (motion blur that renders 'subframes' and blurs them together), and fields rendering. 
924  */
925 float BKE_curframe(Scene *scene)
926 {
927         return BKE_frame_to_ctime(scene, scene->r.cfra);
928 }
929
930 /* This function is used to obtain arbitrary fractional frames */
931 float BKE_frame_to_ctime(Scene *scene, const float frame)
932 {
933         float ctime = frame;
934         ctime += scene->r.subframe;
935         ctime *= scene->r.framelen;     
936         
937         return ctime;
938 }
939
940 /* drivers support/hacks 
941  *      - this method is called from scene_update_tagged_recursive(), so gets included in viewport + render
942  *      - these are always run since the depsgraph can't handle non-object data
943  *      - these happen after objects are all done so that we can read in their final transform values,
944  *        though this means that objects can't refer to scene info for guidance...
945  */
946 static void scene_update_drivers(Main *UNUSED(bmain), Scene *scene)
947 {
948         float ctime = BKE_curframe(scene);
949         
950         /* scene itself */
951         if (scene->adt && scene->adt->drivers.first) {
952                 BKE_animsys_evaluate_animdata(scene, &scene->id, scene->adt, ctime, ADT_RECALC_DRIVERS);
953         }
954         
955         /* world */
956         // TODO: what about world textures? but then those have nodes too...
957         if (scene->world) {
958                 ID *wid = (ID *)scene->world;
959                 AnimData *adt= BKE_animdata_from_id(wid);
960                 
961                 if (adt && adt->drivers.first)
962                         BKE_animsys_evaluate_animdata(scene, wid, adt, ctime, ADT_RECALC_DRIVERS);
963         }
964         
965         /* nodes */
966         if (scene->nodetree) {
967                 ID *nid = (ID *)scene->nodetree;
968                 AnimData *adt= BKE_animdata_from_id(nid);
969                 
970                 if (adt && adt->drivers.first)
971                         BKE_animsys_evaluate_animdata(scene, nid, adt, ctime, ADT_RECALC_DRIVERS);
972         }
973 }
974
975 static void scene_update_tagged_recursive(Main *bmain, Scene *scene, Scene *scene_parent)
976 {
977         Base *base;
978         
979         
980         scene->customdata_mask= scene_parent->customdata_mask;
981
982         /* sets first, we allow per definition current scene to have
983            dependencies on sets, but not the other way around. */
984         if (scene->set)
985                 scene_update_tagged_recursive(bmain, scene->set, scene_parent);
986         
987         /* scene objects */
988         for (base= scene->base.first; base; base= base->next) {
989                 Object *ob= base->object;
990                 
991                 object_handle_update(scene_parent, ob);
992                 
993                 if(ob->dup_group && (ob->transflag & OB_DUPLIGROUP))
994                         group_handle_recalc_and_update(scene_parent, ob, ob->dup_group);
995                         
996                 /* always update layer, so that animating layers works */
997                 base->lay= ob->lay;
998         }
999         
1000         /* scene drivers... */
1001         scene_update_drivers(bmain, scene);
1002
1003         /* update sound system animation */
1004         sound_update_scene(scene);
1005 }
1006
1007 /* this is called in main loop, doing tagged updates before redraw */
1008 void scene_update_tagged(Main *bmain, Scene *scene)
1009 {
1010         /* keep this first */
1011         BLI_exec_cb(bmain, &scene->id, BLI_CB_EVT_SCENE_UPDATE_PRE);
1012
1013         /* flush recalc flags to dependencies */
1014         DAG_ids_flush_tagged(bmain);
1015
1016         scene->physics_settings.quick_cache_step= 0;
1017
1018         /* update all objects: drivers, matrices, displists, etc. flags set
1019            by depgraph or manual, no layer check here, gets correct flushed
1020
1021            in the future this should handle updates for all datablocks, not
1022            only objects and scenes. - brecht */
1023         scene_update_tagged_recursive(bmain, scene, scene);
1024
1025         /* extra call here to recalc scene animation (for sequencer) */
1026         {
1027                 AnimData *adt= BKE_animdata_from_id(&scene->id);
1028                 float ctime = BKE_curframe(scene);
1029                 
1030                 if (adt && (adt->recalc & ADT_RECALC_ANIM))
1031                         BKE_animsys_evaluate_animdata(scene, &scene->id, adt, ctime, 0);
1032         }
1033         
1034         /* quick point cache updates */
1035         if (scene->physics_settings.quick_cache_step)
1036                 BKE_ptcache_quick_cache_all(bmain, scene);
1037
1038         /* notify editors and python about recalc */
1039         BLI_exec_cb(bmain, &scene->id, BLI_CB_EVT_SCENE_UPDATE_POST);
1040         DAG_ids_check_recalc(bmain, scene, FALSE);
1041
1042         /* clear recalc flags */
1043         DAG_ids_clear_recalc(bmain);
1044 }
1045
1046 /* applies changes right away, does all sets too */
1047 void scene_update_for_newframe(Main *bmain, Scene *sce, unsigned int lay)
1048 {
1049         float ctime = BKE_curframe(sce);
1050         Scene *sce_iter;
1051
1052         /* keep this first */
1053         BLI_exec_cb(bmain, &sce->id, BLI_CB_EVT_FRAME_CHANGE_PRE);
1054         BLI_exec_cb(bmain, &sce->id, BLI_CB_EVT_SCENE_UPDATE_PRE);
1055
1056         sound_set_cfra(sce->r.cfra);
1057         
1058         /* clear animation overrides */
1059         // XXX TODO...
1060
1061         for(sce_iter= sce; sce_iter; sce_iter= sce_iter->set) {
1062                 if(sce_iter->theDag==NULL)
1063                         DAG_scene_sort(bmain, sce_iter);
1064         }
1065
1066         /* flush recalc flags to dependencies, if we were only changing a frame
1067            this would not be necessary, but if a user or a script has modified
1068            some datablock before scene_update_tagged was called, we need the flush */
1069         DAG_ids_flush_tagged(bmain);
1070
1071         /* Following 2 functions are recursive
1072          * so dont call within 'scene_update_tagged_recursive' */
1073         DAG_scene_update_flags(bmain, sce, lay, TRUE);   // only stuff that moves or needs display still
1074
1075         /* All 'standard' (i.e. without any dependencies) animation is handled here,
1076          * with an 'local' to 'macro' order of evaluation. This should ensure that
1077          * settings stored nestled within a hierarchy (i.e. settings in a Texture block
1078          * can be overridden by settings from Scene, which owns the Texture through a hierarchy
1079          * such as Scene->World->MTex/Texture) can still get correctly overridden.
1080          */
1081         BKE_animsys_evaluate_all_animation(bmain, sce, ctime);
1082         /*...done with recusrive funcs */
1083
1084         /* object_handle_update() on all objects, groups and sets */
1085         scene_update_tagged_recursive(bmain, sce, sce);
1086
1087         /* notify editors and python about recalc */
1088         BLI_exec_cb(bmain, &sce->id, BLI_CB_EVT_SCENE_UPDATE_POST);
1089         BLI_exec_cb(bmain, &sce->id, BLI_CB_EVT_FRAME_CHANGE_POST);
1090
1091         DAG_ids_check_recalc(bmain, sce, TRUE);
1092
1093         /* clear recalc flags */
1094         DAG_ids_clear_recalc(bmain);
1095 }
1096
1097 /* return default layer, also used to patch old files */
1098 SceneRenderLayer *scene_add_render_layer(Scene *sce, const char *name)
1099 {
1100         SceneRenderLayer *srl;
1101
1102         if(!name)
1103                 name= "RenderLayer";
1104
1105         srl= MEM_callocN(sizeof(SceneRenderLayer), "new render layer");
1106         BLI_strncpy(srl->name, name, sizeof(srl->name));
1107         BLI_uniquename(&sce->r.layers, srl, "RenderLayer", '.', offsetof(SceneRenderLayer, name), sizeof(srl->name));
1108         BLI_addtail(&sce->r.layers, srl);
1109
1110         /* note, this is also in render, pipeline.c, to make layer when scenedata doesnt have it */
1111         srl->lay= (1<<20) -1;
1112         srl->layflag= 0x7FFF;   /* solid ztra halo edge strand */
1113         srl->passflag= SCE_PASS_COMBINED|SCE_PASS_Z;
1114         FRS_add_freestyle_config( srl );
1115
1116         return srl;
1117 }
1118
1119 int scene_remove_render_layer(Main *bmain, Scene *scene, SceneRenderLayer *srl)
1120 {
1121         const int act= BLI_findindex(&scene->r.layers, srl);
1122         Scene *sce;
1123
1124         if (act == -1) {
1125                 return 0;
1126         }
1127         else if ( (scene->r.layers.first == scene->r.layers.last) &&
1128                   (scene->r.layers.first == srl))
1129         {
1130                 /* ensure 1 layer is kept */
1131                 return 0;
1132         }
1133
1134         BLI_remlink(&scene->r.layers, srl);
1135         MEM_freeN(srl);
1136
1137         scene->r.actlay= 0;
1138
1139         for(sce = bmain->scene.first; sce; sce = sce->id.next) {
1140                 if(sce->nodetree) {
1141                         bNode *node;
1142                         for(node = sce->nodetree->nodes.first; node; node = node->next) {
1143                                 if(node->type==CMP_NODE_R_LAYERS && (Scene*)node->id==scene) {
1144                                         if(node->custom1==act)
1145                                                 node->custom1= 0;
1146                                         else if(node->custom1>act)
1147                                                 node->custom1--;
1148                                 }
1149                         }
1150                 }
1151         }
1152
1153         return 1;
1154 }
1155
1156 /* render simplification */
1157
1158 int get_render_subsurf_level(RenderData *r, int lvl)
1159 {
1160         if(r->mode & R_SIMPLIFY)
1161                 return MIN2(r->simplify_subsurf, lvl);
1162         else
1163                 return lvl;
1164 }
1165
1166 int get_render_child_particle_number(RenderData *r, int num)
1167 {
1168         if(r->mode & R_SIMPLIFY)
1169                 return (int)(r->simplify_particles*num);
1170         else
1171                 return num;
1172 }
1173
1174 int get_render_shadow_samples(RenderData *r, int samples)
1175 {
1176         if((r->mode & R_SIMPLIFY) && samples > 0)
1177                 return MIN2(r->simplify_shadowsamples, samples);
1178         else
1179                 return samples;
1180 }
1181
1182 float get_render_aosss_error(RenderData *r, float error)
1183 {
1184         if(r->mode & R_SIMPLIFY)
1185                 return ((1.0f-r->simplify_aosss)*10.0f + 1.0f)*error;
1186         else
1187                 return error;
1188 }
1189
1190 /* helper function for the SETLOOPER macro */
1191 Base *_setlooper_base_step(Scene **sce_iter, Base *base)
1192 {
1193         if(base && base->next) {
1194                 /* common case, step to the next */
1195                 return base->next;
1196         }
1197         else if(base==NULL && (*sce_iter)->base.first) {
1198                 /* first time looping, return the scenes first base */
1199                 return (Base *)(*sce_iter)->base.first;
1200         }
1201         else {
1202                 /* reached the end, get the next base in the set */
1203                 while((*sce_iter= (*sce_iter)->set)) {
1204                         base= (Base *)(*sce_iter)->base.first;
1205                         if(base) {
1206                                 return base;
1207                         }
1208                 }
1209         }
1210
1211         return NULL;
1212 }
1213
1214 int scene_use_new_shading_nodes(Scene *scene)
1215 {
1216         RenderEngineType *type= RE_engines_find(scene->r.engine);
1217         return (type && type->flag & RE_USE_SHADING_NODES);
1218 }
1219