Tomato: initial commit of mask editing tools into SVN
[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_mask.h"
67 #include "BKE_node.h"
68 #include "BKE_object.h"
69 #include "BKE_paint.h"
70 #include "BKE_pointcache.h"
71 #include "BKE_scene.h"
72 #include "BKE_sequencer.h"
73 #include "BKE_world.h"
74
75 #include "BKE_sound.h"
76
77 #include "RE_engine.h"
78
79 //XXX #include "BIF_previewrender.h"
80 //XXX #include "BIF_editseq.h"
81
82 #ifdef WIN32
83 #else
84 #include <sys/time.h>
85 #endif
86
87 void free_avicodecdata(AviCodecData *acd)
88 {
89         if (acd) {
90                 if (acd->lpFormat) {
91                         MEM_freeN(acd->lpFormat);
92                         acd->lpFormat = NULL;
93                         acd->cbFormat = 0;
94                 }
95                 if (acd->lpParms) {
96                         MEM_freeN(acd->lpParms);
97                         acd->lpParms = NULL;
98                         acd->cbParms = 0;
99                 }
100         }
101 }
102
103 void free_qtcodecdata(QuicktimeCodecData *qcd)
104 {
105         if (qcd) {
106                 if (qcd->cdParms) {
107                         MEM_freeN(qcd->cdParms);
108                         qcd->cdParms = NULL;
109                         qcd->cdSize = 0;
110                 }
111         }
112 }
113
114 Scene *copy_scene(Scene *sce, int type)
115 {
116         Scene *scen;
117         ToolSettings *ts;
118         Base *base, *obase;
119         
120         if (type == SCE_COPY_EMPTY) {
121                 ListBase lb;
122                 scen= add_scene(sce->id.name+2);
123                 
124                 lb= scen->r.layers;
125                 scen->r= sce->r;
126                 scen->r.layers= lb;
127                 scen->unit= sce->unit;
128                 scen->physics_settings= sce->physics_settings;
129                 scen->gm= sce->gm;
130                 scen->audio= sce->audio;
131
132                 MEM_freeN(scen->toolsettings);
133         }
134         else {
135                 scen= copy_libblock(&sce->id);
136                 BLI_duplicatelist(&(scen->base), &(sce->base));
137                 
138                 clear_id_newpoins();
139                 
140                 id_us_plus((ID *)scen->world);
141                 id_us_plus((ID *)scen->set);
142                 id_us_plus((ID *)scen->gm.dome.warptext);
143
144                 scen->ed= NULL;
145                 scen->theDag= NULL;
146                 scen->obedit= NULL;
147                 scen->stats= NULL;
148                 scen->fps_info= NULL;
149
150                 BLI_duplicatelist(&(scen->markers), &(sce->markers));
151                 BLI_duplicatelist(&(scen->transform_spaces), &(sce->transform_spaces));
152                 BLI_duplicatelist(&(scen->r.layers), &(sce->r.layers));
153                 BKE_keyingsets_copy(&(scen->keyingsets), &(sce->keyingsets));
154
155                 if (sce->nodetree) {
156                         scen->nodetree= ntreeCopyTree(sce->nodetree); /* copies actions */
157                         ntreeSwitchID(scen->nodetree, &sce->id, &scen->id);
158                 }
159
160                 obase= sce->base.first;
161                 base= scen->base.first;
162                 while (base) {
163                         id_us_plus(&base->object->id);
164                         if (obase==sce->basact) scen->basact= base;
165         
166                         obase= obase->next;
167                         base= base->next;
168                 }
169         }
170
171         /* tool settings */
172         scen->toolsettings= MEM_dupallocN(sce->toolsettings);
173
174         ts= scen->toolsettings;
175         if (ts) {
176                 if (ts->vpaint) {
177                         ts->vpaint= MEM_dupallocN(ts->vpaint);
178                         ts->vpaint->paintcursor= NULL;
179                         ts->vpaint->vpaint_prev= NULL;
180                         ts->vpaint->wpaint_prev= NULL;
181                         copy_paint(&ts->vpaint->paint, &ts->vpaint->paint);
182                 }
183                 if (ts->wpaint) {
184                         ts->wpaint= MEM_dupallocN(ts->wpaint);
185                         ts->wpaint->paintcursor= NULL;
186                         ts->wpaint->vpaint_prev= NULL;
187                         ts->wpaint->wpaint_prev= NULL;
188                         copy_paint(&ts->wpaint->paint, &ts->wpaint->paint);
189                 }
190                 if (ts->sculpt) {
191                         ts->sculpt= MEM_dupallocN(ts->sculpt);
192                         copy_paint(&ts->sculpt->paint, &ts->sculpt->paint);
193                 }
194
195                 copy_paint(&ts->imapaint.paint, &ts->imapaint.paint);
196                 ts->imapaint.paintcursor= NULL;
197                 ts->particle.paintcursor= NULL;
198         }
199         
200         /* make a private copy of the avicodecdata */
201         if (sce->r.avicodecdata) {
202                 scen->r.avicodecdata = MEM_dupallocN(sce->r.avicodecdata);
203                 scen->r.avicodecdata->lpFormat = MEM_dupallocN(scen->r.avicodecdata->lpFormat);
204                 scen->r.avicodecdata->lpParms = MEM_dupallocN(scen->r.avicodecdata->lpParms);
205         }
206         
207         /* make a private copy of the qtcodecdata */
208         if (sce->r.qtcodecdata) {
209                 scen->r.qtcodecdata = MEM_dupallocN(sce->r.qtcodecdata);
210                 scen->r.qtcodecdata->cdParms = MEM_dupallocN(scen->r.qtcodecdata->cdParms);
211         }
212         
213         if (sce->r.ffcodecdata.properties) { /* intentionally check scen not sce. */
214                 scen->r.ffcodecdata.properties= IDP_CopyProperty(sce->r.ffcodecdata.properties);
215         }
216
217         /* NOTE: part of SCE_COPY_LINK_DATA and SCE_COPY_FULL operations
218          * are done outside of blenkernel with ED_objects_single_users! */
219
220         /*  camera */
221         if (type == SCE_COPY_LINK_DATA || type == SCE_COPY_FULL) {
222                 ID_NEW(scen->camera);
223         }
224         
225         /* before scene copy */
226         sound_create_scene(scen);
227
228         /* world */
229         if (type == SCE_COPY_FULL) {
230                 BKE_copy_animdata_id_action((ID *)scen);
231                 if (scen->world) {
232                         id_us_plus((ID *)scen->world);
233                         scen->world= copy_world(scen->world);
234                         BKE_copy_animdata_id_action((ID *)scen->world);
235                 }
236
237                 if (sce->ed) {
238                         scen->ed= MEM_callocN(sizeof(Editing), "addseq");
239                         scen->ed->seqbasep= &scen->ed->seqbase;
240                         seqbase_dupli_recursive(sce, scen, &scen->ed->seqbase, &sce->ed->seqbase, SEQ_DUPE_ALL);
241                 }
242         }
243
244         return scen;
245 }
246
247 /* do not free scene itself */
248 void free_scene(Scene *sce)
249 {
250         Base *base;
251
252         base= sce->base.first;
253         while (base) {
254                 base->object->id.us--;
255                 base= base->next;
256         }
257         /* do not free objects! */
258         
259         if (sce->gpd) {
260 #if 0   // removed since this can be invalid memory when freeing everything
261                 // since the grease pencil data is freed before the scene.
262                 // since grease pencil data is not (yet?), shared between objects
263                 // its probably safe not to do this, some save and reload will free this.
264                 sce->gpd->id.us--;
265 #endif
266                 sce->gpd= NULL;
267         }
268
269         BLI_freelistN(&sce->base);
270         seq_free_editing(sce);
271
272         BKE_free_animdata((ID *)sce);
273         BKE_keyingsets_free(&sce->keyingsets);
274         
275         if (sce->r.avicodecdata) {
276                 free_avicodecdata(sce->r.avicodecdata);
277                 MEM_freeN(sce->r.avicodecdata);
278                 sce->r.avicodecdata = NULL;
279         }
280         if (sce->r.qtcodecdata) {
281                 free_qtcodecdata(sce->r.qtcodecdata);
282                 MEM_freeN(sce->r.qtcodecdata);
283                 sce->r.qtcodecdata = NULL;
284         }
285         if (sce->r.ffcodecdata.properties) {
286                 IDP_FreeProperty(sce->r.ffcodecdata.properties);
287                 MEM_freeN(sce->r.ffcodecdata.properties);
288                 sce->r.ffcodecdata.properties = NULL;
289         }
290         
291         BLI_freelistN(&sce->markers);
292         BLI_freelistN(&sce->transform_spaces);
293         BLI_freelistN(&sce->r.layers);
294         
295         if (sce->toolsettings) {
296                 if (sce->toolsettings->vpaint) {
297                         free_paint(&sce->toolsettings->vpaint->paint);
298                         MEM_freeN(sce->toolsettings->vpaint);
299                 }
300                 if (sce->toolsettings->wpaint) {
301                         free_paint(&sce->toolsettings->wpaint->paint);
302                         MEM_freeN(sce->toolsettings->wpaint);
303                 }
304                 if (sce->toolsettings->sculpt) {
305                         free_paint(&sce->toolsettings->sculpt->paint);
306                         MEM_freeN(sce->toolsettings->sculpt);
307                 }
308                 if (sce->toolsettings->uvsculpt) {
309                         free_paint(&sce->toolsettings->uvsculpt->paint);
310                         MEM_freeN(sce->toolsettings->uvsculpt);
311                 }
312                 free_paint(&sce->toolsettings->imapaint.paint);
313
314                 MEM_freeN(sce->toolsettings);
315                 sce->toolsettings = NULL;       
316         }
317         
318         if (sce->theDag) {
319                 free_forest(sce->theDag);
320                 MEM_freeN(sce->theDag);
321         }
322         
323         if (sce->nodetree) {
324                 ntreeFreeTree(sce->nodetree);
325                 MEM_freeN(sce->nodetree);
326         }
327
328         if (sce->stats)
329                 MEM_freeN(sce->stats);
330         if (sce->fps_info)
331                 MEM_freeN(sce->fps_info);
332
333         sound_destroy_scene(sce);
334 }
335
336 Scene *add_scene(const char *name)
337 {
338         Main *bmain= G.main;
339         Scene *sce;
340         ParticleEditSettings *pset;
341         int a;
342
343         sce= alloc_libblock(&bmain->scene, ID_SCE, name);
344         sce->lay= sce->layact= 1;
345         
346         sce->r.mode= R_GAMMA|R_OSA|R_SHADOW|R_SSS|R_ENVMAP|R_RAYTRACE;
347         sce->r.cfra= 1;
348         sce->r.sfra= 1;
349         sce->r.efra= 250;
350         sce->r.frame_step= 1;
351         sce->r.xsch= 1920;
352         sce->r.ysch= 1080;
353         sce->r.xasp= 1;
354         sce->r.yasp= 1;
355         sce->r.xparts= 8;
356         sce->r.yparts= 8;
357         sce->r.mblur_samples= 1;
358         sce->r.filtertype= R_FILTER_MITCH;
359         sce->r.size= 50;
360
361         sce->r.im_format.planes= R_IMF_PLANES_RGB;
362         sce->r.im_format.imtype= R_IMF_IMTYPE_PNG;
363         sce->r.im_format.quality= 90;
364
365         sce->r.displaymode= R_OUTPUT_AREA;
366         sce->r.framapto= 100;
367         sce->r.images= 100;
368         sce->r.framelen= 1.0;
369         sce->r.blurfac= 0.5;
370         sce->r.frs_sec= 24;
371         sce->r.frs_sec_base= 1;
372         sce->r.edgeint= 10;
373         sce->r.ocres = 128;
374         sce->r.color_mgt_flag |= R_COLOR_MANAGEMENT;
375         sce->r.gauss= 1.0;
376         
377         /* deprecated but keep for upwards compat */
378         sce->r.postgamma= 1.0;
379         sce->r.posthue= 0.0;
380         sce->r.postsat= 1.0;
381         
382         sce->r.bake_mode= 1;    /* prevent to include render stuff here */
383         sce->r.bake_filter= 2;
384         sce->r.bake_osa= 5;
385         sce->r.bake_flag= R_BAKE_CLEAR;
386         sce->r.bake_normal_space= R_BAKE_SPACE_TANGENT;
387         sce->r.scemode= R_DOCOMP|R_DOSEQ|R_EXTENSION;
388         sce->r.stamp= R_STAMP_TIME|R_STAMP_FRAME|R_STAMP_DATE|R_STAMP_CAMERA|R_STAMP_SCENE|R_STAMP_FILENAME|R_STAMP_RENDERTIME;
389         sce->r.stamp_font_id= 12;
390         sce->r.fg_stamp[0]= sce->r.fg_stamp[1]= sce->r.fg_stamp[2]= 0.8f;
391         sce->r.fg_stamp[3]= 1.0f;
392         sce->r.bg_stamp[0]= sce->r.bg_stamp[1]= sce->r.bg_stamp[2]= 0.0f;
393         sce->r.bg_stamp[3]= 0.25f;
394         sce->r.raytrace_options = R_RAYTRACE_USE_INSTANCES;
395
396         sce->r.seq_prev_type= OB_SOLID;
397         sce->r.seq_rend_type= OB_SOLID;
398         sce->r.seq_flag= R_SEQ_GL_PREV;
399
400         sce->r.threads= 1;
401
402         sce->r.simplify_subsurf= 6;
403         sce->r.simplify_particles= 1.0f;
404         sce->r.simplify_shadowsamples= 16;
405         sce->r.simplify_aosss= 1.0f;
406
407         sce->r.border.xmin = 0.0f;
408         sce->r.border.ymin = 0.0f;
409         sce->r.border.xmax = 1.0f;
410         sce->r.border.ymax = 1.0f;
411         
412         sce->toolsettings = MEM_callocN(sizeof(struct ToolSettings), "Tool Settings Struct");
413         sce->toolsettings->cornertype=1;
414         sce->toolsettings->degr = 90; 
415         sce->toolsettings->step = 9;
416         sce->toolsettings->turn = 1;                            
417         sce->toolsettings->extr_offs = 1; 
418         sce->toolsettings->doublimit = 0.001;
419         sce->toolsettings->segments = 32;
420         sce->toolsettings->rings = 32;
421         sce->toolsettings->vertices = 32;
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 0
769         if (ob && *ob) {
770                 printf("Scene: '%s', '%s'\n", (*scene)->id.name+2, (*ob)->id.name+2);
771         }
772 #endif
773
774         /* reset recursion test */
775         in_next_object= 0;
776         
777         return fase;
778 }
779
780 Object *scene_find_camera(Scene *sc)
781 {
782         Base *base;
783         
784         for (base= sc->base.first; base; base= base->next)
785                 if (base->object->type==OB_CAMERA)
786                         return base->object;
787
788         return NULL;
789 }
790
791 #ifdef DURIAN_CAMERA_SWITCH
792 Object *scene_camera_switch_find(Scene *scene)
793 {
794         TimeMarker *m;
795         int cfra = scene->r.cfra;
796         int frame = -(MAXFRAME + 1);
797         Object *camera= NULL;
798
799         for (m= scene->markers.first; m; m= m->next) {
800                 if (m->camera && (m->camera->restrictflag & OB_RESTRICT_RENDER)==0 && (m->frame <= cfra) && (m->frame > frame)) {
801                         camera= m->camera;
802                         frame= m->frame;
803
804                         if (frame == cfra)
805                                 break;
806
807                 }
808         }
809         return camera;
810 }
811 #endif
812
813 int scene_camera_switch_update(Scene *scene)
814 {
815 #ifdef DURIAN_CAMERA_SWITCH
816         Object *camera= scene_camera_switch_find(scene);
817         if (camera) {
818                 scene->camera= camera;
819                 return 1;
820         }
821 #else
822         (void)scene;
823 #endif
824         return 0;
825 }
826
827 char *scene_find_marker_name(Scene *scene, int frame)
828 {
829         ListBase *markers= &scene->markers;
830         TimeMarker *m1, *m2;
831
832         /* search through markers for match */
833         for (m1=markers->first, m2=markers->last; m1 && m2; m1=m1->next, m2=m2->prev) {
834                 if (m1->frame==frame)
835                         return m1->name;
836
837                 if (m1 == m2)
838                         break;
839
840                 if (m2->frame==frame)
841                         return m2->name;
842         }
843
844         return NULL;
845 }
846
847 /* return the current marker for this frame,
848  * we can have more then 1 marker per frame, this just returns the first :/ */
849 char *scene_find_last_marker_name(Scene *scene, int frame)
850 {
851         TimeMarker *marker, *best_marker = NULL;
852         int best_frame = -MAXFRAME*2;
853         for (marker= scene->markers.first; marker; marker= marker->next) {
854                 if (marker->frame==frame) {
855                         return marker->name;
856                 }
857
858                 if ( marker->frame > best_frame && marker->frame < frame) {
859                         best_marker = marker;
860                         best_frame = marker->frame;
861                 }
862         }
863
864         return best_marker ? best_marker->name : NULL;
865 }
866
867
868 Base *scene_add_base(Scene *sce, Object *ob)
869 {
870         Base *b= MEM_callocN(sizeof(*b), "scene_add_base");
871         BLI_addhead(&sce->base, b);
872
873         b->object= ob;
874         b->flag= ob->flag;
875         b->lay= ob->lay;
876
877         return b;
878 }
879
880 void scene_deselect_all(Scene *sce)
881 {
882         Base *b;
883
884         for (b= sce->base.first; b; b= b->next) {
885                 b->flag&= ~SELECT;
886                 b->object->flag= b->flag;
887         }
888 }
889
890 void scene_select_base(Scene *sce, Base *selbase)
891 {
892         scene_deselect_all(sce);
893
894         selbase->flag |= SELECT;
895         selbase->object->flag= selbase->flag;
896
897         sce->basact= selbase;
898 }
899
900 /* checks for cycle, returns 1 if it's all OK */
901 int scene_check_setscene(Main *bmain, Scene *sce)
902 {
903         Scene *scene;
904         int a, totscene;
905         
906         if (sce->set==NULL) return 1;
907         
908         totscene= 0;
909         for (scene= bmain->scene.first; scene; scene= scene->id.next)
910                 totscene++;
911         
912         for (a=0, scene=sce; scene->set; scene=scene->set, a++) {
913                 /* more iterations than scenes means we have a cycle */
914                 if (a > totscene) {
915                         /* the tested scene gets zero'ed, that's typically current scene */
916                         sce->set= NULL;
917                         return 0;
918                 }
919         }
920
921         return 1;
922 }
923
924 /* This function is needed to cope with fractional frames - including two Blender rendering features
925  * mblur (motion blur that renders 'subframes' and blurs them together), and fields rendering. 
926  */
927 float BKE_curframe(Scene *scene)
928 {
929         return BKE_frame_to_ctime(scene, scene->r.cfra);
930 }
931
932 /* This function is used to obtain arbitrary fractional frames */
933 float BKE_frame_to_ctime(Scene *scene, const float frame)
934 {
935         float ctime = frame;
936         ctime += scene->r.subframe;
937         ctime *= scene->r.framelen;     
938         
939         return ctime;
940 }
941
942 /* drivers support/hacks 
943  *      - this method is called from scene_update_tagged_recursive(), so gets included in viewport + render
944  *      - these are always run since the depsgraph can't handle non-object data
945  *      - these happen after objects are all done so that we can read in their final transform values,
946  *        though this means that objects can't refer to scene info for guidance...
947  */
948 static void scene_update_drivers(Main *UNUSED(bmain), Scene *scene)
949 {
950         float ctime = BKE_curframe(scene);
951         
952         /* scene itself */
953         if (scene->adt && scene->adt->drivers.first) {
954                 BKE_animsys_evaluate_animdata(scene, &scene->id, scene->adt, ctime, ADT_RECALC_DRIVERS);
955         }
956         
957         /* world */
958         // TODO: what about world textures? but then those have nodes too...
959         if (scene->world) {
960                 ID *wid = (ID *)scene->world;
961                 AnimData *adt= BKE_animdata_from_id(wid);
962                 
963                 if (adt && adt->drivers.first)
964                         BKE_animsys_evaluate_animdata(scene, wid, adt, ctime, ADT_RECALC_DRIVERS);
965         }
966         
967         /* nodes */
968         if (scene->nodetree) {
969                 ID *nid = (ID *)scene->nodetree;
970                 AnimData *adt= BKE_animdata_from_id(nid);
971                 
972                 if (adt && adt->drivers.first)
973                         BKE_animsys_evaluate_animdata(scene, nid, adt, ctime, ADT_RECALC_DRIVERS);
974         }
975 }
976
977 static void scene_update_tagged_recursive(Main *bmain, Scene *scene, Scene *scene_parent)
978 {
979         Base *base;
980         
981         
982         scene->customdata_mask= scene_parent->customdata_mask;
983
984         /* sets first, we allow per definition current scene to have
985          * dependencies on sets, but not the other way around. */
986         if (scene->set)
987                 scene_update_tagged_recursive(bmain, scene->set, scene_parent);
988         
989         /* scene objects */
990         for (base= scene->base.first; base; base= base->next) {
991                 Object *ob= base->object;
992                 
993                 object_handle_update(scene_parent, ob);
994                 
995                 if (ob->dup_group && (ob->transflag & OB_DUPLIGROUP))
996                         group_handle_recalc_and_update(scene_parent, ob, ob->dup_group);
997                         
998                 /* always update layer, so that animating layers works */
999                 base->lay= ob->lay;
1000         }
1001         
1002         /* scene drivers... */
1003         scene_update_drivers(bmain, scene);
1004
1005         /* update sound system animation */
1006         sound_update_scene(scene);
1007
1008         /* update masking curves */
1009         BKE_mask_update_scene(bmain, scene);
1010 }
1011
1012 /* this is called in main loop, doing tagged updates before redraw */
1013 void scene_update_tagged(Main *bmain, Scene *scene)
1014 {
1015         /* keep this first */
1016         BLI_exec_cb(bmain, &scene->id, BLI_CB_EVT_SCENE_UPDATE_PRE);
1017
1018         /* flush recalc flags to dependencies */
1019         DAG_ids_flush_tagged(bmain);
1020
1021         scene->physics_settings.quick_cache_step= 0;
1022
1023         /* update all objects: drivers, matrices, displists, etc. flags set
1024          * by depgraph or manual, no layer check here, gets correct flushed
1025          *
1026          * in the future this should handle updates for all datablocks, not
1027          * only objects and scenes. - brecht */
1028         scene_update_tagged_recursive(bmain, scene, scene);
1029
1030         /* extra call here to recalc scene animation (for sequencer) */
1031         {
1032                 AnimData *adt= BKE_animdata_from_id(&scene->id);
1033                 float ctime = BKE_curframe(scene);
1034                 
1035                 if (adt && (adt->recalc & ADT_RECALC_ANIM))
1036                         BKE_animsys_evaluate_animdata(scene, &scene->id, adt, ctime, 0);
1037         }
1038         
1039         /* quick point cache updates */
1040         if (scene->physics_settings.quick_cache_step)
1041                 BKE_ptcache_quick_cache_all(bmain, scene);
1042
1043         /* notify editors and python about recalc */
1044         BLI_exec_cb(bmain, &scene->id, BLI_CB_EVT_SCENE_UPDATE_POST);
1045         DAG_ids_check_recalc(bmain, scene, FALSE);
1046
1047         /* clear recalc flags */
1048         DAG_ids_clear_recalc(bmain);
1049 }
1050
1051 /* applies changes right away, does all sets too */
1052 void scene_update_for_newframe(Main *bmain, Scene *sce, unsigned int lay)
1053 {
1054         float ctime = BKE_curframe(sce);
1055         Scene *sce_iter;
1056
1057         /* keep this first */
1058         BLI_exec_cb(bmain, &sce->id, BLI_CB_EVT_FRAME_CHANGE_PRE);
1059         BLI_exec_cb(bmain, &sce->id, BLI_CB_EVT_SCENE_UPDATE_PRE);
1060
1061         sound_set_cfra(sce->r.cfra);
1062         
1063         /* clear animation overrides */
1064         // XXX TODO...
1065
1066         for (sce_iter= sce; sce_iter; sce_iter= sce_iter->set) {
1067                 if (sce_iter->theDag==NULL)
1068                         DAG_scene_sort(bmain, sce_iter);
1069         }
1070
1071         /* flush recalc flags to dependencies, if we were only changing a frame
1072          * this would not be necessary, but if a user or a script has modified
1073          * some datablock before scene_update_tagged was called, we need the flush */
1074         DAG_ids_flush_tagged(bmain);
1075
1076         /* Following 2 functions are recursive
1077          * so don't call within 'scene_update_tagged_recursive' */
1078         DAG_scene_update_flags(bmain, sce, lay, TRUE);   // only stuff that moves or needs display still
1079
1080         BKE_mask_evaluate_all_masks(bmain, ctime);
1081
1082         /* All 'standard' (i.e. without any dependencies) animation is handled here,
1083          * with an 'local' to 'macro' order of evaluation. This should ensure that
1084          * settings stored nestled within a hierarchy (i.e. settings in a Texture block
1085          * can be overridden by settings from Scene, which owns the Texture through a hierarchy
1086          * such as Scene->World->MTex/Texture) can still get correctly overridden.
1087          */
1088         BKE_animsys_evaluate_all_animation(bmain, sce, ctime);
1089         /*...done with recusrive funcs */
1090
1091         /* object_handle_update() on all objects, groups and sets */
1092         scene_update_tagged_recursive(bmain, sce, sce);
1093
1094         /* notify editors and python about recalc */
1095         BLI_exec_cb(bmain, &sce->id, BLI_CB_EVT_SCENE_UPDATE_POST);
1096         BLI_exec_cb(bmain, &sce->id, BLI_CB_EVT_FRAME_CHANGE_POST);
1097
1098         DAG_ids_check_recalc(bmain, sce, TRUE);
1099
1100         /* clear recalc flags */
1101         DAG_ids_clear_recalc(bmain);
1102 }
1103
1104 /* return default layer, also used to patch old files */
1105 SceneRenderLayer *scene_add_render_layer(Scene *sce, const char *name)
1106 {
1107         SceneRenderLayer *srl;
1108
1109         if (!name)
1110                 name= "RenderLayer";
1111
1112         srl= MEM_callocN(sizeof(SceneRenderLayer), "new render layer");
1113         BLI_strncpy(srl->name, name, sizeof(srl->name));
1114         BLI_uniquename(&sce->r.layers, srl, "RenderLayer", '.', offsetof(SceneRenderLayer, name), sizeof(srl->name));
1115         BLI_addtail(&sce->r.layers, srl);
1116
1117         /* note, this is also in render, pipeline.c, to make layer when scenedata doesnt have it */
1118         srl->lay= (1<<20) -1;
1119         srl->layflag= 0x7FFF;   /* solid ztra halo edge strand */
1120         srl->passflag= SCE_PASS_COMBINED|SCE_PASS_Z;
1121
1122         return srl;
1123 }
1124
1125 int scene_remove_render_layer(Main *bmain, Scene *scene, SceneRenderLayer *srl)
1126 {
1127         const int act= BLI_findindex(&scene->r.layers, srl);
1128         Scene *sce;
1129
1130         if (act == -1) {
1131                 return 0;
1132         }
1133         else if ( (scene->r.layers.first == scene->r.layers.last) &&
1134                   (scene->r.layers.first == srl))
1135         {
1136                 /* ensure 1 layer is kept */
1137                 return 0;
1138         }
1139
1140         BLI_remlink(&scene->r.layers, srl);
1141         MEM_freeN(srl);
1142
1143         scene->r.actlay= 0;
1144
1145         for (sce = bmain->scene.first; sce; sce = sce->id.next) {
1146                 if (sce->nodetree) {
1147                         bNode *node;
1148                         for (node = sce->nodetree->nodes.first; node; node = node->next) {
1149                                 if (node->type==CMP_NODE_R_LAYERS && (Scene*)node->id==scene) {
1150                                         if (node->custom1==act)
1151                                                 node->custom1= 0;
1152                                         else if (node->custom1>act)
1153                                                 node->custom1--;
1154                                 }
1155                         }
1156                 }
1157         }
1158
1159         return 1;
1160 }
1161
1162 /* render simplification */
1163
1164 int get_render_subsurf_level(RenderData *r, int lvl)
1165 {
1166         if (r->mode & R_SIMPLIFY)
1167                 return MIN2(r->simplify_subsurf, lvl);
1168         else
1169                 return lvl;
1170 }
1171
1172 int get_render_child_particle_number(RenderData *r, int num)
1173 {
1174         if (r->mode & R_SIMPLIFY)
1175                 return (int)(r->simplify_particles*num);
1176         else
1177                 return num;
1178 }
1179
1180 int get_render_shadow_samples(RenderData *r, int samples)
1181 {
1182         if ((r->mode & R_SIMPLIFY) && samples > 0)
1183                 return MIN2(r->simplify_shadowsamples, samples);
1184         else
1185                 return samples;
1186 }
1187
1188 float get_render_aosss_error(RenderData *r, float error)
1189 {
1190         if (r->mode & R_SIMPLIFY)
1191                 return ((1.0f-r->simplify_aosss)*10.0f + 1.0f)*error;
1192         else
1193                 return error;
1194 }
1195
1196 /* helper function for the SETLOOPER macro */
1197 Base *_setlooper_base_step(Scene **sce_iter, Base *base)
1198 {
1199         if (base && base->next) {
1200                 /* common case, step to the next */
1201                 return base->next;
1202         }
1203         else if (base==NULL && (*sce_iter)->base.first) {
1204                 /* first time looping, return the scenes first base */
1205                 return (Base *)(*sce_iter)->base.first;
1206         }
1207         else {
1208                 /* reached the end, get the next base in the set */
1209                 while ((*sce_iter= (*sce_iter)->set)) {
1210                         base= (Base *)(*sce_iter)->base.first;
1211                         if (base) {
1212                                 return base;
1213                         }
1214                 }
1215         }
1216
1217         return NULL;
1218 }
1219
1220 int scene_use_new_shading_nodes(Scene *scene)
1221 {
1222         RenderEngineType *type= RE_engines_find(scene->r.engine);
1223         return (type && type->flag & RE_USE_SHADING_NODES);
1224 }
1225