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