update for .B.blend
[blender.git] / source / blender / blenkernel / intern / scene.c
1 /*  scene.c
2  *  
3  * 
4  * $Id$
5  *
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  */
31
32 #include <stddef.h>
33 #include <stdio.h>
34 #include <string.h>
35
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #ifndef WIN32 
41 #include <unistd.h>
42 #else
43 #include <io.h>
44 #endif
45 #include "MEM_guardedalloc.h"
46
47 #include "DNA_anim_types.h"
48 #include "DNA_armature_types.h" 
49 #include "DNA_color_types.h"
50 #include "DNA_constraint_types.h"
51 #include "DNA_curve_types.h"
52 #include "DNA_group_types.h"
53 #include "DNA_lamp_types.h"
54 #include "DNA_material_types.h"
55 #include "DNA_meta_types.h"
56 #include "DNA_object_types.h"
57 #include "DNA_scene_types.h"
58 #include "DNA_screen_types.h"
59 #include "DNA_texture_types.h"
60 #include "DNA_userdef_types.h"
61
62 #include "BKE_action.h"                 
63 #include "BKE_anim.h"
64 #include "BKE_animsys.h"
65 #include "BKE_armature.h"               
66 #include "BKE_colortools.h"
67 #include "BKE_colortools.h"
68 #include "BKE_constraint.h"
69 #include "BKE_depsgraph.h"
70 #include "BKE_global.h"
71 #include "BKE_group.h"
72 #include "BKE_ipo.h"
73 #include "BKE_idprop.h"
74 #include "BKE_image.h"
75 #include "BKE_key.h"
76 #include "BKE_library.h"
77 #include "BKE_main.h"
78 #include "BKE_node.h"
79 #include "BKE_object.h"
80 #include "BKE_paint.h"
81 #include "BKE_pointcache.h"
82 #include "BKE_scene.h"
83 #include "BKE_sequencer.h"
84 #include "BKE_world.h"
85 #include "BKE_utildefines.h"
86 #include "BKE_sound.h"
87
88 //XXX #include "BIF_previewrender.h"
89 //XXX #include "BIF_editseq.h"
90
91 #ifndef DISABLE_PYTHON
92 #include "BPY_extern.h"
93 #endif
94
95 #include "BLI_math.h"
96 #include "BLI_blenlib.h"
97
98 //XXX #include "nla.h"
99
100 #ifdef WIN32
101 #else
102 #include <sys/time.h>
103 #endif
104
105 void free_avicodecdata(AviCodecData *acd)
106 {
107         if (acd) {
108                 if (acd->lpFormat){
109                         MEM_freeN(acd->lpFormat);
110                         acd->lpFormat = NULL;
111                         acd->cbFormat = 0;
112                 }
113                 if (acd->lpParms){
114                         MEM_freeN(acd->lpParms);
115                         acd->lpParms = NULL;
116                         acd->cbParms = 0;
117                 }
118         }
119 }
120
121 void free_qtcodecdata(QuicktimeCodecData *qcd)
122 {
123         if (qcd) {
124                 if (qcd->cdParms){
125                         MEM_freeN(qcd->cdParms);
126                         qcd->cdParms = NULL;
127                         qcd->cdSize = 0;
128                 }
129         }
130 }
131
132 Scene *copy_scene(Main *bmain, Scene *sce, int type)
133 {
134         Scene *scen;
135         ToolSettings *ts;
136         Base *base, *obase;
137         
138         if(type == SCE_COPY_EMPTY) {
139                 ListBase lb;
140                 scen= add_scene(sce->id.name+2);
141                 
142                 lb= scen->r.layers;
143                 scen->r= sce->r;
144                 scen->r.layers= lb;
145         }
146         else {
147                 scen= copy_libblock(sce);
148                 BLI_duplicatelist(&(scen->base), &(sce->base));
149                 
150                 clear_id_newpoins();
151                 
152                 id_us_plus((ID *)scen->world);
153                 id_us_plus((ID *)scen->set);
154                 id_us_plus((ID *)scen->ima);
155                 id_us_plus((ID *)scen->gm.dome.warptext);
156
157                 scen->ed= NULL;
158                 scen->theDag= NULL;
159                 scen->obedit= NULL;
160                 scen->toolsettings= MEM_dupallocN(sce->toolsettings);
161                 scen->stats= NULL;
162                 scen->fps_info= NULL;
163
164                 ts= scen->toolsettings;
165                 if(ts) {
166                         if(ts->vpaint) {
167                                 ts->vpaint= MEM_dupallocN(ts->vpaint);
168                                 ts->vpaint->paintcursor= NULL;
169                                 ts->vpaint->vpaint_prev= NULL;
170                                 ts->vpaint->wpaint_prev= NULL;
171                                 copy_paint(&ts->vpaint->paint, &ts->vpaint->paint);
172                         }
173                         if(ts->wpaint) {
174                                 ts->wpaint= MEM_dupallocN(ts->wpaint);
175                                 ts->wpaint->paintcursor= NULL;
176                                 ts->wpaint->vpaint_prev= NULL;
177                                 ts->wpaint->wpaint_prev= NULL;
178                                 copy_paint(&ts->wpaint->paint, &ts->wpaint->paint);
179                         }
180                         if(ts->sculpt) {
181                                 ts->sculpt= MEM_dupallocN(ts->sculpt);
182                                 copy_paint(&ts->sculpt->paint, &ts->sculpt->paint);
183                         }
184
185                         copy_paint(&ts->imapaint.paint, &ts->imapaint.paint);
186                         ts->imapaint.paintcursor= NULL;
187
188                         ts->particle.paintcursor= NULL;
189                 }
190                 
191                 BLI_duplicatelist(&(scen->markers), &(sce->markers));
192                 BLI_duplicatelist(&(scen->transform_spaces), &(sce->transform_spaces));
193                 BLI_duplicatelist(&(scen->r.layers), &(sce->r.layers));
194                 BKE_keyingsets_copy(&(scen->keyingsets), &(sce->keyingsets));
195                 
196                 scen->nodetree= ntreeCopyTree(sce->nodetree, 0);
197                 
198                 obase= sce->base.first;
199                 base= scen->base.first;
200                 while(base) {
201                         id_us_plus(&base->object->id);
202                         if(obase==sce->basact) scen->basact= base;
203         
204                         obase= obase->next;
205                         base= base->next;
206                 }
207         }
208         
209         /* make a private copy of the avicodecdata */
210         if(sce->r.avicodecdata) {
211                 scen->r.avicodecdata = MEM_dupallocN(sce->r.avicodecdata);
212                 scen->r.avicodecdata->lpFormat = MEM_dupallocN(scen->r.avicodecdata->lpFormat);
213                 scen->r.avicodecdata->lpParms = MEM_dupallocN(scen->r.avicodecdata->lpParms);
214         }
215         
216         /* make a private copy of the qtcodecdata */
217         if(sce->r.qtcodecdata) {
218                 scen->r.qtcodecdata = MEM_dupallocN(sce->r.qtcodecdata);
219                 scen->r.qtcodecdata->cdParms = MEM_dupallocN(scen->r.qtcodecdata->cdParms);
220         }
221         
222         /* NOTE: part of SCE_COPY_LINK_DATA and SCE_COPY_FULL operations
223          * are done outside of blenkernel with ED_objects_single_users! */
224
225     /*  camera */
226         if(type == SCE_COPY_LINK_DATA || type == SCE_COPY_FULL) {
227             ID_NEW(scen->camera);
228         }
229
230         /* world */
231         if(type == SCE_COPY_FULL) {
232         if(scen->world) {
233             id_us_plus((ID *)scen->world);
234             scen->world= copy_world(scen->world);
235         }
236         }
237
238         sound_create_scene(scen);
239
240         return scen;
241 }
242
243 /* do not free scene itself */
244 void free_scene(Scene *sce)
245 {
246         Base *base;
247
248         base= sce->base.first;
249         while(base) {
250                 base->object->id.us--;
251                 base= base->next;
252         }
253         /* do not free objects! */
254         
255         if(sce->gpd) {
256 #if 0   // removed since this can be invalid memory when freeing everything
257         // since the grease pencil data is free'd before the scene.
258         // since grease pencil data is not (yet?), shared between objects
259         // its probably safe not to do this, some save and reload will free this.
260                 sce->gpd->id.us--;
261 #endif
262                 sce->gpd= NULL;
263         }
264
265         BLI_freelistN(&sce->base);
266         seq_free_editing(sce);
267
268         BKE_free_animdata((ID *)sce);
269         BKE_keyingsets_free(&sce->keyingsets);
270         
271         if (sce->r.avicodecdata) {
272                 free_avicodecdata(sce->r.avicodecdata);
273                 MEM_freeN(sce->r.avicodecdata);
274                 sce->r.avicodecdata = NULL;
275         }
276         if (sce->r.qtcodecdata) {
277                 free_qtcodecdata(sce->r.qtcodecdata);
278                 MEM_freeN(sce->r.qtcodecdata);
279                 sce->r.qtcodecdata = NULL;
280         }
281         if (sce->r.ffcodecdata.properties) {
282                 IDP_FreeProperty(sce->r.ffcodecdata.properties);
283                 MEM_freeN(sce->r.ffcodecdata.properties);
284                 sce->r.ffcodecdata.properties = NULL;
285         }
286         
287         BLI_freelistN(&sce->markers);
288         BLI_freelistN(&sce->transform_spaces);
289         BLI_freelistN(&sce->r.layers);
290         
291         if(sce->toolsettings) {
292                 if(sce->toolsettings->vpaint) {
293                         free_paint(&sce->toolsettings->vpaint->paint);
294                         MEM_freeN(sce->toolsettings->vpaint);
295                 }
296                 if(sce->toolsettings->wpaint) {
297                         free_paint(&sce->toolsettings->wpaint->paint);
298                         MEM_freeN(sce->toolsettings->wpaint);
299                 }
300                 if(sce->toolsettings->sculpt) {
301                         free_paint(&sce->toolsettings->sculpt->paint);
302                         MEM_freeN(sce->toolsettings->sculpt);
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(char *name)
329 {
330         Scene *sce;
331         ParticleEditSettings *pset;
332         int a;
333
334         sce= alloc_libblock(&G.main->scene, ID_SCE, name);
335         sce->lay= 1;
336         
337         sce->r.mode= R_GAMMA|R_OSA|R_SHADOW|R_SSS|R_ENVMAP|R_RAYTRACE;
338         sce->r.cfra= 1;
339         sce->r.sfra= 1;
340         sce->r.efra= 250;
341         sce->r.frame_step= 1;
342         sce->r.xsch= 1920;
343         sce->r.ysch= 1080;
344         sce->r.xasp= 1;
345         sce->r.yasp= 1;
346         sce->r.xparts= 8;
347         sce->r.yparts= 8;
348         sce->r.size= 25;
349         sce->r.planes= 24;
350         sce->r.quality= 90;
351         sce->r.framapto= 100;
352         sce->r.images= 100;
353         sce->r.framelen= 1.0;
354         sce->r.frs_sec= 25;
355         sce->r.frs_sec_base= 1;
356         sce->r.ocres = 128;
357         sce->r.color_mgt_flag |= R_COLOR_MANAGEMENT;
358         
359         sce->r.bake_mode= 1;    /* prevent to include render stuff here */
360         sce->r.bake_filter= 8;
361         sce->r.bake_osa= 5;
362         sce->r.bake_flag= R_BAKE_CLEAR;
363         sce->r.bake_normal_space= R_BAKE_SPACE_TANGENT;
364
365         sce->r.scemode= R_DOCOMP|R_DOSEQ|R_EXTENSION;
366         sce->r.stamp= R_STAMP_TIME|R_STAMP_FRAME|R_STAMP_DATE|R_STAMP_SCENE|R_STAMP_CAMERA|R_STAMP_RENDERTIME;
367         sce->r.stamp_font_id= 12;
368         
369         sce->r.threads= 1;
370
371         sce->r.simplify_subsurf= 6;
372         sce->r.simplify_particles= 1.0f;
373         sce->r.simplify_shadowsamples= 16;
374         sce->r.simplify_aosss= 1.0f;
375
376         sce->r.cineonblack= 95;
377         sce->r.cineonwhite= 685;
378         sce->r.cineongamma= 1.7f;
379
380         sce->r.border.xmin= 0.0f;
381         sce->r.border.ymin= 0.0f;
382         sce->r.border.xmax= 1.0f;
383         sce->r.border.ymax= 1.0f;
384         
385         sce->toolsettings = MEM_callocN(sizeof(struct ToolSettings),"Tool Settings Struct");
386         sce->toolsettings->cornertype=1;
387         sce->toolsettings->degr = 90; 
388         sce->toolsettings->step = 9;
389         sce->toolsettings->turn = 1;                            
390         sce->toolsettings->extr_offs = 1; 
391         sce->toolsettings->doublimit = 0.001;
392         sce->toolsettings->segments = 32;
393         sce->toolsettings->rings = 32;
394         sce->toolsettings->vertices = 32;
395         sce->toolsettings->editbutflag = 1;
396         sce->toolsettings->uvcalc_radius = 1.0f;
397         sce->toolsettings->uvcalc_cubesize = 1.0f;
398         sce->toolsettings->uvcalc_mapdir = 1;
399         sce->toolsettings->uvcalc_mapalign = 1;
400         sce->toolsettings->unwrapper = 1;
401         sce->toolsettings->select_thresh= 0.01f;
402         sce->toolsettings->jointrilimit = 0.8f;
403
404         sce->toolsettings->selectmode= SCE_SELECT_VERTEX;
405         sce->toolsettings->normalsize= 0.1;
406         sce->toolsettings->autokey_mode= U.autokey_mode;
407
408         sce->toolsettings->skgen_resolution = 100;
409         sce->toolsettings->skgen_threshold_internal     = 0.01f;
410         sce->toolsettings->skgen_threshold_external     = 0.01f;
411         sce->toolsettings->skgen_angle_limit                    = 45.0f;
412         sce->toolsettings->skgen_length_ratio                   = 1.3f;
413         sce->toolsettings->skgen_length_limit                   = 1.5f;
414         sce->toolsettings->skgen_correlation_limit              = 0.98f;
415         sce->toolsettings->skgen_symmetry_limit                 = 0.1f;
416         sce->toolsettings->skgen_postpro = SKGEN_SMOOTH;
417         sce->toolsettings->skgen_postpro_passes = 1;
418         sce->toolsettings->skgen_options = SKGEN_FILTER_INTERNAL|SKGEN_FILTER_EXTERNAL|SKGEN_FILTER_SMART|SKGEN_HARMONIC|SKGEN_SUB_CORRELATION|SKGEN_STICK_TO_EMBEDDING;
419         sce->toolsettings->skgen_subdivisions[0] = SKGEN_SUB_CORRELATION;
420         sce->toolsettings->skgen_subdivisions[1] = SKGEN_SUB_LENGTH;
421         sce->toolsettings->skgen_subdivisions[2] = SKGEN_SUB_ANGLE;
422
423         sce->toolsettings->proportional_size = 1.0f;
424
425         sce->physics_settings.gravity[0] = 0.0f;
426         sce->physics_settings.gravity[1] = 0.0f;
427         sce->physics_settings.gravity[2] = -9.81f;
428         sce->physics_settings.flag = PHYS_GLOBAL_GRAVITY;
429
430         sce->unit.scale_length = 1.0f;
431
432         pset= &sce->toolsettings->particle;
433         pset->flag= PE_KEEP_LENGTHS|PE_LOCK_FIRST|PE_DEFLECT_EMITTER|PE_AUTO_VELOCITY;
434         pset->emitterdist= 0.25f;
435         pset->totrekey= 5;
436         pset->totaddkey= 5;
437         pset->brushtype= PE_BRUSH_NONE;
438         pset->draw_step= 2;
439         pset->fade_frames= 2;
440         pset->selectmode= SCE_SELECT_PATH;
441         for(a=0; a<PE_TOT_BRUSH; a++) {
442                 pset->brush[a].strength= 50;
443                 pset->brush[a].size= 50;
444                 pset->brush[a].step= 10;
445         }
446         pset->brush[PE_BRUSH_CUT].strength= 100;
447         
448         sce->jumpframe = 10;
449         sce->r.ffcodecdata.audio_mixrate = 44100;
450
451         sce->audio.distance_model = 2.0;
452         sce->audio.doppler_factor = 1.0;
453         sce->audio.speed_of_sound = 343.3;
454
455         strcpy(sce->r.backbuf, "//backbuf");
456         strcpy(sce->r.pic, U.renderdir);
457
458         BLI_init_rctf(&sce->r.safety, 0.1f, 0.9f, 0.1f, 0.9f);
459         sce->r.osa= 8;
460
461         /* note; in header_info.c the scene copy happens..., if you add more to renderdata it has to be checked there */
462         scene_add_render_layer(sce);
463         
464         /* game data */
465         sce->gm.stereoflag = STEREO_NOSTEREO;
466         sce->gm.stereomode = STEREO_ANAGLYPH;
467         sce->gm.eyeseparation = 0.10;
468
469         sce->gm.dome.angle = 180;
470         sce->gm.dome.mode = DOME_FISHEYE;
471         sce->gm.dome.res = 4;
472         sce->gm.dome.resbuf = 1.0f;
473         sce->gm.dome.tilt = 0;
474
475         sce->gm.xplay= 800;
476         sce->gm.yplay= 600;
477         sce->gm.freqplay= 60;
478         sce->gm.depth= 32;
479
480         sce->gm.gravity= 9.8f;
481         sce->gm.physicsEngine= WOPHY_BULLET;
482         sce->gm.mode = 32; //XXX ugly harcoding, still not sure we should drop mode. 32 == 1 << 5 == use_occlusion_culling 
483         sce->gm.occlusionRes = 128;
484         sce->gm.ticrate = 60;
485         sce->gm.maxlogicstep = 5;
486         sce->gm.physubstep = 1;
487         sce->gm.maxphystep = 5;
488
489         sce->gm.flag = GAME_DISPLAY_LISTS;
490         sce->gm.matmode = GAME_MAT_MULTITEX;
491
492         sound_create_scene(sce);
493
494         return sce;
495 }
496
497 Base *object_in_scene(Object *ob, Scene *sce)
498 {
499         Base *base;
500         
501         base= sce->base.first;
502         while(base) {
503                 if(base->object == ob) return base;
504                 base= base->next;
505         }
506         return NULL;
507 }
508
509 void set_scene_bg(Scene *scene)
510 {
511         Scene *sce;
512         Base *base;
513         Object *ob;
514         Group *group;
515         GroupObject *go;
516         int flag;
517         
518         /* check for cyclic sets, for reading old files but also for definite security (py?) */
519         scene_check_setscene(scene);
520         
521         /* deselect objects (for dataselect) */
522         for(ob= G.main->object.first; ob; ob= ob->id.next)
523                 ob->flag &= ~(SELECT|OB_FROMGROUP);
524
525         /* group flags again */
526         for(group= G.main->group.first; group; group= group->id.next) {
527                 go= group->gobject.first;
528                 while(go) {
529                         if(go->ob) go->ob->flag |= OB_FROMGROUP;
530                         go= go->next;
531                 }
532         }
533
534         /* sort baselist */
535         DAG_scene_sort(scene);
536         
537         /* ensure dags are built for sets */
538         for(sce= scene->set; sce; sce= sce->set)
539                 if(sce->theDag==NULL)
540                         DAG_scene_sort(sce);
541
542         /* copy layers and flags from bases to objects */
543         for(base= scene->base.first; base; base= base->next) {
544                 ob= base->object;
545                 ob->lay= base->lay;
546                 
547                 /* group patch... */
548                 base->flag &= ~(OB_FROMGROUP);
549                 flag= ob->flag & (OB_FROMGROUP);
550                 base->flag |= flag;
551                 
552                 /* not too nice... for recovering objects with lost data */
553                 //if(ob->pose==NULL) base->flag &= ~OB_POSEMODE;
554                 ob->flag= base->flag;
555                 
556                 ob->ctime= -1234567.0;  /* force ipo to be calculated later */
557         }
558         /* no full animation update, this to enable render code to work (render code calls own animation updates) */
559 }
560
561 /* called from creator.c */
562 void set_scene_name(char *name)
563 {
564         Scene *sce;
565
566         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
567                 if (BLI_streq(name, sce->id.name+2)) {
568                         set_scene_bg(sce);
569                         return;
570                 }
571         }
572         
573         //XXX error("Can't find scene: %s", name);
574 }
575
576 void unlink_scene(Main *bmain, Scene *sce, Scene *newsce)
577 {
578         Scene *sce1;
579         bScreen *sc;
580
581         /* check all sets */
582         for(sce1= bmain->scene.first; sce1; sce1= sce1->id.next)
583                 if(sce1->set == sce)
584                         sce1->set= NULL;
585         
586         /* check all sequences */
587         clear_scene_in_allseqs(sce);
588
589         /* check render layer nodes in other scenes */
590         clear_scene_in_nodes(bmain, sce);
591         
592         /* al screens */
593         for(sc= bmain->screen.first; sc; sc= sc->id.next)
594                 if(sc->scene == sce)
595                         sc->scene= newsce;
596
597         free_libblock(&bmain->scene, sce);
598 }
599
600 /* used by metaballs
601  * doesnt return the original duplicated object, only dupli's
602  */
603 int next_object(Scene *scene, int val, Base **base, Object **ob)
604 {
605         static ListBase *duplilist= NULL;
606         static DupliObject *dupob;
607         static int fase= F_START, in_next_object= 0;
608         int run_again=1;
609         
610         /* init */
611         if(val==0) {
612                 fase= F_START;
613                 dupob= NULL;
614                 
615                 /* XXX particle systems with metas+dupligroups call this recursively */
616                 /* see bug #18725 */
617                 if(in_next_object) {
618                         printf("ERROR: MetaBall generation called recursively, not supported\n");
619                         
620                         return F_ERROR;
621                 }
622         }
623         else {
624                 in_next_object= 1;
625                 
626                 /* run_again is set when a duplilist has been ended */
627                 while(run_again) {
628                         run_again= 0;
629
630                         /* the first base */
631                         if(fase==F_START) {
632                                 *base= scene->base.first;
633                                 if(*base) {
634                                         *ob= (*base)->object;
635                                         fase= F_SCENE;
636                                 }
637                                 else {
638                                     /* exception: empty scene */
639                                         if(scene->set && scene->set->base.first) {
640                                                 *base= scene->set->base.first;
641                                                 *ob= (*base)->object;
642                                                 fase= F_SET;
643                                         }
644                                 }
645                         }
646                         else {
647                                 if(*base && fase!=F_DUPLI) {
648                                         *base= (*base)->next;
649                                         if(*base) *ob= (*base)->object;
650                                         else {
651                                                 if(fase==F_SCENE) {
652                                                         /* scene is finished, now do the set */
653                                                         if(scene->set && scene->set->base.first) {
654                                                                 *base= scene->set->base.first;
655                                                                 *ob= (*base)->object;
656                                                                 fase= F_SET;
657                                                         }
658                                                 }
659                                         }
660                                 }
661                         }
662                         
663                         if(*base == NULL) fase= F_START;
664                         else {
665                                 if(fase!=F_DUPLI) {
666                                         if( (*base)->object->transflag & OB_DUPLI) {
667                                                 /* groups cannot be duplicated for mballs yet, 
668                                                 this enters eternal loop because of 
669                                                 makeDispListMBall getting called inside of group_duplilist */
670                                                 if((*base)->object->dup_group == NULL) {
671                                                         duplilist= object_duplilist(scene, (*base)->object);
672                                                         
673                                                         dupob= duplilist->first;
674
675                                                         if(!dupob)
676                                                                 free_object_duplilist(duplilist);
677                                                 }
678                                         }
679                                 }
680                                 /* handle dupli's */
681                                 if(dupob) {
682                                         
683                                         copy_m4_m4(dupob->ob->obmat, dupob->mat);
684                                         
685                                         (*base)->flag |= OB_FROMDUPLI;
686                                         *ob= dupob->ob;
687                                         fase= F_DUPLI;
688                                         
689                                         dupob= dupob->next;
690                                 }
691                                 else if(fase==F_DUPLI) {
692                                         fase= F_SCENE;
693                                         (*base)->flag &= ~OB_FROMDUPLI;
694                                         
695                                         for(dupob= duplilist->first; dupob; dupob= dupob->next) {
696                                                 copy_m4_m4(dupob->ob->obmat, dupob->omat);
697                                         }
698                                         
699                                         free_object_duplilist(duplilist);
700                                         duplilist= NULL;
701                                         run_again= 1;
702                                 }
703                         }
704                 }
705         }
706         
707         /* reset recursion test */
708         in_next_object= 0;
709         
710         return fase;
711 }
712
713 Object *scene_find_camera(Scene *sc)
714 {
715         Base *base;
716         
717         for (base= sc->base.first; base; base= base->next)
718                 if (base->object->type==OB_CAMERA)
719                         return base->object;
720
721         return NULL;
722 }
723
724 #ifdef DURIAN_CAMERA_SWITCH
725 Object *scene_find_camera_switch(Scene *scene)
726 {
727         TimeMarker *m;
728         int cfra = scene->r.cfra;
729         int frame = -(MAXFRAME + 1);
730         Object *camera= NULL;
731
732         for (m= scene->markers.first; m; m= m->next) {
733                 if(m->camera && (m->camera->restrictflag & OB_RESTRICT_RENDER)==0 && (m->frame <= cfra) && (m->frame > frame)) {
734                         camera= m->camera;
735                         frame= m->frame;
736
737                         if(frame == cfra)
738                                 break;
739
740                 }
741         }
742         return camera;
743 }
744 #endif
745
746 char *scene_find_marker_name(Scene *scene, int frame)
747 {
748         ListBase *markers= &scene->markers;
749         TimeMarker *m1, *m2;
750
751         /* search through markers for match */
752         for (m1=markers->first, m2=markers->last; m1 && m2; m1=m1->next, m2=m2->prev) {
753                 if (m1->frame==frame)
754                         return m1->name;
755
756                 if (m1 == m2)
757                         break;
758
759                 if (m2->frame==frame)
760                         return m2->name;
761         }
762
763         return NULL;
764 }
765
766 /* return the current marker for this frame,
767 we can have more then 1 marker per frame, this just returns the first :/ */
768 char *scene_find_last_marker_name(Scene *scene, int frame)
769 {
770         TimeMarker *marker, *best_marker = NULL;
771         int best_frame = -MAXFRAME*2;
772         for (marker= scene->markers.first; marker; marker= marker->next) {
773                 if (marker->frame==frame) {
774                         return marker->name;
775                 }
776
777                 if ( marker->frame > best_frame && marker->frame < frame) {
778                         best_marker = marker;
779                         best_frame = marker->frame;
780                 }
781         }
782
783         return best_marker ? best_marker->name : NULL;
784 }
785
786
787 Base *scene_add_base(Scene *sce, Object *ob)
788 {
789         Base *b= MEM_callocN(sizeof(*b), "scene_add_base");
790         BLI_addhead(&sce->base, b);
791
792         b->object= ob;
793         b->flag= ob->flag;
794         b->lay= ob->lay;
795
796         return b;
797 }
798
799 void scene_deselect_all(Scene *sce)
800 {
801         Base *b;
802
803         for (b= sce->base.first; b; b= b->next) {
804                 b->flag&= ~SELECT;
805                 b->object->flag= b->flag;
806         }
807 }
808
809 void scene_select_base(Scene *sce, Base *selbase)
810 {
811         scene_deselect_all(sce);
812
813         selbase->flag |= SELECT;
814         selbase->object->flag= selbase->flag;
815
816         sce->basact= selbase;
817 }
818
819 /* checks for cycle, returns 1 if it's all OK */
820 int scene_check_setscene(Scene *sce)
821 {
822         Scene *scene;
823         int a, totscene;
824         
825         if(sce->set==NULL) return 1;
826         
827         totscene= 0;
828         for(scene= G.main->scene.first; scene; scene= scene->id.next)
829                 totscene++;
830         
831         for(a=0, scene=sce; scene->set; scene=scene->set, a++) {
832                 /* more iterations than scenes means we have a cycle */
833                 if(a > totscene) {
834                         /* the tested scene gets zero'ed, that's typically current scene */
835                         sce->set= NULL;
836                         return 0;
837                 }
838         }
839
840         return 1;
841 }
842
843 /* This (evil) function is needed to cope with two legacy Blender rendering features
844 * mblur (motion blur that renders 'subframes' and blurs them together), and fields 
845 * rendering. Thus, the use of ugly globals from object.c
846 */
847 // BAD... EVIL... JUJU...!!!!
848 // XXX moved here temporarily
849 float frame_to_float (Scene *scene, int cfra)           /* see also bsystem_time in object.c */
850 {
851         extern float bluroffs;  /* bad stuff borrowed from object.c */
852         extern float fieldoffs;
853         float ctime;
854         
855         ctime= (float)cfra;
856         ctime+= bluroffs+fieldoffs;
857         ctime*= scene->r.framelen;
858         
859         return ctime;
860 }
861
862 static void scene_update_newframe(Scene *sce, unsigned int lay)
863 {
864         Base *base;
865         Object *ob;
866         
867         for(base= sce->base.first; base; base= base->next) {
868                 ob= base->object;
869                 
870                 object_handle_update(sce, ob);   // bke_object.h
871                 
872                 /* only update layer when an ipo */
873                         // XXX old animation system
874                 //if(ob->ipo && has_ipo_code(ob->ipo, OB_LAY) ) {
875                 //      base->lay= ob->lay;
876                 //}
877         }
878 }
879
880 /* this is called in main loop, doing tagged updates before redraw */
881 void scene_update_tagged(Scene *scene)
882 {
883         Scene *sce;
884         Base *base;
885         float ctime = frame_to_float(scene, scene->r.cfra); 
886
887         /* update all objects: drivers, matrices, displists, etc. flags set
888            by depgraph or manual, no layer check here, gets correct flushed */
889
890         /* sets first, we allow per definition current scene to have
891            dependencies on sets, but not the other way around. */
892         if(scene->set) {
893                 for(SETLOOPER(scene->set, base))
894                         object_handle_update(scene, base->object);
895         }
896         
897         for(base= scene->base.first; base; base= base->next) {
898                 object_handle_update(scene, base->object);
899         }
900
901         /* recalc scene animation data here (for sequencer) */
902         {
903                 AnimData *adt= BKE_animdata_from_id(&scene->id);
904
905                 if(adt && (adt->recalc & ADT_RECALC_ANIM))
906                         BKE_animsys_evaluate_animdata(&scene->id, adt, ctime, 0);
907         }
908
909         BKE_ptcache_quick_cache_all(scene);
910
911         /* in the future this should handle updates for all datablocks, not
912            only objects and scenes. - brecht */
913 }
914
915 /* applies changes right away, does all sets too */
916 void scene_update_for_newframe(Scene *sce, unsigned int lay)
917 {
918         float ctime = frame_to_float(sce, sce->r.cfra);
919         Scene *sce_iter;
920         
921         /* clear animation overrides */
922         // XXX TODO...
923
924         for(sce_iter= sce; sce_iter; sce_iter= sce_iter->set) {
925                 if(sce_iter->theDag==NULL)
926                         DAG_scene_sort(sce_iter);
927         }
928
929
930         /* Following 2 functions are recursive
931          * so dont call within 'scene_update_newframe' */
932         DAG_scene_update_flags(sce, lay);   // only stuff that moves or needs display still
933
934         /* All 'standard' (i.e. without any dependencies) animation is handled here,
935          * with an 'local' to 'macro' order of evaluation. This should ensure that
936          * settings stored nestled within a hierarchy (i.e. settings in a Texture block
937          * can be overridden by settings from Scene, which owns the Texture through a hierarchy
938          * such as Scene->World->MTex/Texture) can still get correctly overridden.
939          */
940         BKE_animsys_evaluate_all_animation(G.main, ctime);
941         /*...done with recusrive funcs */
942
943
944         /* sets first, we allow per definition current scene to have dependencies on sets */
945         for(sce_iter= sce->set; sce_iter; sce_iter= sce_iter->set)
946                 scene_update_newframe(sce_iter, lay);
947
948         scene_update_newframe(sce, lay);
949 }
950
951 /* return default layer, also used to patch old files */
952 void scene_add_render_layer(Scene *sce)
953 {
954         SceneRenderLayer *srl;
955 //      int tot= 1 + BLI_countlist(&sce->r.layers);
956         
957         srl= MEM_callocN(sizeof(SceneRenderLayer), "new render layer");
958         strcpy(srl->name, "RenderLayer");
959         BLI_uniquename(&sce->r.layers, srl, "RenderLayer", '.', offsetof(SceneRenderLayer, name), sizeof(srl->name));
960         BLI_addtail(&sce->r.layers, srl);
961
962         /* note, this is also in render, pipeline.c, to make layer when scenedata doesnt have it */
963         srl->lay= (1<<20) -1;
964         srl->layflag= 0x7FFF;   /* solid ztra halo edge strand */
965         srl->passflag= SCE_PASS_COMBINED|SCE_PASS_Z;
966 }
967
968 /* render simplification */
969
970 int get_render_subsurf_level(RenderData *r, int lvl)
971 {
972         if(r->mode & R_SIMPLIFY)
973                 return MIN2(r->simplify_subsurf, lvl);
974         else
975                 return lvl;
976 }
977
978 int get_render_child_particle_number(RenderData *r, int num)
979 {
980         if(r->mode & R_SIMPLIFY)
981                 return (int)(r->simplify_particles*num);
982         else
983                 return num;
984 }
985
986 int get_render_shadow_samples(RenderData *r, int samples)
987 {
988         if((r->mode & R_SIMPLIFY) && samples > 0)
989                 return MIN2(r->simplify_shadowsamples, samples);
990         else
991                 return samples;
992 }
993
994 float get_render_aosss_error(RenderData *r, float error)
995 {
996         if(r->mode & R_SIMPLIFY)
997                 return ((1.0f-r->simplify_aosss)*10.0f + 1.0f)*error;
998         else
999                 return error;
1000 }
1001