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