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