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